diff options
| author | 2022-11-15 13:55:07 +0200 | |
|---|---|---|
| committer | 2022-11-15 13:55:07 +0200 | |
| commit | 33226c20c41e3fe2de3330b6692c90bc4818b119 (patch) | |
| tree | 4e66df2da513eb7270c1d0ebc23aef3e264099f8 | |
| parent | Replace my zig-curl with zelda (diff) | |
| download | zup-33226c20c41e3fe2de3330b6692c90bc4818b119.tar.gz zup-33226c20c41e3fe2de3330b6692c90bc4818b119.tar.xz zup-33226c20c41e3fe2de3330b6692c90bc4818b119.zip | |
Don't download the archive to a file
| m--------- | libs/libarchive | 0 | ||||
| -rw-r--r-- | src/http.zig | 23 | ||||
| -rw-r--r-- | src/install.zig | 32 | ||||
| -rw-r--r-- | src/main.zig | 1 |
4 files changed, 13 insertions, 43 deletions
diff --git a/libs/libarchive b/libs/libarchive | |||
| Subproject 50747939555b62e3b830c1ec307f746e35b94ec | Subproject 188925b4d595d49f5dbc735778bf0438953ce43 | ||
diff --git a/src/http.zig b/src/http.zig deleted file mode 100644 index bb3b3ac..0000000 --- a/src/http.zig +++ /dev/null | |||
| @@ -1,23 +0,0 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const zelda = @import("zelda"); | ||
| 3 | |||
| 4 | const Allocator = std.mem.Allocator; | ||
| 5 | const File = std.fs.File; | ||
| 6 | |||
| 7 | // TODO: zelda should be able to do this internally so I wouldn't have to allocate memory for the | ||
| 8 | // file | ||
| 9 | pub fn downloadToFile(allocator: Allocator, file: *File, url: []const u8) !void { | ||
| 10 | var response = try zelda.get(allocator, url); | ||
| 11 | defer response.deinit(); | ||
| 12 | |||
| 13 | if (response.status_code.group() != .success) { | ||
| 14 | std.log.err("HTTP Error: {}", .{response.status_code}); | ||
| 15 | return error.HttpError; | ||
| 16 | } | ||
| 17 | |||
| 18 | if (response.body) |body| { | ||
| 19 | try file.writer().writeAll(body); | ||
| 20 | } else { | ||
| 21 | std.log.warn("Downloaded nothing from {s}...", .{url}); | ||
| 22 | } | ||
| 23 | } | ||
diff --git a/src/install.zig b/src/install.zig index 47b804c..5eb5a29 100644 --- a/src/install.zig +++ b/src/install.zig | |||
| @@ -51,26 +51,20 @@ 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 | const filename = std.fs.path.basename(installation.url.?); | 54 | |
| 55 | 55 | std.log.info("Downloading from {s}...", .{installation.url.?}); | |
| 56 | // TODO: Platform-agnostic tempfile creation | 56 | var response = try zelda.get(allocator, installation.url.?); |
| 57 | var tmpname = try std.fmt.allocPrintZ(allocator, "/tmp/{s}", .{filename}); | 57 | defer response.deinit(); |
| 58 | defer allocator.free(tmpname); | 58 | |
| 59 | 59 | if (response.status_code.group() != .success) { | |
| 60 | var tmpdir = try std.fs.openDirAbsolute(std.fs.path.dirname(tmpname).?, .{}); | 60 | std.log.err("HTTP Error: {}", .{response.status_code}); |
| 61 | defer tmpdir.close(); | 61 | return error.HttpError; |
| 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 | }; | ||
| 70 | } | 62 | } |
| 71 | 63 | ||
| 72 | std.log.info("Downloading to /tmp/{s}...", .{filename}); | 64 | if (response.body == null) { |
| 73 | try zup.http.downloadToFile(allocator, &tmpfile, installation.url.?); | 65 | std.log.err("HTTP Error, no body received", .{}); |
| 66 | return error.HttpError; | ||
| 67 | } | ||
| 74 | 68 | ||
| 75 | std.log.info("Extracting...", .{}); | 69 | std.log.info("Extracting...", .{}); |
| 76 | var archive = try ArchiveRead.init(); | 70 | var archive = try ArchiveRead.init(); |
| @@ -78,7 +72,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I | |||
| 78 | 72 | ||
| 79 | try archive.supportFilter(.all); | 73 | try archive.supportFilter(.all); |
| 80 | try archive.supportFormat(.all); | 74 | try archive.supportFormat(.all); |
| 81 | try archive.openFilename(tmpname, 10240); | 75 | try archive.openMemory(response.body.?); |
| 82 | 76 | ||
| 83 | while (try archive.nextHeader()) |entry_c| { | 77 | while (try archive.nextHeader()) |entry_c| { |
| 84 | var entry = entry_c; | 78 | var entry = entry_c; |
diff --git a/src/main.zig b/src/main.zig index 56b1b77..11aae8f 100644 --- a/src/main.zig +++ b/src/main.zig | |||
| @@ -2,7 +2,6 @@ pub const Config = @import("Config.zig"); | |||
| 2 | pub const Installation = @import("Installation.zig"); | 2 | pub const Installation = @import("Installation.zig"); |
| 3 | pub const Installations = Installation.Installations; | 3 | pub const Installations = Installation.Installations; |
| 4 | pub const SubCommand = @import("subcommand.zig").SubCommand; | 4 | pub const SubCommand = @import("subcommand.zig").SubCommand; |
| 5 | pub const http = @import("http.zig"); | ||
| 6 | 5 | ||
| 7 | pub const help = SubCommand(Help); | 6 | pub const help = SubCommand(Help); |
| 8 | pub const install = SubCommand(@import("install.zig")); | 7 | pub const install = SubCommand(@import("install.zig")); |