Repository navigation
Expand file tree
/
Copy pathpaths.zig
More file actions
142 lines (129 loc) · 6.05 KB
/
Copy pathpaths.zig
File metadata and controls
142 lines (129 loc) · 6.05 KB
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
//! Where minipg keeps things outside the .pgdb: runtime state (run.json,
//! the control socket), logs, and the root cache. The .pgdb itself holds
//! only data and config.
//!
//! what Linux macOS
//! runtime $XDG_RUNTIME_DIR/minipg/<key>/ $TMPDIR/minipg-$UID/<key>/
//! logs ~/.local/state/minipg/logs/<key>.log ~/Library/Logs/minipg/<key>.log
//! roots ~/.cache/minipg/roots/<digest>/ ~/Library/Caches/minipg/roots/<digest>/
//!
//! what Windows
//! runtime %LOCALAPPDATA%\minipg\run\<key>\
//! logs %LOCALAPPDATA%\minipg\logs\<key>.log
//! roots %LOCALAPPDATA%\minipg\roots\<digest>\
//!
//! <key> is a hash of the .pgdb directory's device and inode (on Windows,
//! its volume and file index), so it follows the directory through
//! renames. Linux falls back to ~/.cache/minipg/run/ without
//! XDG_RUNTIME_DIR, and honors XDG_STATE_HOME and XDG_CACHE_HOME. Runtime
//! directories are mode 0700; on Windows, %LOCALAPPDATA% is already the
//! user's own.
const std = @import("std");
const builtin = @import("builtin");
const sys = @import("sys.zig");
const Environ = std.process.Environ;
pub const Dirs = struct {
/// Parent of the per-.pgdb runtime directories.
runtime: []const u8,
/// Directory of per-.pgdb log files.
logs: []const u8,
/// The root cache.
roots: []const u8,
pub fn runtimeFor(d: Dirs, arena: std.mem.Allocator, k: []const u8) ![]const u8 {
return std.fs.path.join(arena, &.{ d.runtime, k });
}
pub fn logFor(d: Dirs, arena: std.mem.Allocator, k: []const u8) ![]const u8 {
return std.fmt.allocPrint(arena, "{s}{c}{s}.log", .{ d.logs, std.fs.path.sep, k });
}
};
fn get(env: *const Environ.Map, name: []const u8) ?[]const u8 {
const v = env.get(name) orelse return null;
return if (v.len > 0) v else null;
}
/// What `dirs` needs and couldn't find, for the error message.
pub const home_var = if (sys.is_windows) "LOCALAPPDATA" else "HOME";
pub fn dirs(arena: std.mem.Allocator, env: *const Environ.Map) !Dirs {
if (sys.is_windows) {
const local = get(env, "LOCALAPPDATA") orelse return error.NoHomeDir;
return .{
.runtime = try std.fs.path.join(arena, &.{ local, "minipg", "run" }),
.logs = try std.fs.path.join(arena, &.{ local, "minipg", "logs" }),
.roots = try std.fs.path.join(arena, &.{ local, "minipg", "roots" }),
};
}
const home = get(env, "HOME") orelse return error.NoHomeDir;
if (builtin.os.tag == .macos) {
const tmp = get(env, "TMPDIR") orelse "/tmp";
return .{
.runtime = try std.fmt.allocPrint(arena, "{s}/minipg-{d}", .{ std.mem.trimEnd(u8, tmp, "/"), std.c.getuid() }),
.logs = try std.fs.path.join(arena, &.{ home, "Library/Logs/minipg" }),
.roots = try std.fs.path.join(arena, &.{ home, "Library/Caches/minipg/roots" }),
};
}
const cache = get(env, "XDG_CACHE_HOME") orelse try std.fs.path.join(arena, &.{ home, ".cache" });
const state = get(env, "XDG_STATE_HOME") orelse try std.fs.path.join(arena, &.{ home, ".local/state" });
const runtime = if (get(env, "XDG_RUNTIME_DIR")) |r|
try std.fs.path.join(arena, &.{ r, "minipg" })
else
try std.fs.path.join(arena, &.{ cache, "minipg/run" });
return .{
.runtime = runtime,
.logs = try std.fs.path.join(arena, &.{ state, "minipg/logs" }),
.roots = try std.fs.path.join(arena, &.{ cache, "minipg/roots" }),
};
}
/// The key for a .pgdb directory: its device and inode, hashed.
pub fn key(arena: std.mem.Allocator, dir: []const u8) ![]const u8 {
const z = try arena.dupeZ(u8, dir);
var h: std.hash.Wyhash = .init(0);
if (sys.is_windows) {
const id = try sys.fileId(arena, dir);
h.update(std.mem.asBytes(&id));
return std.fmt.allocPrint(arena, "{x:0>16}", .{h.final()});
}
if (builtin.os.tag == .linux) {
const linux = std.os.linux;
var st: linux.Statx = undefined;
if (linux.errno(linux.statx(linux.AT.FDCWD, z, 0, .{ .INO = true }, &st)) != .SUCCESS) return error.FileNotFound;
h.update(std.mem.asBytes(&st.dev_major));
h.update(std.mem.asBytes(&st.dev_minor));
h.update(std.mem.asBytes(&st.ino));
return std.fmt.allocPrint(arena, "{x:0>16}", .{h.final()});
}
// fstatat, not stat: Zig 0.16's std.c.stat names a private.stat it
// never declares, so it fails to compile for aarch64-macos.
var st: std.c.Stat = undefined;
if (std.c.fstatat(std.c.AT.FDCWD, z, &st, 0) != 0) return error.FileNotFound;
h.update(std.mem.asBytes(&st.dev));
h.update(std.mem.asBytes(&st.ino));
return std.fmt.allocPrint(arena, "{x:0>16}", .{h.final()});
}
/// Creates `path` and its parents, and makes `path` itself mode 0700.
pub fn makePrivateDir(io: std.Io, arena: std.mem.Allocator, path: []const u8) !void {
try std.Io.Dir.cwd().createDirPath(io, path);
try chmod(arena, path, 0o700);
}
/// Sets a file's mode on POSIX; a no-op on Windows, which has none.
pub fn chmod(arena: std.mem.Allocator, path: []const u8, mode: u32) !void {
if (sys.is_windows) return;
_ = std.c.chmod(try arena.dupeZ(u8, path), @intCast(mode));
}
/// Writes `data` to `path` through a temporary file and a rename, so
/// readers see the old contents or the new, never part.
pub fn writeAtomic(io: std.Io, arena: std.mem.Allocator, path: []const u8, data: []const u8, mode: u32) !void {
const tmp = try std.fmt.allocPrint(arena, "{s}.tmp{d}", .{ path, sys.pid() });
const cwd = std.Io.Dir.cwd();
try cwd.writeFile(io, .{ .sub_path = tmp, .data = data });
try chmod(arena, tmp, mode);
cwd.rename(tmp, cwd, path, io) catch |err| {
cwd.deleteFile(io, tmp) catch {};
return err;
};
}
test key {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = try key(arena.allocator(), ".");
try std.testing.expectEqual(@as(usize, 16), a.len);
try std.testing.expectEqualStrings(a, try key(arena.allocator(), "."));
}