Compare commits

...

1 Commits

Author SHA1 Message Date
Meghan Denny
a120688ef0 zig: remove bun.FD.int() method 2024-07-27 00:24:25 -07:00
13 changed files with 39 additions and 50 deletions

View File

@@ -459,7 +459,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.cast(), 0o777);
}
return cloned_executable_fd;

View File

@@ -953,7 +953,7 @@ pub const FilePoll = struct {
pub fn unregisterWithFd(this: *FilePoll, loop: *Loop, fd: bun.FileDescriptor, force_unregister: bool) JSC.Maybe(void) {
if (Environment.allow_assert) {
bun.assert(fd.int() >= 0 and fd != bun.invalid_fd);
bun.assert(fd != bun.invalid_fd);
}
defer this.deactivate(loop);

View File

@@ -2130,13 +2130,13 @@ pub const DNSResolver = struct {
vm.eventLoop().enter();
defer vm.eventLoop().exit();
var channel = this.channel orelse {
_ = this.polls.orderedRemove(poll.fd.int());
_ = this.polls.orderedRemove(poll.fd.cast());
poll.deinit();
return;
};
channel.process(
poll.fd.int(),
poll.fd.cast(),
poll.isReadable(),
poll.isWritable(),
);

View File

@@ -139,11 +139,12 @@ pub const ProcessExitHandler = struct {
}
}
};
pub const PidFDType = if (Environment.isLinux) fd_t else u0;
pub const PidFDType = if (Environment.isLinux) bun.FD else u0;
const pidfd_invalid = if (Environment.isLinux) bun.invalid_fd else 0;
pub const Process = struct {
pid: pid_t = 0,
pidfd: PidFDType = 0,
pidfd: PidFDType = pidfd_invalid,
status: Status = Status{ .running = {} },
poller: Poller = Poller{
.detached = {},
@@ -174,7 +175,7 @@ pub const Process = struct {
) *Process {
return Process.new(.{
.pid = posix.pid,
.pidfd = posix.pidfd orelse 0,
.pidfd = posix.pidfd orelse pidfd_invalid,
.event_loop = JSC.EventLoopHandle.init(event_loop),
.sync = sync_,
.poller = .{ .detached = {} },
@@ -454,9 +455,9 @@ pub const Process = struct {
}
if (comptime Environment.isLinux) {
if (this.pidfd != bun.invalid_fd.int() and this.pidfd > 0) {
if (this.pidfd != bun.invalid_fd) {
_ = bun.sys.close(bun.toFD(this.pidfd));
this.pidfd = @intCast(bun.invalid_fd.int());
this.pidfd = bun.invalid_fd;
}
}
}
@@ -1125,7 +1126,7 @@ pub const PosixSpawnResult = struct {
);
while (true) {
switch (bun.C.getErrno(rc)) {
.SUCCESS => return JSC.Maybe(PidFDType){ .result = @intCast(rc) },
.SUCCESS => return JSC.Maybe(PidFDType){ .result = bun.toFD(@as(u32, @intCast(rc))) },
.INTR => {
rc = std.os.linux.pidfd_open(
@intCast(this.pid),

View File

@@ -4272,8 +4272,8 @@ pub const NodeFS = struct {
.err => |err| return Maybe(Return.CopyFile){ .err = err.withPath(args.dest.slice()) },
};
defer {
_ = std.c.ftruncate(dest_fd.int(), @as(std.c.off_t, @intCast(@as(u63, @truncate(wrote)))));
_ = C.fchmod(dest_fd.int(), stat_.mode);
_ = std.c.ftruncate(dest_fd.cast(), @as(std.c.off_t, @intCast(@as(u63, @truncate(wrote)))));
_ = C.fchmod(dest_fd.cast(), stat_.mode);
_ = Syscall.close(dest_fd);
}
@@ -4493,7 +4493,7 @@ pub const NodeFS = struct {
return Syscall.fchown(args.fd, args.uid, args.gid);
}
return Maybe(Return.Fchown).errnoSys(C.fchown(args.fd.int(), args.uid, args.gid), .fchown) orelse
return Maybe(Return.Fchown).errnoSys(C.fchown(args.fd.cast(), args.uid, args.gid), .fchown) orelse
Maybe(Return.Fchown).success;
}
@@ -4501,7 +4501,7 @@ pub const NodeFS = struct {
if (Environment.isWindows) {
return Syscall.fdatasync(args.fd);
}
return Maybe(Return.Fdatasync).errnoSysFd(system.fdatasync(args.fd.int()), .fdatasync, args.fd) orelse Maybe(Return.Fdatasync).success;
return Maybe(Return.Fdatasync).errnoSysFd(system.fdatasync(args.fd.cast()), .fdatasync, args.fd) orelse Maybe(Return.Fdatasync).success;
}
pub fn fstat(_: *NodeFS, args: Arguments.Fstat, comptime _: Flavor) Maybe(Return.Fstat) {
@@ -4515,7 +4515,7 @@ pub const NodeFS = struct {
if (Environment.isWindows) {
return Syscall.fsync(args.fd);
}
return Maybe(Return.Fsync).errnoSys(system.fsync(args.fd.int()), .fsync) orelse
return Maybe(Return.Fsync).errnoSys(system.fsync(args.fd.cast()), .fsync) orelse
Maybe(Return.Fsync).success;
}
@@ -4544,7 +4544,7 @@ pub const NodeFS = struct {
args.atime,
};
return if (Maybe(Return.Futimes).errnoSys(system.futimens(args.fd.int(), &times), .futimens)) |err|
return if (Maybe(Return.Futimes).errnoSys(system.futimens(args.fd.cast(), &times), .futimens)) |err|
err
else
Maybe(Return.Futimes).success;
@@ -6607,8 +6607,8 @@ pub const NodeFS = struct {
}
};
defer {
_ = std.c.ftruncate(dest_fd.int(), @as(std.c.off_t, @intCast(@as(u63, @truncate(wrote)))));
_ = C.fchmod(dest_fd.int(), stat_.mode);
_ = std.c.ftruncate(dest_fd.cast(), @as(std.c.off_t, @intCast(@as(u63, @truncate(wrote)))));
_ = C.fchmod(dest_fd.cast(), stat_.mode);
_ = Syscall.close(dest_fd);
}

View File

@@ -3136,7 +3136,7 @@ pub const FileSink = struct {
.result => |stat| {
this.pollable = bun.sys.isPollable(stat.mode);
if (!this.pollable and isatty == null) {
isatty = std.posix.isatty(fd.int());
isatty = std.posix.isatty(fd.cast());
}
if (isatty) |is| {
@@ -3497,7 +3497,7 @@ pub const FileReader = struct {
const fd = if (file.pathlike == .fd)
if (file.pathlike.fd.isStdio()) brk: {
if (comptime Environment.isPosix) {
const rc = bun.C.open_as_nonblocking_tty(file.pathlike.fd.int(), bun.O.RDONLY);
const rc = bun.C.open_as_nonblocking_tty(file.pathlike.fd.cast(), bun.O.RDONLY);
if (rc > -1) {
is_nonblocking_tty = true;
file.is_atty = true;
@@ -3533,7 +3533,7 @@ pub const FileReader = struct {
if (comptime Environment.isPosix) {
if ((file.is_atty orelse false) or
(fd.int() < 3 and std.posix.isatty(fd.cast())) or
(fd.cast() < 3 and std.posix.isatty(fd.cast())) or
(file.pathlike == .fd and
bun.FDTag.get(file.pathlike.fd) != .none and
std.posix.isatty(file.pathlike.fd.cast())))

View File

@@ -99,18 +99,6 @@ pub const FileDescriptor = enum(FileDescriptorInt) {
// invalid = @intFromEnum(invalid_fd),
_,
/// Do not use this function in new code.
///
/// Interpreting a FD as an integer is almost certainly a mistake.
/// On Windows, it is always a mistake, as the integer is bitcast of a tagged packed struct.
///
/// TODO(@paperdave): remove this API.
pub inline fn int(self: FileDescriptor) std.posix.fd_t {
if (Environment.isWindows)
@compileError("FileDescriptor.int() is not allowed on Windows.");
return @intFromEnum(self);
}
pub inline fn writeTo(fd: FileDescriptor, writer: anytype, endian: std.builtin.Endian) !void {
try writer.writeInt(FileDescriptorInt, @intFromEnum(fd), endian);
}
@@ -126,7 +114,7 @@ pub const FileDescriptor = enum(FileDescriptorInt) {
///
/// This may be needed in places where a FileDescriptor is given to `std` or `kernel32` apis
pub inline fn cast(fd: FileDescriptor) std.posix.fd_t {
if (!Environment.isWindows) return fd.int();
if (!Environment.isWindows) return @intFromEnum(fd);
// if not having this check, the cast may crash zig compiler?
if (@inComptime() and fd == invalid_fd) return FDImpl.invalid.system();
return FDImpl.decode(fd).system();
@@ -161,7 +149,7 @@ pub const FileDescriptor = enum(FileDescriptorInt) {
}
pub fn eq(this: FileDescriptor, that: FileDescriptor) bool {
if (Environment.isPosix) return this.int() == that.int();
if (Environment.isPosix) return this.cast() == that.cast();
const this_ = FDImpl.decode(this);
const that_ = FDImpl.decode(that);
@@ -858,7 +846,7 @@ pub const FDHashMapContext = struct {
// the goal here is to do zero work and widen the 32 bit type to 64
// this should compile error if FileDescriptor somehow is larger than 64 bits.
comptime assert(@bitSizeOf(FileDescriptor) <= 64);
return @intCast(fd.int());
return @bitCast(@as(i64, @intFromEnum(fd)));
}
pub fn eql(_: @This(), a: FileDescriptor, b: FileDescriptor) bool {
return a == b;

View File

@@ -1372,7 +1372,7 @@ pub const FileSystem = struct {
if (is_symlink) {
var file = try if (existing_fd != .zero)
std.fs.File{ .handle = existing_fd.int() }
existing_fd.asFile()
else if (store_fd)
std.fs.openFileAbsoluteZ(absolute_path_c, .{ .mode = .read_only })
else

View File

@@ -519,7 +519,7 @@ pub const Poll = struct {
kqueue_event.* = switch (comptime action) {
.readable => .{
.ident = @as(u64, @intCast(fd.int())),
.ident = @intCast(fd.cast()),
.filter = std.posix.system.EVFILT_READ,
.data = 0,
.fflags = 0,
@@ -528,7 +528,7 @@ pub const Poll = struct {
.ext = .{ generation_number, 0 },
},
.writable => .{
.ident = @as(u64, @intCast(fd.int())),
.ident = @intCast(fd.cast()),
.filter = std.posix.system.EVFILT_WRITE,
.data = 0,
.fflags = 0,
@@ -537,7 +537,7 @@ pub const Poll = struct {
.ext = .{ generation_number, 0 },
},
.cancel => if (poll.flags.contains(.poll_readable)) .{
.ident = @as(u64, @intCast(fd.int())),
.ident = @intCast(fd.cast()),
.filter = std.posix.system.EVFILT_READ,
.data = 0,
.fflags = 0,
@@ -545,7 +545,7 @@ pub const Poll = struct {
.flags = std.c.EV_DELETE,
.ext = .{ poll.generation_number, 0 },
} else if (poll.flags.contains(.poll_writable)) .{
.ident = @as(u64, @intCast(fd.int())),
.ident = @intCast(fd.cast()),
.filter = std.posix.system.EVFILT_WRITE,
.data = 0,
.fflags = 0,

View File

@@ -626,7 +626,7 @@ pub const linux_fs = if (bun.Environment.isLinux) @cImport({
///
/// Support for FICLONE is dependent on the filesystem driver.
pub fn ioctl_ficlone(dest_fd: bun.FileDescriptor, srcfd: bun.FileDescriptor) usize {
return std.os.linux.ioctl(dest_fd.cast(), linux_fs.FICLONE, @intCast(srcfd.int()));
return std.os.linux.ioctl(dest_fd.cast(), linux_fs.FICLONE, @intCast(srcfd.cast()));
}
pub const RWFFlagSupport = enum(u8) {

View File

@@ -12535,7 +12535,7 @@ pub fn FlagParser(comptime Opts: type) type {
pub fn isPollable(fd: bun.FileDescriptor, mode: bun.Mode) bool {
if (bun.Environment.isWindows) return false;
if (bun.Environment.isLinux) return posix.S.ISFIFO(mode) or posix.S.ISSOCK(mode) or posix.isatty(fd.int());
if (bun.Environment.isLinux) return posix.S.ISFIFO(mode) or posix.S.ISSOCK(mode) or posix.isatty(fd.cast());
// macos allows regular files to be pollable: ISREG(mode) == true
return posix.S.ISFIFO(mode) or posix.S.ISSOCK(mode) or posix.isatty(fd.int()) or posix.S.ISREG(mode);
return posix.S.ISFIFO(mode) or posix.S.ISSOCK(mode) or posix.isatty(fd.cast()) or posix.S.ISREG(mode);
}

View File

@@ -659,7 +659,7 @@ pub fn mkdiratW(dir_fd: bun.FileDescriptor, file_path: []const u16, _: i32) Mayb
pub fn fstatat(fd: bun.FileDescriptor, path: [:0]const u8) Maybe(bun.Stat) {
if (Environment.isWindows) @compileError("Use fstat on Windows");
var stat_ = mem.zeroes(bun.Stat);
if (Maybe(bun.Stat).errnoSys(sys.fstatat(fd.int(), path, &stat_, 0), .fstatat)) |err| {
if (Maybe(bun.Stat).errnoSys(sys.fstatat(fd.cast(), path, &stat_, 0), .fstatat)) |err| {
log("fstatat({}, {s}) = {s}", .{ fd, path, @tagName(err.getErrno()) });
return err;
}
@@ -2889,7 +2889,7 @@ pub fn readNonblocking(fd: bun.FileDescriptor, buf: []u8) Maybe(usize) {
var debug_timer = bun.Output.DebugTimer.start();
// Note that there is a bug on Linux Kernel 5
const rc = C.sys_preadv2(@intCast(fd.int()), &iovec, 1, -1, std.os.linux.RWF.NOWAIT);
const rc = C.sys_preadv2(fd.cast(), &iovec, 1, -1, std.os.linux.RWF.NOWAIT);
if (comptime Environment.isDebug) {
log("preadv2({}, {d}) = {d} ({})", .{ fd, buf.len, rc, debug_timer });
@@ -2933,7 +2933,7 @@ pub fn writeNonblocking(fd: bun.FileDescriptor, buf: []const u8) Maybe(usize) {
var debug_timer = bun.Output.DebugTimer.start();
const rc = C.sys_pwritev2(@intCast(fd.int()), &iovec, 1, -1, std.os.linux.RWF.NOWAIT);
const rc = C.sys_pwritev2(fd.cast(), &iovec, 1, -1, std.os.linux.RWF.NOWAIT);
if (comptime Environment.isDebug) {
log("pwritev2({}, {d}) = {d} ({})", .{ fd, buf.len, rc, debug_timer });

View File

@@ -972,7 +972,7 @@ pub fn NewWatcher(comptime ContextType: type) type {
event.fflags = std.c.NOTE_WRITE | std.c.NOTE_RENAME | std.c.NOTE_DELETE;
// id
event.ident = @intCast(fd.int());
event.ident = @intCast(fd.cast());
// Store the hash for fast filtering later
event.udata = @as(usize, @intCast(watchlist_id));
@@ -1068,7 +1068,7 @@ pub fn NewWatcher(comptime ContextType: type) type {
event.fflags = std.c.NOTE_WRITE | std.c.NOTE_RENAME | std.c.NOTE_DELETE;
// id
event.ident = @intCast(fd.int());
event.ident = @intCast(fd.cast());
// Store the hash for fast filtering later
event.udata = @as(usize, @intCast(watchlist_id));
@@ -1233,7 +1233,7 @@ pub fn NewWatcher(comptime ContextType: type) type {
if (this.indexOf(hash)) |index| {
if (comptime FeatureFlags.atomic_file_watcher) {
// On Linux, the file descriptor might be out of date.
if (fd.int() > 0) {
if (fd.cast() > 0) {
var fds = this.watchlist.items(.fd);
fds[index] = fd;
}