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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <ftw.h>
#include <sha1.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define VCX_DIR ".vcx"
#define HEAD VCX_DIR "/head"
#define TMP_DIR VCX_DIR "/tmp"
#define OBJ_DIR VCX_DIR "/obj"
#define EXCLUDE_PATHS ".vcxignore"
#define BUF_LEN 8192
#define MAX_DEPTH 256
#define MAX_PATH_LEN 4096
#define HASH_LEN SHA1_DIGEST_STRING_LENGTH
#define KIND_DEL 'D'
#define KIND_MOD 'M'
#define KIND_NEW 'N'
#define MALLOC(s) _xmalloc((s), __FILE__, __LINE__)
#define REALLOC(p, s) _xrealloc((p), (s), __FILE__, __LINE__)
static inline void init(int argc, char *argv[]);
static inline void status(int argc, char *argv[]);
static inline void add(int argc, char *argv[]);
static inline void init_stg_area(void);
static inline int cmp_links(const char *link1, const char *link2);
static inline int cmp_files(const char *file1, const char *file2);
static inline void stage_entry(const char *path, char kind, int islnk);
static inline void print_status(const char *file, char kind, int islnk);
static inline int scan_head(const char * path, const struct stat *st, int flag, struct FTW *ftwbuf);
static inline int scan_tree(const char * path, const struct stat *st, int flag, struct FTW *ftwbuf);
static inline void mkcopy(const char *path, char dst[HASH_LEN]);
static inline void mklink(const char *src, const char obj[HASH_LEN]);
static inline void mkdirs(const char *path);
static inline void copy_link(const char *src);
static inline int copy_file(const char *src, const char *dst);
static inline int linklen(const char *lnk);
static inline void concat(char *dst, size_t n, const char *arg1, ...);
static inline void *_xmalloc(size_t s, const char *file, int line);
static inline void *_xrealloc(void *ptr, size_t s, const char *file, int line);
struct command {
char *name;
void (*func)(int argc, char *argv[]);
};
struct command cmd[] = {
{"init", init},
{"add", add},
{"status", status},
{NULL, NULL}
};
int main(int argc, char *argv[])
{
if (argc < 2)
errx(1, "Usage: %s <command> [<args>]", argv[0]);
for (int i = 0; cmd[i].name != NULL; i++) {
if (strcmp(argv[1], cmd[i].name) == 0) {
cmd[i].func(argc - 1, argv + 1);
return 0;
}
}
return 0;
}
static inline void init(int argc, char *argv[])
{
if (mkdir(VCX_DIR, 0755) == -1) {
if (errno != EEXIST)
err(1, "Failed to create %s", VCX_DIR);
}
if (mkdir(HEAD, 0755) == -1) {
if (errno != EEXIST)
err(1, "Failed to create %s", HEAD);
}
if (mkdir(OBJ_DIR, 0755) == -1) {
if (errno != EEXIST)
err(1, "Failed to create %s", OBJ_DIR);
}
printf("Repository ready\n");
}
static void (*scan_res_cb)(const char *file, char kind, int islnk) = NULL;
static inline void status(int argc, char *argv[])
{
scan_res_cb = print_status;
if (nftw(HEAD, scan_head, MAX_DEPTH, FTW_PHYS) == -1)
err(1, "Failed to scan head");
if (nftw(".", scan_tree, MAX_DEPTH, FTW_PHYS) == -1)
err(1, "Failed to scan work tree");
}
static inline void add(int argc, char *argv[])
{
size_t i;
struct stat st;
char *wt_path;
if (argc < 2)
errx(1, "Usage: %s [<files>]", argv[0]);
init_stg_area();
scan_res_cb = stage_entry;
for (i = 1; i < argc; i++) {
wt_path = argv[i];
if (lstat(wt_path, &st) == -1)
err(1, "%s", wt_path);
size_t hd_sz = strlen(HEAD) + strlen(wt_path) + 2;
char hd_path[hd_sz];
concat(hd_path, hd_sz, HEAD, "/", wt_path, NULL);
size_t tmp_sz = strlen(TMP_DIR) + strlen(wt_path) + 2;
char tmp_path[tmp_sz];
concat(tmp_path, tmp_sz, TMP_DIR, "/", wt_path, NULL);
if (nftw(hd_path, scan_head, MAX_DEPTH, FTW_PHYS) == -1)
err(1, "Failed to scan head");
if (nftw(wt_path, scan_tree, MAX_DEPTH, FTW_PHYS) == -1)
err(1, "Failed to scan work tree");
}
}
int scan_head(const char * path, const struct stat *sb, int flag,
struct FTW *ftwbuf)
{
struct stat wt_stat;
if (flag == FTW_F) {
const char *filename = path + ftwbuf->base;
size_t wt_sz = strlen(filename) + 3;
char wt_path[wt_sz];
concat(wt_path, wt_sz, ".", "/", filename, NULL);
if (lstat(wt_path, &wt_stat) == -1) {
if (errno == ENOENT) {
// file/link doesn't matter for deletions
scan_res_cb(wt_path, KIND_DEL, 0);
return 0;
}
else
err(1, "lstat failed for %s", wt_path);
}
if (wt_stat.st_size == sb->st_size &&
wt_stat.st_mtime == sb->st_mtime)
return 0; // no change
if (S_ISREG(wt_stat.st_mode)) {
if (cmp_files(path, wt_path) == 1)
scan_res_cb(wt_path, KIND_MOD, 0);
} else if (S_ISLNK(wt_stat.st_mode)) {
if (cmp_links(path, wt_path) == 1)
scan_res_cb(wt_path, KIND_MOD, 1);
}
}
return 0;
}
static inline int scan_tree(const char * path, const struct stat *st,
int flag, struct FTW *ftwbuf)
{
if (fnmatch("./.vcx/*", path, 0) == 0)
return 0;
if (flag == FTW_F) {
const char *filename = path + ftwbuf->base;
size_t hd_sz = strlen(HEAD) + strlen(filename) + 2;
char hd_path[hd_sz];
concat(hd_path, hd_sz, HEAD, "/", filename, NULL);
if (access(hd_path, F_OK) != 0)
scan_res_cb(path, KIND_NEW, S_ISLNK(st->st_mode) != 0);
}
return 0;
}
static inline int cmp_files(const char *file1, const char *file2)
{
int rc;
int fd1, fd2;
ssize_t r1, r2;
char buf1[BUF_LEN], buf2[BUF_LEN];
if ((fd1 = open(file1, O_RDONLY)) < 0)
err(1, "Couldn't open %s to compare", file1);
if ((fd2 = open(file2, O_RDONLY)) < 0)
err(1, "Couldn't open %s to compare", file2);
rc = 0;
do {
r1 = read(fd1, buf1, BUF_LEN);
r2 = read(fd2, buf2, BUF_LEN);
if (r1 != r2 || memcmp(buf1, buf2, r1) != 0) {
rc = 1;
break;
}
} while (r1 > 0);
close(fd1);
close(fd2);
return rc;
}
static inline int cmp_links(const char *link1, const char *link2)
{
ssize_t len1, len2;
int buflen1, buflen2;
buflen1 = linklen(link1);
char buf1[buflen1];
len1 = readlink(link1, buf1, sizeof(buf1) - 1);
buflen2 = linklen(link2);
char buf2[buflen2];
len2 = readlink(link2, buf2, sizeof(buf2) - 1);
if (len1 < 0 || len2 < 0)
err(1, "readlink error on %s or %s", link1, link2);
if (len1 != len2)
return 1;
buf1[len1] = '\0';
buf2[len2] = '\0';
return strcmp(buf1, buf2);
}
static inline void init_stg_area(void)
{
pid_t rm_pid, cp_pid;
rm_pid = fork();
if (rm_pid == 0) {
if (access(TMP_DIR, F_OK) == 0) {
execlp("rm", "rm", "-rf", TMP_DIR, NULL);
err(1, "Failed to delete %s", TMP_DIR);
}
_exit(0);
} else {
waitpid(rm_pid, NULL, 0);
cp_pid = fork();
if (cp_pid == 0) {
execlp("cp", "cp", "-RP", HEAD, TMP_DIR, NULL);
err(1, "Failed to copy %s to %s", HEAD, TMP_DIR);
}
waitpid(cp_pid, NULL, 0);
}
}
static inline void stage_entry(const char *path, char kind, int islnk)
{
char objname[HASH_LEN];
switch (kind) {
case KIND_MOD:
break;
case KIND_NEW:
if (!islnk) {
mkcopy(path, objname);
mklink(path, objname);
} else
copy_link(path);
break;
case KIND_DEL:
break;
default:
break;
}
}
static inline void mkcopy(const char *path, char dst[HASH_LEN])
{
int len, obj_len;
char obj_name[HASH_LEN];
len = strlen(path);
obj_len = HASH_LEN - 1;
if (SHA1Data((const uint8_t *)path, len, obj_name) == NULL)
err(1, "Failed to compute hash for %s", path);
size_t obj_sz = strlen(OBJ_DIR) + obj_len + 6;
char obj_path[obj_sz];
concat(obj_path, obj_sz, OBJ_DIR, "/", obj_name, ".tmp", NULL);
copy_file(path, obj_path);
}
static inline void mklink(const char *src, const char obj[HASH_LEN])
{
// HASH_LEN includes null byte
size_t lnkt_sz = strlen("../obj/") + HASH_LEN + 4;
char lnk_target[lnkt_sz];
concat(lnk_target, lnkt_sz, "../obj/", obj, ".tmp", NULL);
size_t lnkp_sz = strlen(TMP_DIR) + strlen(src) + 2;
char lnk_path[lnkp_sz];
concat(lnk_path, lnkp_sz, TMP_DIR, "/", src, NULL);
mkdirs(lnk_path);
if (symlink(lnk_target, lnk_path) != 0)
err(1, "symlink error on %s", src);
}
static inline void print_status(const char *file, char kind, int islnk)
{
printf("[%c] %s\n", kind, file);
}
static inline int linklen(const char *lnk)
{
int len;
struct stat st;
if (lstat(lnk, &st) != 0)
err(1, "lstat %s", lnk);
len = st.st_size + 1;
if (MAX_PATH_LEN < len)
errx(1, "Link too long: %s", lnk);
return len;
}
static inline void concat(char *dst, size_t n, const char *arg1, ...)
{
va_list ap;
const char *part;
int curlen, partlen;
if (!dst || n == 0)
return;
dst[0] = '\0';
curlen = 0;
if (!arg1)
return;
va_start(ap, arg1);
part = arg1;
while (part) {
partlen = strlen(part);
if (curlen + partlen + 1 > n) {
dst[curlen] = '\0';
va_end(ap);
errx(1, "Path too long: %s%s", dst, part);
}
memcpy(dst + curlen, part, partlen);
curlen += partlen;
part = va_arg(ap, const char *);
}
dst[curlen] = '\0';
va_end(ap);
}
static inline int diff(const char *file1, const char *file2)
{
pid_t pid;
int null_fd, status, rc;
pid = fork();
if (pid == 0) {
null_fd = open("/dev/null", O_WRONLY);
if (null_fd == -1)
err(1, "open() failed on /dev/null");
dup2(null_fd, STDOUT_FILENO);
dup2(null_fd, STDERR_FILENO);
close(null_fd);
execlp("diff", "diff", "-q", file1, file2, (char *)NULL);
err(1, "execlp failed for diff");
} else if (pid > 0) {
waitpid(pid, &status, 0);
// diff returns 0 if files are same, 1 if different, >1 if error
rc = WEXITSTATUS(status);
if (rc > 1)
err(1, "diff error");
return rc;
}
err(1, "fork");
}
static inline int copy_file(const char *src, const char *dst)
{
int fdin, fdout;
struct stat st;
char buf[BUF_LEN], *outptr;
ssize_t nread, nwrite, res;
if ((fdin = open(src, O_RDONLY)) < 0)
err(1, "open failed for %s", src);
if (fstat(fdin, &st) < 0) {
close(fdin);
err(1, "fstat failed for %s", src);
}
mkdirs(dst);
fdout = open(dst, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode & 0777);
if (fdout < 0) {
close(fdin);
err(1, "open failed for %s", dst);
}
while ((nread = read(fdin, buf, sizeof(buf))) > 0) {
nwrite = 0;
outptr = buf;
// Handle partial writes (possible with large files/slow disks)
while (nwrite < nread) {
res = write(fdout, outptr + nwrite, nread - nwrite);
if (res < 0) {
if (errno == EINTR) // interrupted by a signal - retry
continue;
close(fdin);
close(fdout);
err(1, "Copy error on %s", src); // bad error: e.g, disk full
}
nwrite += res;
}
}
close(fdin);
close(fdout);
return (nread < 0) ? -1 : 0;
}
static inline void copy_link(const char *src)
{
int len;
ssize_t n;
len = linklen(src);
char target[len];
n = readlink(src, target, len);
if (n == -1)
err(1, "readlink %s", src);
target[n] = '\0';
size_t dst_sz = strlen(TMP_DIR) + strlen(src) + 2;
char dst[dst_sz];
concat(dst, dst_sz, TMP_DIR, src, NULL);
mkdirs(dst);
if (symlink(target, dst) != 0)
err(1, "Link copy error %s", src);
}
static inline void mkdirs(const char *path)
{
char *p;
int pathlen;
static char buf[MAX_PATH_LEN];
pathlen = strlen(path);
if (MAX_PATH_LEN < pathlen + 1)
errx(1, "Path too long: %s", path);
buf[0] = '\0';
strcpy(buf, path);
for (p = buf; *p; p++) {
if (*p == '/') {
*p = '\0';
if (mkdir(buf, 0755) != 0) {
if (errno != EEXIST)
err(1, "mkdir failed for %s", buf);
}
*p = '/';
}
}
}
static inline void *_xmalloc(size_t s, const char *file, int line)
{
void *p;
if (!(p = malloc(s)))
err(1, "%s:%d: malloc", file, line);
return p;
}
static inline void *_xrealloc(void *ptr, size_t s, const char *file,
int line)
{
void *p;
if (!(p = realloc(ptr, s)))
err(1, "%s:%d: realloc", file, line);
return p;
}
|