diff options
Diffstat (limited to 'src/install.zig')
| -rw-r--r-- | src/install.zig | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/install.zig b/src/install.zig index 5eb5a29..27db4bb 100644 --- a/src/install.zig +++ b/src/install.zig | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | const libarchive = @import("libarchive"); | 1 | const libarchive = @import("libarchive"); |
| 2 | const curl = @import("curl"); | ||
| 2 | const std = @import("std"); | 3 | const std = @import("std"); |
| 3 | const xdg = @import("xdg"); | 4 | const xdg = @import("xdg"); |
| 4 | const zelda = @import("zelda"); | ||
| 5 | const zup = @import("zup"); | 5 | const zup = @import("zup"); |
| 6 | 6 | ||
| 7 | const Allocator = std.mem.Allocator; | 7 | const Allocator = std.mem.Allocator; |
| @@ -51,20 +51,26 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I | |||
| 51 | defer allocator.free(installation_dir); | 51 | defer allocator.free(installation_dir); |
| 52 | 52 | ||
| 53 | std.log.info("Installing {s}, version {}", .{ name, installation.version }); | 53 | std.log.info("Installing {s}, version {}", .{ name, installation.version }); |
| 54 | 54 | const filename = std.fs.path.basename(installation.url.?); | |
| 55 | std.log.info("Downloading from {s}...", .{installation.url.?}); | 55 | |
| 56 | var response = try zelda.get(allocator, installation.url.?); | 56 | // TODO: Platform-agnostic tempfile creation |
| 57 | defer response.deinit(); | 57 | var tmpname = try std.fmt.allocPrintZ(allocator, "/tmp/{s}", .{filename}); |
| 58 | 58 | defer allocator.free(tmpname); | |
| 59 | if (response.status_code.group() != .success) { | 59 | |
| 60 | std.log.err("HTTP Error: {}", .{response.status_code}); | 60 | var tmpdir = try std.fs.openDirAbsolute(std.fs.path.dirname(tmpname).?, .{}); |
| 61 | return error.HttpError; | 61 | defer tmpdir.close(); |
| 62 | |||
| 63 | var tmpfile = try tmpdir.createFile(filename, .{}); | ||
| 64 | defer { | ||
| 65 | tmpfile.close(); | ||
| 66 | std.log.info("Deleting /tmp/{s}...", .{filename}); | ||
| 67 | tmpdir.deleteFile(filename) catch |err| { | ||
| 68 | std.log.warn("Failed to delete /tmp/{s}: {}", .{ filename, err }); | ||
| 69 | }; | ||
| 62 | } | 70 | } |
| 63 | 71 | ||
| 64 | if (response.body == null) { | 72 | std.log.info("Downloading to /tmp/{s}...", .{filename}); |
| 65 | std.log.err("HTTP Error, no body received", .{}); | 73 | try curl.easyDownloadToFile(&tmpfile, installation.url.?); |
| 66 | return error.HttpError; | ||
| 67 | } | ||
| 68 | 74 | ||
| 69 | std.log.info("Extracting...", .{}); | 75 | std.log.info("Extracting...", .{}); |
| 70 | var archive = try ArchiveRead.init(); | 76 | var archive = try ArchiveRead.init(); |
| @@ -72,7 +78,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I | |||
| 72 | 78 | ||
| 73 | try archive.supportFilter(.all); | 79 | try archive.supportFilter(.all); |
| 74 | try archive.supportFormat(.all); | 80 | try archive.supportFormat(.all); |
| 75 | try archive.openMemory(response.body.?); | 81 | try archive.openFilename(tmpname, 10240); |
| 76 | 82 | ||
| 77 | while (try archive.nextHeader()) |entry_c| { | 83 | while (try archive.nextHeader()) |entry_c| { |
| 78 | var entry = entry_c; | 84 | var entry = entry_c; |