authorgravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2024-08-19 23:11:32-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-19 23:30:14-07:00
log0a70455095a19a4c18497e6786ca6139c1cc2892
treee3b5ca0499e459ac413a2e4152dcae5ad6a9b833
parentdffc8c44f9a01aa05ea364ffdc71509d15bc2601

Fix handling of empty XDG environment variables

Closes #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.

3 files changed, 13 insertions(+), 6 deletions(-)

lib/std/debug/Dwarf.zig+5-3
...@@ -2275,9 +2275,11 @@ pub const ElfModule = struct {...@@ -2275,9 +2275,11 @@ pub const ElfModule = struct {
2275 break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;2275 break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
2276 }2276 }
2277 if (std.posix.getenv("XDG_CACHE_HOME")) |cache_path| {2277 if (std.posix.getenv("XDG_CACHE_HOME")) |cache_path| {
2278 const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk;2278 if (cache_path.len > 0) {
2279 defer gpa.free(path);2279 const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk;
2280 break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;2280 defer gpa.free(path);
2281 break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
2282 }
2281 }2283 }
2282 if (std.posix.getenv("HOME")) |home_path| {2284 if (std.posix.getenv("HOME")) |home_path| {
2283 const path = std.fs.path.join(gpa, &[_][]const u8{ home_path, ".cache", "debuginfod_client" }) catch break :blk;2285 const path = std.fs.path.join(gpa, &[_][]const u8{ home_path, ".cache", "debuginfod_client" }) catch break :blk;
lib/std/fs/get_app_data_dir.zig+3-1
...@@ -32,7 +32,9 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi...@@ -32,7 +32,9 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
32 },32 },
33 .linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => {33 .linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => {
34 if (posix.getenv("XDG_DATA_HOME")) |xdg| {34 if (posix.getenv("XDG_DATA_HOME")) |xdg| {
35 return fs.path.join(allocator, &[_][]const u8{ xdg, appname });35 if (xdg.len > 0) {
36 return fs.path.join(allocator, &[_][]const u8{ xdg, appname });
37 }
36 }38 }
3739
38 const home_dir = posix.getenv("HOME") orelse {40 const home_dir = posix.getenv("HOME") orelse {
src/introspect.zig+5-2
...@@ -90,8 +90,11 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 {...@@ -90,8 +90,11 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 {
9090
91 if (builtin.os.tag != .windows) {91 if (builtin.os.tag != .windows) {
92 if (std.zig.EnvVar.XDG_CACHE_HOME.getPosix()) |cache_root| {92 if (std.zig.EnvVar.XDG_CACHE_HOME.getPosix()) |cache_root| {
93 return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });93 if (cache_root.len > 0) {
94 } else if (std.zig.EnvVar.HOME.getPosix()) |home| {94 return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
95 }
96 }
97 if (std.zig.EnvVar.HOME.getPosix()) |home| {
95 return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname });98 return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname });
96 }99 }
97 }100 }