move current expandHome functionality into separate expandHomeUnix function

This commit is contained in:
z-jxy
2024-12-28 21:06:56 -05:00
committed by Mitchell Hashimoto
parent 138a8f1602
commit 5ae2cc01ac
2 changed files with 22 additions and 18 deletions

View File

@ -4364,26 +4364,21 @@ pub const RepeatablePath = struct {
// Check if the path starts with a tilde and expand it to the home directory on linux/mac // Check if the path starts with a tilde and expand it to the home directory on linux/mac
if (std.mem.startsWith(u8, path, "~/")) { if (std.mem.startsWith(u8, path, "~/")) {
if (try internal_os.expandHome(path, &buf)) |expanded_path| { const expanded: []u8 = try internal_os.expandHome(path, &buf) orelse {
log.debug( // Blank this path so that we don't attempt to resolve it again
"expanding file path from home directory: path={s}",
.{expanded_path},
);
switch (self.value.items[i]) {
.optional, .required => |*p| p.* = try alloc.dupeZ(u8, expanded_path),
}
continue;
} else {
try diags.append(alloc, .{
.message = try std.fmt.allocPrintZ(
alloc,
"error expanding home path {s}",
.{path},
),
});
self.value.items[i] = .{ .required = "" }; self.value.items[i] = .{ .required = "" };
continue; continue;
};
log.debug(
"expanding file path from home directory: path={s}",
.{expanded},
);
switch (self.value.items[i]) {
.optional, .required => |*p| p.* = try alloc.dupeZ(u8, expanded),
} }
continue;
} }
const abs = dir.realpath(path, &buf) catch |err| abs: { const abs = dir.realpath(path, &buf) catch |err| abs: {

View File

@ -115,7 +115,16 @@ fn trimSpace(input: []const u8) []const u8 {
/// Errors if `home` fails or if the size of the expanded path is larger than `buf.len`. /// Errors if `home` fails or if the size of the expanded path is larger than `buf.len`.
/// ///
/// Returns null if the value returned from `home` is null, otherwise returns a slice to the expanded path. /// Returns null if the value returned from `home` is null, otherwise returns a slice to the expanded path.
pub fn expandHome(path: []const u8, buf: []u8) !?[]u8 { pub inline fn expandHome(path: []const u8, buf: []u8) !?[]u8 {
return switch (builtin.os.tag) {
inline .linux, .macos => expandHomeUnix(path, buf),
.ios => return null,
else => @compileError("unimplemented"),
};
}
fn expandHomeUnix(path: []const u8, buf: []u8) !?[]u8 {
if (!std.mem.startsWith(u8, path, "~/")) return null;
const home_dir = try home(buf) orelse return null; const home_dir = try home(buf) orelse return null;
const rest = path[1..]; // Skip the ~ const rest = path[1..]; // Skip the ~
const expanded_len = home_dir.len + rest.len; const expanded_len = home_dir.len + rest.len;