mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
Implement Dir.deleteFile in terms of deleteFileZ/deleteFileW
Reduces duplicate code, consistent with other fn/fnZ/fnW implementations
This commit is contained in:
parent
14c3c47fb7
commit
505bc9817a
@ -1110,20 +1110,18 @@ pub const Dir = struct {
|
||||
/// Delete a file name and possibly the file it refers to, based on an open directory handle.
|
||||
/// Asserts that the path parameter has no null bytes.
|
||||
pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void {
|
||||
os.unlinkat(self.fd, sub_path, 0) catch |err| switch (err) {
|
||||
error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
|
||||
error.AccessDenied => |e| switch (builtin.os.tag) {
|
||||
// non-Linux POSIX systems return EPERM when trying to delete a directory, so
|
||||
// we need to handle that case specifically and translate the error
|
||||
.macosx, .ios, .freebsd, .netbsd, .dragonfly => {
|
||||
const fstat = os.fstatat(self.fd, sub_path, 0) catch return e;
|
||||
const is_dir = fstat.mode & os.S_IFMT == os.S_IFDIR;
|
||||
return if (is_dir) error.IsDir else e;
|
||||
},
|
||||
else => return e,
|
||||
},
|
||||
else => |e| return e,
|
||||
};
|
||||
if (builtin.os.tag == .windows) {
|
||||
const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
|
||||
return self.deleteFileW(sub_path_w.span().ptr);
|
||||
} else if (builtin.os.tag == .wasi) {
|
||||
os.unlinkatWasi(self.fd, sub_path, 0) catch |err| switch (err) {
|
||||
error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
|
||||
else => |e| return e,
|
||||
};
|
||||
} else {
|
||||
const sub_path_c = try os.toPosixPath(sub_path);
|
||||
return self.deleteFileZ(&sub_path_c);
|
||||
}
|
||||
}
|
||||
|
||||
pub const deleteFileC = @compileError("deprecated: renamed to deleteFileZ");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user