@@ -329,14 +329,8 @@ test "accessAbsolute" {
329
329
var tmp = tmpDir (.{});
330
330
defer tmp .cleanup ();
331
331
332
- var arena = ArenaAllocator .init (testing .allocator );
333
- defer arena .deinit ();
334
- const allocator = arena .allocator ();
335
-
336
- const base_path = blk : {
337
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ] });
338
- break :blk try fs .realpathAlloc (allocator , relative_path );
339
- };
332
+ const base_path = try tmp .dir .realpathAlloc (testing .allocator , "." );
333
+ defer testing .allocator .free (base_path );
340
334
341
335
try fs .accessAbsolute (base_path , .{});
342
336
}
@@ -347,32 +341,62 @@ test "openDirAbsolute" {
347
341
var tmp = tmpDir (.{});
348
342
defer tmp .cleanup ();
349
343
344
+ const tmp_ino = (try tmp .dir .stat ()).inode ;
345
+
350
346
try tmp .dir .makeDir ("subdir" );
351
- var arena = ArenaAllocator .init (testing .allocator );
352
- defer arena .deinit ();
353
- const allocator = arena .allocator ();
347
+ const sub_path = try tmp .dir .realpathAlloc (testing .allocator , "subdir" );
348
+ defer testing .allocator .free (sub_path );
354
349
355
- const base_path = blk : {
356
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ], "subdir" });
357
- break :blk try fs .realpathAlloc (allocator , relative_path );
358
- };
350
+ // Can open subdir
351
+ var tmp_sub = try fs .openDirAbsolute (sub_path , .{});
352
+ defer tmp_sub .close ();
353
+
354
+ const sub_ino = (try tmp_sub .stat ()).inode ;
355
+
356
+ {
357
+ // Can open parent
358
+ const dir_path = try fs .path .join (testing .allocator , &.{ sub_path , ".." });
359
+ defer testing .allocator .free (dir_path );
360
+
361
+ var dir = try fs .openDirAbsolute (dir_path , .{});
362
+ defer dir .close ();
363
+
364
+ const ino = (try dir .stat ()).inode ;
365
+ try testing .expectEqual (tmp_ino , ino );
366
+ }
359
367
360
368
{
361
- var dir = try fs .openDirAbsolute (base_path , .{});
369
+ // Can open subdir + "."
370
+ const dir_path = try fs .path .join (testing .allocator , &.{ sub_path , "." });
371
+ defer testing .allocator .free (dir_path );
372
+
373
+ var dir = try fs .openDirAbsolute (dir_path , .{});
362
374
defer dir .close ();
375
+
376
+ const ino = (try dir .stat ()).inode ;
377
+ try testing .expectEqual (sub_ino , ino );
363
378
}
364
379
365
- for ([_ ][]const u8 { "." , ".." }) | sub_path | {
366
- const dir_path = try fs .path .join (allocator , &.{ base_path , sub_path });
380
+ {
381
+ // Can open subdir + "..", with extra "."
382
+ const dir_path = try fs .path .join (testing .allocator , &.{ sub_path , "." , ".." , "." });
383
+ defer testing .allocator .free (dir_path );
384
+
367
385
var dir = try fs .openDirAbsolute (dir_path , .{});
368
386
defer dir .close ();
387
+
388
+ const ino = (try dir .stat ()).inode ;
389
+ try testing .expectEqual (tmp_ino , ino );
369
390
}
370
391
}
371
392
372
393
test "openDir cwd parent '..'" {
373
- if (native_os == .wasi ) return error .SkipZigTest ;
374
-
375
- var dir = try fs .cwd ().openDir (".." , .{});
394
+ var dir = fs .cwd ().openDir (".." , .{}) catch | err | {
395
+ if (native_os == .wasi and err == error .AccessDenied ) {
396
+ return ; // This is okay. WASI disallows escaping from the fs sandbox
397
+ }
398
+ return err ;
399
+ };
376
400
defer dir .close ();
377
401
}
378
402
@@ -388,7 +412,15 @@ test "openDir non-cwd parent '..'" {
388
412
var subdir = try tmp .dir .makeOpenPath ("subdir" , .{});
389
413
defer subdir .close ();
390
414
391
- var dir = try subdir .openDir (".." , .{});
415
+ var dir = subdir .openDir (".." , .{}) catch | err | {
416
+ if (native_os == .wasi and err == error .AccessDenied ) {
417
+ // This is odd (we're asking for the parent of a subdirectory,
418
+ // which should be safely inside the sandbox), but this is the
419
+ // current wasmtime behavior (with and without libc).
420
+ return error .SkipZigTest ;
421
+ }
422
+ return err ;
423
+ };
392
424
defer dir .close ();
393
425
394
426
if (supports_absolute_paths ) {
@@ -421,10 +453,7 @@ test "readLinkAbsolute" {
421
453
defer arena .deinit ();
422
454
const allocator = arena .allocator ();
423
455
424
- const base_path = blk : {
425
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ] });
426
- break :blk try fs .realpathAlloc (allocator , relative_path );
427
- };
456
+ const base_path = try tmp .dir .realpathAlloc (allocator , "." );
428
457
429
458
{
430
459
const target_path = try fs .path .join (allocator , &.{ base_path , "file.txt" });
@@ -770,17 +799,24 @@ test "file operations on directories" {
770
799
try testing .expectError (error .IsDir , ctx .dir .createFile (test_dir_name , .{}));
771
800
try testing .expectError (error .IsDir , ctx .dir .deleteFile (test_dir_name ));
772
801
switch (native_os ) {
773
- // no error when reading a directory.
774
- .dragonfly , .netbsd = > {},
775
- // Currently, WASI will return error.Unexpected (via ENOTCAPABLE) when attempting fd_read on a directory handle.
776
- // TODO: Re-enable on WASI once https://github.com/bytecodealliance/wasmtime/issues/1935 is resolved.
777
- .wasi = > {},
802
+ .dragonfly , .netbsd = > {
803
+ // no error when reading a directory. See https://github.com/ziglang/zig/issues/5732
804
+ const buf = try ctx .dir .readFileAlloc (testing .allocator , test_dir_name , std .math .maxInt (usize ));
805
+ testing .allocator .free (buf );
806
+ },
807
+ .wasi = > {
808
+ // WASI return EBADF, which gets mapped to NotOpenForReading.
809
+ // See https://github.com/bytecodealliance/wasmtime/issues/1935
810
+ try testing .expectError (error .NotOpenForReading , ctx .dir .readFileAlloc (testing .allocator , test_dir_name , std .math .maxInt (usize )));
811
+ },
778
812
else = > {
779
813
try testing .expectError (error .IsDir , ctx .dir .readFileAlloc (testing .allocator , test_dir_name , std .math .maxInt (usize )));
780
814
},
781
815
}
816
+
782
817
// Note: The `.mode = .read_write` is necessary to ensure the error occurs on all platforms.
783
818
// TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732
819
+ // Beware, wasmtime v23.0.2 (at least) does not error here
784
820
try testing .expectError (error .IsDir , ctx .dir .openFile (test_dir_name , .{ .mode = .read_write }));
785
821
786
822
if (ctx .path_type == .absolute and supports_absolute_paths ) {
@@ -1004,10 +1040,7 @@ test "renameAbsolute" {
1004
1040
defer arena .deinit ();
1005
1041
const allocator = arena .allocator ();
1006
1042
1007
- const base_path = blk : {
1008
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp_dir .sub_path [0.. ] });
1009
- break :blk try fs .realpathAlloc (allocator , relative_path );
1010
- };
1043
+ const base_path = try tmp_dir .dir .realpathAlloc (allocator , "." );
1011
1044
1012
1045
try testing .expectError (error .FileNotFound , fs .renameAbsolute (
1013
1046
try fs .path .join (allocator , &.{ base_path , "missing_file_name" }),
@@ -1397,7 +1430,6 @@ test "sendfile" {
1397
1430
defer tmp .cleanup ();
1398
1431
1399
1432
try tmp .dir .makePath ("os_test_tmp" );
1400
- defer tmp .dir .deleteTree ("os_test_tmp" ) catch {};
1401
1433
1402
1434
var dir = try tmp .dir .openDir ("os_test_tmp" , .{});
1403
1435
defer dir .close ();
@@ -1462,7 +1494,6 @@ test "copyRangeAll" {
1462
1494
defer tmp .cleanup ();
1463
1495
1464
1496
try tmp .dir .makePath ("os_test_tmp" );
1465
- defer tmp .dir .deleteTree ("os_test_tmp" ) catch {};
1466
1497
1467
1498
var dir = try tmp .dir .openDir ("os_test_tmp" , .{});
1468
1499
defer dir .close ();
@@ -1781,10 +1812,7 @@ test "'.' and '..' in absolute functions" {
1781
1812
defer arena .deinit ();
1782
1813
const allocator = arena .allocator ();
1783
1814
1784
- const base_path = blk : {
1785
- const relative_path = try fs .path .join (allocator , &.{ ".zig-cache" , "tmp" , tmp .sub_path [0.. ] });
1786
- break :blk try fs .realpathAlloc (allocator , relative_path );
1787
- };
1815
+ const base_path = try tmp .dir .realpathAlloc (allocator , "." );
1788
1816
1789
1817
const subdir_path = try fs .path .join (allocator , &.{ base_path , "./subdir" });
1790
1818
try fs .makeDirAbsolute (subdir_path );
0 commit comments