forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_uv.zig
417 lines (347 loc) · 13.6 KB
/
sys_uv.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! 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 posix = std.posix;
const bun = @import("root").bun;
const assertIsValidWindowsPath = bun.strings.assertIsValidWindowsPath;
const fd_t = bun.FileDescriptor;
const default_allocator = bun.default_allocator;
const kernel32 = bun.windows;
const linux = posix.linux;
const uv = bun.windows.libuv;
const C = bun.C;
const E = C.E;
const Environment = bun.Environment;
const FDImpl = bun.FDImpl;
const FileDescriptor = bun.FileDescriptor;
const JSC = bun.JSC;
const MAX_PATH_BYTES = bun.MAX_PATH_BYTES;
const Maybe = JSC.Maybe;
const SystemError = JSC.SystemError;
comptime {
bun.assert(Environment.isWindows);
}
pub const log = bun.sys.syslog;
pub const Error = bun.sys.Error;
// libuv dont suppport openat (https://github.com/libuv/libuv/issues/4167)
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 open(file_path: [:0]const u8, c_flags: bun.Mode, _perm: bun.Mode) Maybe(bun.FileDescriptor) {
assertIsValidWindowsPath(u8, file_path);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const flags = uv.O.fromBunO(c_flags);
var perm = _perm;
if (perm == 0) {
// Set a sensible default, otherwise on windows the file will be unuseable
perm = 0o644;
}
const rc = uv.uv_fs_open(uv.Loop.get(), &req, file_path.ptr, flags, perm, null);
log("uv open({s}, {d}, {d}) = {d}", .{ file_path, flags, perm, rc.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .open } }
else
.{ .result = bun.toFD(@as(i32, @intCast(req.result.int()))) };
}
pub fn mkdir(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
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) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
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.int() });
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) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .chown } }
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fchown } }
else
.{ .result = {} };
}
pub fn access(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .access } }
else
.{ .result = {} };
}
pub fn rmdir(file_path: [:0]const u8) Maybe(void) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .rmdir } }
else
.{ .result = {} };
}
pub fn unlink(file_path: [:0]const u8) Maybe(void) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .unlink } }
else
.{ .result = {} };
}
pub fn readlink(file_path: [:0]const u8, buf: []u8) Maybe([:0]u8) {
assertIsValidWindowsPath(u8, file_path);
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.int() });
return .{ .err = .{ .errno = errno, .syscall = .readlink } };
} else {
// Seems like `rc` does not contain the size?
bun.assert(rc.int() == 0);
const slice = bun.span(req.ptrAs([*:0]u8));
if (slice.len > buf.len) {
log("uv readlink({s}) = {d}, {s} TRUNCATED", .{ file_path, rc.int(), slice });
return .{ .err = .{ .errno = @intFromEnum(E.NOMEM), .syscall = .readlink } };
}
log("uv readlink({s}) = {d}, {s}", .{ file_path, rc.int(), slice });
@memcpy(buf[0..slice.len], slice);
buf[slice.len] = 0;
return .{ .result = buf[0..slice.len :0] };
}
}
pub fn rename(from: [:0]const u8, to: [:0]const u8) Maybe(void) {
assertIsValidWindowsPath(u8, from);
assertIsValidWindowsPath(u8, to);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .rename } }
else
.{ .result = {} };
}
pub fn link(from: [:0]const u8, to: [:0]const u8) Maybe(void) {
assertIsValidWindowsPath(u8, from);
assertIsValidWindowsPath(u8, to);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
const rc = uv.uv_fs_link(uv.Loop.get(), &req, from.ptr, to.ptr, null);
log("uv link({s}, {s}) = {d}", .{ from, to, rc.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .link } }
else
.{ .result = {} };
}
pub fn symlinkUV(from: [:0]const u8, to: [:0]const u8, flags: c_int) Maybe(void) {
assertIsValidWindowsPath(u8, from);
assertIsValidWindowsPath(u8, to);
var req: uv.fs_t = uv.fs_t.uninitialized;
defer req.deinit();
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .symlink } }
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.int() });
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.int() });
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fdatasync, .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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .fsync, .fd = fd } }
else
.{ .result = {} };
}
pub fn stat(path: [:0]const u8) Maybe(bun.Stat) {
assertIsValidWindowsPath(u8, path);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .stat } }
else
.{ .result = req.statbuf };
}
pub fn lstat(path: [:0]const u8) Maybe(bun.Stat) {
assertIsValidWindowsPath(u8, path);
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.int() });
return if (rc.errno()) |errno|
.{ .err = .{ .errno = errno, .syscall = .lstat } }
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 bun.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.int(), debug_timer });
}
if (rc.errno()) |errno| {
return .{ .err = .{ .errno = errno, .fd = fd, .syscall = .read } };
} else {
return .{ .result = @as(usize, @intCast(rc.int())) };
}
}
pub fn pwritev(fd: FileDescriptor, bufs: []const bun.PlatformIOVecConst, position: i64) Maybe(usize) {
const uv_fd = bun.uvfdcast(fd);
comptime bun.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.int(), debug_timer });
}
if (rc.errno()) |errno| {
return .{ .err = .{ .errno = errno, .fd = fd, .syscall = .write } };
} else {
return .{ .result = @as(usize, @intCast(rc.int())) };
}
}
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);
}
pub const Tag = @import("./sys.zig").Tag;