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
|
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <ftw.h>
#include <limits.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 MAX_DEPTH 256
#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 print_status(const char *file, char kind);
static inline int cmp_links(const char *link1, const char *link2);
static inline int cmp_content(const char *file1, const char *file2);
static inline char *concat_path(const char *dir, const char *file);
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 *_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}, {"status", status}, {"add", add},
{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);
}
if (mkdir(TMP_DIR, 0755) == -1) {
if (errno != EEXIST)
err(1, "Failed to create %s", TMP_DIR);
}
printf("Repository ready\n");
}
static void (*scan_res_cb)(const char *file, char kind) = 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[])
{
char **paths;
if (argc < 2)
errx(1, "Usage: %s [<files>]", argv[0]);
paths = &argv[1];
}
int scan_head(const char * path, const struct stat *sb, int flag,
struct FTW *ftwbuf)
{
char *wt_path;
struct stat wt_stat;
if (flag == FTW_F) {
const char *filename = path + ftwbuf->base;
wt_path = concat_path(".", filename);
if (lstat(wt_path, &wt_stat) == -1) {
if (errno == ENOENT) {
scan_res_cb(wt_path, 'D');
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_content(path, wt_path) == 1)
scan_res_cb(wt_path, 'M');
} else if (S_ISLNK(wt_stat.st_mode)) {
if (cmp_links(path, wt_path) == 1)
scan_res_cb(wt_path, 'M');
}
}
return 0;
}
static inline int scan_tree(const char * path, const struct stat *st,
int flag, struct FTW *ftwbuf)
{
char *hd_path;
if (fnmatch("./.vcx/*", path, FNM_PATHNAME) == 0)
return 0;
if (flag == FTW_F) {
const char *filename = path + ftwbuf->base;
hd_path = concat_path(HEAD, filename);
if (access(hd_path, F_OK) != 0)
scan_res_cb(path, 'N');
}
return 0;
}
static inline int cmp_content(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 cmp_links(const char *link1, const char *link2)
{
ssize_t len1, len2;
char buf1[PATH_MAX], buf2[PATH_MAX];
len1 = readlink(link1, buf1, sizeof(buf1) - 1);
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 print_status(const char *file, char kind)
{
printf("[%c] %s\n", kind, file);
}
static inline char *concat_path(const char *dir, const char *file)
{
static char *path_buf = NULL;
static size_t path_buflen = 1024;
size_t path_len;
if (!path_buf)
path_buf = MALLOC(sizeof(path_buf[0]) * path_buflen);
path_len = strlen(dir) + strlen(file) + 2;
if (path_len > path_buflen) {
path_buflen <<= 1;
path_buf = REALLOC(path_buf, sizeof(path_buf[0]) * path_buflen);
}
snprintf(path_buf, path_buflen, "%s/%s", dir, file);
return path_buf;
}
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;
}
|