diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/os/TempDir.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/os/TempDir.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/os/TempDir.zig | 83 |
1 files changed, 64 insertions, 19 deletions
diff --git a/vendor/libghostty-vt/src/os/TempDir.zig b/vendor/libghostty-vt/src/os/TempDir.zig index c3d344d9..c25589a2 100644 --- a/vendor/libghostty-vt/src/os/TempDir.zig +++ b/vendor/libghostty-vt/src/os/TempDir.zig @@ -3,8 +3,9 @@ const TempDir = @This(); const std = @import("std"); -const Dir = std.fs.Dir; +const Dir = std.Io.Dir; const file = @import("file.zig"); +const global = @import("../global.zig"); const log = std.log.scoped(.tempdir); @@ -27,10 +28,10 @@ pub fn init() !TempDir { var tmp_path_buf: [file.random_basename_len:0]u8 = undefined; const dir = dir: { - const cwd = std.fs.cwd(); - const tmp_dir = try file.allocTmpDir(std.heap.page_allocator); + const cwd = std.Io.Dir.cwd(); + const tmp_dir = try file.allocTmpDir(std.heap.page_allocator, global.environ()); defer file.freeTmpDir(std.heap.page_allocator, tmp_dir); - break :dir try cwd.openDir(tmp_dir, .{}); + break :dir try cwd.openDir(global.io(), tmp_dir, .{}); }; // We now loop forever until we can find a directory that we can create. @@ -38,13 +39,13 @@ pub fn init() !TempDir { const tmp_path = try file.randomBasename(&tmp_path_buf); tmp_path_buf[tmp_path.len] = 0; - dir.makeDir(tmp_path) catch |err| switch (err) { + dir.createDir(global.io(), tmp_path, .default_dir) catch |err| switch (err) { error.PathAlreadyExists => continue, else => |e| return e, }; return TempDir{ - .dir = try dir.openDir(tmp_path, .{}), + .dir = try dir.openDir(global.io(), tmp_path, .{}), .parent = dir, .name_buf = tmp_path_buf, }; @@ -60,25 +61,69 @@ pub fn name(self: *TempDir) []const u8 { /// Finish with the temporary directory. This deletes all contents in the /// 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}); + self.close(.delete); +} + +pub const CloseMode = enum { delete, retain }; + +/// Close the directory handles, optionally retaining the temporary directory +/// and its contents on disk. +pub fn close(self: *TempDir, mode: CloseMode) void { + self.dir.close(global.io()); + switch (mode) { + .delete => self.parent.deleteTree(global.io(), self.name()) catch |err| + log.err("error deleting temp dir err={}", .{err}), + .retain => {}, + } + self.parent.close(global.io()); } test { const testing = std.testing; - var td = try init(); - errdefer td.deinit(); + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + var path_len: usize = undefined; + { + var td = try init(); + errdefer td.deinit(); - const nameval = td.name(); - try testing.expect(nameval.len > 0); + const nameval = td.name(); + try testing.expect(nameval.len > 0); + + // Can open a new handle to it proves it exists. + var dir = try td.parent.openDir(testing.io, nameval, .{}); + dir.close(testing.io); + + path_len = try td.dir.realPath(testing.io, &path_buf); + + // Don't check the raw handles after close: descriptor numbers can be + // reused immediately, so a valid number does not imply that we leaked it. + td.deinit(); + } - // Can open a new handle to it proves it exists. - var dir = try td.parent.openDir(nameval, .{}); - dir.close(); + try testing.expectError( + error.FileNotFound, + Dir.openDirAbsolute(testing.io, path_buf[0..path_len], .{}), + ); +} + +test "close retains temporary directory" { + const testing = std.testing; + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + var path_len: usize = undefined; + { + var td = try init(); + errdefer td.deinit(); + + path_len = try td.dir.realPath(testing.io, &path_buf); + + // Don't check the raw handles after close: descriptor numbers can be + // reused immediately, so a valid number does not imply that we leaked it. + td.close(.retain); + } + defer Dir.deleteDirAbsolute(testing.io, path_buf[0..path_len]) catch {}; - // Should be deleted after we deinit - td.deinit(); - try testing.expectError(error.FileNotFound, td.parent.openDir(nameval, .{})); + var dir = try Dir.openDirAbsolute(testing.io, path_buf[0..path_len], .{}); + dir.close(testing.io); } |
