From 5ae2cc01ac19da64fe95c7e9717971712ca3625c Mon Sep 17 00:00:00 2001 From: z-jxy Date: Sat, 28 Dec 2024 21:06:56 -0500 Subject: [PATCH] move current `expandHome` functionality into separate `expandHomeUnix` function --- src/config/Config.zig | 29 ++++++++++++----------------- src/os/homedir.zig | 11 ++++++++++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 5a9fdcc3f..27b5f9d03 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -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 if (std.mem.startsWith(u8, path, "~/")) { - if (try internal_os.expandHome(path, &buf)) |expanded_path| { - log.debug( - "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}, - ), - }); + const expanded: []u8 = try internal_os.expandHome(path, &buf) orelse { + // Blank this path so that we don't attempt to resolve it again self.value.items[i] = .{ .required = "" }; 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: { diff --git a/src/os/homedir.zig b/src/os/homedir.zig index b03e7f354..aea7a0017 100644 --- a/src/os/homedir.zig +++ b/src/os/homedir.zig @@ -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`. /// /// 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 rest = path[1..]; // Skip the ~ const expanded_len = home_dir.len + rest.len;