authorgravatar for gereeter+code@gmail.comJonathan S <gereeter+code@gmail.com> 2019-11-22 15:56:46-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-25 23:46:47-05:00
log07120c87451636f65d2917f0355b4493034a2c0f
tree182b0f7778fe835a0820a733a8c68ac246b4e724
parent17bc1f62a51ad5fc44dc97acf6ddb33e6d00b0c4

Use `O_PATH` where available in `std.fs.Dir.openPathTraverse`.


1 files changed, 11 insertions(+), 3 deletions(-)

lib/std/fs.zig+11-3
......@@ -817,8 +817,13 @@ pub const Dir = struct {
817817
818818 /// Same as `openDirTraverse` except the parameter is null-terminated.
819819 pub fn openDirTraverseC(self: Dir, sub_path_c: [*:0]const u8) OpenError!Dir {
820 // TODO: use O_PATH where supported
821 return self.openDirListC(sub_path_c);
820 if (builtin.os == .windows) {
821 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
822 return self.openDirTraverseW(&sub_path_w);
823 } else {
824 const O_PATH = if (@hasDecl(os, "O_PATH")) os.O_PATH else 0;
825 return self.openDirFlagsC(sub_path_c, os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC | O_PATH);
826 }
822827 }
823828
824829 /// Same as `openDirList` except the parameter is null-terminated.
......@@ -826,9 +831,12 @@ pub const Dir = struct {
826831 if (builtin.os == .windows) {
827832 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
828833 return self.openDirListW(&sub_path_w);
834 } else {
835 return self.openDirFlagsC(sub_path_c, os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC);
829836 }
837 }
830838
831 const flags = os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC;
839 fn openDirFlagsC(self: Dir, sub_path_c: [*]const u8, flags: u32) OpenError!Dir {
832840 const fd = os.openatC(self.fd, sub_path_c, flags, 0) catch |err| switch (err) {
833841 error.FileTooBig => unreachable, // can't happen for directories
834842 error.IsDir => unreachable, // we're providing O_DIRECTORY