authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-01-15 18:11:54+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-03 13:25:43-05:00
log627618a38d291d9cc76c8e30e33cad60dc26cf11
tree147fb6c132985cb88461abb58fcf1d18a36ce5eb
parentdfb420e6d779b9b6d60a277401aadba2800e3572
signature Commit is signed but in an unrecognized format.

std: add Dir.changeDir as wrapper around fchdir


4 files changed, 23 insertions(+), 0 deletions(-)

lib/std/c.zig+1
...@@ -105,6 +105,7 @@ pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;...@@ -105,6 +105,7 @@ pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
105pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;105pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
106pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;106pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
107pub extern "c" fn chdir(path: [*:0]const u8) c_int;107pub extern "c" fn chdir(path: [*:0]const u8) c_int;
108pub extern "c" fn fchdir(fd: fd_t) c_int;
108pub extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int;109pub extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int;
109pub extern "c" fn dup(fd: fd_t) c_int;110pub extern "c" fn dup(fd: fd_t) c_int;
110pub extern "c" fn dup2(old_fd: fd_t, new_fd: fd_t) c_int;111pub extern "c" fn dup2(old_fd: fd_t, new_fd: fd_t) c_int;
lib/std/fs.zig+4
...@@ -890,6 +890,10 @@ pub const Dir = struct {...@@ -890,6 +890,10 @@ pub const Dir = struct {
890 try os.mkdiratC(self.fd, sub_path, default_new_dir_mode);890 try os.mkdiratC(self.fd, sub_path, default_new_dir_mode);
891 }891 }
892892
893 pub fn changeTo(self: Dir) !void {
894 try os.fchdir(self.fd);
895 }
896
893 /// Deprecated; call `openDirList` directly.897 /// Deprecated; call `openDirList` directly.
894 pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {898 pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {
895 return self.openDirList(sub_path);899 return self.openDirList(sub_path);
lib/std/os.zig+14
...@@ -1706,6 +1706,20 @@ pub fn chdirC(dir_path: [*:0]const u8) ChangeCurDirError!void {...@@ -1706,6 +1706,20 @@ pub fn chdirC(dir_path: [*:0]const u8) ChangeCurDirError!void {
1706 }1706 }
1707}1707}
17081708
1709pub fn fchdir(dirfd: fd_t) ChangeCurDirError!void {
1710 while (true) {
1711 switch (errno(system.fchdir(dirfd))) {
1712 0 => return,
1713 EACCES => return error.AccessDenied,
1714 EBADF => unreachable,
1715 ENOTDIR => return error.NotDir,
1716 EINTR => continue,
1717 EIO => return error.FileSystem,
1718 else => |err| return unexpectedErrno(err),
1719 }
1720 }
1721}
1722
1709pub const ReadLinkError = error{1723pub const ReadLinkError = error{
1710 AccessDenied,1724 AccessDenied,
1711 FileSystem,1725 FileSystem,
lib/std/os/linux.zig+4
...@@ -76,6 +76,10 @@ pub fn chdir(path: [*:0]const u8) usize {...@@ -76,6 +76,10 @@ pub fn chdir(path: [*:0]const u8) usize {
76 return syscall1(SYS_chdir, @ptrToInt(path));76 return syscall1(SYS_chdir, @ptrToInt(path));
77}77}
7878
79pub fn fchdir(fd: fd_t) usize {
80 return syscall1(SYS_fchdir, @bitCast(usize, @as(isize, fd)));
81}
82
79pub fn chroot(path: [*:0]const u8) usize {83pub fn chroot(path: [*:0]const u8) usize {
80 return syscall1(SYS_chroot, @ptrToInt(path));84 return syscall1(SYS_chroot, @ptrToInt(path));
81}85}