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 {...@@ -30,7 +30,7 @@ pub const Diagnostics = struct {
30 errors: std.ArrayListUnmanaged(Error) = .{},30 errors: std.ArrayListUnmanaged(Error) = .{},
3131
32 root_entries: usize = 0,32 root_entries: usize = 0,
33 root_dir: ?[]const u8 = null,33 root_dir: []const u8 = "",
3434
35 pub const Error = union(enum) {35 pub const Error = union(enum) {
36 unable_to_create_sym_link: struct {36 unable_to_create_sym_link: struct {
...@@ -55,10 +55,8 @@ pub const Diagnostics = struct {...@@ -55,10 +55,8 @@ pub const Diagnostics = struct {
55 d.root_dir = try d.allocator.dupe(u8, root_dir);55 d.root_dir = try d.allocator.dupe(u8, root_dir);
56 return;56 return;
57 }57 }
58 if (d.root_dir) |r| {58 d.allocator.free(d.root_dir);
59 d.allocator.free(r);59 d.root_dir = "";
60 d.root_dir = null;
61 }
62 }60 }
63 }61 }
6462
...@@ -103,10 +101,7 @@ pub const Diagnostics = struct {...@@ -103,10 +101,7 @@ pub const Diagnostics = struct {
103 }101 }
104 }102 }
105 d.errors.deinit(d.allocator);103 d.errors.deinit(d.allocator);
106 if (d.root_dir) |r| {104 d.allocator.free(d.root_dir);
107 d.allocator.free(r);
108 d.root_dir = null;
109 }
110 d.* = undefined;105 d.* = undefined;
111 }106 }
112};107};
...@@ -1060,7 +1055,7 @@ test "pipeToFileSystem root_dir" {...@@ -1060,7 +1055,7 @@ test "pipeToFileSystem root_dir" {
1060 };1055 };
10611056
1062 // there is no root_dir1057 // there is no root_dir
1063 try testing.expect(diagnostics.root_dir == null);1058 try testing.expectEqual(0, diagnostics.root_dir.len);
1064 try testing.expectEqual(3, diagnostics.root_entries);1059 try testing.expectEqual(3, diagnostics.root_entries);
1065 }1060 }
10661061
...@@ -1082,7 +1077,7 @@ test "pipeToFileSystem root_dir" {...@@ -1082,7 +1077,7 @@ test "pipeToFileSystem root_dir" {
1082 };1077 };
10831078
1084 // root_dir found1079 // root_dir found
1085 try testing.expectEqualStrings("example", diagnostics.root_dir.?);1080 try testing.expectEqualStrings("example", diagnostics.root_dir);
1086 try testing.expectEqual(1, diagnostics.root_entries);1081 try testing.expectEqual(1, diagnostics.root_entries);
1087 }1082 }
1088}1083}
src/Package/Fetch.zig+13-21
...@@ -465,10 +465,7 @@ fn runResource(...@@ -465,10 +465,7 @@ fn runResource(
465 var unpack_result = try unpackResource(f, resource, uri_path, tmp_directory);465 var unpack_result = try unpackResource(f, resource, uri_path, tmp_directory);
466 defer unpack_result.deinit();466 defer unpack_result.deinit();
467467
468 var pkg_path: Cache.Path = .{468 var pkg_path: Cache.Path = .{ .root_dir = tmp_directory, .sub_path = unpack_result.root_dir };
469 .root_dir = tmp_directory,
470 .sub_path = if (unpack_result.root_dir) |root_dir| root_dir else "",
471 };
472469
473 // Apply btrfs workaround if needed. Reopen tmp_directory.470 // Apply btrfs workaround if needed. Reopen tmp_directory.
474 if (native_os == .linux and f.job_queue.work_around_btrfs_bug) {471 if (native_os == .linux and f.job_queue.work_around_btrfs_bug) {
...@@ -503,8 +500,8 @@ fn runResource(...@@ -503,8 +500,8 @@ fn runResource(
503 // directory.500 // directory.
504 f.actual_hash = try computeHash(f, pkg_path, filter);501 f.actual_hash = try computeHash(f, pkg_path, filter);
505502
506 break :blk if (unpack_result.root_dir) |root_dir|503 break :blk if (unpack_result.root_dir.len > 0)
507 try fs.path.join(arena, &.{ tmp_dir_sub_path, root_dir })504 try fs.path.join(arena, &.{ tmp_dir_sub_path, unpack_result.root_dir })
508 else505 else
509 tmp_dir_sub_path;506 tmp_dir_sub_path;
510 };507 };
...@@ -1185,8 +1182,8 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: anytype) RunError!UnpackRes...@@ -1185,8 +1182,8 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: anytype) RunError!UnpackRes
1185 ));1182 ));
11861183
1187 var res = UnpackResult.init(gpa);1184 var res = UnpackResult.init(gpa);
1188 if (diagnostics.root_dir) |root_dir| {1185 if (diagnostics.root_dir.len > 0) {
1189 res.root_dir = try gpa.dupe(u8, root_dir);1186 res.root_dir = try gpa.dupe(u8, diagnostics.root_dir);
1190 }1187 }
1191 if (diagnostics.errors.items.len > 0) {1188 if (diagnostics.errors.items.len > 0) {
1192 try res.rootErrorMessage("unable to unpack tarball");1189 try res.rootErrorMessage("unable to unpack tarball");
...@@ -1748,9 +1745,9 @@ const UnpackResult = struct {...@@ -1748,9 +1745,9 @@ const UnpackResult = struct {
1748 errors: std.ArrayListUnmanaged(Error) = .{},1745 errors: std.ArrayListUnmanaged(Error) = .{},
1749 root_error_message: []const u8 = "",1746 root_error_message: []const u8 = "",
17501747
1751 // A `null` value indicates the `tmp_directory` is populated directly with the package contents.1748 // A non empty value means that the package contents are inside a
1752 // A non-null value means that the package contents are inside a sub-directory indicated by the named path.1749 // sub-directory indicated by the named path.
1753 root_dir: ?[]const u8 = null,1750 root_dir: []const u8 = "",
17541751
1755 const Error = union(enum) {1752 const Error = union(enum) {
1756 unable_to_create_sym_link: struct {1753 unable_to_create_sym_link: struct {
...@@ -1802,9 +1799,7 @@ const UnpackResult = struct {...@@ -1802,9 +1799,7 @@ const UnpackResult = struct {
1802 }1799 }
1803 self.errors.deinit(self.allocator);1800 self.errors.deinit(self.allocator);
1804 self.allocator.free(self.root_error_message);1801 self.allocator.free(self.root_error_message);
1805 if (self.root_dir) |root_dir| {1802 self.allocator.free(self.root_dir);
1806 self.allocator.free(root_dir);
1807 }
1808 self.* = undefined;1803 self.* = undefined;
1809 }1804 }
18101805
...@@ -1837,11 +1832,10 @@ const UnpackResult = struct {...@@ -1837,11 +1832,10 @@ const UnpackResult = struct {
1837 // Filter errors by manifest inclusion rules.1832 // Filter errors by manifest inclusion rules.
1838 fn filterErrors(self: *UnpackResult, filter: Filter) !void {1833 fn filterErrors(self: *UnpackResult, filter: Filter) !void {
1839 var i = self.errors.items.len;1834 var i = self.errors.items.len;
1840 const root_dir: []const u8 = if (self.root_dir) |root_dir| root_dir else "";
1841 while (i > 0) {1835 while (i > 0) {
1842 i -= 1;1836 i -= 1;
1843 const item = self.errors.items[i];1837 const item = self.errors.items[i];
1844 if (item.excluded(filter, root_dir)) {1838 if (item.excluded(filter, self.root_dir)) {
1845 _ = self.errors.swapRemove(i);1839 _ = self.errors.swapRemove(i);
1846 item.free(self.allocator);1840 item.free(self.allocator);
1847 }1841 }
...@@ -1861,8 +1855,6 @@ const UnpackResult = struct {...@@ -1861,8 +1855,6 @@ const UnpackResult = struct {
1861 if (self.errors.items.len == 0 and self.root_error_message.len == 0)1855 if (self.errors.items.len == 0 and self.root_error_message.len == 0)
1862 return;1856 return;
18631857
1864 const root_dir = if (self.root_dir) |root_dir| root_dir else "";
1865
1866 const notes_len: u32 = @intCast(self.errors.items.len);1858 const notes_len: u32 = @intCast(self.errors.items.len);
1867 try eb.addRootErrorMessage(.{1859 try eb.addRootErrorMessage(.{
1868 .msg = try eb.addString(self.root_error_message),1860 .msg = try eb.addString(self.root_error_message),
...@@ -1875,21 +1867,21 @@ const UnpackResult = struct {...@@ -1875,21 +1867,21 @@ const UnpackResult = struct {
1875 .unable_to_create_sym_link => |info| {1867 .unable_to_create_sym_link => |info| {
1876 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{1868 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
1877 .msg = try eb.printString("unable to create symlink from '{s}' to '{s}': {s}", .{1869 .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),
1879 }),1871 }),
1880 }));1872 }));
1881 },1873 },
1882 .unable_to_create_file => |info| {1874 .unable_to_create_file => |info| {
1883 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{1875 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
1884 .msg = try eb.printString("unable to create file '{s}': {s}", .{1876 .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),
1886 }),1878 }),
1887 }));1879 }));
1888 },1880 },
1889 .unsupported_file_type => |info| {1881 .unsupported_file_type => |info| {
1890 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{1882 eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
1891 .msg = try eb.printString("file '{s}' has unsupported type '{c}'", .{1883 .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,
1893 }),1885 }),
1894 }));1886 }));
1895 },1887 },