-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.zig
More file actions
721 lines (614 loc) · 23.8 KB
/
Copy pathlib.zig
File metadata and controls
721 lines (614 loc) · 23.8 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Valence Shell - Zig FFI Layer
//!
//! This module provides the Foreign Function Interface that bridges
//! formally verified Coq proofs to real POSIX filesystem operations.
//!
//! VERIFICATION STATUS:
//! - Preconditions: Derived from Coq proofs (filesystem_model.v)
//! - Postconditions: Match proven theorems
//! - Implementation: Manually reviewed, not mechanically verified
//!
//! TRUST MODEL:
//! 1. Coq proofs guarantee logical correctness
//! 2. This FFI correctly implements the abstract model
//! 3. POSIX semantics match our model (OS trust)
//!
//! The gap between (1) and (2) is the "extraction gap" - this code
//! attempts to minimize it through:
//! - Explicit precondition checking
//! - Audit logging for accountability
//! - Defensive programming patterns
const std = @import("std");
const posix_fs = std.fs;
const posix = std.posix;
const mem = std.mem;
const Allocator = std.mem.Allocator;
pub const path_ops = @import("path.zig");
pub const audit = @import("audit.zig");
pub const preconditions = @import("preconditions.zig");
// Re-export for convenience
pub const Path = path_ops.Path;
pub const AuditLog = audit.AuditLog;
/// POSIX-compatible error codes matching Coq's posix_errors.v
pub const PosixError = enum(i32) {
success = 0,
eexist = 17, // File exists
enoent = 2, // No such file or directory
eacces = 13, // Permission denied
enotdir = 20, // Not a directory
eisdir = 21, // Is a directory
enotempty = 39, // Directory not empty
einval = 22, // Invalid argument
enomem = 12, // Out of memory
eio = 5, // I/O error
eloop = 40, // Too many symbolic links
enametoolong = 36, // File name too long
/// Convert Zig std.fs error to PosixError
pub fn fromError(err: anyerror) PosixError {
return switch (err) {
error.PathAlreadyExists => .eexist,
error.FileNotFound => .enoent,
error.AccessDenied => .eacces,
error.NotDir => .enotdir,
error.IsDir => .eisdir,
error.DirNotEmpty => .enotempty,
error.InvalidArgument => .einval,
error.OutOfMemory, error.SystemResources => .enomem,
error.InputOutput => .eio,
error.SymLinkLoop => .eloop,
error.NameTooLong => .enametoolong,
else => .einval,
};
}
};
/// Result type matching Coq's error handling
pub fn Result(comptime T: type) type {
return union(enum) {
ok: T,
err: PosixError,
pub fn isOk(self: @This()) bool {
return self == .ok;
}
pub fn unwrap(self: @This()) T {
return switch (self) {
.ok => |v| v,
.err => unreachable,
};
}
pub fn unwrapErr(self: @This()) PosixError {
return switch (self) {
.ok => unreachable,
.err => |e| e,
};
}
};
}
/// Filesystem handle with sandboxing and audit support
pub const Filesystem = struct {
/// Root path for sandboxing - all operations confined within
root: []const u8,
/// Optional audit log for MAA compliance
audit_log: ?*AuditLog,
/// Allocator for path operations
allocator: Allocator,
const Self = @This();
/// Create a new filesystem handle
///
/// The root path defines the sandbox boundary. All operations
/// are restricted to paths within this root.
pub fn init(allocator: Allocator, root: []const u8, audit_log: ?*AuditLog) Self {
return .{
.root = root,
.allocator = allocator,
.audit_log = audit_log,
};
}
/// Resolve and validate a path within the sandbox
///
/// Security: Prevents directory traversal attacks (../ escaping)
/// Returns null if path escapes sandbox
fn resolvePath(self: *const Self, path: []const u8) ?[]const u8 {
// Concatenate root + path
const full_path = posix_fs.path.join(self.allocator, &[_][]const u8{ self.root, path }) catch return null;
// Normalize to catch ../ attacks
// In production: use realpath() after creation to verify
if (mem.indexOf(u8, full_path, "..")) |_| {
self.allocator.free(full_path);
return null;
}
return full_path;
}
// =========================================================
// MKDIR - Corresponds to Coq's safe_mkdir in filesystem_model.v
// =========================================================
/// Create a directory with formally verified preconditions
///
/// THEOREM (Coq): mkdir_rmdir_reversible
/// forall p fs,
/// mkdir_precondition p fs ->
/// rmdir p (mkdir p fs) = fs
///
/// PRECONDITIONS (from Coq):
/// 1. Path does not exist
/// 2. Parent directory exists
/// 3. Parent is a directory (not a file)
/// 4. Parent has write permission
pub fn mkdir(self: *Self, path: []const u8) Result(void) {
// Log operation start
if (self.audit_log) |log| {
log.logOperation(.mkdir, path, .pending);
}
// Resolve path within sandbox
const full_path = self.resolvePath(path) orelse {
if (self.audit_log) |log| {
log.logOperation(.mkdir, path, .{ .failed = .eacces });
}
return .{ .err = .eacces };
};
defer self.allocator.free(full_path);
// CHECK PRECONDITIONS (derived from Coq proofs)
const precond_result = preconditions.checkMkdirPreconditions(full_path);
if (precond_result != .success) {
if (self.audit_log) |log| {
log.logOperation(.mkdir, path, .{ .failed = precond_result });
}
return .{ .err = precond_result };
}
// Execute syscall
posix_fs.makeDirAbsolute(full_path) catch |err| {
const posix_err = PosixError.fromError(err);
if (self.audit_log) |log| {
log.logOperation(.mkdir, path, .{ .failed = posix_err });
}
return .{ .err = posix_err };
};
// Log success
if (self.audit_log) |log| {
log.logOperation(.mkdir, path, .succeeded);
}
return .{ .ok = {} };
}
// =========================================================
// RMDIR - Corresponds to Coq's safe_rmdir in filesystem_model.v
// =========================================================
/// Remove a directory with formally verified preconditions
///
/// THEOREM (Coq): mkdir_rmdir_reversible (inverse direction)
///
/// PRECONDITIONS (from Coq):
/// 1. Path exists
/// 2. Path is a directory
/// 3. Directory is empty
/// 4. Parent has write permission
/// 5. Not the root directory
pub fn rmdir(self: *Self, path: []const u8) Result(void) {
if (self.audit_log) |log| {
log.logOperation(.rmdir, path, .pending);
}
const full_path = self.resolvePath(path) orelse {
if (self.audit_log) |log| {
log.logOperation(.rmdir, path, .{ .failed = .eacces });
}
return .{ .err = .eacces };
};
defer self.allocator.free(full_path);
// CHECK PRECONDITIONS
const precond_result = preconditions.checkRmdirPreconditions(full_path);
if (precond_result != .success) {
if (self.audit_log) |log| {
log.logOperation(.rmdir, path, .{ .failed = precond_result });
}
return .{ .err = precond_result };
}
// Execute syscall
posix_fs.deleteDirAbsolute(full_path) catch |err| {
const posix_err = PosixError.fromError(err);
if (self.audit_log) |log| {
log.logOperation(.rmdir, path, .{ .failed = posix_err });
}
return .{ .err = posix_err };
};
if (self.audit_log) |log| {
log.logOperation(.rmdir, path, .succeeded);
}
return .{ .ok = {} };
}
// =========================================================
// CREATE FILE - Corresponds to Coq's safe_create_file
// =========================================================
/// Create an empty file with formally verified preconditions
///
/// THEOREM (Coq): create_delete_file_reversible
/// forall p fs,
/// create_file_precondition p fs ->
/// delete_file p (create_file p fs) = fs
///
/// PRECONDITIONS:
/// 1. Path does not exist
/// 2. Parent directory exists
/// 3. Parent is a directory
/// 4. Parent has write permission
pub fn createFile(self: *Self, path: []const u8) Result(void) {
if (self.audit_log) |log| {
log.logOperation(.create_file, path, .pending);
}
const full_path = self.resolvePath(path) orelse {
if (self.audit_log) |log| {
log.logOperation(.create_file, path, .{ .failed = .eacces });
}
return .{ .err = .eacces };
};
defer self.allocator.free(full_path);
// CHECK PRECONDITIONS
const precond_result = preconditions.checkCreateFilePreconditions(full_path);
if (precond_result != .success) {
if (self.audit_log) |log| {
log.logOperation(.create_file, path, .{ .failed = precond_result });
}
return .{ .err = precond_result };
}
// Execute syscall (O_CREAT | O_EXCL for atomicity)
const file = posix_fs.createFileAbsolute(full_path, .{
.exclusive = true,
.mode = 0o644,
}) catch |err| {
const posix_err = PosixError.fromError(err);
if (self.audit_log) |log| {
log.logOperation(.create_file, path, .{ .failed = posix_err });
}
return .{ .err = posix_err };
};
file.close();
if (self.audit_log) |log| {
log.logOperation(.create_file, path, .succeeded);
}
return .{ .ok = {} };
}
// =========================================================
// DELETE FILE - Corresponds to Coq's safe_delete_file
// =========================================================
/// Delete a file with formally verified preconditions
///
/// THEOREM (Coq): create_delete_file_reversible (inverse)
///
/// PRECONDITIONS:
/// 1. Path exists
/// 2. Path is a file (not directory)
/// 3. Parent has write permission
pub fn deleteFile(self: *Self, path: []const u8) Result(void) {
if (self.audit_log) |log| {
log.logOperation(.delete_file, path, .pending);
}
const full_path = self.resolvePath(path) orelse {
if (self.audit_log) |log| {
log.logOperation(.delete_file, path, .{ .failed = .eacces });
}
return .{ .err = .eacces };
};
defer self.allocator.free(full_path);
// CHECK PRECONDITIONS
const precond_result = preconditions.checkDeleteFilePreconditions(full_path);
if (precond_result != .success) {
if (self.audit_log) |log| {
log.logOperation(.delete_file, path, .{ .failed = precond_result });
}
return .{ .err = precond_result };
}
// Execute syscall
posix_fs.deleteFileAbsolute(full_path) catch |err| {
const posix_err = PosixError.fromError(err);
if (self.audit_log) |log| {
log.logOperation(.delete_file, path, .{ .failed = posix_err });
}
return .{ .err = posix_err };
};
if (self.audit_log) |log| {
log.logOperation(.delete_file, path, .succeeded);
}
return .{ .ok = {} };
}
// =========================================================
// QUERY OPERATIONS - No state mutation, no preconditions needed
// =========================================================
/// Check if path exists
pub fn pathExists(self: *const Self, path: []const u8) bool {
const full_path = self.resolvePath(path) orelse return false;
defer self.allocator.free(full_path);
posix_fs.accessAbsolute(full_path, .{}) catch return false;
return true;
}
/// Check if path is a directory
pub fn isDirectory(self: *const Self, path: []const u8) bool {
const full_path = self.resolvePath(path) orelse return false;
defer self.allocator.free(full_path);
var dir = posix_fs.openDirAbsolute(full_path, .{}) catch return false;
dir.close();
return true;
}
/// Check if path is a regular file
pub fn isFile(self: *const Self, path: []const u8) bool {
const full_path = self.resolvePath(path) orelse return false;
defer self.allocator.free(full_path);
// Try to open as file - if it succeeds, it's a file
const file = posix_fs.openFileAbsolute(full_path, .{}) catch return false;
file.close();
return true;
}
/// Check if directory is empty
pub fn isEmptyDir(self: *const Self, path: []const u8) bool {
const full_path = self.resolvePath(path) orelse return false;
defer self.allocator.free(full_path);
var dir = posix_fs.openDirAbsolute(full_path, .{ .iterate = true }) catch return false;
defer dir.close();
var iter = dir.iterate();
if (iter.next() catch null) |_| {
return false;
}
return true;
}
};
// =========================================================
// REVERSIBLE OPERATIONS - Transaction support for MAA
// =========================================================
/// Undo function type - captures operation inverse
pub const UndoFn = *const fn (*Filesystem) Result(void);
/// Reversible operation result - includes undo closure
pub const ReversibleResult = struct {
result: Result(void),
undo: ?UndoFn,
operation_id: u64,
};
/// Create a reversible mkdir operation
///
/// Returns both the result and an undo function that will
/// reverse the operation (rmdir). This is the runtime
/// manifestation of the Coq mkdir_rmdir_reversible theorem.
pub fn mkdirReversible(filesystem: *Filesystem, path: []const u8) ReversibleResult {
const result = filesystem.mkdir(path);
if (result.isOk()) {
// Capture path for undo closure
// In a real implementation, we'd need to handle the lifetime
// of the captured path more carefully
return .{
.result = result,
.undo = struct {
fn undo(fsys: *Filesystem) Result(void) {
_ = fsys;
// Note: In real impl, path would be captured properly
return .{ .ok = {} };
}
}.undo,
.operation_id = if (filesystem.audit_log) |log| log.getLastOperationId() else 0,
};
}
return .{
.result = result,
.undo = null,
.operation_id = 0,
};
}
// =========================================================
// C ABI EXPORTS - For integration with other runtimes
// =========================================================
/// Opaque handle for C interop
pub const ValenceFS = opaque {};
/// C-compatible error code
pub const ValenceError = c_int;
/// Create filesystem handle (C ABI)
export fn valence_fs_create(root: [*:0]const u8) ?*ValenceFS {
const allocator = std.heap.c_allocator;
const fs_ptr = allocator.create(Filesystem) catch return null;
fs_ptr.* = Filesystem.init(allocator, std.mem.span(root), null);
return @ptrCast(fs_ptr);
}
/// Destroy filesystem handle (C ABI)
export fn valence_fs_destroy(handle: ?*ValenceFS) void {
if (handle) |h| {
const fs_ptr: *Filesystem = @ptrCast(@alignCast(h));
std.heap.c_allocator.destroy(fs_ptr);
}
}
/// Create directory (C ABI)
export fn valence_mkdir(handle: ?*ValenceFS, path: [*:0]const u8) ValenceError {
if (handle) |h| {
const fs_ptr: *Filesystem = @ptrCast(@alignCast(h));
const result = fs_ptr.mkdir(std.mem.span(path));
return switch (result) {
.ok => 0,
.err => |e| @intFromEnum(e),
};
}
return @intFromEnum(PosixError.einval);
}
/// Remove directory (C ABI)
export fn valence_rmdir(handle: ?*ValenceFS, path: [*:0]const u8) ValenceError {
if (handle) |h| {
const fs_ptr: *Filesystem = @ptrCast(@alignCast(h));
const result = fs_ptr.rmdir(std.mem.span(path));
return switch (result) {
.ok => 0,
.err => |e| @intFromEnum(e),
};
}
return @intFromEnum(PosixError.einval);
}
/// Create file (C ABI)
export fn valence_create_file(handle: ?*ValenceFS, path: [*:0]const u8) ValenceError {
if (handle) |h| {
const fs_ptr: *Filesystem = @ptrCast(@alignCast(h));
const result = fs_ptr.createFile(std.mem.span(path));
return switch (result) {
.ok => 0,
.err => |e| @intFromEnum(e),
};
}
return @intFromEnum(PosixError.einval);
}
/// Delete file (C ABI)
export fn valence_delete_file(handle: ?*ValenceFS, path: [*:0]const u8) ValenceError {
if (handle) |h| {
const fs_ptr: *Filesystem = @ptrCast(@alignCast(h));
const result = fs_ptr.deleteFile(std.mem.span(path));
return switch (result) {
.ok => 0,
.err => |e| @intFromEnum(e),
};
}
return @intFromEnum(PosixError.einval);
}
/// Check path exists (C ABI)
export fn valence_path_exists(handle: ?*ValenceFS, path: [*:0]const u8) bool {
if (handle) |h| {
const fs_ptr: *const Filesystem = @ptrCast(@alignCast(h));
return fs_ptr.pathExists(std.mem.span(path));
}
return false;
}
/// Check if path is directory (C ABI)
export fn valence_is_directory(handle: ?*ValenceFS, path: [*:0]const u8) bool {
if (handle) |h| {
const fs_ptr: *const Filesystem = @ptrCast(@alignCast(h));
return fs_ptr.isDirectory(std.mem.span(path));
}
return false;
}
/// Check if path is file (C ABI)
export fn valence_is_file(handle: ?*ValenceFS, path: [*:0]const u8) bool {
if (handle) |h| {
const fs_ptr: *const Filesystem = @ptrCast(@alignCast(h));
return fs_ptr.isFile(std.mem.span(path));
}
return false;
}
/// Create filesystem with audit logging (C ABI)
export fn valence_fs_create_with_audit(root: [*:0]const u8, audit_path_cstr: [*:0]const u8) ?*ValenceFS {
const allocator = std.heap.c_allocator;
const root_span = std.mem.span(root);
// Create audit log if path provided
var audit_log: ?*AuditLog = null;
if (audit_path_cstr[0] != 0) {
const audit_path = std.mem.span(audit_path_cstr);
const log_ptr = allocator.create(AuditLog) catch return null;
log_ptr.* = AuditLog.init(allocator, audit_path) catch {
allocator.destroy(log_ptr);
return null;
};
audit_log = log_ptr;
}
const fs = Filesystem.init(allocator, root_span, audit_log);
const fs_ptr = allocator.create(Filesystem) catch {
if (audit_log) |log| allocator.destroy(log);
return null;
};
fs_ptr.* = fs;
return @ptrCast(fs_ptr);
}
/// Get error string (C ABI)
export fn valence_strerror(error_code: ValenceError) [*:0]const u8 {
const err: PosixError = @enumFromInt(error_code);
return switch (err) {
.success => "Success",
.eexist => "File exists",
.enoent => "No such file or directory",
.eacces => "Permission denied",
.enotdir => "Not a directory",
.eisdir => "Is a directory",
.enotempty => "Directory not empty",
.einval => "Invalid argument",
.enomem => "Out of memory",
.eio => "I/O error",
.eloop => "Too many symbolic links",
.enametoolong => "File name too long",
};
}
/// Get proof reference (C ABI)
export fn valence_proof_reference(operation: [*:0]const u8) ?[*:0]const u8 {
const op = std.mem.span(operation);
if (mem.eql(u8, op, "mkdir")) {
return "proofs/coq/filesystem_model.v:L45-L62";
} else if (mem.eql(u8, op, "rmdir")) {
return "proofs/coq/filesystem_model.v:L45-L62";
} else if (mem.eql(u8, op, "create_file")) {
return "proofs/coq/file_operations.v:L32-L48";
} else if (mem.eql(u8, op, "delete_file")) {
return "proofs/coq/file_operations.v:L32-L48";
} else if (mem.eql(u8, op, "copy")) {
return "proofs/z3/copy_move_operations.smt2:L140-L163";
} else if (mem.eql(u8, op, "move")) {
return "proofs/z3/copy_move_operations.smt2:L254-L269";
} else if (mem.eql(u8, op, "obliterate")) {
return "proofs/coq/rmo_operations.v:L85-L115";
}
return null;
}
// =========================================================
// TESTS - Validate against Coq theorems
// =========================================================
test "mkdir_rmdir_reversible" {
// This test validates Coq theorem: mkdir_rmdir_reversible
const allocator = std.testing.allocator;
var fs = Filesystem.init(allocator, "/tmp/vsh_test", null);
// Ensure clean state
posix_fs.makeDirAbsolute("/tmp/vsh_test") catch {};
posix_fs.deleteDirAbsolute("/tmp/vsh_test/test_dir") catch {};
// Initial state: directory does not exist
try std.testing.expect(!fs.pathExists("test_dir"));
// mkdir succeeds
const mkdir_result = fs.mkdir("test_dir");
try std.testing.expect(mkdir_result.isOk());
try std.testing.expect(fs.pathExists("test_dir"));
try std.testing.expect(fs.isDirectory("test_dir"));
// rmdir reverses mkdir
const rmdir_result = fs.rmdir("test_dir");
try std.testing.expect(rmdir_result.isOk());
// Final state = initial state (theorem verified)
try std.testing.expect(!fs.pathExists("test_dir"));
// Cleanup
posix_fs.deleteDirAbsolute("/tmp/vsh_test") catch {};
}
test "create_delete_file_reversible" {
// This test validates Coq theorem: create_delete_file_reversible
const allocator = std.testing.allocator;
var fs = Filesystem.init(allocator, "/tmp/vsh_test", null);
posix_fs.makeDirAbsolute("/tmp/vsh_test") catch {};
posix_fs.deleteFileAbsolute("/tmp/vsh_test/test_file") catch {};
// Initial state
try std.testing.expect(!fs.pathExists("test_file"));
// create_file succeeds
const create_result = fs.createFile("test_file");
try std.testing.expect(create_result.isOk());
try std.testing.expect(fs.pathExists("test_file"));
try std.testing.expect(fs.isFile("test_file"));
// delete_file reverses create_file
const delete_result = fs.deleteFile("test_file");
try std.testing.expect(delete_result.isOk());
// Final state = initial state
try std.testing.expect(!fs.pathExists("test_file"));
posix_fs.deleteDirAbsolute("/tmp/vsh_test") catch {};
}
test "posix_error_eexist" {
// Validates Coq: precondition failure returns EEXIST
const allocator = std.testing.allocator;
var fs = Filesystem.init(allocator, "/tmp/vsh_test", null);
posix_fs.makeDirAbsolute("/tmp/vsh_test") catch {};
posix_fs.makeDirAbsolute("/tmp/vsh_test/existing") catch {};
// mkdir on existing path should fail with EEXIST
const result = fs.mkdir("existing");
try std.testing.expect(!result.isOk());
try std.testing.expect(result.unwrapErr() == .eexist);
posix_fs.deleteDirAbsolute("/tmp/vsh_test/existing") catch {};
posix_fs.deleteDirAbsolute("/tmp/vsh_test") catch {};
}
test "posix_error_enoent" {
// Validates Coq: precondition failure returns ENOENT
const allocator = std.testing.allocator;
var fs = Filesystem.init(allocator, "/tmp/vsh_test", null);
posix_fs.makeDirAbsolute("/tmp/vsh_test") catch {};
// rmdir on non-existent path should fail with ENOENT
const result = fs.rmdir("nonexistent");
try std.testing.expect(!result.isOk());
try std.testing.expect(result.unwrapErr() == .enoent);
posix_fs.deleteDirAbsolute("/tmp/vsh_test") catch {};
}