file descriptor rewrite (#18790)

This commit is contained in:
chloe caruso
2025-04-15 09:37:11 -07:00
committed by GitHub
parent caeea11706
commit 903706dccf
110 changed files with 1708 additions and 1892 deletions

View File

@@ -486,7 +486,7 @@ pub const StandaloneModuleGraph = struct {
// Make the file writable so we can delete it
_ = Syscall.fchmod(fd, 0o777);
}
_ = Syscall.close(fd);
fd.close();
_ = Syscall.unlink(name);
}
}.toClean;
@@ -579,7 +579,7 @@ pub const StandaloneModuleGraph = struct {
}
unreachable;
};
const self_fd = brk2: {
const self_fd: bun.FileDescriptor = brk2: {
for (0..3) |retry| {
switch (Syscall.open(self_exe, bun.O.CLOEXEC | bun.O.RDONLY, 0)) {
.result => |res| break :brk2 res,
@@ -601,9 +601,9 @@ pub const StandaloneModuleGraph = struct {
unreachable;
};
defer _ = Syscall.close(self_fd);
defer self_fd.close();
bun.copyFile(self_fd.cast(), fd.cast()).unwrap() catch |err| {
bun.copyFile(self_fd, fd).unwrap() catch |err| {
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to copy bun executable into temporary file: {s}", .{@errorName(err)});
cleanup(zname, fd);
Global.exit(1);
@@ -659,7 +659,7 @@ pub const StandaloneModuleGraph = struct {
Global.exit(1);
};
if (comptime !Environment.isWindows) {
_ = bun.C.fchmod(cloned_executable_fd.int(), 0o777);
_ = bun.C.fchmod(cloned_executable_fd.native(), 0o777);
}
return cloned_executable_fd;
},
@@ -727,7 +727,7 @@ pub const StandaloneModuleGraph = struct {
// the final 8 bytes in the file are the length of the module graph with padding, excluding the trailer and offsets
_ = Syscall.write(cloned_executable_fd, std.mem.asBytes(&total_byte_count));
if (comptime !Environment.isWindows) {
_ = bun.C.fchmod(cloned_executable_fd.int(), 0o777);
_ = bun.C.fchmod(cloned_executable_fd.native(), 0o777);
}
return cloned_executable_fd;
@@ -791,7 +791,7 @@ pub const StandaloneModuleGraph = struct {
.{ .windows_hide_console = windows_hide_console },
target,
);
fd.assertKind(.system);
bun.debugAssert(fd.kind == .system);
if (Environment.isWindows) {
var outfile_buf: bun.OSPathBuffer = undefined;
@@ -803,7 +803,7 @@ pub const StandaloneModuleGraph = struct {
break :brk outfile_buf_u16[0..outfile_w.len :0];
};
bun.C.moveOpenedFileAtLoose(fd, bun.toFD(root_dir.fd), outfile_slice, true).unwrap() catch |err| {
bun.C.moveOpenedFileAtLoose(fd, .fromStdDir(root_dir), outfile_slice, true).unwrap() catch |err| {
if (err == error.EISDIR) {
Output.errGeneric("{} is a directory. Please choose a different --outfile or delete the directory", .{bun.fmt.utf16(outfile_slice)});
} else {
@@ -814,7 +814,7 @@ pub const StandaloneModuleGraph = struct {
Global.exit(1);
};
_ = bun.sys.close(fd);
fd.close();
if (windows_icon) |icon_utf8| {
var icon_buf: bun.OSPathBuffer = undefined;
@@ -836,7 +836,7 @@ pub const StandaloneModuleGraph = struct {
fd,
bun.FD.cwd(),
bun.sliceTo(&(try std.posix.toPosixPath(temp_location)), 0),
bun.toFD(root_dir.fd),
.fromStdDir(root_dir),
bun.sliceTo(&(try std.posix.toPosixPath(std.fs.path.basename(outfile))), 0),
) catch |err| {
if (err == error.IsDir) {
@@ -871,7 +871,7 @@ pub const StandaloneModuleGraph = struct {
// Do not invoke libuv here.
const self_exe = openSelf() catch return null;
defer _ = Syscall.close(self_exe);
defer self_exe.close();
var trailer_bytes: [4096]u8 = undefined;
std.posix.lseek_END(self_exe.cast(), -4096) catch return null;
@@ -1010,7 +1010,7 @@ pub const StandaloneModuleGraph = struct {
switch (Environment.os) {
.linux => {
if (std.fs.openFileAbsoluteZ("/proc/self/exe", .{})) |easymode| {
return bun.toFD(easymode.handle);
return .fromStdFile(easymode);
} else |_| {
if (bun.argv.len > 0) {
// The user doesn't have /proc/ mounted, so now we just guess and hope for the best.
@@ -1021,7 +1021,7 @@ pub const StandaloneModuleGraph = struct {
"",
bun.argv[0],
)) |path| {
return bun.toFD((try std.fs.cwd().openFileZ(path, .{})).handle);
return .fromStdFile(try std.fs.cwd().openFileZ(path, .{}));
}
}
@@ -1033,7 +1033,7 @@ pub const StandaloneModuleGraph = struct {
// opened with no modification.
const self_exe_path = try bun.selfExePath();
const file = try std.fs.openFileAbsoluteZ(self_exe_path.ptr, .{});
return bun.toFD(file.handle);
return .fromStdFile(file);
},
.windows => {
const image_path_unicode_string = std.os.windows.peb().ProcessParameters.ImagePathName;
@@ -1050,7 +1050,7 @@ pub const StandaloneModuleGraph = struct {
}
return bun.sys.openFileAtWindows(
bun.FileDescriptor.cwd(),
.cwd(),
nt_path,
.{
.access_mask = w.SYNCHRONIZE | w.GENERIC_READ,