authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-04 19:45:35+02:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-09 15:00:22+02:00
log3d5a9237f7984a37f6aa086bd2b0783305f644e5
tree88b986ada709557757583374c563ce48ebe0fcce
parent5f0b434f9031179609c97676bb43425f8710ac95

fetch: use empty string instead of null for root_dir

Make it consistent with Cache.Path sub_path. Remove null check in many locations.

2 files changed, 19 insertions(+), 32 deletions(-)

lib/std/tar.zig+6-11
......@@ -30,7 +30,7 @@ pub const Diagnostics = struct {
3030 errors: std.ArrayListUnmanaged(Error) = .{},
3131
3232 root_entries: usize = 0,
33 root_dir: ?[]const u8 = null,
33 root_dir: []const u8 = "",
3434
3535 pub const Error = union(enum) {
3636 unable_to_create_sym_link: struct {
......@@ -55,10 +55,8 @@ pub const Diagnostics = struct {
5555 d.root_dir = try d.allocator.dupe(u8, root_dir);
5656 return;
5757 }
58 if (d.root_dir) |r| {
59 d.allocator.free(r);
60 d.root_dir = null;
61 }
58 d.allocator.free(d.root_dir);
59 d.root_dir = "";
6260 }
6361 }
6462
......@@ -103,10 +101,7 @@ pub const Diagnostics = struct {
103101 }
104102 }
105103 d.errors.deinit(d.allocator);
106 if (d.root_dir) |r| {
107 d.allocator.free(r);
108 d.root_dir = null;
109 }
104 d.allocator.free(d.root_dir);
110105 d.* = undefined;
111106 }
112107};
......@@ -1060,7 +1055,7 @@ test "pipeToFileSystem root_dir" {
10601055 };
10611056
10621057 // there is no root_dir
1063 try testing.expect(diagnostics.root_dir == null);
1058 try testing.expectEqual(0, diagnostics.root_dir.len);
10641059 try testing.expectEqual(3, diagnostics.root_entries);
10651060 }
10661061
......@@ -1082,7 +1077,7 @@ test "pipeToFileSystem root_dir" {
10821077 };
10831078
10841079 // root_dir found
1085 try testing.expectEqualStrings("example", diagnostics.root_dir.?);
1080 try testing.expectEqualStrings("example", diagnostics.root_dir);
10861081 try testing.expectEqual(1, diagnostics.root_entries);
10871082 }
10881083}
src/Package/Fetch.zig+13-21
......@@ -465,10 +465,7 @@ fn runResource(
465465 var unpack_result = try unpackResource(f, resource, uri_path, tmp_directory);
466466 defer unpack_result.deinit();
467467
468 var pkg_path: Cache.Path = .{
469 .root_dir = tmp_directory,
470 .sub_path = if (unpack_result.root_dir) |root_dir| root_dir else "",
471 };
468 var pkg_path: Cache.Path = .{ .root_dir = tmp_directory, .sub_path = unpack_result.root_dir };
472469
473470 // Apply btrfs workaround if needed. Reopen tmp_directory.
474471 if (native_os == .linux and f.job_queue.work_around_btrfs_bug) {
......@@ -503,8 +500,8 @@ fn runResource(
503500 // directory.
504501 f.actual_hash = try computeHash(f, pkg_path, filter);
505502
506 break :blk if (unpack_result.root_dir) |root_dir|
507 try fs.path.join(arena, &.{ tmp_dir_sub_path, root_dir })
503 break :blk if (unpack_result.root_dir.len > 0)
504 try fs.path.join(arena, &.{ tmp_dir_sub_path, unpack_result.root_dir })
508505 else
509506 tmp_dir_sub_path;
510507 };
......@@ -1185,8 +1182,8 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: anytype) RunError!UnpackRes
11851182 ));
11861183
11871184 var res = UnpackResult.init(gpa);
1188 if (diagnostics.root_dir) |root_dir| {
1189 res.root_dir = try gpa.dupe(u8, root_dir);
1185 if (diagnostics.root_dir.len > 0) {
1186 res.root_dir = try gpa.dupe(u8, diagnostics.root_dir);
11901187 }
11911188 if (diagnostics.errors.items.len > 0) {
11921189 try res.rootErrorMessage("unable to unpack tarball");
......@@ -1748,9 +1745,9 @@ const UnpackResult = struct {
17481745 errors: std.ArrayListUnmanaged(Error) = .{},
17491746 root_error_message: []const u8 = "",
17501747
1751 // A `null` value indicates the `tmp_directory` is populated directly with the package contents.
1752 // A non-null value means that the package contents are inside a sub-directory indicated by the named path.
1753 root_dir: ?[]const u8 = null,
1748 // A non empty value means that the package contents are inside a
1749 // sub-directory indicated by the named path.
1750 root_dir: []const u8 = "",
17541751
17551752 const Error = union(enum) {
17561753 unable_to_create_sym_link: struct {
......@@ -1802,9 +1799,7 @@ const UnpackResult = struct {
18021799 }
18031800 self.errors.deinit(self.allocator);
18041801 self.allocator.free(self.root_error_message);
1805 if (self.root_dir) |root_dir| {
1806 self.allocator.free(root_dir);
1807 }
1802 self.allocator.free(self.root_dir);
18081803 self.* = undefined;
18091804 }
18101805
......@@ -1837,11 +1832,10 @@ const UnpackResult = struct {
18371832 // Filter errors by manifest inclusion rules.
18381833 fn filterErrors(self: *UnpackResult, filter: Filter) !void {
18391834 var i = self.errors.items.len;
1840 const root_dir: []const u8 = if (self.root_dir) |root_dir| root_dir else "";
18411835 while (i > 0) {
18421836 i -= 1;
18431837 const item = self.errors.items[i];
1844 if (item.excluded(filter, root_dir)) {
1838 if (item.excluded(filter, self.root_dir)) {
18451839 _ = self.errors.swapRemove(i);
18461840 item.free(self.allocator);
18471841 }
......@@ -1861,8 +1855,6 @@ const UnpackResult = struct {
18611855 if (self.errors.items.len == 0 and self.root_error_message.len == 0)
18621856 return;
18631857
1864 const root_dir = if (self.root_dir) |root_dir| root_dir else "";
1865
18661858 const notes_len: u32 = @intCast(self.errors.items.len);
18671859 try eb.addRootErrorMessage(.{
18681860 .msg = try eb.addString(self.root_error_message),
......@@ -1875,21 +1867,21 @@ const UnpackResult = struct {
18751867 .unable_to_create_sym_link => |info| {
18761868 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
18771869 .msg = try eb.printString("unable to create symlink from '{s}' to '{s}': {s}", .{
1878 stripRoot(info.file_name, root_dir), info.link_name, @errorName(info.code),
1870 stripRoot(info.file_name, self.root_dir), info.link_name, @errorName(info.code),
18791871 }),
18801872 }));
18811873 },
18821874 .unable_to_create_file => |info| {
18831875 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
18841876 .msg = try eb.printString("unable to create file '{s}': {s}", .{
1885 stripRoot(info.file_name, root_dir), @errorName(info.code),
1877 stripRoot(info.file_name, self.root_dir), @errorName(info.code),
18861878 }),
18871879 }));
18881880 },
18891881 .unsupported_file_type => |info| {
18901882 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
18911883 .msg = try eb.printString("file '{s}' has unsupported type '{c}'", .{
1892 stripRoot(info.file_name, root_dir), info.file_type,
1884 stripRoot(info.file_name, self.root_dir), info.file_type,
18931885 }),
18941886 }));
18951887 },