mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
refactor: handle tilde before checking realpath
This commit is contained in:

committed by
Mitchell Hashimoto

parent
f184258f0e
commit
7c9c982df7
@ -4361,8 +4361,7 @@ pub const RepeatablePath = struct {
|
|||||||
// If it isn't absolute, we need to make it absolute relative
|
// If it isn't absolute, we need to make it absolute relative
|
||||||
// to the base.
|
// to the base.
|
||||||
var buf: [std.fs.max_path_bytes]u8 = undefined;
|
var buf: [std.fs.max_path_bytes]u8 = undefined;
|
||||||
const abs = dir.realpath(path, &buf) catch |err| abs: {
|
|
||||||
if (err == error.FileNotFound) {
|
|
||||||
// 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 (path[0] == '~') {
|
if (path[0] == '~') {
|
||||||
const home_env_var = switch (builtin.os.tag) {
|
const home_env_var = switch (builtin.os.tag) {
|
||||||
@ -4370,7 +4369,21 @@ pub const RepeatablePath = struct {
|
|||||||
.windows => null,
|
.windows => null,
|
||||||
else => null,
|
else => null,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (home_env_var) |home_dir| {
|
if (home_env_var) |home_dir| {
|
||||||
|
// very unlikely to happen
|
||||||
|
if (!std.fs.path.isAbsolute(home_dir)) {
|
||||||
|
try diags.append(alloc, .{
|
||||||
|
.message = try std.fmt.allocPrintZ(
|
||||||
|
alloc,
|
||||||
|
"error resolving file path {s}: HOME environment variable is not an absolute path",
|
||||||
|
.{path},
|
||||||
|
),
|
||||||
|
});
|
||||||
|
self.value.items[i] = .{ .required = "" };
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
if (expanded_len > buf.len) {
|
if (expanded_len > buf.len) {
|
||||||
@ -4384,12 +4397,25 @@ pub const RepeatablePath = struct {
|
|||||||
self.value.items[i] = .{ .required = "" };
|
self.value.items[i] = .{ .required = "" };
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@memcpy(buf[0..home_dir.len], home_dir);
|
@memcpy(buf[0..home_dir.len], home_dir);
|
||||||
@memcpy(buf[home_dir.len..expanded_len], rest);
|
@memcpy(buf[home_dir.len..expanded_len], rest);
|
||||||
break :abs buf[0..expanded_len];
|
|
||||||
|
log.debug(
|
||||||
|
"expanding file path from home directory: path={s}",
|
||||||
|
.{buf[0..expanded_len]},
|
||||||
|
);
|
||||||
|
|
||||||
|
switch (self.value.items[i]) {
|
||||||
|
.optional, .required => |*p| p.* = try alloc.dupeZ(u8, buf[0..expanded_len]),
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const abs = dir.realpath(path, &buf) catch |err| abs: {
|
||||||
|
if (err == error.FileNotFound) {
|
||||||
// The file doesn't exist. Try to resolve the relative path
|
// The file doesn't exist. Try to resolve the relative path
|
||||||
// another way.
|
// another way.
|
||||||
const resolved = try std.fs.path.resolve(alloc, &.{ base, path });
|
const resolved = try std.fs.path.resolve(alloc, &.{ base, path });
|
||||||
|
Reference in New Issue
Block a user