diff options
author | 2023-05-11 18:23:33 -0300 | |
---|---|---|
committer | 2023-05-11 14:23:33 -0700 | |
commit | bc7d0adcf94d65b9238c6cb0aceb38c86cc323b3 (patch) | |
tree | 83d85a76019faaba44ca85341c4cdc9c89d89e1e /src | |
parent | d032b73b10905c278fc109b9648612fa0fc94ff7 (diff) | |
download | bun-bc7d0adcf94d65b9238c6cb0aceb38c86cc323b3.tar.gz bun-bc7d0adcf94d65b9238c6cb0aceb38c86cc323b3.tar.zst bun-bc7d0adcf94d65b9238c6cb0aceb38c86cc323b3.zip |
fix(fs) mkdtemp and mkdtempSync errors (#2851)
* fix mkdtemp
* fmt
* fix errno
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/fs.exports.js | 10 | ||||
-rw-r--r-- | src/bun.js/node/node_fs.zig | 18 |
2 files changed, 19 insertions, 9 deletions
diff --git a/src/bun.js/fs.exports.js b/src/bun.js/fs.exports.js index dfb1947e7..df2fbf382 100644 --- a/src/bun.js/fs.exports.js +++ b/src/bun.js/fs.exports.js @@ -157,9 +157,15 @@ export var access = function access(...args) { function callbackify(fsFunction, args) { try { const result = fsFunction.apply(fs, args.slice(0, args.length - 1)); - queueMicrotask(() => args[args.length - 1](null, result)); + const callback = args[args.length - 1]; + if (typeof callback === "function") { + queueMicrotask(() => callback(null, result)); + } } catch (e) { - queueMicrotask(() => args[args.length - 1](e)); + const callback = args[args.length - 1]; + if (typeof callback === "function") { + queueMicrotask(() => callback(e)); + } } } diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index fe62e77ec..f7300f28e 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -3028,15 +3028,19 @@ pub const NodeFS = struct { prefix_buf[len..][0..6].* = "XXXXXX".*; prefix_buf[len..][6] = 0; + // The mkdtemp() function returns a pointer to the modified template + // string on success, and NULL on failure, in which case errno is set to + // indicate the error + const rc = C.mkdtemp(prefix_buf); - switch (std.c.getErrno(@ptrToInt(rc))) { - .SUCCESS => {}, - else => |errno| return .{ .err = Syscall.Error{ .errno = @truncate(Syscall.Error.Int, @enumToInt(errno)), .syscall = .mkdtemp } }, + if (rc) |ptr| { + return .{ + .result = JSC.ZigString.dupeForJS(bun.sliceTo(ptr, 0), bun.default_allocator) catch unreachable, + }; } - - return .{ - .result = JSC.ZigString.dupeForJS(bun.sliceTo(rc.?, 0), bun.default_allocator) catch unreachable, - }; + // std.c.getErrno(rc) returns SUCCESS if rc is null so we call std.c._errno() directly + const errno = @intToEnum(std.c.E, std.c._errno().*); + return .{ .err = Syscall.Error{ .errno = @truncate(Syscall.Error.Int, @enumToInt(errno)), .syscall = .mkdtemp } }; } pub fn open(this: *NodeFS, args: Arguments.Open, comptime flavor: Flavor) Maybe(Return.Open) { switch (comptime flavor) { |