| ... | @@ -3,6 +3,7 @@ const os = std.os; | ... | @@ -3,6 +3,7 @@ const os = std.os; |
| 3 | const testing = std.testing; | 3 | const testing = std.testing; |
| 4 | const expect = testing.expect; | 4 | const expect = testing.expect; |
| 5 | const expectEqual = testing.expectEqual; | 5 | const expectEqual = testing.expectEqual; |
| | 6 | const expectError = testing.expectError; |
| 6 | const io = std.io; | 7 | const io = std.io; |
| 7 | const fs = std.fs; | 8 | const fs = std.fs; |
| 8 | const mem = std.mem; | 9 | const mem = std.mem; |
| ... | @@ -19,6 +20,95 @@ const tmpDir = std.testing.tmpDir; | ... | @@ -19,6 +20,95 @@ const tmpDir = std.testing.tmpDir; |
| 19 | const Dir = std.fs.Dir; | 20 | const Dir = std.fs.Dir; |
| 20 | const ArenaAllocator = std.heap.ArenaAllocator; | 21 | const ArenaAllocator = std.heap.ArenaAllocator; |
| 21 | | 22 | |
| | 23 | test "open smoke test" { |
| | 24 | if (builtin.os.tag == .windows) return error.SkipZigTest; |
| | 25 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| | 26 | |
| | 27 | // TODO verify file attributes using `fstat` |
| | 28 | |
| | 29 | var tmp = tmpDir(.{}); |
| | 30 | defer tmp.cleanup(); |
| | 31 | |
| | 32 | // Get base abs path |
| | 33 | var arena = ArenaAllocator.init(testing.allocator); |
| | 34 | defer arena.deinit(); |
| | 35 | |
| | 36 | const base_path = blk: { |
| | 37 | const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); |
| | 38 | break :blk try fs.realpathAlloc(&arena.allocator, relative_path); |
| | 39 | }; |
| | 40 | |
| | 41 | var file_path: []u8 = undefined; |
| | 42 | var fd: os.fd_t = undefined; |
| | 43 | |
| | 44 | // Create some file using `open`. |
| | 45 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); |
| | 46 | fd = try os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666); |
| | 47 | os.close(fd); |
| | 48 | |
| | 49 | // Try this again with the same flags. This op should fail with error.PathAlreadyExists. |
| | 50 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); |
| | 51 | expectError(error.PathAlreadyExists, os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666)); |
| | 52 | |
| | 53 | // Try opening without `O_EXCL` flag. |
| | 54 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); |
| | 55 | fd = try os.open(file_path, os.O_RDWR | os.O_CREAT, 0o666); |
| | 56 | os.close(fd); |
| | 57 | |
| | 58 | // Try opening as a directory which should fail. |
| | 59 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); |
| | 60 | expectError(error.NotDir, os.open(file_path, os.O_RDWR | os.O_DIRECTORY, 0o666)); |
| | 61 | |
| | 62 | // Create some directory |
| | 63 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" }); |
| | 64 | try os.mkdir(file_path, 0o666); |
| | 65 | |
| | 66 | // Open dir using `open` |
| | 67 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" }); |
| | 68 | fd = try os.open(file_path, os.O_RDONLY | os.O_DIRECTORY, 0o666); |
| | 69 | os.close(fd); |
| | 70 | |
| | 71 | // Try opening as file which should fail. |
| | 72 | file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" }); |
| | 73 | expectError(error.IsDir, os.open(file_path, os.O_RDWR, 0o666)); |
| | 74 | } |
| | 75 | |
| | 76 | test "openat smoke test" { |
| | 77 | if (builtin.os.tag == .windows) return error.SkipZigTest; |
| | 78 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| | 79 | |
| | 80 | // TODO verify file attributes using `fstatat` |
| | 81 | |
| | 82 | var tmp = tmpDir(.{}); |
| | 83 | defer tmp.cleanup(); |
| | 84 | |
| | 85 | var fd: os.fd_t = undefined; |
| | 86 | |
| | 87 | // Create some file using `openat`. |
| | 88 | fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666); |
| | 89 | os.close(fd); |
| | 90 | |
| | 91 | // Try this again with the same flags. This op should fail with error.PathAlreadyExists. |
| | 92 | expectError(error.PathAlreadyExists, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666)); |
| | 93 | |
| | 94 | // Try opening without `O_EXCL` flag. |
| | 95 | fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT, 0o666); |
| | 96 | os.close(fd); |
| | 97 | |
| | 98 | // Try opening as a directory which should fail. |
| | 99 | expectError(error.NotDir, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_DIRECTORY, 0o666)); |
| | 100 | |
| | 101 | // Create some directory |
| | 102 | try os.mkdirat(tmp.dir.fd, "some_dir", 0o666); |
| | 103 | |
| | 104 | // Open dir using `open` |
| | 105 | fd = try os.openat(tmp.dir.fd, "some_dir", os.O_RDONLY | os.O_DIRECTORY, 0o666); |
| | 106 | os.close(fd); |
| | 107 | |
| | 108 | // Try opening as file which should fail. |
| | 109 | expectError(error.IsDir, os.openat(tmp.dir.fd, "some_dir", os.O_RDWR, 0o666)); |
| | 110 | } |
| | 111 | |
| 22 | test "symlink with relative paths" { | 112 | test "symlink with relative paths" { |
| 23 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 113 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 24 | | 114 | |