-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ zig-out-* | |
# libarchive | ||
*.o | ||
*.a | ||
.zig-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,48 @@ | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
const tools = @import("tools.zig"); | ||
|
||
pub fn extract_tarxz_to_dir(allocator: std.mem.Allocator, outDir: std.fs.Dir, file: std.fs.File) !void { | ||
var buffered_reader = std.io.bufferedReader(file.reader()); | ||
var decompressed = try std.compress.xz.decompress(allocator, buffered_reader.reader()); | ||
defer decompressed.deinit(); | ||
try std.tar.pipeToFileSystem(outDir, decompressed.reader(), .{ .mode_mode = .executable_bit_only, .strip_components = 1 }); | ||
if (builtin.os.tag == .windows) { | ||
try extract_zip_dir(outDir, file); | ||
} else { | ||
var buffered_reader = std.io.bufferedReader(file.reader()); | ||
var decompressed = try std.compress.xz.decompress(allocator, buffered_reader.reader()); | ||
defer decompressed.deinit(); | ||
try std.tar.pipeToFileSystem(outDir, decompressed.reader(), .{ .mode_mode = .executable_bit_only, .strip_components = 1 }); | ||
} | ||
} | ||
|
||
pub fn extract_zip_dir(outDir: std.fs.Dir, file: std.fs.File) !void { | ||
var arena = std.heap.ArenaAllocator.init(tools.getAllocator()); | ||
defer arena.deinit(); | ||
|
||
const allocator = arena.allocator(); | ||
|
||
const tmp_path = try tools.getZvmPathSegment(allocator, "tmpdir"); | ||
defer std.fs.deleteDirAbsolute(tmp_path) catch unreachable; | ||
|
||
// make tmp dir | ||
try std.fs.makeDirAbsolute(tmp_path); | ||
var tmp_dir = try std.fs.openDirAbsolute(tmp_path, .{ .iterate = true }); | ||
|
||
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 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, outDir, entry.name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
|
||
var allocator: std.mem.Allocator = undefined; | ||
|
||
var home_dir: []const u8 = undefined; | ||
|
||
pub const log = std.log.scoped(.zvm); | ||
|
||
/// init the data | ||
pub fn dataInit(tmp_allocator: std.mem.Allocator) !void { | ||
allocator = tmp_allocator; | ||
// setting the home dir | ||
home_dir = if (builtin.os.tag == .windows) | ||
try std.process.getEnvVarOwned(allocator, "USERPROFILE") | ||
else | ||
std.posix.getenv("HOME") orelse "."; | ||
} | ||
|
||
/// deinit the data | ||
pub fn dataDeinit() void { | ||
if (builtin.os.tag == .windows) | ||
allocator.free(home_dir); | ||
} | ||
|
||
/// get home dir | ||
pub fn getHome() []const u8 { | ||
return home_dir; | ||
} | ||
|
||
/// get the allocator | ||
pub fn getAllocator() std.mem.Allocator { | ||
return allocator; | ||
} | ||
|
||
pub fn getZvmPathSegment(_allocator: std.mem.Allocator, segment: []const u8) ![]u8 { | ||
return std.fs.path.join( | ||
_allocator, | ||
&[_][]const u8{ getHome(), ".zm", segment }, | ||
); | ||
} |