authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-04 10:34:58 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-04 10:34:58 -07:00
log151e112a6a94b8bb0a8b3c3457d179d2c66ad478
tree0e398e804d3d89797f585d08568d26ecb68f498e
parent3caa1188c9e0d926b51908e42905162b4551668a

use an array for 'id' so that we're not returning pointer to a local


7 files changed, 35 insertions(+), 25 deletions(-)

src/cmd/fetch.zig+2-2
......@@ -285,7 +285,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:
285285fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module, alloc: std.mem.Allocator) !void {
286286 for (list) |mod| {
287287 if (mod.type == .system_lib or mod.type == .framework) continue;
288 if (std.mem.eql(u8, mod.id, "root")) {
288 if (std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
289289 try w.writeAll(" pub const _root = \"\";\n");
290290 continue;
291291 }
......@@ -325,7 +325,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul
325325 mod.short_id(),
326326 mod.short_id(),
327327 });
328 if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {
328 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
329329 try w.print(
330330 \\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }},
331331 \\
src/cmd/generate.zig+1-1
......@@ -284,7 +284,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath:
284284 .hg => @panic("TODO"),
285285 .http => @panic("TODO"),
286286 }
287 if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {
287 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
288288 try w.print(" .name = \"{s}\",\n", .{mod.name});
289289 try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath), mod.main });
290290
src/common.zig+4-5
......@@ -46,7 +46,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
4646 }
4747 return zigmod.Module{
4848 .type = .local,
49 .id = "root",
49 .id = zigmod.Module.ROOT,
5050 .name = "root",
5151 .main = m.main,
5252 .deps = try moduledeps.toOwnedSlice(),
......@@ -230,7 +230,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
230230 .system_lib, .framework => {
231231 return zigmod.Module{
232232 .type = d.type,
233 .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path),
233 .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Shake(96), d.path),
234234 .name = d.path,
235235 .only_os = d.only_os,
236236 .except_os = d.except_os,
......@@ -274,7 +274,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
274274 dd.for_build = d.for_build;
275275 const save = dd;
276276 if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..];
277 if (dd.id.len == 0) dd.id = &u.random_string(48);
277 if (std.mem.eql(u8, &dd.id, &zigmod.Dep.EMPTY)) dd.id = u.random_string(48);
278278 if (d.name.len > 0) dd.name = d.name;
279279 if (d.main.len > 0) dd.main = d.main;
280280 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;
......@@ -352,12 +352,11 @@ pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]str
352352 .type = std.meta.stringToEnum(zigmod.Dep.Type, iter.next().?).?,
353353 .path = iter.next().?,
354354 .version = iter.next().?,
355 .id = "",
355 .id = zigmod.Dep.EMPTY,
356356 .name = "",
357357 .main = "",
358358 .yaml = null,
359359 .deps = &.{},
360 .parent_id = "",
361360 };
362361 try list.append([4]string{
363362 try asdep.clean_path(alloc),
src/util/dep.zig+3-2
......@@ -15,7 +15,7 @@ pub const Dep = struct {
1515
1616 type: Type,
1717 path: string,
18 id: string,
18 id: [48]u8,
1919 name: string,
2020 main: string,
2121 version: string,
......@@ -28,7 +28,8 @@ pub const Dep = struct {
2828 deps: []zigmod.Dep,
2929 keep: bool = false,
3030 for_build: bool = false,
31 parent_id: string,
31
32 pub const EMPTY = (" " ** 48).*;
3233
3334 pub const Type = @import("./dep_type.zig").DepType;
3435
src/util/funcs.zig+5-5
......@@ -132,9 +132,9 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)
132132 const data = try file.reader().readAllAlloc(alloc, gb);
133133 const expected = hash.string;
134134 const actual = switch (hash.id) {
135 .blake3 => try do_hash(alloc, std.crypto.hash.Blake3, data),
136 .sha256 => try do_hash(alloc, std.crypto.hash.sha2.Sha256, data),
137 .sha512 => try do_hash(alloc, std.crypto.hash.sha2.Sha512, data),
135 .blake3 => &try do_hash(alloc, std.crypto.hash.Blake3, data),
136 .sha256 => &try do_hash(alloc, std.crypto.hash.sha2.Sha256, data),
137 .sha512 => &try do_hash(alloc, std.crypto.hash.sha2.Sha512, data),
138138 };
139139 const result = std.mem.startsWith(u8, actual, expected);
140140 if (!result) {
......@@ -143,13 +143,13 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)
143143 return result;
144144}
145145
146pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) !string {
146pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) ![algo.digest_length * 2]u8 {
147147 var h = algo.init(.{});
148148 var out: [algo.digest_length]u8 = undefined;
149149 h.update(data);
150150 h.final(&out);
151151 const hex = try std.fmt.allocPrint(alloc, "{x}", .{std.fmt.fmtSliceHexLower(out[0..])});
152 return hex;
152 return hex[0 .. algo.digest_length * 2].*;
153153}
154154
155155/// Returns the result of running `git rev-parse HEAD`
src/util/modfile.zig+9-5
......@@ -15,7 +15,7 @@ const mb = kb * 1024;
1515pub const ModFile = struct {
1616 const Self = @This();
1717
18 id: string,
18 id: [48]u8,
1919 name: string,
2020 main: string,
2121 c_include_dirs: []const string,
......@@ -52,7 +52,9 @@ pub const ModFile = struct {
5252 }
5353
5454 pub fn from_mapping(alloc: std.mem.Allocator, mapping: yaml.Mapping) !Self {
55 const id = mapping.get_string("id") orelse "";
55 var id = zigmod.Dep.EMPTY;
56 std.mem.copyForwards(u8, &id, mapping.get_string("id") orelse &u.random_string(48));
57
5658 const name = mapping.get_string("name") orelse "";
5759 const main = mapping.get_string("main") orelse "";
5860
......@@ -61,7 +63,7 @@ pub const ModFile = struct {
6163 }
6264
6365 return Self{
64 .id = if (id.len == 0) &u.random_string(48) else id,
66 .id = id,
6567 .name = name,
6668 .main = main,
6769 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),
......@@ -120,10 +122,13 @@ pub const ModFile = struct {
120122 }
121123 }
122124
125 var id = zigmod.Dep.EMPTY;
126 std.mem.copyForwards(u8, &id, item.mapping.get_string("id") orelse "");
127
123128 try dep_list.append(zigmod.Dep{
124129 .type = dep_type,
125130 .path = path,
126 .id = item.mapping.get_string("id") orelse "",
131 .id = id,
127132 .name = name,
128133 .main = main,
129134 .version = version.?,
......@@ -136,7 +141,6 @@ pub const ModFile = struct {
136141 .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build),
137142 .keep = std.mem.eql(u8, "true", item.mapping.get_string("keep") orelse ""),
138143 .for_build = for_build,
139 .parent_id = mapping.get_string("id") orelse "",
140144 });
141145 }
142146 }
src/util/module.zig+11-5
......@@ -13,7 +13,7 @@ const common = @import("./../common.zig");
1313
1414pub const Module = struct {
1515 type: zigmod.Dep.Type,
16 id: string,
16 id: [48]u8,
1717 name: string,
1818 main: string,
1919 c_include_dirs: []const string = &.{},
......@@ -28,6 +28,8 @@ pub const Module = struct {
2828 for_build: bool = false,
2929 min_zig_version: ?std.SemanticVersion,
3030
31 pub const ROOT: [48]u8 = ("root" ++ (" " ** 44)).*;
32
3133 pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, cachepath: string, options: *common.CollectOptions) !Module {
3234 var moddeps = std.ArrayList(Module).init(alloc);
3335 errdefer moddeps.deinit();
......@@ -37,9 +39,13 @@ pub const Module = struct {
3739 try moddeps.append(founddep);
3840 }
3941 }
42
43 var id = dep.id;
44 if (std.mem.eql(u8, &id, &zigmod.Dep.EMPTY)) id = u.random_string(48);
45
4046 return Module{
4147 .type = dep.type,
42 .id = if (dep.id.len > 0) dep.id else &u.random_string(48),
48 .id = id,
4349 .name = dep.name,
4450 .main = dep.main,
4551 .c_include_dirs = dep.c_include_dirs,
......@@ -57,7 +63,7 @@ pub const Module = struct {
5763 }
5864
5965 pub fn eql(self: Module, another: Module) bool {
60 return std.mem.eql(u8, self.id, another.id);
66 return std.mem.eql(u8, &self.id, &another.id);
6167 }
6268
6369 pub fn get_hash(self: Module, alloc: std.mem.Allocator, cdpath: string) !string {
......@@ -131,8 +137,8 @@ pub const Module = struct {
131137 return false;
132138 }
133139
134 pub fn short_id(self: Module) string {
135 return u.slice(u8, self.id, 0, 12);
140 pub fn short_id(self: *const Module) string {
141 return u.slice(u8, &self.id, 0, @min(12, std.mem.indexOfScalar(u8, &self.id, ' ') orelse self.id.len));
136142 }
137143
138144 pub fn minZigVersion(self: Module) ?std.SemanticVersion {