Skip to content

Commit

Permalink
Fix handling of empty XDG environment variables
Browse files Browse the repository at this point in the history
Closes ziglang#21132

According to the XDG Base Directory specification
(https://specifications.freedesktop.org/basedir-spec/latest/#variables),
empty values for these environment variables should be treated the same
as if they are unset. Specifically, for the instances changed in this
commit,

> $XDG_DATA_HOME defines the base directory relative to which
> user-specific data files should be stored. If $XDG_DATA_HOME is either
> not set **or empty**, a default equal to $HOME/.local/share should be
> used.

and

> $XDG_CACHE_HOME defines the base directory relative to which
> user-specific non-essential data files should be stored. If
> $XDG_CACHE_HOME is either not set **or empty**, a default equal to
> $HOME/.cache should be used.

(emphasis mine)

In addition to the case mentioned in the linked issue, all other uses of
XDG environment variables were corrected.
  • Loading branch information
ianprime0509 authored and andrewrk committed Aug 20, 2024
1 parent dffc8c4 commit 0a70455
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/std/debug/Dwarf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2275,9 +2275,11 @@ pub const ElfModule = struct {
break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
}
if (std.posix.getenv("XDG_CACHE_HOME")) |cache_path| {
const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk;
defer gpa.free(path);
break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
if (cache_path.len > 0) {
const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk;
defer gpa.free(path);
break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
}
}
if (std.posix.getenv("HOME")) |home_path| {
const path = std.fs.path.join(gpa, &[_][]const u8{ home_path, ".cache", "debuginfod_client" }) catch break :blk;
Expand Down
4 changes: 3 additions & 1 deletion lib/std/fs/get_app_data_dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
},
.linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => {
if (posix.getenv("XDG_DATA_HOME")) |xdg| {
return fs.path.join(allocator, &[_][]const u8{ xdg, appname });
if (xdg.len > 0) {
return fs.path.join(allocator, &[_][]const u8{ xdg, appname });
}
}

const home_dir = posix.getenv("HOME") orelse {
Expand Down
7 changes: 5 additions & 2 deletions src/introspect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 {

if (builtin.os.tag != .windows) {
if (std.zig.EnvVar.XDG_CACHE_HOME.getPosix()) |cache_root| {
return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
} else if (std.zig.EnvVar.HOME.getPosix()) |home| {
if (cache_root.len > 0) {
return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
}
}
if (std.zig.EnvVar.HOME.getPosix()) |home| {
return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname });
}
}
Expand Down

0 comments on commit 0a70455

Please sign in to comment.