summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 32232c5b94a21698060248b921cc8f7554cf1f4c (plain)
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
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <sha1.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "mem.h"
#include "stack.h"

#define CVN    ".cvn"
#define INDEX  CVN "/index"

#define MAX_DEPTH       256
#define HASH_INPUT_LEN 4096
#define HASH_LEN       SHA1_DIGEST_LENGTH

static inline void init(int argc, char *argv[]);
static inline void status(int argc, char *argv[]);

static inline void scan_index(void);
static inline void scan_wtree(void);
static inline int ignore(const char *path);

struct command {
	char *name;
	void (*func)(int argc, char *argv[]);
};

struct command cmd[] = {
	{"init", init},
	{"status", status},
	{NULL, NULL}
};

int main(int argc, char *argv[])
{
	int i;

	if (argc < 2)
		errx(1, "Usage: %s <command> [<args>]", argv[0]);

	for (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[])
{
	int opt, fd;
	char *branch;

	optind = 1; 
	while ((opt = getopt(argc, argv, "b:")) != -1) {
		switch (opt) {
			case 'b': 
				branch = optarg;
				break;
			default: 
				break;
		}
	}

	if (mkdir(CVN, 0755) == -1) {
		if (errno != EEXIST)
			err(1, "Failed to create repository");
	}

	if ((fd = open(INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
		if (errno != EEXIST)
			err(1, "Failed to create index");
	} else
		close(fd);

	printf("Initialized repository in %s\n", CVN);
}

static inline void status(int argc, char *argv[])
{
	scan_index();
	scan_wtree();
}

static inline void scan_index(void)
{
	int fd;
	FILE *fp;
	char *line;
	size_t len;
	ssize_t bytes_read;

	if ((fd = open(INDEX, O_RDONLY)) == -1)
		err(1, "Failed to open index");
	
	if (!(fp = fdopen(fd, "r")))
		err(1, "fdopen");

	while ((bytes_read = getline(&line, &len, fp)) != -1) {
		printf("idx: %s (%zu)\n", line, bytes_read);
	}

	if (bytes_read == -1 && ferror(fp))
		err(1, "Failed to read %s", INDEX);
}

struct hashed_file {
	char *name;
	uint8_t hash[HASH_LEN];
};

static char hash_input[HASH_INPUT_LEN];
static struct stack *hash_at_level[MAX_DEPTH] = {0};

static inline void print_hash(const uint8_t hash[HASH_LEN], 
	const char *path)
{
	printf("0x");
	for (int i = 0; i < HASH_LEN; i++)
		printf("%02x", hash[i]);
	printf(": %s\n", path);
}

static inline void insert_hash(size_t level, const char *filename,
	const uint8_t *hash)
{
	struct stack *st;
	struct hashed_file *hf;

	if (level >= 0) { 
		hf = MALLOC(sizeof(struct hashed_file));
		hf->name = strdup(filename);
		memcpy(hf->hash, hash, HASH_LEN);
		st = hash_at_level[level];
		if (st)
			push(st, (void *)hf);
		else {
			st = MALLOC(sizeof(struct stack));
			stack_alloc(st);
			push(st, (void *)hf);
			hash_at_level[level] = st;
		}
	}
}

void hash_file(const char *path, uint8_t *hash)
{
	int fd;
	SHA1_CTX ctx;
	ssize_t bytes_read;

	if ((fd = open(path, O_RDONLY)) == -1)
		err(1, "Failed to open %s", path);

	SHA1Init(&ctx);
	while ((bytes_read = read(fd, hash_input, sizeof(hash_input))) > 0)
		SHA1Update(&ctx, hash_input, (size_t)bytes_read);
	if (bytes_read == -1)
		err(1, "Failed to read %s", path);

	SHA1Final(hash, &ctx);
	close(fd);
}

void hash_path(const char *path, uint8_t *hash)
{
	SHA1_CTX ctx;
	ssize_t lnk_len;

	lnk_len = readlink(path, hash_input, HASH_INPUT_LEN - 1);
	if (lnk_len == -1)
		err(1, "readlink failed for %s", path);
	hash_input[lnk_len] = '\0';

	SHA1Init(&ctx);
	SHA1Update(&ctx, hash_input, lnk_len);
	SHA1Final(hash, &ctx);
}

int compare_items(const void *a, const void *b) {
	const void *pa = *(const void **)a;
	const void *pb = *(const void **)b;

	const struct hashed_file *hfa = (const struct hashed_file *)pa;
	const struct hashed_file *hfb = (const struct hashed_file *)pb;

	return strcmp(hfb->name, hfa->name);
}

static inline void hash_dir(size_t level, uint8_t *hash)
{
	SHA1_CTX ctx;
	struct stack *st;
	struct hashed_file *hf;

	st = hash_at_level[level];
	if (st && st->len > 0) {
		SHA1Init(&ctx);
		qsort(st->items, st->len, sizeof(st->items[0]), compare_items);
		while (st->len > 0) {
			hf = pop(st);
			SHA1Update(&ctx, hf->hash, HASH_LEN);
			free(hf->name);
			free(hf);
		}
		SHA1Final(hash, &ctx);
		stack_free(st);
		hash_at_level[level] = NULL;
	}
}

int compute_hash(const char * path, const struct stat *st, 
	int tflag, struct FTW *ftwbuf)
{
	int level;
	mode_t mode;
	unsigned char hash[HASH_LEN];

	const char *filename = path + ftwbuf->base;
	if (ignore(path))
		return 0;

	mode = st->st_mode;
	level = ftwbuf->level;

	if (tflag == FTW_F) {
		if (S_ISREG(mode))
			hash_file(path, hash);
		else if (S_ISLNK(mode))
			hash_path(path, hash);
	} else if (tflag == FTW_DP) {
		hash_dir(level, hash);
	}

	insert_hash(level - 1, filename, hash);
	print_hash(hash, path);
	return 0;
}

static inline void scan_wtree(void)
{
	if (nftw(".", compute_hash, MAX_DEPTH, FTW_DEPTH | FTW_PHYS) == -1)
		err(1, "nftw");
}

static inline int ignore(const char *path)
{
	return memcmp(path, "./.cvn", 6) == 0 ||
	       strcmp(path, "./cvn") == 0 ||
	       strcmp(path, ".") == 0;
}