diff options
author | Justin Bedo <cu@cua0.org> | 2024-10-31 13:58:33 +1100 |
---|---|---|
committer | Justin Bedo <cu@cua0.org> | 2024-10-31 13:58:33 +1100 |
commit | 00d758934e7d5827e01585887a36913a326535ea (patch) | |
tree | 27fd0aea46ae380b36e43114e8b2114781aefd4f | |
parent | 824deda0e1a7e46d4e17ff3e472dcddec220b308 (diff) |
migrate zig code and target baseline cpu
-rw-r--r-- | strip-store-paths/default.nix | 5 | ||||
-rw-r--r-- | strip-store-paths/strip-store-paths.zig | 33 |
2 files changed, 20 insertions, 18 deletions
diff --git a/strip-store-paths/default.nix b/strip-store-paths/default.nix index 40fb546..473df87 100644 --- a/strip-store-paths/default.nix +++ b/strip-store-paths/default.nix @@ -4,14 +4,15 @@ stdenv.mkDerivation { name = "strip-store-paths"; nativeBuildInputs = [ zig ]; src = ./strip-store-paths.zig; + + XDG_CACHE_HOME = "Cache"; unpackPhase = '' cp $src strip-store-paths.zig ''; buildPhase = '' - export HOME=$TMPDIR - zig build-exe -OReleaseFast strip-store-paths.zig + zig build-exe -OReleaseFast -mcpu=baseline strip-store-paths.zig ''; installPhase = '' diff --git a/strip-store-paths/strip-store-paths.zig b/strip-store-paths/strip-store-paths.zig index e55aeaf..64c9036 100644 --- a/strip-store-paths/strip-store-paths.zig +++ b/strip-store-paths/strip-store-paths.zig @@ -5,17 +5,17 @@ pub const File = struct { len: u64, allocator: std.mem.Allocator, - pub fn init(fd: std.os.fd_t, allocator: std.mem.Allocator) !File { - const stats = try std.os.fstat(fd); + pub fn init(fd: std.posix.fd_t, allocator: std.mem.Allocator) !File { + const stats = try std.posix.fstat(fd); if (stats.size == 0) { return error.ZeroFile; } - const ptr = try std.os.mmap(null, @intCast(stats.size), std.os.PROT.READ | std.os.PROT.WRITE, std.os.MAP.SHARED, fd, 0); + const ptr = try std.posix.mmap(null, @intCast(stats.size), std.posix.PROT.READ | std.posix.PROT.WRITE, .{ .TYPE = .SHARED}, fd, 0); return File{ .ptr = ptr, .len = @intCast(stats.size), .allocator = allocator }; } pub fn deinit(self: *File) void { - std.os.munmap(self.ptr); + std.posix.munmap(self.ptr); self.ptr = undefined; self.len = 0; } @@ -27,30 +27,31 @@ pub fn main() !void { const allocator = arena.allocator(); const args = try std.process.argsAlloc(allocator); - if (args.len != 2) { + if (args.len == 1) { + std.process.exit(0); + } else if (args.len > 2) { std.debug.print("usage: {s} file\n", .{args[0]}); std.process.exit(1); } const path = args[1]; // mmap input - var file = try std.fs.cwd().openFile(path, .{ .mode = .read_write }); - defer file.close(); - const len = try file.getEndPos(); - if (len == 0) { - return; - } - const ptr = try std.posix.mmap(null, len, std.posix.PROT.READ | std.posix.PROT.WRITE, .{ .TYPE = .SHARED }, file.handle, 0); - defer std.posix.munmap(ptr); + const fd = try std.posix.open(path, .{.ACCMODE = .RDWR, .CREAT = false, .TRUNC = false}, 0); + var input = File.init(fd, allocator) catch |err| if (err == error.ZeroFile) { + return; + } else { + return err; + }; + defer input.deinit(); // search for /nix/store var i: usize = 0; const needle = "/nix/store/"; - while (std.mem.indexOfPos(u8, ptr, i, needle)) |idx| { + while (std.mem.indexOfPos(u8, input.ptr, i, needle)) |idx| { i = idx + needle.len; const j = i + 32; // pos of - in a true path - if (j < len and ptr[j] == '-') { - std.mem.copyForwards(u8, ptr[i..], "00000000000000000000000000000000"); + if (j < input.len and input.ptr[j] == '-') { + @memcpy(input.ptr[i..], "00000000000000000000000000000000"); } } } |