forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.zig
89 lines (81 loc) · 3.24 KB
/
tmp.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
const bun = @import("root").bun;
const std = @import("std");
const Environment = bun.Environment;
const O = bun.O;
// O_TMPFILE doesn't seem to work very well.
const allow_tmpfile = false;
// To be used with files
// not folders!
pub const Tmpfile = struct {
destination_dir: bun.FileDescriptor = bun.invalid_fd,
tmpfilename: [:0]const u8 = "",
fd: bun.FileDescriptor = bun.invalid_fd,
using_tmpfile: bool = allow_tmpfile,
pub fn create(
destination_dir: bun.FileDescriptor,
tmpfilename: [:0]const u8,
) bun.JSC.Maybe(Tmpfile) {
const perm = 0o644;
var tmpfile = Tmpfile{
.destination_dir = destination_dir,
.tmpfilename = tmpfilename,
};
open: while (true) {
if (comptime allow_tmpfile) {
switch (bun.sys.openat(destination_dir, ".", O.WRONLY | O.TMPFILE | O.CLOEXEC, perm)) {
.result => |fd| {
tmpfile.fd = switch (bun.sys.makeLibUVOwnedFD(fd, .open, .close_on_fail)) {
.result => |owned_fd| owned_fd,
.err => |err| return .{ .err = err },
};
break :open;
},
.err => |err| {
switch (err.getErrno()) {
.INVAL, .OPNOTSUPP, .NOSYS => {
tmpfile.using_tmpfile = false;
},
else => return .{ .err = err },
}
},
}
}
tmpfile.fd = switch (bun.sys.openat(destination_dir, tmpfilename, O.CREAT | O.CLOEXEC | O.WRONLY, perm)) {
.result => |fd| switch (bun.sys.toLibUVOwnedFD(fd, .open, .close_on_fail)) {
.result => |owned_fd| owned_fd,
.err => |err| return .{ .err = err },
},
.err => |err| return .{ .err = err },
};
break :open;
}
return .{ .result = tmpfile };
}
pub fn finish(this: *Tmpfile, destname: [:0]const u8) !void {
if (comptime allow_tmpfile) {
if (this.using_tmpfile) {
var retry = true;
const basename: [:0]const u8 = @ptrCast(std.fs.path.basename(destname));
while (retry) {
const ret = bun.sys.linkatTmpfile(this.fd, this.destination_dir, basename);
switch (ret) {
.result => {
return;
},
.err => |err| {
if (err.getErrno() == .EXIST and retry) {
_ = bun.sys.unlinkat(this.destination_dir, basename);
retry = false;
continue;
} else {
try ret.unwrap();
return;
}
},
}
}
}
}
try bun.C.moveFileZWithHandle(this.fd, this.destination_dir, this.tmpfilename, this.destination_dir, destname);
}
};