From 57894786d4349b30a8b218a773b844d481784c0e Mon Sep 17 00:00:00 2001 From: Will Pragnell Date: Thu, 14 Sep 2023 18:26:08 -0700 Subject: [PATCH] windows: implement tmpDir --- src/os/TempDir.zig | 8 +++++--- src/os/file.zig | 10 ++++++++++ src/os/homedir.zig | 6 ++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/os/TempDir.zig b/src/os/TempDir.zig index 0422a2cef..7c1354bae 100644 --- a/src/os/TempDir.zig +++ b/src/os/TempDir.zig @@ -61,8 +61,9 @@ pub fn name(self: *TempDir) []const u8 { } /// Finish with the temporary directory. This deletes all contents in the -/// directory. This is safe to call multiple times. +/// directory. pub fn deinit(self: *TempDir) void { + self.dir.close(); self.parent.deleteTree(self.name()) catch |err| log.err("error deleting temp dir err={}", .{err}); } @@ -78,13 +79,14 @@ const b64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345 test { var td = try init(); - defer td.deinit(); + errdefer td.deinit(); const nameval = td.name(); try testing.expect(nameval.len > 0); // Can open a new handle to it proves it exists. - _ = try td.parent.openDir(nameval, .{}); + var dir = try td.parent.openDir(nameval, .{}); + dir.close(); // Should be deleted after we deinit td.deinit(); diff --git a/src/os/file.zig b/src/os/file.zig index 7e54909b3..1e309ec6a 100644 --- a/src/os/file.zig +++ b/src/os/file.zig @@ -53,6 +53,16 @@ pub fn fixMaxFiles() void { /// Return the recommended path for temporary files. pub fn tmpDir() ?[]const u8 { + if (builtin.os.tag == .windows) { + // TODO: what is a good fallback path on windows? + const w_temp = std.os.getenvW(std.unicode.utf8ToUtf16LeStringLiteral("TMP")) orelse return null; + var buf = [_]u8{0} ** 256; // 256 is the maximum path length on windows + const len = std.unicode.utf16leToUtf8(buf[0..], w_temp[0..w_temp.len]) catch { + log.warn("failed to convert temp dir path from windows string", .{}); + return null; + }; + return buf[0..len]; + } if (std.os.getenv("TMPDIR")) |v| return v; if (std.os.getenv("TMP")) |v| return v; return "/tmp"; diff --git a/src/os/homedir.zig b/src/os/homedir.zig index d73405c09..548f137e7 100644 --- a/src/os/homedir.zig +++ b/src/os/homedir.zig @@ -84,8 +84,7 @@ fn homeWindows(buf: []u8) !?[]u8 { const fba = fba_instance.allocator(); const drive = std.process.getEnvVarOwned(fba, "HOMEDRIVE") catch |err| switch (err) { error.OutOfMemory => return Error.BufferTooSmall, - error.InvalidUtf8, - error.EnvironmentVariableNotFound => return null, + error.InvalidUtf8, error.EnvironmentVariableNotFound => return null, }; // could shift the contents if this ever happens if (drive.ptr != buf.ptr) @panic("codebug"); @@ -98,8 +97,7 @@ fn homeWindows(buf: []u8) !?[]u8 { const fba = fba_instance.allocator(); const homepath = std.process.getEnvVarOwned(fba, "HOMEPATH") catch |err| switch (err) { error.OutOfMemory => return Error.BufferTooSmall, - error.InvalidUtf8, - error.EnvironmentVariableNotFound => return null, + error.InvalidUtf8, error.EnvironmentVariableNotFound => return null, }; // could shift the contents if this ever happens if (homepath.ptr != path_buf.ptr) @panic("codebug");