From 33226c20c41e3fe2de3330b6692c90bc4818b119 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 15 Nov 2022 13:55:07 +0200 Subject: Don't download the archive to a file --- src/http.zig | 23 ----------------------- src/install.zig | 32 +++++++++++++------------------- src/main.zig | 1 - 3 files changed, 13 insertions(+), 43 deletions(-) delete mode 100644 src/http.zig (limited to 'src') 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 @@ -const std = @import("std"); -const zelda = @import("zelda"); - -const Allocator = std.mem.Allocator; -const File = std.fs.File; - -// TODO: zelda should be able to do this internally so I wouldn't have to allocate memory for the -// file -pub fn downloadToFile(allocator: Allocator, file: *File, url: []const u8) !void { - var response = try zelda.get(allocator, url); - defer response.deinit(); - - if (response.status_code.group() != .success) { - std.log.err("HTTP Error: {}", .{response.status_code}); - return error.HttpError; - } - - if (response.body) |body| { - try file.writer().writeAll(body); - } else { - std.log.warn("Downloaded nothing from {s}...", .{url}); - } -} 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 defer allocator.free(installation_dir); std.log.info("Installing {s}, version {}", .{ name, installation.version }); - const filename = std.fs.path.basename(installation.url.?); - - // TODO: Platform-agnostic tempfile creation - var tmpname = try std.fmt.allocPrintZ(allocator, "/tmp/{s}", .{filename}); - defer allocator.free(tmpname); - - var tmpdir = try std.fs.openDirAbsolute(std.fs.path.dirname(tmpname).?, .{}); - defer tmpdir.close(); - - var tmpfile = try tmpdir.createFile(filename, .{}); - defer { - tmpfile.close(); - std.log.info("Deleting /tmp/{s}...", .{filename}); - tmpdir.deleteFile(filename) catch |err| { - std.log.warn("Failed to delete /tmp/{s}: {}", .{ filename, err }); - }; + + std.log.info("Downloading from {s}...", .{installation.url.?}); + var response = try zelda.get(allocator, installation.url.?); + defer response.deinit(); + + if (response.status_code.group() != .success) { + std.log.err("HTTP Error: {}", .{response.status_code}); + return error.HttpError; } - std.log.info("Downloading to /tmp/{s}...", .{filename}); - try zup.http.downloadToFile(allocator, &tmpfile, installation.url.?); + if (response.body == null) { + std.log.err("HTTP Error, no body received", .{}); + return error.HttpError; + } std.log.info("Extracting...", .{}); var archive = try ArchiveRead.init(); @@ -78,7 +72,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I try archive.supportFilter(.all); try archive.supportFormat(.all); - try archive.openFilename(tmpname, 10240); + try archive.openMemory(response.body.?); while (try archive.nextHeader()) |entry_c| { 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"); pub const Installation = @import("Installation.zig"); pub const Installations = Installation.Installations; pub const SubCommand = @import("subcommand.zig").SubCommand; -pub const http = @import("http.zig"); pub const help = SubCommand(Help); pub const install = SubCommand(@import("install.zig")); -- cgit v1.2.3