-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatch.zig
More file actions
216 lines (156 loc) · 5.38 KB
/
catch.zig
File metadata and controls
216 lines (156 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const std = @import("std");
const print = std.debug.print;
const fs = std.fs;
const Allocator = std.mem.Allocator;
const Buffer = struct {
buffer: []u8,
index: usize,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator) Buffer {
return .{
.allocator = allocator,
.buffer = &.{},
.index = 0
};
}
pub fn writeFile(self: *Buffer, file_name: []const u8, bytes: []u8) !void {
const addition = file_name.len + 1 + bytes.len + @sizeOf(usize);
if(self.buffer.len < self.index + addition) {
const new_size = (self.index + addition) * 2;
self.buffer = try self.allocator.realloc(self.buffer, new_size);
}
for(0..file_name.len) |i| {
self.buffer[self.index] = file_name[i];
self.index += 1;
}
self.buffer[self.index] = 0;
self.index += 1;
const size_ptr: *align(1) usize = @ptrCast(&self.buffer[self.index]);
size_ptr.* = bytes.len;
self.index += @sizeOf(usize);
for(0..bytes.len) |i| {
self.buffer[self.index] = bytes[i];
self.index += 1;
}
}
pub fn cleanup(self: *Buffer) void {
self.allocator.free(self.buffer);
}
pub fn get(self: Buffer) []const u8 {
return self.buffer[0..self.index];
}
};
const File = struct {
name: []const u8,
bytes: []const u8,
};
pub fn read_files(allocator: Allocator, bytes: []const u8) !std.ArrayList(File) {
var result: std.ArrayList(File) = .empty;
var i = @as(usize, 0);
while(i < bytes.len){
const start = i;
var cur = i+1;
while(bytes[cur] != 0) : (cur += 1) {}
const name = bytes[start..cur];
i += cur-start+1;
const size_ptr: *const align(1) usize = @ptrCast(&bytes[i]);
i += @sizeOf(usize);
const bytes_start = i;
const bytes_end = i + size_ptr.*;
i += size_ptr.*;
try result.append(allocator, .{
.bytes = bytes[bytes_start..bytes_end],
.name = name
});
}
return result;
}
pub fn collect_files(file_name: []const u8, allocator: std.mem.Allocator) !Buffer {
var dir = try fs.cwd().openDir(file_name, .{ .iterate = true });
var walker = try dir.walk(allocator);
defer walker.deinit();
var buffer = Buffer.init(allocator);
while (try walker.next()) |entry| {
if(entry.kind == .directory) continue;
if(std.mem.startsWith(u8, entry.path, ".")) continue;
const bytes = try fs.cwd().readFileAlloc(allocator, entry.path, std.math.maxInt(usize));
defer allocator.free(bytes);
try buffer.writeFile(entry.path, bytes);
}
return buffer;
}
pub fn do_catch(allocator: std.mem.Allocator, file_name: []const u8) !void {
var buffer = try collect_files(".", allocator);
defer buffer.cleanup();
try std.fs.cwd().writeFile(.{
.data = buffer.get(),
.flags = .{},
.sub_path = file_name
});
}
pub fn get_directory_path(allocator: Allocator, file_path: []const u8) ![]const u8 {
var copy = try allocator.alloc(u8, file_path.len);
const last_slash = std.mem.lastIndexOf(u8, file_path, "/") orelse return ".";
for(0..last_slash+1) |i| {
copy[i] = file_path[i];
}
copy = try allocator.realloc(copy, last_slash+1);
return copy;
}
pub fn do_release(allocator: std.mem.Allocator, file_path: []const u8, target_dir: []const u8, capture_current: bool) !void {
if(capture_current and !file_exists(".catch-current")) {
try do_catch(allocator, ".catch-current");
}
const bytes = try fs.cwd().readFileAlloc(allocator, file_path, std.math.maxInt(usize));
defer allocator.free(bytes);
var files = try read_files(allocator, bytes);
defer files.deinit(allocator);
for (files.items) |file| {
const strings: [3][]const u8 = .{target_dir, "/", file.name};
const path = try std.mem.concat(allocator, u8, &strings);
defer allocator.free(path);
const directory_path = try get_directory_path(allocator, path);
defer allocator.free(directory_path);
std.fs.cwd().makePath(directory_path) catch {};
print("{s}\n", .{path});
try std.fs.cwd().writeFile(.{
.data = file.bytes,
.flags = .{},
.sub_path = path
});
}
}
fn command(arg1: ?[]const u8, c: []const u8) bool {
if(arg1) |a| {
return std.mem.eql(u8, a, c);
}
return false;
}
fn file_exists(path: []const u8) bool {
fs.cwd().access(path, .{}) catch {
return false;
};
return true;
}
pub fn main() !void {
const args = std.os.argv;
var arg1: ?[]const u8 = null;
if(args.len >= 2) {
arg1 = std.mem.span(args[1]);
}
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer if (gpa.deinit() != .ok) @panic("leak");
const allocator = gpa.allocator();
if(command(arg1, "release") or command(arg1, "r")) {
if(file_exists(".releasestop")) return;
try do_release(allocator, ".catch",".", true);
return;
}
if(command(arg1, "reset")) {
if(file_exists(".releasestop")) return;
try do_release(allocator,".catch-current", ".", false);
try std.fs.cwd().deleteFile(".catch-current");
return;
}
try do_catch(allocator, ".catch");
}