Skip to content

Commit

Permalink
Use linux syscall interface more in I/O (#15067)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Nov 11, 2024
1 parent d713001 commit 2b9abc2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 88 deletions.
21 changes: 8 additions & 13 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4002,11 +4002,7 @@ pub const NodeFS = struct {

pub fn access(this: *NodeFS, args: Arguments.Access, comptime _: Flavor) Maybe(Return.Access) {
const path = args.path.sliceZ(&this.sync_error_buf);
if (Environment.isWindows) {
return Syscall.access(path, @intFromEnum(args.mode));
}
const rc = Syscall.system.access(path, @intFromEnum(args.mode));
return Maybe(Return.Access).errnoSysP(rc, .access, path) orelse Maybe(Return.Access).success;
return Syscall.access(path, @intFromEnum(args.mode));
}

pub fn appendFile(this: *NodeFS, args: Arguments.AppendFile, comptime flavor: Flavor) Maybe(Return.AppendFile) {
Expand Down Expand Up @@ -5925,16 +5921,15 @@ pub const NodeFS = struct {
// on mac, it's relatively positioned
0
else brk: {
// on linux, it's absolutely positioned
const pos = bun.sys.system.lseek(
fd.cast(),
// on linux, it's absolutely positione

switch (Syscall.lseek(
fd,
@as(std.posix.off_t, @intCast(0)),
std.os.linux.SEEK.CUR,
);

switch (bun.sys.getErrno(pos)) {
.SUCCESS => break :brk @as(usize, @intCast(pos)),
else => break :preallocate,
)) {
.err => break :preallocate,
.result => |pos| break :brk @as(usize, @intCast(pos)),
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/fd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub const FDImpl = packed struct {
const fd = this.encode();
bun.assert(fd != bun.invalid_fd);
bun.assert(fd.cast() >= 0);
break :result switch (bun.C.getErrno(bun.sys.system.close(fd.cast()))) {
break :result switch (bun.C.getErrno(bun.sys.syscall.close(fd.cast()))) {
.BADF => bun.sys.Error{ .errno = @intFromEnum(posix.E.BADF), .syscall = .close, .fd = fd },
else => null,
};
Expand All @@ -251,7 +251,7 @@ pub const FDImpl = packed struct {
const fd = this.encode();
bun.assert(fd != bun.invalid_fd);
bun.assert(fd.cast() >= 0);
break :result switch (bun.C.getErrno(bun.sys.system.@"close$NOCANCEL"(fd.cast()))) {
break :result switch (bun.C.getErrno(bun.sys.syscall.@"close$NOCANCEL"(fd.cast()))) {
.BADF => bun.sys.Error{ .errno = @intFromEnum(posix.E.BADF), .syscall = .close, .fd = fd },
else => null,
};
Expand Down
18 changes: 8 additions & 10 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2191,13 +2191,12 @@ pub fn NewPackageInstall(comptime kind: PkgInstallKind) type {

pub fn isDanglingSymlink(path: [:0]const u8) bool {
if (comptime Environment.isLinux) {
const rc = Syscall.system.open(path, .{ .PATH = true }, @as(u32, 0));
switch (Syscall.getErrno(rc)) {
.SUCCESS => {
_ = bun.sys.close(bun.toFD(@as(i32, @intCast(rc))));
switch (Syscall.open(path, bun.O.PATH, @as(u32, 0))) {
.err => return true,
.result => |fd| {
_ = bun.sys.close(fd);
return false;
},
else => return true,
}
} else if (comptime Environment.isWindows) {
switch (bun.sys.sys_uv.open(path, 0, 0)) {
Expand All @@ -2210,13 +2209,12 @@ pub fn NewPackageInstall(comptime kind: PkgInstallKind) type {
},
}
} else {
const rc = Syscall.system.open(path, .{}, .{});
switch (Syscall.getErrno(rc)) {
.SUCCESS => {
_ = Syscall.system.close(rc);
switch (Syscall.open(path, bun.O.PATH, @as(u32, 0))) {
.err => return true,
.result => |fd| {
_ = bun.sys.close(fd);
return false;
},
else => return true,
}
}
}
Expand Down
Loading

0 comments on commit 2b9abc2

Please sign in to comment.