summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 97083ba38633bf34075b950b418bc7e77c4702ea (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
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>

#define REPO   ".cvn"
#define INDEX  "index"

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

static inline void traverse(void);
static inline uint8_t 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[])
{
	uint8_t i;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <command> [<args>]\n", argv[0]);
		return 1;
	}
	
	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[])
{
	char *branch;
	int opt, repo_fd, idx_fd;

	optind = 1; 
	branch = "master";

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

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

	if ((repo_fd = open(REPO, O_RDONLY | O_DIRECTORY)) == -1) {
		perror("Failed to open repository");
		return;
	}

	if ((idx_fd = openat(repo_fd, INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
		if (errno != EEXIST) {
			close(repo_fd);
			perror("Failed to create index");
			return;
		}
	} else
		close(idx_fd);

	close(repo_fd);
	printf("Initialized repository in %s\n", REPO);
}

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

static inline uint8_t ignore(const char *path)
{
	return strcmp(path, ".") == 0 || 
	       strcmp(path, "..") == 0 || 
	       strcmp(path, REPO) == 0;
}

static inline void traverse(void)
{
	DIR *dir;
	char *path, *rel_path;
	struct stat st;
	struct dirent *entry;
	int i, subdirs_len;
	char **subdirs;

	i = 0;
	// On 64-bit systems: 512 * 8 (ptr size) = 4096 - typical page size / 64 cache lines
	// Page-aligned buffers that fit in CPU cache lines reduce mmu overhead (play well
	// with the CPU's TLB (Tranlation Lookaside Buffer).
	subdirs_len = 512;
	subdirs = malloc(sizeof(subdirs[0]) * subdirs_len);
	if (!subdirs)
		err(1, "malloc() failed");
	subdirs[i++] = strdup(".");

	while (i > 0) {
		path = subdirs[--i];
		if (!(dir = opendir(path))) {
			warn("Failed to open directory %s", path);
			free(path);
			continue;
		}

		while ((entry = readdir(dir)) != NULL) {
			if (ignore(entry->d_name))
				continue;
			if (asprintf(&rel_path, "%s/%s", path, entry->d_name) == -1)
				err(1, "asprintf() failed");
			if (lstat(rel_path, &st) == -1) {
				warn("lstat() failed: %s", rel_path);
				free(rel_path);
				continue;
			}
			if (S_ISDIR(st.st_mode)) {
				if (i >= subdirs_len) {
					subdirs_len <<= 1;
					subdirs = realloc(subdirs, sizeof(subdirs[0]) * subdirs_len);
					if (!subdirs)
						err(1, "realloc() failed");
				}
				subdirs[i++] = rel_path;
			} else {
				printf("File: %s\n", rel_path);
				free(rel_path);
			}
		}
		free(path);
		closedir(dir);
	}
	free(subdirs);
}