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
|
/// pardes (build.zig): `path`, opened for reading only once it is known
/// to be a regular file. Elsewhere than Linux, a stat by path first.
fn pardesOpenRegular(io: std.Io, path: []const u8) !std.Io.File {
if (comptime builtin.target.os.tag != .linux) {
if ((try std.Io.Dir.cwd().statFile(io, path, .{})).kind != .file) return error.NotRegularFile;
return std.Io.Dir.cwd().openFile(io, path, .{});
}
const probe = try std.Io.Dir.cwd().openFile(io, path, .{ .path_only = true });
defer probe.close(io);
const stat = try probe.stat(io);
if (stat.kind != .file) return error.NotRegularFile;
var buf: [32]u8 = undefined;
const same = std.fmt.bufPrint(&buf, "/proc/self/fd/{d}", .{probe.handle}) catch unreachable;
return std.Io.Dir.cwd().openFile(io, same, .{});
}
/// pardes (build.zig): a medium's read as ghostty makes it, handed to the
/// host's `LoadingImage.read_medium`: its bytes, the caller's.
const PardesMedium = struct {
loading: *LoadingImage,
io: std.Io,
alloc: Allocator,
t: command.Transmission,
path: []const u8,
fn read(ctx: *const anyopaque) LoadingImage.MediumError![]u8 {
const m: *const PardesMedium = @ptrCast(@alignCast(ctx));
switch (m.t.medium) {
.direct => unreachable, // read by the caller
.file => try m.loading.readFile(.file, m.io, m.alloc, m.t, m.path),
.temporary_file => try m.loading.readFile(.temporary_file, m.io, m.alloc, m.t, m.path),
.shared_memory => try m.loading.readSharedMemory(m.io, m.alloc, m.t, m.path),
}
return m.loading.data.toOwnedSlice(m.alloc);
}
};
|