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;
105105pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
106106pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
107107pub extern "c" fn chdir(path: [*:0]const u8) c_int;
108pub extern "c" fn fchdir(fd: fd_t) c_int;
108109pub extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int;
109110pub extern "c" fn dup(fd: fd_t) c_int;
110111pub 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 {
890890 try os.mkdiratC(self.fd, sub_path, default_new_dir_mode);
891891 }
892892
893 pub fn changeTo(self: Dir) !void {
894 try os.fchdir(self.fd);
895 }
896
893897 /// Deprecated; call `openDirList` directly.
894898 pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {
895899 return self.openDirList(sub_path);
lib/std/os.zig+14
......@@ -1706,6 +1706,20 @@ pub fn chdirC(dir_path: [*:0]const u8) ChangeCurDirError!void {
17061706 }
17071707}
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
17091723pub const ReadLinkError = error{
17101724 AccessDenied,
17111725 FileSystem,
lib/std/os/linux.zig+4
......@@ -76,6 +76,10 @@ pub fn chdir(path: [*:0]const u8) usize {
7676 return syscall1(SYS_chdir, @ptrToInt(path));
7777}
7878
79pub fn fchdir(fd: fd_t) usize {
80 return syscall1(SYS_fchdir, @bitCast(usize, @as(isize, fd)));
81}
82
7983pub fn chroot(path: [*:0]const u8) usize {
8084 return syscall1(SYS_chroot, @ptrToInt(path));
8185}