Files
bun.sh/src/sys_uv.zig
dave caruso 441612917d windows: more windows stuff (#6938)
* fix(win/upgrade): do not show powershell expand-archive info while upgrading

* start working bun run

* experiment: `bun.new`

* you can now bun run

* Update src/install/install.zig

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>

* Update src/install/install.zig

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>

* stuff

* fix stuff

* fix this

* farther but not really

* sadfs

* path hell

not sure how much worse or better this makes things. its a mess. windows path handlign is a mess aaaaaaaaaaaaaaaa

* path.resolve bs

* remove old build system stuff from pr

* a

* fix some path.parse/join cases

* path closer not perfect

* normalize and join tests tests  done

* paths

* implement path.relative

* ,

* stuff

* assert

* fix compile

* hate

* the code isnt great

* stuff

* housekeeping for build system

* blah

* explain windows sitaution in docs

* some progress? not much though

* zig compiler crashes here

* fix

* yippee

* ok

* a

* ala wala

* fix builds on stuff

* clean

* the tests now run

* a

* aa

* dedupe uv event loop

* fix fs test accuracy

* stuff

* [autofix.ci] apply automated fixes

* huge updat e

* [autofix.ci] apply automated fixes

* url

* [autofix.ci] apply automated fixes

* start windows spawnSync

* [autofix.ci] apply automated fixes

* add --webkit for update submodules

* add better err message for `bun setup`

* fix unix platform build

* .

* [autofix.ci] apply automated fixes

* un-upgrade libarchive

* z

* asdfghj

* wrk

* todo -> panic

* ok

* a

* [autofix.ci] apply automated fixes

* fix build scripts l ol

* dfghj

* fa

* [autofix.ci] apply automated fixes

* aaaa

* a

* l

* [autofix.ci] apply automated fixes

* more logs

* [autofix.ci] apply automated fixes

* j

* fix init_command

* CORE DUMP HELL

* i swear im being pranked by the github actions gods

* fadsjkfdshjkhjkdfsahjkdfshjksdafjkhhjkfdsahfsdkjhfsdjkahf

* thanks IAS

* this is the correct fix

* personal review

* ddisablbe these

* revisions!

* ok

* fix submodule

* stuff

* fix libarchive

* [autofix.ci] apply automated fixes

* stuff

* [autofix.ci] apply automated fixes

* a

* fix addressToJS on windows

* make dns async again

* dx: add flag to update submodules ps1 to clone webkit

* dns error case for libuv

* dx improvements on windows

* newline

* obvious fix

* install steps

* extra note

* fix fs test

* Update building-windows.md

* fix builtins bundler to support \r\n line endnigs

* better

* some windows stuff

* a

* a

* a

* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

* [autofix.ci] apply automated fixes

* bunfile text works

* fix build on the mac

* hellooooooooooo

* install steps

* ci for baseline?

* fix

* aaa

* wow

* install script revamp

* bug

* OK

* ok

* aaaaaaaaaaaaaa

* okay

* fix the node test runner lol

* fix napi stuff

---------

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: cirospaciari <ciro.spaciai@gmail.com>
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
2023-12-14 16:56:33 -08:00

378 lines
12 KiB
Zig

//! bun.sys.sys_uv is a polyfill of bun.sys but with libuv.
//! TODO: Probably should merge this into bun.sys itself with isWindows checks
const std = @import("std");
const os = std.os;
const Environment = @import("root").bun.Environment;
const default_allocator = @import("root").bun.default_allocator;
const JSC = @import("root").bun.JSC;
const SystemError = JSC.SystemError;
const bun = @import("root").bun;
const MAX_PATH_BYTES = bun.MAX_PATH_BYTES;
const fd_t = bun.FileDescriptor;
const C = @import("root").bun.C;
const E = C.E;
const linux = os.linux;
const Maybe = JSC.Maybe;
const kernel32 = bun.windows;
const uv = bun.windows.libuv;
const FileDescriptor = bun.FileDescriptor;
const FDImpl = bun.FDImpl;
comptime {
std.debug.assert(Environment.isWindows);
}
pub const log = bun.sys.syslog;
pub const Error = bun.sys.Error;
pub const open = bun.sys.open;
pub const openat = bun.sys.openat;
pub const getFdPath = bun.sys.getFdPath;
pub const setFileOffset = bun.sys.setFileOffset;
pub const openatOSPath = bun.sys.openatOSPath;
pub const mkdirOSPath = bun.sys.mkdirOSPath;
// Note: `req = undefined; req.deinit()` has a saftey-check in a debug build
pub fn mkdir(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_mkdir(uv.Loop.get(), &req, file_path.ptr, flags, null);
log("uv mkdir({s}, {d}) = {d}", .{ file_path, flags, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn chmod(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_chmod(uv.Loop.get(), &req, file_path.ptr, flags, null);
log("uv chmod({s}, {d}) = {d}", .{ file_path, flags, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .chmod } }
else
.{ .result = {} };
}
pub fn fchmod(fd: FileDescriptor, flags: bun.Mode) Maybe(void) {
const uv_fd = bun.uvfdcast(fd);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_fchmod(uv.Loop.get(), &req, uv_fd, flags, null);
log("uv fchmod({}, {d}) = {d}", .{ uv_fd, flags, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fchmod } }
else
.{ .result = {} };
}
pub fn chown(file_path: [:0]const u8, uid: uv.uv_uid_t, gid: uv.uv_uid_t) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_chown(uv.Loop.get(), &req, file_path.ptr, uid, gid, null);
log("uv chown({s}, {d}, {d}) = {d}", .{ file_path, uid, gid, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn fchown(fd: FileDescriptor, uid: uv.uv_uid_t, gid: uv.uv_uid_t) Maybe(void) {
const uv_fd = bun.uvfdcast(fd);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_fchown(uv.Loop.get(), &req, uv_fd, uid, gid, null);
log("uv chown({}, {d}, {d}) = {d}", .{ uv_fd, uid, gid, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn access(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_access(uv.Loop.get(), &req, file_path.ptr, flags, null);
log("uv access({s}, {d}) = {d}", .{ file_path, flags, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .access } }
else
.{ .result = {} };
}
pub fn rmdir(file_path: [:0]const u8) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_rmdir(uv.Loop.get(), &req, file_path.ptr, null);
log("uv rmdir({s}) = {d}", .{ file_path, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn unlink(file_path: [:0]const u8) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_unlink(uv.Loop.get(), &req, file_path.ptr, null);
log("uv unlink({s}) = {d}", .{ file_path, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn readlink(file_path: [:0]const u8, buf: []u8) Maybe(usize) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
// Edge cases: http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_realpath
const rc = uv.uv_fs_readlink(uv.Loop.get(), &req, file_path.ptr, null);
if (rc.errno()) |errno| {
log("uv readlink({s}) = {d}, [err]", .{ file_path, rc.value });
return .{ .err = .{ .errno = errno, .syscall = .mkdir } };
} else {
// Seems like `rc` does not contain the errno?
std.debug.assert(rc.value == 0);
const slice = bun.span(req.ptrAs([*:0]u8));
if (slice.len > buf.len) {
log("uv readlink({s}) = {d}, {s} TRUNCATED", .{ file_path, rc.value, slice });
return .{ .err = .{ .errno = @intFromEnum(E.NOMEM), .syscall = .mkdir } };
}
log("uv readlink({s}) = {d}, {s}", .{ file_path, rc.value, slice });
@memcpy(buf[0..slice.len], slice);
return .{ .result = slice.len };
}
}
pub fn rename(from: [:0]const u8, to: [:0]const u8) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_rename(uv.Loop.get(), &req, from.ptr, to.ptr, null);
log("uv rename({s}, {s}) = {d}", .{ from, to, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn link(from: [:0]const u8, to: [:0]const u8) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
// TODO: i think the flags here are what let us do directory junctions
const rc = uv.uv_fs_link(uv.Loop.get(), &req, from.ptr, to.ptr, null);
log("uv link({s}, {s}) = {d}", .{ from, to, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn symlinkUV(from: [:0]const u8, to: [:0]const u8, flags: c_int) Maybe(void) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
// TODO: i think the flags here are what let us do directory junctions
const rc = uv.uv_fs_symlink(uv.Loop.get(), &req, from.ptr, to.ptr, flags, null);
log("uv symlink({s}, {s}) = {d}", .{ from, to, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .mkdir } }
else
.{ .result = {} };
}
pub fn ftruncate(fd: FileDescriptor, size: isize) Maybe(void) {
const uv_fd = bun.uvfdcast(fd);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_ftruncate(uv.Loop.get(), &req, uv_fd, size, null);
log("uv ftruncate({}, {d}) = {d}", .{ uv_fd, size, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .ftruncate, .fd = fd } }
else
.{ .result = {} };
}
pub fn fstat(fd: FileDescriptor) Maybe(bun.Stat) {
const uv_fd = bun.uvfdcast(fd);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_fstat(uv.Loop.get(), &req, uv_fd, null);
log("uv fstat({}) = {d}", .{ uv_fd, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fstat, .fd = fd } }
else
.{ .result = req.statbuf };
}
pub fn fdatasync(fd: FileDescriptor) Maybe(void) {
const uv_fd = bun.uvfdcast(fd);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_fdatasync(uv.Loop.get(), &req, uv_fd, null);
log("uv fdatasync({}) = {d}", .{ uv_fd, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fstat, .fd = fd } }
else
.{ .result = {} };
}
pub fn fsync(fd: FileDescriptor) Maybe(void) {
const uv_fd = bun.uvfdcast(fd);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_fsync(uv.Loop.get(), &req, uv_fd, null);
log("uv fsync({d}) = {d}", .{ uv_fd, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fstat, .fd = fd } }
else
.{ .result = {} };
}
pub fn stat(path: [:0]const u8) Maybe(bun.Stat) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_stat(uv.Loop.get(), &req, path.ptr, null);
log("uv stat({s}) = {d}", .{ path, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .stat } }
else
.{ .result = req.statbuf };
}
pub fn lstat(path: [:0]const u8) Maybe(bun.Stat) {
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_lstat(uv.Loop.get(), &req, path.ptr, null);
log("uv lstat({s}) = {d}", .{ path, rc.value });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fstat } }
else
.{ .result = req.statbuf };
}
pub fn close(fd: FileDescriptor) ?bun.sys.Error {
return FDImpl.decode(fd).close();
}
pub fn closeAllowingStdoutAndStderr(fd: FileDescriptor) ?bun.sys.Error {
return FDImpl.decode(fd).closeAllowingStdoutAndStderr();
}
pub fn preadv(fd: FileDescriptor, bufs: []const bun.PlatformIOVec, position: i64) Maybe(usize) {
const uv_fd = bun.uvfdcast(fd);
comptime std.debug.assert(bun.PlatformIOVec == uv.uv_buf_t);
const debug_timer = bun.Output.DebugTimer.start();
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_read(
uv.Loop.get(),
&req,
uv_fd,
bufs.ptr,
@intCast(bufs.len),
position,
null,
);
if (Environment.isDebug) {
var total_bytes: usize = 0;
for (bufs) |buf| {
total_bytes += buf.len;
}
log("uv read({}, {d} total bytes) = {d} ({any})", .{ uv_fd, total_bytes, rc.value, debug_timer });
}
if (rc.errno()) |errno| {
return .{ .err = .{ .errno = errno, .fd = fd, .syscall = .read } };
} else {
return .{ .result = @as(usize, @intCast(rc.value)) };
}
}
pub fn pwritev(fd: FileDescriptor, bufs: []const bun.PlatformIOVec, position: i64) Maybe(usize) {
const uv_fd = bun.uvfdcast(fd);
comptime std.debug.assert(bun.PlatformIOVec == uv.uv_buf_t);
const debug_timer = bun.Output.DebugTimer.start();
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_write(
uv.Loop.get(),
&req,
uv_fd,
bufs.ptr,
@intCast(bufs.len),
position,
null,
);
if (Environment.isDebug) {
var total_bytes: usize = 0;
for (bufs) |buf| {
total_bytes += buf.len;
}
log("uv write({}, {d} total bytes) = {d} ({any})", .{ uv_fd, total_bytes, rc.value, debug_timer });
}
if (rc.errno()) |errno| {
return .{ .err = .{ .errno = errno, .fd = fd, .syscall = .read } };
} else {
return .{ .result = @as(usize, @intCast(rc.value)) };
}
}
pub inline fn readv(fd: FileDescriptor, bufs: []bun.PlatformIOVec) Maybe(usize) {
return preadv(fd, bufs, -1);
}
pub inline fn pread(fd: FileDescriptor, buf: []u8, position: i64) Maybe(usize) {
var bufs: [1]bun.PlatformIOVec = .{bun.platformIOVecCreate(buf)};
return preadv(fd, &bufs, position);
}
pub inline fn read(fd: FileDescriptor, buf: []u8) Maybe(usize) {
var bufs: [1]bun.PlatformIOVec = .{bun.platformIOVecCreate(buf)};
return readv(fd, &bufs);
}
pub inline fn writev(fd: FileDescriptor, bufs: []bun.PlatformIOVec) Maybe(usize) {
return pwritev(fd, bufs, -1);
}
pub inline fn pwrite(fd: FileDescriptor, buf: []const u8, position: i64) Maybe(usize) {
var bufs: [1]bun.PlatformIOVec = .{bun.platformIOVecCreate(buf)};
return pwritev(fd, &bufs, position);
}
pub inline fn write(fd: FileDescriptor, buf: []const u8) Maybe(usize) {
var bufs: [1]bun.PlatformIOVec = .{bun.platformIOVecCreate(buf)};
return writev(fd, &bufs);
}