Skip to content

Commit

Permalink
zig fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Dec 15, 2024
1 parent d8a3741 commit a0b7649
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
18 changes: 9 additions & 9 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ pub fn main() !u8 {
const bin_dir = try std.fs.path.join(allocator, &.{ test_dir, "bin" });
try std.fs.cwd().makeDir(bin_dir);
const install_sub_path = if (builtin.os.tag == .windows) "bin\\zig" else "install";
const install_dir = try std.fs.path.join(allocator, &.{test_dir, install_sub_path });
const install_dir = try std.fs.path.join(allocator, &.{ test_dir, install_sub_path });
try std.fs.cwd().makeDir(install_dir);

const zigup = try std.fs.path.join(allocator, &.{
test_dir,
"bin",
"zigup" ++ comptime builtin.target.exeFileExt()
"zigup" ++ comptime builtin.target.exeFileExt(),
});
try std.fs.cwd().copyFile(
zigup_src_exe,
Expand All @@ -64,17 +64,17 @@ pub fn main() !u8 {
.{},
);
if (builtin.os.tag == .windows) {
const zigup_src_pdb = try std.mem.concat(
allocator, u8, &.{ zigup_src_exe[0 .. zigup_src_exe.len-4], ".pdb" }
);
const zigup_src_pdb = try std.mem.concat(allocator, u8, &.{
zigup_src_exe[0 .. zigup_src_exe.len - 4],
".pdb",
});
defer allocator.free(zigup_src_pdb);
const zigup_pdb = try std.fs.path.join(allocator, &.{ test_dir, "bin\\zigup.pdb" });
defer allocator.free(zigup_pdb);
try std.fs.cwd().copyFile(zigup_src_pdb, std.fs.cwd(), zigup_pdb, .{});
}

const install_args = if (builtin.os.tag == .windows) [_][]const u8{
} else [_][]const u8{
const install_args = if (builtin.os.tag == .windows) [_][]const u8{} else [_][]const u8{
"--install-dir", install_dir,
};
const zigup_args = &[_][]const u8{zigup} ++ install_args;
Expand Down Expand Up @@ -314,7 +314,7 @@ pub fn main() !u8 {
{
const fake_zig = try std.fs.path.join(allocator, &.{
bin2_dir,
"zig" ++ comptime builtin.target.exeFileExt()
"zig" ++ comptime builtin.target.exeFileExt(),
});
defer allocator.free(fake_zig);
var file = try std.fs.cwd().createFile(fake_zig, .{});
Expand Down Expand Up @@ -348,7 +348,7 @@ pub fn main() !u8 {
// NOTE: this test will eventually break when these builds are cleaned up,
// we should support downloading from bazel and use that instead since
// it should be more permanent
try runNoCapture(zigup_args ++ &[_][]const u8{ "0.14.0-dev.1550+4fba7336a" });
try runNoCapture(zigup_args ++ &[_][]const u8{"0.14.0-dev.1550+4fba7336a"});

std.log.info("Success", .{});
return 0;
Expand Down
12 changes: 7 additions & 5 deletions unzip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ fn usage() noreturn {
}

var windows_args_arena = if (builtin.os.tag == .windows)
std.heap.ArenaAllocator.init(std.heap.page_allocator) else struct{}{};
std.heap.ArenaAllocator.init(std.heap.page_allocator)
else
struct {}{};
pub fn cmdlineArgs() [][*:0]u8 {
if (builtin.os.tag == .windows) {
const slices = std.process.argsAlloc(windows_args_arena.allocator()) catch |err| switch (err) {
Expand All @@ -29,7 +31,7 @@ pub fn cmdlineArgs() [][*:0]u8 {
}
return args;
}
return std.os.argv.ptr[1 .. std.os.argv.len];
return std.os.argv.ptr[1..std.os.argv.len];
}

pub fn main() !void {
Expand All @@ -55,7 +57,7 @@ pub fn main() !void {
fatal("unknown cmdline option '{s}'", .{arg});
}
}
break :blk cmd_args[0 .. non_option_len];
break :blk cmd_args[0..non_option_len];
};

if (cmd_args.len != 1) usage();
Expand All @@ -68,15 +70,15 @@ pub fn main() !void {
try std.fs.cwd().makePath(dir);
break :blk try std.fs.cwd().openDir(dir, .{});
},
else => fatal("failed to open output directory '{s}' with {s}", .{dir, @errorName(err)}),
else => fatal("failed to open output directory '{s}' with {s}", .{ dir, @errorName(err) }),
};
}
break :blk std.fs.cwd();
};
defer if (cmdline_opt.dir_arg) |_| out_dir.close();

const zip_file = std.fs.cwd().openFile(zip_file_arg, .{}) catch |err|
fatal("open '{s}' failed: {s}", .{zip_file_arg, @errorName(err)});
fatal("open '{s}' failed: {s}", .{ zip_file_arg, @errorName(err) });
defer zip_file.close();
try std.zip.extract(out_dir, zip_file.seekableStream(), .{
.allow_backslashes = true,
Expand Down
36 changes: 24 additions & 12 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,40 @@ const DownloadResult = union(enum) {
};
fn download(allocator: Allocator, url: []const u8, writer: anytype) DownloadResult {
const uri = std.Uri.parse(url) catch |err| std.debug.panic(
"failed to parse url '{s}' with {s}", .{url, @errorName(err)}
"failed to parse url '{s}' with {s}",
.{ url, @errorName(err) },
);

var client = std.http.Client{ .allocator = allocator };
defer client.deinit();

client.initDefaultProxies(allocator) catch |err| return .{ .err = std.fmt.allocPrint(
allocator, "failed to query the HTTP proxy settings with {s}", .{ @errorName(err) }
allocator,
"failed to query the HTTP proxy settings with {s}",
.{@errorName(err)},
) catch |e| oom(e) };

var header_buffer: [4096]u8 = undefined;
var request = client.open(.GET, uri, .{
.server_header_buffer = &header_buffer,
.keep_alive = false,
}) catch |err| return .{ .err = std.fmt.allocPrint(
allocator, "failed to connect to the HTTP server with {s}", .{ @errorName(err) }
allocator,
"failed to connect to the HTTP server with {s}",
.{@errorName(err)},
) catch |e| oom(e) };

defer request.deinit();

request.send() catch |err| return .{ .err = std.fmt.allocPrint(
allocator, "failed to send the HTTP request with {s}", .{ @errorName(err) }
allocator,
"failed to send the HTTP request with {s}",
.{@errorName(err)},
) catch |e| oom(e) };
request.wait() catch |err| return .{ .err = std.fmt.allocPrint(
allocator, "failed to read the HTTP response headers with {s}", .{ @errorName(err) }
allocator,
"failed to read the HTTP response headers with {s}",
.{@errorName(err)},
) catch |e| oom(e) };

if (request.response.status != .ok) return .{ .err = std.fmt.allocPrint(
Expand All @@ -90,20 +99,23 @@ fn download(allocator: Allocator, url: []const u8, writer: anytype) DownloadResu
var buf: [std.mem.page_size]u8 = undefined;
while (true) {
const len = request.reader().read(&buf) catch |err| return .{ .err = std.fmt.allocPrint(
allocator, "failed to read the HTTP response body with {s}'", .{ @errorName(err) }
allocator,
"failed to read the HTTP response body with {s}'",
.{@errorName(err)},
) catch |e| oom(e) };
if (len == 0)
return .ok;
writer.writeAll(buf[0..len]) catch |err| return .{ .err = std.fmt.allocPrint(
allocator, "failed to write the HTTP response body with {s}'", .{ @errorName(err) }
allocator,
"failed to write the HTTP response body with {s}'",
.{@errorName(err)},
) catch |e| oom(e) };
}
}

const DownloadStringResult = union(enum) {
ok: []u8,
err: []u8,

};
fn downloadToString(allocator: Allocator, url: []const u8) DownloadStringResult {
var response_array_list = ArrayList(u8).initCapacity(allocator, 20 * 1024) catch |e| oom(e); // 20 KB (modify if response is expected to be bigger)
Expand Down Expand Up @@ -457,7 +469,7 @@ fn fetchDownloadIndex(allocator: Allocator) !DownloadIndex {
const text = switch (downloadToString(allocator, download_index_url)) {
.ok => |text| text,
.err => |err| {
std.log.err("download '{s}' failed: {s}", .{download_index_url, err});
std.log.err("download '{s}' failed: {s}", .{ download_index_url, err });
return error.AlreadyReported;
},
};
Expand Down Expand Up @@ -513,7 +525,7 @@ pub fn loggyUpdateSymlink(target_path: []const u8, sym_link_path: []const u8, fl
error.NotLink => {
std.debug.print(
"unable to update/overwrite the 'zig' PATH symlink, the file '{s}' already exists and is not a symlink\n",
.{ sym_link_path},
.{sym_link_path},
);
std.process.exit(1);
},
Expand Down Expand Up @@ -932,7 +944,7 @@ fn createExeLink(link_target: []const u8, path_link: []const u8) !void {
error.IsDir => {
std.debug.print(
"unable to create the exe link, the path '{s}' is a directory\n",
.{ path_link},
.{path_link},
);
std.process.exit(1);
},
Expand Down Expand Up @@ -985,7 +997,7 @@ fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const
}) {
.ok => {},
.err => |err| {
std.log.err("download '{s}' failed: {s}", .{url, err});
std.log.err("download '{s}' failed: {s}", .{ url, err });
// this removes the installing dir if the http request fails so we dont have random directories
try loggyDeleteTreeAbsolute(installing_dir);
return error.AlreadyReported;
Expand Down
18 changes: 10 additions & 8 deletions zip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ fn usage() noreturn {
}

var windows_args_arena = if (builtin.os.tag == .windows)
std.heap.ArenaAllocator.init(std.heap.page_allocator) else struct{}{};
std.heap.ArenaAllocator.init(std.heap.page_allocator)
else
struct {}{};
pub fn cmdlineArgs() [][*:0]u8 {
if (builtin.os.tag == .windows) {
const slices = std.process.argsAlloc(windows_args_arena.allocator()) catch |err| switch (err) {
Expand All @@ -31,7 +33,7 @@ pub fn cmdlineArgs() [][*:0]u8 {
}
return args;
}
return std.os.argv.ptr[1 .. std.os.argv.len];
return std.os.argv.ptr[1..std.os.argv.len];
}

pub fn main() !void {
Expand All @@ -52,7 +54,7 @@ pub fn main() !void {
fatal("unknown cmdline option '{s}'", .{arg});
}
}
break :blk cmd_args[0 .. non_option_len];
break :blk cmd_args[0..non_option_len];
};

if (cmd_args.len < 2) usage();
Expand Down Expand Up @@ -87,7 +89,8 @@ pub fn main() !void {
.whiteout,
.door,
.event_port,
.unknown => fatal("file '{s}' is an unsupported type {s}", .{path, @tagName(stat.kind)}),
.unknown,
=> fatal("file '{s}' is an unsupported type {s}", .{ path, @tagName(stat.kind) }),
}
}

Expand All @@ -96,15 +99,15 @@ pub fn main() !void {

{
const zip_file = std.fs.cwd().createFile(zip_file_arg, .{}) catch |err|
fatal("create file '{s}' failed: {s}", .{zip_file_arg, @errorName(err)});
fatal("create file '{s}' failed: {s}", .{ zip_file_arg, @errorName(err) });
defer zip_file.close();
try writeZip(zip_file, file_entries.items, store);
}

// go fix up the local file headers
{
const zip_file = std.fs.cwd().openFile(zip_file_arg, .{ .mode = .read_write }) catch |err|
fatal("open file '{s}' failed: {s}", .{zip_file_arg, @errorName(err)});
fatal("open file '{s}' failed: {s}", .{ zip_file_arg, @errorName(err) });
defer zip_file.close();
for (file_entries.items, 0..) |file, i| {
try zip_file.seekTo(store[i].file_offset);
Expand Down Expand Up @@ -156,7 +159,7 @@ fn writeZip(
var full_rw_buf: [std.mem.page_size]u8 = undefined;
var remaining = file_entry.size;
while (remaining > 0) {
const buf = full_rw_buf[0 .. @min(remaining, full_rw_buf.len)];
const buf = full_rw_buf[0..@min(remaining, full_rw_buf.len)];
const read_len = try file.reader().read(buf);
std.debug.assert(read_len == buf.len);
hash.update(buf);
Expand Down Expand Up @@ -220,7 +223,6 @@ pub fn Crc32Reader(comptime ReaderType: type) type {
};
}


fn isBadFilename(filename: []const u8) bool {
if (std.mem.indexOfScalar(u8, filename, '\\')) |_|
return true;
Expand Down

0 comments on commit a0b7649

Please sign in to comment.