-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.c
More file actions
122 lines (104 loc) · 3.08 KB
/
index.c
File metadata and controls
122 lines (104 loc) · 3.08 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
#include "mvcs.h"
/* Read index file */
int read_index(index_entry_t **entries, int *count) {
FILE *f = fopen(INDEX_FILE, "r");
if (!f) {
*entries = NULL;
*count = 0;
return 0;
}
index_entry_t *ent = malloc(sizeof(index_entry_t) * MAX_INDEX_ENTRIES);
if (!ent) {
fclose(f);
return -1;
}
*count = 0;
char line[MAX_PATH_LEN + HASH_HEX_SIZE + 20];
while (fgets(line, sizeof(line), f) && *count < MAX_INDEX_ENTRIES) {
char hash_hex[HASH_HEX_SIZE];
int mode;
char path[MAX_PATH_LEN];
if (sscanf(line, "%64s %o %4095s", hash_hex, &mode, path) == 3) {
hex_to_hash(hash_hex, ent[*count].hash);
ent[*count].mode = mode;
strncpy(ent[*count].path, path, MAX_PATH_LEN - 1);
ent[*count].path[MAX_PATH_LEN - 1] = '\0';
(*count)++;
}
}
fclose(f);
*entries = ent;
return 0;
}
/* Write index file */
int write_index(index_entry_t *entries, int count) {
FILE *f = fopen(INDEX_FILE, "w");
if (!f) return -1;
for (int i = 0; i < count; i++) {
char hash_hex[HASH_HEX_SIZE];
hash_to_hex(entries[i].hash, hash_hex);
fprintf(f, "%s %o %s\n", hash_hex, entries[i].mode, entries[i].path);
}
fclose(f);
return 0;
}
/* Add file to index */
int add_to_index(const char *path) {
/* Check if file exists */
if (!file_exists(path)) {
fprintf(stderr, "File not found: %s\n", path);
return -1;
}
if (is_directory(path)) {
fprintf(stderr, "Cannot add directory (not implemented): %s\n", path);
return -1;
}
/* Hash the file */
unsigned char hash[HASH_SIZE];
if (write_blob(path, hash) != 0) {
fprintf(stderr, "Failed to hash file: %s\n", path);
return -1;
}
/* Read current index */
index_entry_t *entries;
int count;
if (read_index(&entries, &count) != 0) {
return -1;
}
/* Check if file already in index */
int found = -1;
for (int i = 0; i < count; i++) {
if (strcmp(entries[i].path, path) == 0) {
found = i;
break;
}
}
/* Get file mode */
struct stat st;
if (stat(path, &st) != 0) {
fprintf(stderr, "Failed to stat file: %s\n", path);
free(entries);
return -1;
}
int mode = st.st_mode & 0777;
/* Update or add entry */
if (found >= 0) {
memcpy(entries[found].hash, hash, HASH_SIZE);
entries[found].mode = mode;
} else {
if (count >= MAX_INDEX_ENTRIES) {
fprintf(stderr, "Index full\n");
if (entries) free(entries);
return -1;
}
memcpy(entries[count].hash, hash, HASH_SIZE);
entries[count].mode = mode;
strncpy(entries[count].path, path, MAX_PATH_LEN - 1);
entries[count].path[MAX_PATH_LEN - 1] = '\0';
count++;
}
/* Write updated index */
int ret = write_index(entries, count);
free(entries);
return ret;
}