Initial revision of "git", the information manager from hell
[git/graveyard.git] / update-cache.c
blob5085a5cb53ee52e1886ff6d46c609bdb2fc6d6cd
1 #include "cache.h"
3 static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
5 int len = len1 < len2 ? len1 : len2;
6 int cmp;
8 cmp = memcmp(name1, name2, len);
9 if (cmp)
10 return cmp;
11 if (len1 < len2)
12 return -1;
13 if (len1 > len2)
14 return 1;
15 return 0;
18 static int cache_name_pos(const char *name, int namelen)
20 int first, last;
22 first = 0;
23 last = active_nr;
24 while (last > first) {
25 int next = (last + first) >> 1;
26 struct cache_entry *ce = active_cache[next];
27 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
28 if (!cmp)
29 return -next-1;
30 if (cmp < 0) {
31 last = next;
32 continue;
34 first = next+1;
36 return first;
39 static int remove_file_from_cache(char *path)
41 int pos = cache_name_pos(path, strlen(path));
42 if (pos < 0) {
43 pos = -pos-1;
44 active_nr--;
45 if (pos < active_nr)
46 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
50 static int add_cache_entry(struct cache_entry *ce)
52 int pos;
54 pos = cache_name_pos(ce->name, ce->namelen);
56 /* existing match? Just replace it */
57 if (pos < 0) {
58 active_cache[-pos-1] = ce;
59 return 0;
62 /* Make sure the array is big enough .. */
63 if (active_nr == active_alloc) {
64 active_alloc = alloc_nr(active_alloc);
65 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
68 /* Add it in.. */
69 active_nr++;
70 if (active_nr > pos)
71 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
72 active_cache[pos] = ce;
73 return 0;
76 static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
78 z_stream stream;
79 int max_out_bytes = namelen + st->st_size + 200;
80 void *out = malloc(max_out_bytes);
81 void *metadata = malloc(namelen + 200);
82 void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
83 SHA_CTX c;
85 close(fd);
86 if (!out || (int)(long)in == -1)
87 return -1;
89 memset(&stream, 0, sizeof(stream));
90 deflateInit(&stream, Z_BEST_COMPRESSION);
93 * ASCII size + nul byte
94 */
95 stream.next_in = metadata;
96 stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
97 stream.next_out = out;
98 stream.avail_out = max_out_bytes;
99 while (deflate(&stream, 0) == Z_OK)
100 /* nothing */;
103 * File content
105 stream.next_in = in;
106 stream.avail_in = st->st_size;
107 while (deflate(&stream, Z_FINISH) == Z_OK)
108 /*nothing */;
110 deflateEnd(&stream);
112 SHA1_Init(&c);
113 SHA1_Update(&c, out, stream.total_out);
114 SHA1_Final(ce->sha1, &c);
116 return write_sha1_buffer(ce->sha1, out, stream.total_out);
119 static int add_file_to_cache(char *path)
121 int size, namelen;
122 struct cache_entry *ce;
123 struct stat st;
124 int fd;
126 fd = open(path, O_RDONLY);
127 if (fd < 0) {
128 if (errno == ENOENT)
129 return remove_file_from_cache(path);
130 return -1;
132 if (fstat(fd, &st) < 0) {
133 close(fd);
134 return -1;
136 namelen = strlen(path);
137 size = cache_entry_size(namelen);
138 ce = malloc(size);
139 memset(ce, 0, size);
140 memcpy(ce->name, path, namelen);
141 ce->ctime.sec = st.st_ctime;
142 ce->ctime.nsec = st.st_ctim.tv_nsec;
143 ce->mtime.sec = st.st_mtime;
144 ce->mtime.nsec = st.st_mtim.tv_nsec;
145 ce->st_dev = st.st_dev;
146 ce->st_ino = st.st_ino;
147 ce->st_mode = st.st_mode;
148 ce->st_uid = st.st_uid;
149 ce->st_gid = st.st_gid;
150 ce->st_size = st.st_size;
151 ce->namelen = namelen;
153 if (index_fd(path, namelen, ce, fd, &st) < 0)
154 return -1;
156 return add_cache_entry(ce);
159 static int write_cache(int newfd, struct cache_entry **cache, int entries)
161 SHA_CTX c;
162 struct cache_header hdr;
163 int i;
165 hdr.signature = CACHE_SIGNATURE;
166 hdr.version = 1;
167 hdr.entries = entries;
169 SHA1_Init(&c);
170 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
171 for (i = 0; i < entries; i++) {
172 struct cache_entry *ce = cache[i];
173 int size = ce_size(ce);
174 SHA1_Update(&c, ce, size);
176 SHA1_Final(hdr.sha1, &c);
178 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
179 return -1;
181 for (i = 0; i < entries; i++) {
182 struct cache_entry *ce = cache[i];
183 int size = ce_size(ce);
184 if (write(newfd, ce, size) != size)
185 return -1;
187 return 0;
191 * We fundamentally don't like some paths: we don't want
192 * dot or dot-dot anywhere, and in fact, we don't even want
193 * any other dot-files (.dircache or anything else). They
194 * are hidden, for chist sake.
196 * Also, we don't want double slashes or slashes at the
197 * end that can make pathnames ambiguous.
199 static int verify_path(char *path)
201 char c;
203 goto inside;
204 for (;;) {
205 if (!c)
206 return 1;
207 if (c == '/') {
208 inside:
209 c = *path++;
210 if (c != '/' && c != '.' && c != '\0')
211 continue;
212 return 0;
214 c = *path++;
218 int main(int argc, char **argv)
220 int i, newfd, entries;
222 entries = read_cache();
223 if (entries < 0) {
224 perror("cache corrupted");
225 return -1;
228 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
229 if (newfd < 0) {
230 perror("unable to create new cachefile");
231 return -1;
233 for (i = 1 ; i < argc; i++) {
234 char *path = argv[i];
235 if (!verify_path(path)) {
236 fprintf(stderr, "Ignoring path %s\n", argv[i]);
237 continue;
239 if (add_file_to_cache(path)) {
240 fprintf(stderr, "Unable to add %s to database\n", path);
241 goto out;
244 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
245 return 0;
246 out:
247 unlink(".dircache/index.lock");