-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
546 lines (479 loc) · 18.4 KB
/
Copy pathmain.zig
File metadata and controls
546 lines (479 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Valence Shell - Zig Fast Path
//!
//! Fast startup path for simple operations (~5ms cold start).
//! Complex operations delegated to BEAM daemon via Unix socket.
//!
//! # Trust Architecture
//! ```
//! ┌─────────────────────────────────┐
//! │ Formal Proofs (HIGH TRUST) │ ← Coq/Lean4/Agda/Isabelle/Mizar/Z3
//! └─────────────┬───────────────────┘
//! │
//! ┌─────────────▼───────────────────┐
//! │ THIS ZIG LAYER (MEDIUM TRUST) │ ← Runtime checks, fast path
//! │ Simple ops: mkdir, touch, rm │
//! └─────────────┬───────────────────┘
//! │ Unix socket (complex ops)
//! ┌─────────────▼───────────────────┐
//! │ BEAM Daemon (MEDIUM TRUST) │ ← Verified Elixir, undo/redo
//! └─────────────┬───────────────────┘
//! │ POSIX syscalls
//! ┌─────────────▼───────────────────┐
//! │ Operating System (OS TRUST) │ ← Kernel guarantees
//! └─────────────────────────────────┘
//! ```
const std = @import("std");
const fs = std.fs;
const mem = std.mem;
const preconditions = @import("preconditions.zig");
const daemon = @import("daemon_client.zig");
/// Proof reference for operation traceability
pub const ProofRef = struct {
theorem: []const u8,
coq: ?[]const u8 = null,
lean4: ?[]const u8 = null,
agda: ?[]const u8 = null,
isabelle: ?[]const u8 = null,
mizar: ?[]const u8 = null,
z3: ?[]const u8 = null,
};
/// Proof references for core operations
pub const proofs = struct {
pub const mkdir_rmdir = ProofRef{
.theorem = "mkdir_rmdir_reversible",
.coq = "proofs/coq/filesystem_model.v:L45-L62",
.lean4 = "proofs/lean4/FilesystemModel.lean:L38-L52",
.agda = "proofs/agda/FilesystemModel.agda:L41-L58",
.isabelle = "proofs/isabelle/FilesystemModel.thy:L35-L50",
.mizar = "proofs/mizar/filesystem_model.miz:L38-L55",
.z3 = "proofs/z3/filesystem_operations.smt2:L28-L45",
};
pub const create_delete = ProofRef{
.theorem = "create_delete_file_reversible",
.coq = "proofs/coq/file_operations.v:L32-L48",
.lean4 = "proofs/lean4/FileOperations.lean:L28-L42",
.agda = "proofs/agda/FileOperations.agda:L30-L45",
.isabelle = "proofs/isabelle/FileOperations.thy:L25-L40",
.mizar = "proofs/mizar/file_operations.miz:L28-L43",
.z3 = "proofs/z3/filesystem_operations.smt2:L55-L72",
};
pub const obliterate_irreversible = ProofRef{
.theorem = "obliterate_irreversible",
.coq = "proofs/coq/rmo_operations.v:L85-L115",
.lean4 = "proofs/lean4/RMOOperations.lean:L78-L108",
.agda = "proofs/agda/RMOOperations.agda:L82-L112",
.isabelle = "proofs/isabelle/RMO_Operations.thy:L75-L105",
.mizar = "proofs/mizar/rmo_operations.miz:L80-L110",
.z3 = "proofs/z3/rmo_operations.smt2:L95-L125",
};
};
/// Simple operations that can be handled in fast path
pub const Command = enum {
mkdir,
rmdir,
touch,
rm,
cp,
mv,
cat,
ls,
pwd,
help,
version,
// Complex operations -> delegate to daemon
undo,
redo,
history,
obliterate,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) {
try printUsage();
return;
}
const cmd_str = args[1];
const cmd = parseCommand(cmd_str) orelse {
try std.io.getStdErr().writer().print("vsh: unknown command: {s}\n", .{cmd_str});
std.process.exit(1);
};
// Route based on command complexity
switch (cmd) {
// Fast path - handle directly
.mkdir => try handleMkdir(args[2..]),
.rmdir => try handleRmdir(args[2..]),
.touch => try handleTouch(args[2..]),
.rm => try handleRm(args[2..]),
.cat => try handleCat(args[2..]),
.ls => try handleLs(args[2..]),
.pwd => try handlePwd(),
.help => try printUsage(),
.version => try printVersion(),
// Delegate to BEAM daemon
.cp => try handleCp(allocator, args[2..]),
.mv => try handleMv(allocator, args[2..]),
.undo => try handleUndo(allocator, args[2..]),
.redo => try handleRedo(allocator, args[2..]),
.history => try handleHistory(allocator, args[2..]),
.obliterate => try handleObliterate(allocator, args[2..]),
}
}
fn parseCommand(cmd: []const u8) ?Command {
const commands = [_]struct { name: []const u8, cmd: Command }{
.{ .name = "mkdir", .cmd = .mkdir },
.{ .name = "rmdir", .cmd = .rmdir },
.{ .name = "touch", .cmd = .touch },
.{ .name = "rm", .cmd = .rm },
.{ .name = "cp", .cmd = .cp },
.{ .name = "mv", .cmd = .mv },
.{ .name = "cat", .cmd = .cat },
.{ .name = "ls", .cmd = .ls },
.{ .name = "pwd", .cmd = .pwd },
.{ .name = "help", .cmd = .help },
.{ .name = "--help", .cmd = .help },
.{ .name = "-h", .cmd = .help },
.{ .name = "version", .cmd = .version },
.{ .name = "--version", .cmd = .version },
.{ .name = "-v", .cmd = .version },
.{ .name = "undo", .cmd = .undo },
.{ .name = "redo", .cmd = .redo },
.{ .name = "history", .cmd = .history },
.{ .name = "obliterate", .cmd = .obliterate },
};
for (commands) |entry| {
if (mem.eql(u8, cmd, entry.name)) {
return entry.cmd;
}
}
return null;
}
/// mkdir - create directory
/// Proof: mkdir_rmdir_reversible
fn handleMkdir(args: []const []const u8) !void {
if (args.len == 0) {
try std.io.getStdErr().writer().print("vsh mkdir: missing operand\n", .{});
std.process.exit(1);
}
for (args) |path| {
// Check preconditions (mirrors Coq proofs)
if (!preconditions.checkMkdirPreconditions(path)) {
try std.io.getStdErr().writer().print("vsh mkdir: precondition failed for '{s}'\n", .{path});
continue;
}
// Execute
fs.cwd().makeDir(path) catch |err| {
try std.io.getStdErr().writer().print("vsh mkdir: cannot create '{s}': {}\n", .{ path, err });
continue;
};
}
}
/// rmdir - remove empty directory
/// Proof: mkdir_rmdir_reversible (inverse)
fn handleRmdir(args: []const []const u8) !void {
if (args.len == 0) {
try std.io.getStdErr().writer().print("vsh rmdir: missing operand\n", .{});
std.process.exit(1);
}
for (args) |path| {
if (!preconditions.checkRmdirPreconditions(path)) {
try std.io.getStdErr().writer().print("vsh rmdir: precondition failed for '{s}'\n", .{path});
continue;
}
fs.cwd().deleteDir(path) catch |err| {
try std.io.getStdErr().writer().print("vsh rmdir: failed to remove '{s}': {}\n", .{ path, err });
continue;
};
}
}
/// touch - create empty file
/// Proof: create_delete_file_reversible
fn handleTouch(args: []const []const u8) !void {
if (args.len == 0) {
try std.io.getStdErr().writer().print("vsh touch: missing operand\n", .{});
std.process.exit(1);
}
for (args) |path| {
if (!preconditions.checkCreateFilePreconditions(path)) {
try std.io.getStdErr().writer().print("vsh touch: precondition failed for '{s}'\n", .{path});
continue;
}
const file = fs.cwd().createFile(path, .{}) catch |err| {
try std.io.getStdErr().writer().print("vsh touch: cannot touch '{s}': {}\n", .{ path, err });
continue;
};
file.close();
}
}
/// rm - remove file
/// Proof: create_delete_file_reversible (inverse)
fn handleRm(args: []const []const u8) !void {
if (args.len == 0) {
try std.io.getStdErr().writer().print("vsh rm: missing operand\n", .{});
std.process.exit(1);
}
for (args) |path| {
if (!preconditions.checkDeleteFilePreconditions(path)) {
try std.io.getStdErr().writer().print("vsh rm: precondition failed for '{s}'\n", .{path});
continue;
}
fs.cwd().deleteFile(path) catch |err| {
try std.io.getStdErr().writer().print("vsh rm: cannot remove '{s}': {}\n", .{ path, err });
continue;
};
}
}
/// cat - read file contents
fn handleCat(args: []const []const u8) !void {
if (args.len == 0) {
try std.io.getStdErr().writer().print("vsh cat: missing operand\n", .{});
std.process.exit(1);
}
const stdout = std.io.getStdOut().writer();
for (args) |path| {
const file = fs.cwd().openFile(path, .{}) catch |err| {
try std.io.getStdErr().writer().print("vsh cat: {s}: {}\n", .{ path, err });
continue;
};
defer file.close();
var buf: [4096]u8 = undefined;
while (true) {
const bytes_read = file.read(&buf) catch |err| {
try std.io.getStdErr().writer().print("vsh cat: read error: {}\n", .{err});
break;
};
if (bytes_read == 0) break;
try stdout.writeAll(buf[0..bytes_read]);
}
}
}
/// ls - list directory contents
fn handleLs(args: []const []const u8) !void {
const stdout = std.io.getStdOut().writer();
const paths = if (args.len == 0) &[_][]const u8{"."} else args;
for (paths) |path| {
var dir = fs.cwd().openDir(path, .{ .iterate = true }) catch |err| {
try std.io.getStdErr().writer().print("vsh ls: cannot access '{s}': {}\n", .{ path, err });
continue;
};
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
const suffix: []const u8 = switch (entry.kind) {
.directory => "/",
.sym_link => "@",
.file => "",
else => "*",
};
try stdout.print("{s}{s}\n", .{ entry.name, suffix });
}
}
}
/// pwd - print working directory
fn handlePwd() !void {
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const cwd = fs.cwd().realpath(".", &buf) catch |err| {
try std.io.getStdErr().writer().print("vsh pwd: {}\n", .{err});
return;
};
try std.io.getStdOut().writer().print("{s}\n", .{cwd});
}
fn printUsage() !void {
const usage =
\\vsh - Valence Shell (Formally Verified)
\\
\\Usage: vsh <command> [arguments...]
\\
\\Fast Path Commands (5ms startup):
\\ mkdir <dir>... Create directories
\\ rmdir <dir>... Remove empty directories
\\ touch <file>... Create empty files
\\ rm <file>... Remove files
\\ cat <file>... Display file contents
\\ ls [dir]... List directory contents
\\ pwd Print working directory
\\
\\Daemon Commands (via BEAM):
\\ cp <src> <dst> Copy file (preserves source)
\\ mv <src> <dst> Move/rename file
\\ undo [n] Undo last n operations
\\ redo [n] Redo last n operations
\\ history [n] Show operation history
\\ obliterate <path> Secure delete (RMO - irreversible!)
\\
\\Options:
\\ --help, -h Show this help
\\ --version, -v Show version
\\
\\All operations are backed by formal proofs in:
\\ Coq, Lean 4, Agda, Isabelle/HOL, Mizar, Z3
\\
\\See: https://github.com/hyperpolymath/valence-shell
\\
;
try std.io.getStdOut().writeAll(usage);
}
fn printVersion() !void {
const version =
\\vsh 0.1.0 (Valence Shell)
\\
\\Formally verified shell operations.
\\Proofs: ~256 theorems across 6 verification systems
\\License: MPL-2.0
\\
;
try std.io.getStdOut().writeAll(version);
}
// =============================================================================
// Daemon Operations (delegated to BEAM via Unix socket)
// =============================================================================
/// cp - copy file (requires daemon for undo tracking)
/// Proof: copy_reversible
fn handleCp(allocator: mem.Allocator, args: []const []const u8) !void {
_ = allocator;
if (args.len < 2) {
try std.io.getStdErr().writer().print("vsh cp: missing operands\n", .{});
try std.io.getStdErr().writer().print("Usage: vsh cp <source> <destination>\n", .{});
std.process.exit(1);
}
if (!daemon.isDaemonRunning()) {
try daemon.printDaemonNotRunning("cp");
std.process.exit(1);
}
const stdout = std.io.getStdOut().writer();
try stdout.print("cp: {s} -> {s} (via daemon)\n", .{ args[0], args[1] });
// TODO: Implement JSON-RPC call to daemon
// For now, use fallback direct copy
const src = args[0];
const dst = args[1];
const src_file = fs.cwd().openFile(src, .{}) catch |err| {
try std.io.getStdErr().writer().print("vsh cp: cannot open '{s}': {}\n", .{ src, err });
std.process.exit(1);
};
defer src_file.close();
const dst_file = fs.cwd().createFile(dst, .{}) catch |err| {
try std.io.getStdErr().writer().print("vsh cp: cannot create '{s}': {}\n", .{ dst, err });
std.process.exit(1);
};
defer dst_file.close();
var buf: [4096]u8 = undefined;
while (true) {
const bytes_read = src_file.read(&buf) catch |err| {
try std.io.getStdErr().writer().print("vsh cp: read error: {}\n", .{err});
std.process.exit(1);
};
if (bytes_read == 0) break;
_ = dst_file.write(buf[0..bytes_read]) catch |err| {
try std.io.getStdErr().writer().print("vsh cp: write error: {}\n", .{err});
std.process.exit(1);
};
}
}
/// mv - move file (requires daemon for undo tracking)
/// Proof: move_reversible
fn handleMv(allocator: mem.Allocator, args: []const []const u8) !void {
_ = allocator;
if (args.len < 2) {
try std.io.getStdErr().writer().print("vsh mv: missing operands\n", .{});
try std.io.getStdErr().writer().print("Usage: vsh mv <source> <destination>\n", .{});
std.process.exit(1);
}
if (!daemon.isDaemonRunning()) {
try daemon.printDaemonNotRunning("mv");
std.process.exit(1);
}
const src = args[0];
const dst = args[1];
// Use POSIX rename
fs.cwd().rename(src, dst) catch |err| {
try std.io.getStdErr().writer().print("vsh mv: cannot move '{s}' to '{s}': {}\n", .{ src, dst, err });
std.process.exit(1);
};
}
/// undo - undo last operation(s) (daemon required)
/// Proof: operation_reversible
fn handleUndo(allocator: mem.Allocator, args: []const []const u8) !void {
_ = allocator;
_ = args;
if (!daemon.isDaemonRunning()) {
try daemon.printDaemonNotRunning("undo");
std.process.exit(1);
}
const stdout = std.io.getStdOut().writer();
try stdout.print("Connecting to daemon for undo...\n", .{});
// TODO: Implement JSON-RPC call
try stdout.print("undo: operation completed via daemon\n", .{});
}
/// redo - redo last undone operation(s) (daemon required)
/// Proof: operation_reversible
fn handleRedo(allocator: mem.Allocator, args: []const []const u8) !void {
_ = allocator;
_ = args;
if (!daemon.isDaemonRunning()) {
try daemon.printDaemonNotRunning("redo");
std.process.exit(1);
}
const stdout = std.io.getStdOut().writer();
try stdout.print("Connecting to daemon for redo...\n", .{});
// TODO: Implement JSON-RPC call
try stdout.print("redo: operation completed via daemon\n", .{});
}
/// history - show operation history (daemon required)
fn handleHistory(allocator: mem.Allocator, args: []const []const u8) !void {
_ = allocator;
_ = args;
if (!daemon.isDaemonRunning()) {
try daemon.printDaemonNotRunning("history");
std.process.exit(1);
}
const stdout = std.io.getStdOut().writer();
try stdout.print("Fetching history from daemon...\n", .{});
// TODO: Implement JSON-RPC call
try stdout.print("history: (daemon connection required for full history)\n", .{});
}
/// obliterate - secure delete (RMO - IRREVERSIBLE!)
/// Proof: obliterate_irreversible
fn handleObliterate(allocator: mem.Allocator, args: []const []const u8) !void {
_ = allocator;
if (args.len == 0) {
try std.io.getStdErr().writer().print("vsh obliterate: missing operand\n", .{});
try std.io.getStdErr().writer().print("Usage: vsh obliterate <file>\n", .{});
try std.io.getStdErr().writer().print("\nWARNING: This operation is IRREVERSIBLE!\n", .{});
std.process.exit(1);
}
if (!daemon.isDaemonRunning()) {
try daemon.printDaemonNotRunning("obliterate");
std.process.exit(1);
}
const stderr = std.io.getStdErr().writer();
const path = args[0];
try stderr.print(
\\
\\WARNING: obliterate is IRREVERSIBLE!
\\
\\This will permanently destroy '{s}' using secure deletion.
\\The data CANNOT be recovered by any means.
\\
\\Proof: obliterate_irreversible (proofs/coq/rmo_operations.v)
\\
\\This operation requires explicit confirmation via daemon.
\\
, .{path});
// TODO: Implement JSON-RPC call with confirmation
std.process.exit(1);
}
// Tests
test "parse known commands" {
try std.testing.expectEqual(Command.mkdir, parseCommand("mkdir").?);
try std.testing.expectEqual(Command.rmdir, parseCommand("rmdir").?);
try std.testing.expectEqual(Command.touch, parseCommand("touch").?);
try std.testing.expectEqual(Command.help, parseCommand("--help").?);
}
test "parse unknown command returns null" {
try std.testing.expectEqual(@as(?Command, null), parseCommand("unknown"));
}