authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-26 16:08:26-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-26 16:08:26-07:00
log505bc9817a2b78c97819f481b3cd98102c7ff964
tree617d5a141c27fed6434ef197f2239cd7b7adb42d
parent14c3c47fb7315e6199751082f9ef544972bb13e6

Implement Dir.deleteFile in terms of deleteFileZ/deleteFileW

Reduces duplicate code, consistent with other fn/fnZ/fnW implementations

1 files changed, 12 insertions(+), 14 deletions(-)

lib/std/fs.zig+12-14
...@@ -1110,20 +1110,18 @@ pub const Dir = struct {...@@ -1110,20 +1110,18 @@ pub const Dir = struct {
1110 /// Delete a file name and possibly the file it refers to, based on an open directory handle.1110 /// Delete a file name and possibly the file it refers to, based on an open directory handle.
1111 /// Asserts that the path parameter has no null bytes.1111 /// Asserts that the path parameter has no null bytes.
1112 pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void {1112 pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void {
1113 os.unlinkat(self.fd, sub_path, 0) catch |err| switch (err) {1113 if (builtin.os.tag == .windows) {
1114 error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR1114 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1115 error.AccessDenied => |e| switch (builtin.os.tag) {1115 return self.deleteFileW(sub_path_w.span().ptr);
1116 // non-Linux POSIX systems return EPERM when trying to delete a directory, so1116 } else if (builtin.os.tag == .wasi) {
1117 // we need to handle that case specifically and translate the error1117 os.unlinkatWasi(self.fd, sub_path, 0) catch |err| switch (err) {
1118 .macosx, .ios, .freebsd, .netbsd, .dragonfly => {1118 error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
1119 const fstat = os.fstatat(self.fd, sub_path, 0) catch return e;1119 else => |e| return e,
1120 const is_dir = fstat.mode & os.S_IFMT == os.S_IFDIR;1120 };
1121 return if (is_dir) error.IsDir else e;1121 } else {
1122 },1122 const sub_path_c = try os.toPosixPath(sub_path);
1123 else => return e,1123 return self.deleteFileZ(&sub_path_c);
1124 },1124 }
1125 else => |e| return e,
1126 };
1127 }1125 }
11281126
1129 pub const deleteFileC = @compileError("deprecated: renamed to deleteFileZ");1127 pub const deleteFileC = @compileError("deprecated: renamed to deleteFileZ");