diff options
author | 2023-09-24 18:27:55 -0700 | |
---|---|---|
committer | 2023-09-24 18:27:55 -0700 | |
commit | 96411580c1ea89635d8c38e2f77f8ea55316c52b (patch) | |
tree | efd20cdba1ab14ed4dde6a4e97e9416163d9c531 | |
parent | 3f463786a5f03817949581aa3904d673a3cd087e (diff) | |
download | bun-96411580c1ea89635d8c38e2f77f8ea55316c52b.tar.gz bun-96411580c1ea89635d8c38e2f77f8ea55316c52b.tar.zst bun-96411580c1ea89635d8c38e2f77f8ea55316c52b.zip |
Do not use removefileat() (#6001)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/bun.js/node/node_fs.zig | 100 | ||||
-rw-r--r-- | src/darwin_c.zig | 23 |
2 files changed, 9 insertions, 114 deletions
diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index dab438628..173f750a5 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -4584,46 +4584,8 @@ pub const NodeFS = struct { var to = args.new_path.sliceZ(&to_buf); return Syscall.rename(from, to); } - pub fn rmdir(this: *NodeFS, args: Arguments.RmDir, comptime flavor: Flavor) Maybe(Return.Rmdir) { - _ = flavor; - - if (comptime Environment.isMac) { - if (args.recursive) { - var dest = args.path.sliceZ(&this.sync_error_buf); - - var flags: u32 = bun.C.darwin.RemoveFileFlags.cross_mount | - bun.C.darwin.RemoveFileFlags.allow_long_paths | - bun.C.darwin.RemoveFileFlags.recursive; - - while (true) { - if (Maybe(Return.Rmdir).errnoSys(bun.C.darwin.removefileat(std.os.AT.FDCWD, dest, null, flags), .rmdir)) |errno| { - switch (@as(os.E, @enumFromInt(errno.err.errno))) { - .AGAIN, .INTR => continue, - .NOENT => return Maybe(Return.Rmdir).success, - .MLINK => { - var copy: [bun.MAX_PATH_BYTES]u8 = undefined; - @memcpy(copy[0..dest.len], dest); - copy[dest.len] = 0; - var dest_copy = copy[0..dest.len :0]; - switch (Syscall.unlink(dest_copy).getErrno()) { - .AGAIN, .INTR => continue, - .NOENT => return errno, - .SUCCESS => continue, - else => return errno, - } - }, - .SUCCESS => unreachable, - else => return errno, - } - } - - return Maybe(Return.Rmdir).success; - } - } - - return Maybe(Return.Rmdir).errnoSysP(system.rmdir(args.path.sliceZ(&this.sync_error_buf)), .rmdir, args.path.slice()) orelse - Maybe(Return.Rmdir).success; - } else if (comptime Environment.isLinux) { + pub fn rmdir(this: *NodeFS, args: Arguments.RmDir, comptime _: Flavor) Maybe(Return.Rmdir) { + if (comptime Environment.isPosix) { if (args.recursive) { std.fs.cwd().deleteTree(args.path.slice()) catch |err| { const errno: std.os.E = switch (err) { @@ -4663,59 +4625,16 @@ pub const NodeFS = struct { return Maybe(Return.Rmdir).errnoSysP(system.rmdir(args.path.sliceZ(&this.sync_error_buf)), .rmdir, args.path.slice()) orelse Maybe(Return.Rmdir).success; } + + return Maybe(Return.Rmdir).todo; } pub fn rm(this: *NodeFS, args: Arguments.RmDir, comptime flavor: Flavor) Maybe(Return.Rm) { _ = flavor; - if (comptime Environment.isMac) { - var dest = args.path.sliceZ(&this.sync_error_buf); - - while (true) { - var flags: u32 = 0; - if (args.recursive) { - flags |= bun.C.darwin.RemoveFileFlags.cross_mount; - flags |= bun.C.darwin.RemoveFileFlags.allow_long_paths; - flags |= bun.C.darwin.RemoveFileFlags.recursive; - } - - if (Maybe(Return.Rm).errnoSys(bun.C.darwin.removefileat(std.os.AT.FDCWD, dest, null, flags), .unlink)) |errno| { - switch (@as(os.E, @enumFromInt(errno.err.errno))) { - .AGAIN, .INTR => continue, - .NOENT => { - if (args.force) { - return Maybe(Return.Rm).success; - } - - return errno; - }, - - .MLINK => { - var copy: [bun.MAX_PATH_BYTES]u8 = undefined; - @memcpy(copy[0..dest.len], dest); - copy[dest.len] = 0; - var dest_copy = copy[0..dest.len :0]; - switch (Syscall.unlink(dest_copy).getErrno()) { - .AGAIN, .INTR => continue, - .NOENT => { - if (args.force) { - continue; - } - - return errno; - }, - .SUCCESS => continue, - else => return errno, - } - }, - .SUCCESS => unreachable, - else => return errno, - } - } - - return Maybe(Return.Rm).success; - } - } else if (comptime Environment.isLinux or Environment.isWindows) { + if (comptime Environment.isPosix) { + // We cannot use removefileat() on macOS because it does not handle write-protected files as expected. if (args.recursive) { + // TODO: switch to an implementation which does not use any "unreachable" std.fs.cwd().deleteTree(args.path.slice()) catch |err| { const errno: E = switch (err) { error.InvalidHandle => .BADF, @@ -4752,10 +4671,9 @@ pub const NodeFS = struct { }; return Maybe(Return.Rm).success; } - } - if (comptime Environment.isPosix) { - var dest = args.path.osPath(&this.sync_error_buf); + var dest = args.path.sliceZ(&this.sync_error_buf); + std.os.unlinkZ(dest) catch |er| { // empircally, it seems to return AccessDenied when the // file is actually a directory on macOS. diff --git a/src/darwin_c.zig b/src/darwin_c.zig index 1e494db47..3d3b28f3f 100644 --- a/src/darwin_c.zig +++ b/src/darwin_c.zig @@ -728,29 +728,6 @@ const IO_CTL_RELATED = struct { pub usingnamespace IO_CTL_RELATED; -pub const RemoveFileFlags = struct { - /// If path is a directory, recurse (depth first traversal) - pub const recursive: u32 = (1 << 0); - /// Remove contents but not directory itself - pub const keep_parent: u32 = (1 << 1); - /// 7 pass DoD algorithm - pub const secure_7_pass: u32 = (1 << 2); - /// 35-pass Gutmann algorithm (overrides REMOVEFILE_SECURE_7_PASS) - pub const secure_35_pass: u32 = (1 << 3); - /// 1 pass single overwrite), - pub const secure_1_pass: u32 = (1 << 4); - /// 3 pass overwrite - pub const secure_3_pass: u32 = (1 << 5); - /// Single-pass overwrite, with 0 instead of random data - pub const secure_1_pass_zero: u32 = (1 << 6); - /// Cross mountpoints when deleting recursively. << 6), - pub const cross_mount: u32 = (1 << 7); - /// Paths may be longer than PATH_MAX - requires temporarily changing cwd - pub const allow_long_paths: u32 = (1 << 8); -}; -pub const removefile_state_t = opaque {}; -pub extern fn removefileat(fd: c_int, path: [*c]const u8, state: ?*removefile_state_t, flags: u32) c_int; - // As of Zig v0.11.0-dev.1393+38eebf3c4, ifaddrs.h is not included in the headers pub const ifaddrs = extern struct { ifa_next: ?*ifaddrs, |