diff options
Diffstat (limited to 'strip-store-paths/strip-store-paths.zig')
-rw-r--r-- | strip-store-paths/strip-store-paths.zig | 33 |
1 files changed, 17 insertions, 16 deletions
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"); } } } |