|
| 1 | +//! A simple script to apply a patch to a file |
| 2 | +//! Does minimal validation and is just enough for patching Lua 5.1 |
| 3 | + |
| 4 | +pub fn main() !void { |
| 5 | + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 6 | + defer arena.deinit(); |
| 7 | + |
| 8 | + const allocator = arena.allocator(); |
| 9 | + |
| 10 | + const args = try std.process.argsAlloc(allocator); |
| 11 | + if (args.len != 4) @panic("Wrong number of arguments"); |
| 12 | + |
| 13 | + const file_path = args[1]; |
| 14 | + const patch_file_path = args[2]; |
| 15 | + const output_path = args[3]; |
| 16 | + |
| 17 | + const patch_file = try std.fs.openFileAbsolute(patch_file_path, .{ .mode = .read_only }); |
| 18 | + defer patch_file.close(); |
| 19 | + const chunk_details = Chunk.next(allocator, patch_file) orelse @panic("No chunk data found"); |
| 20 | + |
| 21 | + const file = try std.fs.openFileAbsolute(file_path, .{ .mode = .read_only }); |
| 22 | + defer file.close(); |
| 23 | + |
| 24 | + const output = try std.fs.createFileAbsolute(output_path, .{}); |
| 25 | + defer output.close(); |
| 26 | + |
| 27 | + var state: State = .copy; |
| 28 | + |
| 29 | + var line_number: usize = 1; |
| 30 | + while (true) : (line_number += 1) { |
| 31 | + if (line_number == chunk_details.src) state = .chunk; |
| 32 | + |
| 33 | + switch (state) { |
| 34 | + .copy => { |
| 35 | + const line = getLine(allocator, file) orelse return; |
| 36 | + _ = try output.write(line); |
| 37 | + _ = try output.write("\n"); |
| 38 | + }, |
| 39 | + .chunk => { |
| 40 | + const chunk = chunk_details.lines[line_number - chunk_details.src]; |
| 41 | + switch (chunk.action) { |
| 42 | + .remove => { |
| 43 | + const line = getLine(allocator, file) orelse return; |
| 44 | + if (!std.mem.eql(u8, chunk.buf, line)) @panic("Failed to apply patch"); |
| 45 | + }, |
| 46 | + .keep => { |
| 47 | + const line = getLine(allocator, file) orelse return; |
| 48 | + if (!std.mem.eql(u8, chunk.buf, line)) @panic("Failed to apply patch"); |
| 49 | + _ = try output.write(line); |
| 50 | + _ = try output.write("\n"); |
| 51 | + }, |
| 52 | + .add => { |
| 53 | + _ = try output.write(chunk.buf); |
| 54 | + _ = try output.write("\n"); |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + if (line_number - chunk_details.src == chunk_details.lines.len - 1) state = .copy; |
| 59 | + }, |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn getLine(allocator: Allocator, file: File) ?[]u8 { |
| 65 | + return file.reader().readUntilDelimiterAlloc(allocator, '\n', std.math.maxInt(usize)) catch |err| switch (err) { |
| 66 | + error.EndOfStream => return null, |
| 67 | + else => @panic("Error"), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +const State = enum { copy, chunk }; |
| 72 | + |
| 73 | +const Chunk = struct { |
| 74 | + lines: []Line, |
| 75 | + src: usize, |
| 76 | + dst: usize, |
| 77 | + |
| 78 | + const Line = struct { |
| 79 | + const Action = enum { remove, keep, add }; |
| 80 | + |
| 81 | + action: Action, |
| 82 | + buf: []const u8, |
| 83 | + }; |
| 84 | + |
| 85 | + fn next(allocator: Allocator, file: File) ?Chunk { |
| 86 | + while (true) { |
| 87 | + const line = getLine(allocator, file) orelse return null; |
| 88 | + if (std.mem.startsWith(u8, line, "@@")) { |
| 89 | + const end = std.mem.indexOfPosLinear(u8, line, 3, "@@").?; |
| 90 | + const details = line[4 .. end - 2]; |
| 91 | + |
| 92 | + const space_index = std.mem.indexOfScalar(u8, details, ' ').?; |
| 93 | + const src = getLineNumber(details[0..space_index]); |
| 94 | + const dst = getLineNumber(details[space_index + 1 ..]); |
| 95 | + |
| 96 | + var lines: std.ArrayListUnmanaged(Line) = .empty; |
| 97 | + while (true) { |
| 98 | + const diff_line = getLine(allocator, file) orelse break; |
| 99 | + if (std.mem.startsWith(u8, diff_line, "@@")) break; |
| 100 | + |
| 101 | + const action: Line.Action = switch (diff_line[0]) { |
| 102 | + '-' => .remove, |
| 103 | + ' ' => .keep, |
| 104 | + '+' => .add, |
| 105 | + else => @panic("Bad patch file"), |
| 106 | + }; |
| 107 | + |
| 108 | + lines.append(allocator, .{ .action = action, .buf = diff_line[1..] }) catch unreachable; |
| 109 | + } |
| 110 | + |
| 111 | + return .{ |
| 112 | + .lines = lines.toOwnedSlice(allocator) catch unreachable, |
| 113 | + .src = src, |
| 114 | + .dst = dst, |
| 115 | + }; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + fn getLineNumber(buf: []const u8) usize { |
| 121 | + const comma = std.mem.indexOfScalar(u8, buf, ',').?; |
| 122 | + return std.fmt.parseInt(usize, buf[0..comma], 10) catch unreachable; |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +const std = @import("std"); |
| 127 | +const Allocator = std.mem.Allocator; |
| 128 | +const File = std.fs.File; |
0 commit comments