From 60450715bf75f152ac456bb5852bf42ac6e2ed65 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Mon, 19 Aug 2024 22:52:44 +0800 Subject: [PATCH] fix for windows platform --- src/install.zig | 12 ++++++++++++ src/util/extract.zig | 22 +++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/install.zig b/src/install.zig index 7846572..8d21553 100644 --- a/src/install.zig +++ b/src/install.zig @@ -80,6 +80,18 @@ fn install_zig(version: []const u8) !void { try util_extract.extract(extract_dir, new_file, if (builtin.os.tag == .windows) .zip else .tarxz, false); + // mv + const sub_path = try std.fs.path.join(arena_allocator, &.{ + extract_path, try std.mem.concat( + arena_allocator, + u8, + &.{ "zig-", reverse_platform_str, "-", version }, + ), + }); + defer std.fs.deleteTreeAbsolute(sub_path) catch unreachable; + + try util_tool.copy_dir(sub_path, extract_path); + try alias.set_version(version, false); } diff --git a/src/util/extract.zig b/src/util/extract.zig index 266c44d..cbf743b 100644 --- a/src/util/extract.zig +++ b/src/util/extract.zig @@ -2,6 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const data = @import("data.zig"); +const tool = @import("tool.zig"); const xz = std.compress.xz; const tar = std.tar; @@ -41,26 +42,17 @@ fn extract_zip_dir(out_dir: std.fs.Dir, file: std.fs.File) !void { const allocator = arena.allocator(); // for decompressing zig, we need to make a temp directory const tmp_path = try data.get_zvm_path_segment(allocator, "tmpdir"); - defer std.fs.deleteDirAbsolute(tmp_path) catch unreachable; + defer std.fs.deleteTreeAbsolute(tmp_path) catch unreachable; try std.fs.makeDirAbsolute(tmp_path); var tmp_dir = try std.fs.openDirAbsolute(tmp_path, .{ .iterate = true }); + defer tmp_dir.close(); - // extract zig + // extract zip try std.zip.extract(tmp_dir, file.seekableStream(), .{}); - var iterate = tmp_dir.iterate(); - var sub_dir = blk: { - const entry = try iterate.next() orelse return error.NotFound; - break :blk try tmp_dir.openDir(entry.name, .{ .iterate = true }); - }; - defer sub_dir.close(); + const out_path = try out_dir.realpathAlloc(allocator, ""); + defer allocator.free(out_path); - const sub_path = try sub_dir.realpathAlloc(allocator, ""); - defer std.fs.deleteDirAbsolute(sub_path) catch unreachable; - - var sub_iterate = sub_dir.iterate(); - while (try sub_iterate.next()) |entry| { - try std.fs.rename(sub_dir, entry.name, out_dir, entry.name); - } + try tool.copy_dir(tmp_path, out_path); }