authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 16:42:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 16:42:58-04:00
log7a361751e563c131399050f339dc47edf3c32325
tree5141e62de6134816b62c7ff1399dcdc651f63b11
parentb1537b525fa0cd8d51ff89519254db0f066fc04b
parent46ffc798b6d9bb7be8b016ef7529092d647151ee
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'LemonBoy-travbug'

closes #4747

20 files changed, 157 insertions(+), 138 deletions(-)

doc/docgen.zig+1-1
......@@ -48,7 +48,7 @@ pub fn main() !void {
4848 var toc = try genToc(allocator, &tokenizer);
4949
5050 try fs.cwd().makePath(tmp_dir_name);
51 defer fs.deleteTree(tmp_dir_name) catch {};
51 defer fs.cwd().deleteTree(tmp_dir_name) catch {};
5252
5353 try genHtml(allocator, &tokenizer, &toc, buffered_out_stream.outStream(), zig_exe);
5454 try buffered_out_stream.flush();
lib/std/build.zig+4-4
......@@ -377,7 +377,7 @@ pub const Builder = struct {
377377 if (self.verbose) {
378378 warn("rm {}\n", .{full_path});
379379 }
380 fs.deleteTree(full_path) catch {};
380 fs.cwd().deleteTree(full_path) catch {};
381381 }
382382
383383 // TODO remove empty directories
......@@ -2156,10 +2156,10 @@ pub const LibExeObjStep = struct {
21562156 const build_output_dir = mem.trimRight(u8, output_dir_nl, "\r\n");
21572157
21582158 if (self.output_dir) |output_dir| {
2159 var src_dir = try std.fs.cwd().openDirTraverse(build_output_dir);
2159 var src_dir = try std.fs.cwd().openDir(build_output_dir, .{ .iterate = true });
21602160 defer src_dir.close();
21612161
2162 var dest_dir = try std.fs.cwd().openDirList(output_dir);
2162 var dest_dir = try std.fs.cwd().openDir(output_dir, .{});
21632163 defer dest_dir.close();
21642164
21652165 var it = src_dir.iterate();
......@@ -2365,7 +2365,7 @@ pub const RemoveDirStep = struct {
23652365 const self = @fieldParentPtr(RemoveDirStep, "step", step);
23662366
23672367 const full_path = self.builder.pathFromRoot(self.dir_path);
2368 fs.deleteTree(full_path) catch |err| {
2368 fs.cwd().deleteTree(full_path) catch |err| {
23692369 warn("Unable to remove {}: {}\n", .{ full_path, @errorName(err) });
23702370 return err;
23712371 };
lib/std/build/write_file.zig+1-1
......@@ -78,7 +78,7 @@ pub const WriteFileStep = struct {
7878 warn("unable to make path {}: {}\n", .{ self.output_dir, @errorName(err) });
7979 return err;
8080 };
81 var dir = try fs.cwd().openDirTraverse(self.output_dir);
81 var dir = try fs.cwd().openDir(self.output_dir, .{});
8282 defer dir.close();
8383 for (self.files.toSliceConst()) |file| {
8484 dir.writeFile(file.basename, file.bytes) catch |err| {
lib/std/fs.zig+64-111
......@@ -261,28 +261,6 @@ pub fn deleteDirW(dir_path: [*:0]const u16) !void {
261261 return os.rmdirW(dir_path);
262262}
263263
264/// Removes a symlink, file, or directory.
265/// If `full_path` is relative, this is equivalent to `Dir.deleteTree` with the
266/// current working directory as the open directory handle.
267/// If `full_path` is absolute, this is equivalent to `Dir.deleteTree` with the
268/// base directory.
269pub fn deleteTree(full_path: []const u8) !void {
270 if (path.isAbsolute(full_path)) {
271 const dirname = path.dirname(full_path) orelse return error{
272 /// Attempt to remove the root file system path.
273 /// This error is unreachable if `full_path` is relative.
274 CannotDeleteRootDirectory,
275 }.CannotDeleteRootDirectory;
276
277 var dir = try cwd().openDirList(dirname);
278 defer dir.close();
279
280 return dir.deleteTree(path.basename(full_path));
281 } else {
282 return cwd().deleteTree(full_path);
283 }
284}
285
286264pub const Dir = struct {
287265 fd: os.fd_t,
288266
......@@ -339,7 +317,7 @@ pub const Dir = struct {
339317 if (rc == 0) return null;
340318 if (rc < 0) {
341319 switch (os.errno(rc)) {
342 os.EBADF => unreachable,
320 os.EBADF => unreachable, // Dir is invalid or was opened without iteration ability
343321 os.EFAULT => unreachable,
344322 os.ENOTDIR => unreachable,
345323 os.EINVAL => unreachable,
......@@ -388,7 +366,7 @@ pub const Dir = struct {
388366 );
389367 switch (os.errno(rc)) {
390368 0 => {},
391 os.EBADF => unreachable,
369 os.EBADF => unreachable, // Dir is invalid or was opened without iteration ability
392370 os.EFAULT => unreachable,
393371 os.ENOTDIR => unreachable,
394372 os.EINVAL => unreachable,
......@@ -444,7 +422,7 @@ pub const Dir = struct {
444422 const rc = os.linux.getdents64(self.dir.fd, &self.buf, self.buf.len);
445423 switch (os.linux.getErrno(rc)) {
446424 0 => {},
447 os.EBADF => unreachable,
425 os.EBADF => unreachable, // Dir is invalid or was opened without iteration ability
448426 os.EFAULT => unreachable,
449427 os.ENOTDIR => unreachable,
450428 os.EINVAL => unreachable,
......@@ -518,7 +496,8 @@ pub const Dir = struct {
518496 self.end_index = io.Information;
519497 switch (rc) {
520498 .SUCCESS => {},
521 .ACCESS_DENIED => return error.AccessDenied,
499 .ACCESS_DENIED => return error.AccessDenied, // Double-check that the Dir was opened with iteration ability
500
522501 else => return w.unexpectedStatus(rc),
523502 }
524503 }
......@@ -596,16 +575,6 @@ pub const Dir = struct {
596575 DeviceBusy,
597576 } || os.UnexpectedError;
598577
599 /// Deprecated; call `cwd().openDirList` directly.
600 pub fn open(dir_path: []const u8) OpenError!Dir {
601 return cwd().openDirList(dir_path);
602 }
603
604 /// Deprecated; call `cwd().openDirListC` directly.
605 pub fn openC(dir_path_c: [*:0]const u8) OpenError!Dir {
606 return cwd().openDirListC(dir_path_c);
607 }
608
609578 pub fn close(self: *Dir) void {
610579 if (need_async_thread) {
611580 std.event.Loop.instance.?.close(self.fd);
......@@ -792,79 +761,61 @@ pub const Dir = struct {
792761 try os.fchdir(self.fd);
793762 }
794763
795 /// Deprecated; call `openDirList` directly.
796 pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {
797 return self.openDirList(sub_path);
798 }
799
800 /// Deprecated; call `openDirListC` directly.
801 pub fn openDirC(self: Dir, sub_path_c: [*:0]const u8) OpenError!Dir {
802 return self.openDirListC(sub_path_c);
803 }
804
805 /// Opens a directory at the given path with the ability to access subpaths
806 /// of the result. Calling `iterate` on the result is illegal behavior; to
807 /// list the contents of a directory, open it with `openDirList`.
808 ///
809 /// Call `close` on the result when done.
810 ///
811 /// Asserts that the path parameter has no null bytes.
812 /// TODO collapse this and `openDirList` into one function with an options parameter
813 pub fn openDirTraverse(self: Dir, sub_path: []const u8) OpenError!Dir {
814 if (builtin.os.tag == .windows) {
815 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
816 return self.openDirTraverseW(&sub_path_w);
817 }
764 pub const OpenDirOptions = struct {
765 /// `true` means the opened directory can be used as the `Dir` parameter
766 /// for functions which operate based on an open directory handle. When `false`,
767 /// such operations are Illegal Behavior.
768 access_sub_paths: bool = true,
818769
819 const sub_path_c = try os.toPosixPath(sub_path);
820 return self.openDirTraverseC(&sub_path_c);
821 }
770 /// `true` means the opened directory can be scanned for the files and sub-directories
771 /// of the result. It means the `iterate` function can be called.
772 iterate: bool = false,
773 };
822774
823 /// Opens a directory at the given path with the ability to access subpaths and list contents
824 /// of the result. If the ability to list contents is unneeded, `openDirTraverse` acts the
825 /// same and may be more efficient.
826 ///
827 /// Call `close` on the result when done.
775 /// Opens a directory at the given path. The directory is a system resource that remains
776 /// open until `close` is called on the result.
828777 ///
829778 /// Asserts that the path parameter has no null bytes.
830 /// TODO collapse this and `openDirTraverse` into one function with an options parameter
831 pub fn openDirList(self: Dir, sub_path: []const u8) OpenError!Dir {
779 pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!Dir {
832780 if (builtin.os.tag == .windows) {
833781 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
834 return self.openDirListW(&sub_path_w);
782 return self.openDirW(&sub_path_w, args);
783 } else {
784 const sub_path_c = try os.toPosixPath(sub_path);
785 return self.openDirC(&sub_path_c, args);
835786 }
836
837 const sub_path_c = try os.toPosixPath(sub_path);
838 return self.openDirListC(&sub_path_c);
839787 }
840788
841 /// Same as `openDirTraverse` except the parameter is null-terminated.
842 pub fn openDirTraverseC(self: Dir, sub_path_c: [*:0]const u8) OpenError!Dir {
789 /// Same as `openDir` except the parameter is null-terminated.
790 pub fn openDirC(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) OpenError!Dir {
843791 if (builtin.os.tag == .windows) {
844792 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
845 return self.openDirTraverseW(&sub_path_w);
846 } else {
793 return self.openDirW(&sub_path_w, args);
794 } else if (!args.iterate) {
847795 const O_PATH = if (@hasDecl(os, "O_PATH")) os.O_PATH else 0;
848 return self.openDirFlagsC(sub_path_c, os.O_RDONLY | os.O_CLOEXEC | O_PATH);
796 return self.openDirFlagsC(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | os.O_CLOEXEC | O_PATH);
797 } else {
798 return self.openDirFlagsC(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | os.O_CLOEXEC);
849799 }
850800 }
851801
852 /// Same as `openDirList` except the parameter is null-terminated.
853 pub fn openDirListC(self: Dir, sub_path_c: [*:0]const u8) OpenError!Dir {
854 if (builtin.os.tag == .windows) {
855 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
856 return self.openDirListW(&sub_path_w);
857 } else {
858 return self.openDirFlagsC(sub_path_c, os.O_RDONLY | os.O_CLOEXEC);
859 }
802 /// Same as `openDir` except the path parameter is WTF-16 encoded, NT-prefixed.
803 /// This function asserts the target OS is Windows.
804 pub fn openDirW(self: Dir, sub_path_w: [*:0]const u16, args: OpenDirOptions) OpenError!Dir {
805 const w = os.windows;
806 // TODO remove some of these flags if args.access_sub_paths is false
807 const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA |
808 w.SYNCHRONIZE | w.FILE_TRAVERSE;
809 const flags: u32 = if (args.iterate) base_flags | w.FILE_LIST_DIRECTORY else base_flags;
810 return self.openDirAccessMaskW(sub_path_w, flags);
860811 }
861812
813 /// `flags` must contain `os.O_DIRECTORY`.
862814 fn openDirFlagsC(self: Dir, sub_path_c: [*:0]const u8, flags: u32) OpenError!Dir {
863 const os_flags = flags | os.O_DIRECTORY;
864815 const result = if (need_async_thread)
865 std.event.Loop.instance.?.openatZ(self.fd, sub_path_c, os_flags, 0)
816 std.event.Loop.instance.?.openatZ(self.fd, sub_path_c, flags, 0)
866817 else
867 os.openatC(self.fd, sub_path_c, os_flags, 0);
818 os.openatC(self.fd, sub_path_c, flags, 0);
868819 const fd = result catch |err| switch (err) {
869820 error.FileTooBig => unreachable, // can't happen for directories
870821 error.IsDir => unreachable, // we're providing O_DIRECTORY
......@@ -875,22 +826,6 @@ pub const Dir = struct {
875826 return Dir{ .fd = fd };
876827 }
877828
878 /// Same as `openDirTraverse` except the path parameter is UTF16LE, NT-prefixed.
879 /// This function is Windows-only.
880 pub fn openDirTraverseW(self: Dir, sub_path_w: [*:0]const u16) OpenError!Dir {
881 const w = os.windows;
882
883 return self.openDirAccessMaskW(sub_path_w, w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA | w.SYNCHRONIZE | w.FILE_TRAVERSE);
884 }
885
886 /// Same as `openDirList` except the path parameter is UTF16LE, NT-prefixed.
887 /// This function is Windows-only.
888 pub fn openDirListW(self: Dir, sub_path_w: [*:0]const u16) OpenError!Dir {
889 const w = os.windows;
890
891 return self.openDirAccessMaskW(sub_path_w, w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA | w.SYNCHRONIZE | w.FILE_TRAVERSE | w.FILE_LIST_DIRECTORY);
892 }
893
894829 fn openDirAccessMaskW(self: Dir, sub_path_w: [*:0]const u16, access_mask: u32) OpenError!Dir {
895830 const w = os.windows;
896831
......@@ -1111,7 +1046,7 @@ pub const Dir = struct {
11111046 error.Unexpected,
11121047 => |e| return e,
11131048 }
1114 var dir = self.openDirList(sub_path) catch |err| switch (err) {
1049 var dir = self.openDir(sub_path, .{ .iterate = true }) catch |err| switch (err) {
11151050 error.NotDir => {
11161051 if (got_access_denied) {
11171052 return error.AccessDenied;
......@@ -1144,7 +1079,6 @@ pub const Dir = struct {
11441079
11451080 var dir_name_buf: [MAX_PATH_BYTES]u8 = undefined;
11461081 var dir_name: []const u8 = sub_path;
1147 var parent_dir = self;
11481082
11491083 // Here we must avoid recursion, in order to provide O(1) memory guarantee of this function.
11501084 // Go through each entry and if it is not a directory, delete it. If it is a directory,
......@@ -1176,7 +1110,7 @@ pub const Dir = struct {
11761110 => |e| return e,
11771111 }
11781112
1179 const new_dir = dir.openDirList(entry.name) catch |err| switch (err) {
1113 const new_dir = dir.openDir(entry.name, .{ .iterate = true }) catch |err| switch (err) {
11801114 error.NotDir => {
11811115 if (got_access_denied) {
11821116 return error.AccessDenied;
......@@ -1349,7 +1283,7 @@ pub const Dir = struct {
13491283 }
13501284};
13511285
1352/// Returns an handle to the current working directory that is open for traversal.
1286/// Returns an handle to the current working directory. It is not opened with iteration capability.
13531287/// Closing the returned `Dir` is checked illegal behavior. Iterating over the result is illegal behavior.
13541288/// On POSIX targets, this function is comptime-callable.
13551289pub fn cwd() Dir {
......@@ -1427,6 +1361,25 @@ pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void
14271361 return cwd().deleteFileW(absolute_path_w);
14281362}
14291363
1364/// Removes a symlink, file, or directory.
1365/// This is equivalent to `Dir.deleteTree` with the base directory.
1366/// Asserts that the path is absolute. See `Dir.deleteTree` for a function that
1367/// operates on both absolute and relative paths.
1368/// Asserts that the path parameter has no null bytes.
1369pub fn deleteTreeAbsolute(absolute_path: []const u8) !void {
1370 assert(path.isAbsolute(absolute_path));
1371 const dirname = path.dirname(absolute_path) orelse return error{
1372 /// Attempt to remove the root file system path.
1373 /// This error is unreachable if `absolute_path` is relative.
1374 CannotDeleteRootDirectory,
1375 }.CannotDeleteRootDirectory;
1376
1377 var dir = try cwd().openDir(dirname, .{});
1378 defer dir.close();
1379
1380 return dir.deleteTree(path.basename(absolute_path));
1381}
1382
14301383pub const Walker = struct {
14311384 stack: std.ArrayList(StackItem),
14321385 name_buffer: std.Buffer,
......@@ -1461,7 +1414,7 @@ pub const Walker = struct {
14611414 try self.name_buffer.appendByte(path.sep);
14621415 try self.name_buffer.append(base.name);
14631416 if (base.kind == .Directory) {
1464 var new_dir = top.dir_it.dir.openDirList(base.name) catch |err| switch (err) {
1417 var new_dir = top.dir_it.dir.openDir(base.name, .{ .iterate = true }) catch |err| switch (err) {
14651418 error.NameTooLong => unreachable, // no path sep in base.name
14661419 else => |e| return e,
14671420 };
......@@ -1499,7 +1452,7 @@ pub const Walker = struct {
14991452pub fn walkPath(allocator: *Allocator, dir_path: []const u8) !Walker {
15001453 assert(!mem.endsWith(u8, dir_path, path.sep_str));
15011454
1502 var dir = try cwd().openDirList(dir_path);
1455 var dir = try cwd().openDir(dir_path, .{ .iterate = true });
15031456 errdefer dir.close();
15041457
15051458 var name_buffer = try std.Buffer.init(allocator, dir_path);
lib/std/fs/watch.zig+1-1
......@@ -619,7 +619,7 @@ test "write a file, watch it, write it again" {
619619 if (true) return error.SkipZigTest;
620620
621621 try fs.cwd().makePath(test_tmp_dir);
622 defer os.deleteTree(test_tmp_dir) catch {};
622 defer fs.cwd().deleteTree(test_tmp_dir) catch {};
623623
624624 const allocator = std.heap.page_allocator;
625625 return testFsWatch(&allocator);
lib/std/os.zig+25
......@@ -3163,6 +3163,31 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
31633163 }
31643164}
31653165
3166pub const FcntlError = error{
3167 PermissionDenied,
3168 FileBusy,
3169 ProcessFdQuotaExceeded,
3170 Locked,
3171} || UnexpectedError;
3172
3173pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize {
3174 while (true) {
3175 const rc = system.fcntl(fd, cmd, arg);
3176 switch (errno(rc)) {
3177 0 => return @intCast(usize, rc),
3178 EINTR => continue,
3179 EACCES => return error.Locked,
3180 EBADF => unreachable,
3181 EBUSY => return error.FileBusy,
3182 EINVAL => unreachable, // invalid parameters
3183 EPERM => return error.PermissionDenied,
3184 EMFILE => return error.ProcessFdQuotaExceeded,
3185 ENOTDIR => unreachable, // invalid parameter
3186 else => |err| return unexpectedErrno(err),
3187 }
3188 }
3189}
3190
31663191pub const RealPathError = error{
31673192 FileNotFound,
31683193 AccessDenied,
lib/std/os/bits/dragonfly.zig+2
......@@ -283,6 +283,8 @@ pub const F_LOCK = 1;
283283pub const F_TLOCK = 2;
284284pub const F_TEST = 3;
285285
286pub const FD_CLOEXEC = 1;
287
286288pub const AT_FDCWD = -328243;
287289pub const AT_SYMLINK_NOFOLLOW = 1;
288290pub const AT_REMOVEDIR = 2;
lib/std/os/bits/freebsd.zig+2
......@@ -355,6 +355,8 @@ pub const F_GETOWN_EX = 16;
355355
356356pub const F_GETOWNER_UIDS = 17;
357357
358pub const FD_CLOEXEC = 1;
359
358360pub const SEEK_SET = 0;
359361pub const SEEK_CUR = 1;
360362pub const SEEK_END = 2;
lib/std/os/bits/linux.zig+2
......@@ -136,6 +136,8 @@ pub const MAP_FIXED_NOREPLACE = 0x100000;
136136/// For anonymous mmap, memory could be uninitialized
137137pub const MAP_UNINITIALIZED = 0x4000000;
138138
139pub const FD_CLOEXEC = 1;
140
139141pub const F_OK = 0;
140142pub const X_OK = 1;
141143pub const W_OK = 2;
lib/std/os/bits/netbsd.zig+2
......@@ -312,6 +312,8 @@ pub const F_GETLK = 7;
312312pub const F_SETLK = 8;
313313pub const F_SETLKW = 9;
314314
315pub const FD_CLOEXEC = 1;
316
315317pub const SEEK_SET = 0;
316318pub const SEEK_CUR = 1;
317319pub const SEEK_END = 2;
lib/std/os/linux.zig+4
......@@ -588,6 +588,10 @@ pub fn waitpid(pid: pid_t, status: *u32, flags: u32) usize {
588588 return syscall4(SYS_wait4, @bitCast(usize, @as(isize, pid)), @ptrToInt(status), flags, 0);
589589}
590590
591pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) usize {
592 return syscall3(SYS_fcntl, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, cmd)), arg);
593}
594
591595var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime);
592596
593597// We must follow the C calling convention when we call into the VDSO
lib/std/os/test.zig+36-6
......@@ -1,7 +1,8 @@
11const std = @import("../std.zig");
22const os = std.os;
33const testing = std.testing;
4const expect = std.testing.expect;
4const expect = testing.expect;
5const expectEqual = testing.expectEqual;
56const io = std.io;
67const fs = std.fs;
78const mem = std.mem;
......@@ -19,8 +20,8 @@ test "makePath, put some files in it, deleteTree" {
1920 try fs.cwd().makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");
2021 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
2122 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
22 try fs.deleteTree("os_test_tmp");
23 if (fs.cwd().openDirTraverse("os_test_tmp")) |dir| {
23 try fs.cwd().deleteTree("os_test_tmp");
24 if (fs.cwd().openDir("os_test_tmp", .{})) |dir| {
2425 @panic("expected error");
2526 } else |err| {
2627 expect(err == error.FileNotFound);
......@@ -37,7 +38,7 @@ test "access file" {
3738
3839 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");
3940 try os.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", os.F_OK);
40 try fs.deleteTree("os_test_tmp");
41 try fs.cwd().deleteTree("os_test_tmp");
4142}
4243
4344fn testThreadIdFn(thread_id: *Thread.Id) void {
......@@ -46,9 +47,9 @@ fn testThreadIdFn(thread_id: *Thread.Id) void {
4647
4748test "sendfile" {
4849 try fs.cwd().makePath("os_test_tmp");
49 defer fs.deleteTree("os_test_tmp") catch {};
50 defer fs.cwd().deleteTree("os_test_tmp") catch {};
5051
51 var dir = try fs.cwd().openDirList("os_test_tmp");
52 var dir = try fs.cwd().openDir("os_test_tmp", .{});
5253 defer dir.close();
5354
5455 const line1 = "line1\n";
......@@ -446,3 +447,32 @@ test "getenv" {
446447 expect(os.getenvZ("BOGUSDOESNOTEXISTENVVAR") == null);
447448 }
448449}
450
451test "fcntl" {
452 if (builtin.os.tag == .windows)
453 return error.SkipZigTest;
454
455 const test_out_file = "os_tmp_test";
456
457 const file = try fs.cwd().createFile(test_out_file, .{});
458 defer {
459 file.close();
460 fs.cwd().deleteFile(test_out_file) catch {};
461 }
462
463 // Note: The test assumes createFile opens the file with O_CLOEXEC
464 {
465 const flags = try os.fcntl(file.handle, os.F_GETFD, 0);
466 expect((flags & os.FD_CLOEXEC) != 0);
467 }
468 {
469 _ = try os.fcntl(file.handle, os.F_SETFD, 0);
470 const flags = try os.fcntl(file.handle, os.F_GETFD, 0);
471 expect((flags & os.FD_CLOEXEC) == 0);
472 }
473 {
474 _ = try os.fcntl(file.handle, os.F_SETFD, os.FD_CLOEXEC);
475 const flags = try os.fcntl(file.handle, os.F_GETFD, 0);
476 expect((flags & os.FD_CLOEXEC) != 0);
477 }
478}
lib/std/zig/system.zig+1-1
......@@ -754,7 +754,7 @@ pub const NativeTargetInfo = struct {
754754 const rpath_list = mem.toSliceConst(u8, @ptrCast([*:0]u8, strtab[rpoff..].ptr));
755755 var it = mem.tokenize(rpath_list, ":");
756756 while (it.next()) |rpath| {
757 var dir = fs.cwd().openDirList(rpath) catch |err| switch (err) {
757 var dir = fs.cwd().openDir(rpath, .{}) catch |err| switch (err) {
758758 error.NameTooLong => unreachable,
759759 error.InvalidUtf8 => unreachable,
760760 error.BadPathName => unreachable,
src-self-hosted/compilation.zig+1-2
......@@ -520,8 +520,7 @@ pub const Compilation = struct {
520520
521521 if (comp.tmp_dir.getOrNull()) |tmp_dir_result|
522522 if (tmp_dir_result.*) |tmp_dir| {
523 // TODO evented I/O?
524 fs.deleteTree(tmp_dir) catch {};
523 fs.cwd().deleteTree(tmp_dir) catch {};
525524 } else |_| {};
526525 }
527526
src-self-hosted/libc_installation.zig+5-5
......@@ -280,7 +280,7 @@ pub const LibCInstallation = struct {
280280 // search in reverse order
281281 const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1);
282282 const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " ");
283 var search_dir = fs.cwd().openDirList(search_path) catch |err| switch (err) {
283 var search_dir = fs.cwd().openDir(search_path, .{}) catch |err| switch (err) {
284284 error.FileNotFound,
285285 error.NotDir,
286286 error.NoDevice,
......@@ -335,7 +335,7 @@ pub const LibCInstallation = struct {
335335 const stream = result_buf.outStream();
336336 try stream.print("{}\\Include\\{}\\ucrt", .{ search.path, search.version });
337337
338 var dir = fs.cwd().openDirList(result_buf.toSliceConst()) catch |err| switch (err) {
338 var dir = fs.cwd().openDir(result_buf.toSliceConst(), .{}) catch |err| switch (err) {
339339 error.FileNotFound,
340340 error.NotDir,
341341 error.NoDevice,
......@@ -382,7 +382,7 @@ pub const LibCInstallation = struct {
382382 const stream = result_buf.outStream();
383383 try stream.print("{}\\Lib\\{}\\ucrt\\{}", .{ search.path, search.version, arch_sub_dir });
384384
385 var dir = fs.cwd().openDirList(result_buf.toSliceConst()) catch |err| switch (err) {
385 var dir = fs.cwd().openDir(result_buf.toSliceConst(), .{}) catch |err| switch (err) {
386386 error.FileNotFound,
387387 error.NotDir,
388388 error.NoDevice,
......@@ -437,7 +437,7 @@ pub const LibCInstallation = struct {
437437 const stream = result_buf.outStream();
438438 try stream.print("{}\\Lib\\{}\\um\\{}", .{ search.path, search.version, arch_sub_dir });
439439
440 var dir = fs.cwd().openDirList(result_buf.toSliceConst()) catch |err| switch (err) {
440 var dir = fs.cwd().openDir(result_buf.toSliceConst(), .{}) catch |err| switch (err) {
441441 error.FileNotFound,
442442 error.NotDir,
443443 error.NoDevice,
......@@ -475,7 +475,7 @@ pub const LibCInstallation = struct {
475475
476476 try result_buf.append("\\include");
477477
478 var dir = fs.cwd().openDirList(result_buf.toSliceConst()) catch |err| switch (err) {
478 var dir = fs.cwd().openDir(result_buf.toSliceConst(), .{}) catch |err| switch (err) {
479479 error.FileNotFound,
480480 error.NotDir,
481481 error.NoDevice,
src-self-hosted/main.zig+1-1
......@@ -734,7 +734,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
734734 max_src_size,
735735 ) catch |err| switch (err) {
736736 error.IsDir, error.AccessDenied => {
737 var dir = try fs.cwd().openDirList(file_path);
737 var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
738738 defer dir.close();
739739
740740 var group = event.Group(FmtError!void).init(fmt.allocator);
src-self-hosted/print_targets.zig+1-1
......@@ -72,7 +72,7 @@ pub fn cmdTargets(
7272 };
7373 defer allocator.free(zig_lib_dir);
7474
75 var dir = try std.fs.cwd().openDirList(zig_lib_dir);
75 var dir = try std.fs.cwd().openDir(zig_lib_dir, .{});
7676 defer dir.close();
7777
7878 const vers_txt = try dir.readFileAlloc(allocator, "libc/glibc/vers.txt", 10 * 1024);
src-self-hosted/stage2.zig+1-1
......@@ -319,7 +319,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
319319 const source_code = io.readFileAlloc(fmt.allocator, file_path) catch |err| switch (err) {
320320 error.IsDir, error.AccessDenied => {
321321 // TODO make event based (and dir.next())
322 var dir = try fs.cwd().openDirList(file_path);
322 var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
323323 defer dir.close();
324324
325325 var dir_it = dir.iterate();
src-self-hosted/test.zig+2-2
......@@ -57,11 +57,11 @@ pub const TestContext = struct {
5757 errdefer allocator.free(self.zig_lib_dir);
5858
5959 try std.fs.cwd().makePath(tmp_dir_name);
60 errdefer std.fs.deleteTree(tmp_dir_name) catch {};
60 errdefer std.fs.cwd().deleteTree(tmp_dir_name) catch {};
6161 }
6262
6363 fn deinit(self: *TestContext) void {
64 std.fs.deleteTree(tmp_dir_name) catch {};
64 std.fs.cwd().deleteTree(tmp_dir_name) catch {};
6565 allocator.free(self.zig_lib_dir);
6666 self.zig_compiler.deinit();
6767 }
test/cli.zig+1-1
......@@ -36,7 +36,7 @@ pub fn main() !void {
3636 testMissingOutputPath,
3737 };
3838 for (test_fns) |testFn| {
39 try fs.deleteTree(dir_path);
39 try fs.cwd().deleteTree(dir_path);
4040 try fs.cwd().makeDir(dir_path);
4141 try testFn(zig_exe, dir_path);
4242 }