11const std = @import ("std" );
22const utils = @import ("utils.zig" );
33
4+ pub const PathBuf = [std .fs .MAX_PATH_BYTES ]u8 ;
5+
6+ pub const TempByZigd = "tmp" ;
7+ pub const CacheByZigd = "cached_master" ;
8+ pub const VersionsByZigd = "versions" ;
9+
410/// Version cannot be master!
511/// Returns a bool if it installed/reinstalled zig
612pub fn install_zig (allocator : std.mem.Allocator , zigd_path : []const u8 , download_url : []const u8 , zig_version : ZigVersion ) ! bool {
7- const final_destination = try std . fs . path . join ( allocator , &.{ zigd_path , "versions" , zig_version . as_string }) ;
8- defer allocator . free ( final_destination );
13+ var final_dest_buf : PathBuf = undefined ;
14+ const final_destination = utils . join_path ( & final_dest_buf , &.{ zigd_path , VersionsByZigd , zig_version . as_string } );
915
1016 if (try utils .isDirectory (final_destination )) {
1117 o : while (true ) {
@@ -50,8 +56,8 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
5056 return error .ResponseWasNotOk ;
5157 }
5258
53- const temp_dir_path = try std . fs . path . join ( allocator , &.{ zigd_path , "tmp" }) ;
54- defer allocator . free ( temp_dir_path );
59+ var temp_dir_buf : PathBuf = undefined ;
60+ const temp_dir_path = utils . join_path ( & temp_dir_buf , &.{ zigd_path , "tmp" } );
5561
5662 try utils .createDirectoryIgnoreExist (temp_dir_path );
5763
@@ -63,7 +69,7 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
6369
6470 var temp_storage_closed : bool = false ;
6571 var temporary_storage = try temp_dir .makeOpenPath (temp_name , .{
66- .iterate = true ,
72+ .iterate = utils . os_tag == .windows ,
6773 });
6874 errdefer if (! temp_storage_closed ) temporary_storage .close ();
6975
@@ -88,7 +94,7 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
8894 try temporary_storage .rename (w_path_duped , final_destination );
8995 temporary_storage .close ();
9096 temp_storage_closed = true ;
91- try std . fs . cwd (). deleteDir (temp_name );
97+ try temp_dir . deleteTree (temp_name );
9298 } else {
9399 var xz_decompressor = try std .compress .xz .decompress (allocator , req .reader ());
94100 defer xz_decompressor .deinit ();
@@ -104,7 +110,7 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
104110}
105111
106112pub fn getCachePath (allocator : std.mem.Allocator , zigd_path : []const u8 ) ! []u8 {
107- return try std .fs .path .join (allocator , &.{ zigd_path , "cached_master" });
113+ return try std .fs .path .join (allocator , &.{ zigd_path , CacheByZigd });
108114}
109115
110116pub fn getCacheFile (cache_path : []const u8 ) ! std.fs.File {
@@ -292,14 +298,6 @@ pub fn getZigdPath(allocator: std.mem.Allocator) ![]u8 {
292298
293299// I'm bored, that's why im gonna do it the messy style
294300pub fn garbage_collect_tempdir (zigd_path : []const u8 ) ! void {
295- var buf : [std .fs .MAX_PATH_BYTES ]u8 = undefined ;
296- var idx : usize = 0 ;
297- @memcpy (buf [0 .. idx + zigd_path .len ], zigd_path );
298- idx += zigd_path .len ;
299- buf [idx ] = std .fs .path .sep ;
300- idx += 1 ;
301- @memcpy (buf [idx .. idx + "tmp" .len ], "tmp" );
302- idx += "tmp" .len ;
303-
304- try std .fs .deleteTreeAbsolute (buf [0.. idx ]);
301+ var buf : PathBuf = undefined ;
302+ try std .fs .deleteTreeAbsolute (utils .join_path (& buf , &.{ zigd_path , "tmp" }));
305303}
0 commit comments