authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-18 10:45:17-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-18 10:45:17-04:00
logcd488c9da56a03d2244a67629612445c451441da
tree77fc292e51837f5caafc861607ce8340a9035d7b
parenta8a1b5af07519b49b5ba31ad6c802a7ccc8a5e23

fix std.os.getAppDataDir test on linux


2 files changed, 21 insertions(+), 9 deletions(-)

std/os/get_app_data_dir.zig+18-9
...@@ -35,15 +35,21 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD...@@ -35,15 +35,21 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
35 else => return error.AppDataDirUnavailable,35 else => return error.AppDataDirUnavailable,
36 }36 }
37 },37 },
38 // TODO for macos it should be "~/Library/Application Support/<APPNAME>"38 builtin.Os.macosx => {
39 else => {39 const home_dir = os.getEnvPosix("HOME") orelse {
40 const home_dir = os.getEnvVarOwned(allocator, "HOME") catch |err| switch (err) {40 // TODO look in /etc/passwd
41 error.OutOfMemory => return error.OutOfMemory,41 return error.AppDataDirUnavailable;
42 error.EnvironmentVariableNotFound => return error.AppDataDirUnavailable, // TODO look in /etc/passwd42 };
43 return os.path.join(allocator, home_dir, "Library", "Application Support", appname);
44 },
45 builtin.Os.linux => {
46 const home_dir = os.getEnvPosix("HOME") orelse {
47 // TODO look in /etc/passwd
48 return error.AppDataDirUnavailable;
43 };49 };
44 defer allocator.free(home_dir);
45 return os.path.join(allocator, home_dir, ".local", "share", appname);50 return os.path.join(allocator, home_dir, ".local", "share", appname);
46 },51 },
52 else => @compileError("Unsupported OS"),
47 }53 }
48}54}
4955
...@@ -53,8 +59,11 @@ fn utf16lePtrSlice(ptr: [*]const u16) []const u16 {...@@ -53,8 +59,11 @@ fn utf16lePtrSlice(ptr: [*]const u16) []const u16 {
53 return ptr[0..index];59 return ptr[0..index];
54}60}
5561
56test "getAppDataDir" {62test "std.os.getAppDataDir" {
57 const result = try getAppDataDir(std.debug.global_allocator, "zig");63 var buf: [512]u8 = undefined;
58 std.debug.warn("{}...", result);64 const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
65
66 // We can't actually validate the result
67 _ = getAppDataDir(allocator, "zig") catch return;
59}68}
6069
std/os/index.zig+3
...@@ -498,6 +498,7 @@ pub var linux_aux_raw = []usize{0} ** 38;...@@ -498,6 +498,7 @@ pub var linux_aux_raw = []usize{0} ** 38;
498pub var posix_environ_raw: [][*]u8 = undefined;498pub var posix_environ_raw: [][*]u8 = undefined;
499499
500/// Caller must free result when done.500/// Caller must free result when done.
501/// TODO make this go through libc when we have it
501pub fn getEnvMap(allocator: *Allocator) !BufMap {502pub fn getEnvMap(allocator: *Allocator) !BufMap {
502 var result = BufMap.init(allocator);503 var result = BufMap.init(allocator);
503 errdefer result.deinit();504 errdefer result.deinit();
...@@ -541,6 +542,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {...@@ -541,6 +542,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
541 }542 }
542}543}
543544
545/// TODO make this go through libc when we have it
544pub fn getEnvPosix(key: []const u8) ?[]const u8 {546pub fn getEnvPosix(key: []const u8) ?[]const u8 {
545 for (posix_environ_raw) |ptr| {547 for (posix_environ_raw) |ptr| {
546 var line_i: usize = 0;548 var line_i: usize = 0;
...@@ -563,6 +565,7 @@ pub const GetEnvVarOwnedError = error{...@@ -563,6 +565,7 @@ pub const GetEnvVarOwnedError = error{
563};565};
564566
565/// Caller must free returned memory.567/// Caller must free returned memory.
568/// TODO make this go through libc when we have it
566pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {569pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
567 if (is_windows) {570 if (is_windows) {
568 const key_with_null = try cstr.addNullByte(allocator, key);571 const key_with_null = try cstr.addNullByte(allocator, key);