]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 LT |
6 | #include "cache.h" |
7 | ||
e83c5163 | 8 | struct cache_entry **active_cache = NULL; |
ee267527 | 9 | unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0; |
e83c5163 | 10 | |
415e96c8 JH |
11 | /* |
12 | * This only updates the "non-critical" parts of the directory | |
13 | * cache, ie the parts that aren't tracked by GIT, and only used | |
14 | * to validate the cache. | |
15 | */ | |
16 | void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) | |
17 | { | |
18 | ce->ce_ctime.sec = htonl(st->st_ctime); | |
19 | ce->ce_mtime.sec = htonl(st->st_mtime); | |
2cb45e95 | 20 | #ifdef USE_NSEC |
415e96c8 JH |
21 | ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec); |
22 | ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec); | |
23 | #endif | |
24 | ce->ce_dev = htonl(st->st_dev); | |
25 | ce->ce_ino = htonl(st->st_ino); | |
26 | ce->ce_uid = htonl(st->st_uid); | |
27 | ce->ce_gid = htonl(st->st_gid); | |
28 | ce->ce_size = htonl(st->st_size); | |
29 | } | |
30 | ||
5d728c84 | 31 | int ce_match_stat(struct cache_entry *ce, struct stat *st) |
734aab75 LT |
32 | { |
33 | unsigned int changed = 0; | |
34 | ||
8ae0a8c5 KS |
35 | switch (ntohl(ce->ce_mode) & S_IFMT) { |
36 | case S_IFREG: | |
37 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; | |
ffbe1add KS |
38 | /* We consider only the owner x bit to be relevant for "mode changes" */ |
39 | if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)) | |
40 | changed |= MODE_CHANGED; | |
8ae0a8c5 KS |
41 | break; |
42 | case S_IFLNK: | |
43 | changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0; | |
44 | break; | |
45 | default: | |
46 | die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); | |
47 | } | |
ccc4feb5 | 48 | if (ce->ce_mtime.sec != htonl(st->st_mtime)) |
734aab75 | 49 | changed |= MTIME_CHANGED; |
ccc4feb5 LT |
50 | if (ce->ce_ctime.sec != htonl(st->st_ctime)) |
51 | changed |= CTIME_CHANGED; | |
52 | ||
2cb45e95 | 53 | #ifdef USE_NSEC |
ccc4feb5 LT |
54 | /* |
55 | * nsec seems unreliable - not all filesystems support it, so | |
56 | * as long as it is in the inode cache you get right nsec | |
57 | * but after it gets flushed, you get zero nsec. | |
58 | */ | |
94dfb7f2 | 59 | if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec)) |
ccc4feb5 | 60 | changed |= MTIME_CHANGED; |
94dfb7f2 | 61 | if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec)) |
734aab75 | 62 | changed |= CTIME_CHANGED; |
ccc4feb5 LT |
63 | #endif |
64 | ||
65 | if (ce->ce_uid != htonl(st->st_uid) || | |
66 | ce->ce_gid != htonl(st->st_gid)) | |
734aab75 | 67 | changed |= OWNER_CHANGED; |
2cb45e95 | 68 | if (ce->ce_ino != htonl(st->st_ino)) |
734aab75 | 69 | changed |= INODE_CHANGED; |
2cb45e95 LT |
70 | |
71 | #ifdef USE_STDEV | |
72 | /* | |
73 | * st_dev breaks on network filesystems where different | |
74 | * clients will have different views of what "device" | |
75 | * the filesystem is on | |
76 | */ | |
77 | if (ce->ce_dev != htonl(st->st_dev)) | |
78 | changed |= INODE_CHANGED; | |
79 | #endif | |
80 | ||
ccc4feb5 | 81 | if (ce->ce_size != htonl(st->st_size)) |
734aab75 LT |
82 | changed |= DATA_CHANGED; |
83 | return changed; | |
84 | } | |
85 | ||
958ba6c9 LT |
86 | int base_name_compare(const char *name1, int len1, int mode1, |
87 | const char *name2, int len2, int mode2) | |
88 | { | |
89 | unsigned char c1, c2; | |
90 | int len = len1 < len2 ? len1 : len2; | |
91 | int cmp; | |
92 | ||
93 | cmp = memcmp(name1, name2, len); | |
94 | if (cmp) | |
95 | return cmp; | |
96 | c1 = name1[len]; | |
97 | c2 = name2[len]; | |
98 | if (!c1 && S_ISDIR(mode1)) | |
99 | c1 = '/'; | |
100 | if (!c2 && S_ISDIR(mode2)) | |
101 | c2 = '/'; | |
102 | return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
103 | } | |
104 | ||
95fd5bf8 | 105 | int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2) |
eb38c22f | 106 | { |
95fd5bf8 LT |
107 | int len1 = flags1 & CE_NAMEMASK; |
108 | int len2 = flags2 & CE_NAMEMASK; | |
eb38c22f LT |
109 | int len = len1 < len2 ? len1 : len2; |
110 | int cmp; | |
111 | ||
112 | cmp = memcmp(name1, name2, len); | |
113 | if (cmp) | |
114 | return cmp; | |
115 | if (len1 < len2) | |
116 | return -1; | |
117 | if (len1 > len2) | |
118 | return 1; | |
95fd5bf8 LT |
119 | if (flags1 < flags2) |
120 | return -1; | |
121 | if (flags1 > flags2) | |
122 | return 1; | |
eb38c22f LT |
123 | return 0; |
124 | } | |
125 | ||
126 | int cache_name_pos(const char *name, int namelen) | |
127 | { | |
128 | int first, last; | |
129 | ||
130 | first = 0; | |
131 | last = active_nr; | |
132 | while (last > first) { | |
133 | int next = (last + first) >> 1; | |
134 | struct cache_entry *ce = active_cache[next]; | |
972d1bb0 | 135 | int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags)); |
eb38c22f | 136 | if (!cmp) |
76e7f4ec | 137 | return next; |
eb38c22f LT |
138 | if (cmp < 0) { |
139 | last = next; | |
140 | continue; | |
141 | } | |
142 | first = next+1; | |
143 | } | |
76e7f4ec | 144 | return -first-1; |
eb38c22f LT |
145 | } |
146 | ||
7b937ca3 | 147 | /* Remove entry, return true if there are more entries to go.. */ |
dbbce55b | 148 | int remove_cache_entry_at(int pos) |
7b937ca3 | 149 | { |
ee267527 | 150 | active_cache_changed = 1; |
7b937ca3 LT |
151 | active_nr--; |
152 | if (pos >= active_nr) | |
153 | return 0; | |
154 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); | |
155 | return 1; | |
156 | } | |
157 | ||
197ee8c9 LT |
158 | int remove_file_from_cache(char *path) |
159 | { | |
160 | int pos = cache_name_pos(path, strlen(path)); | |
c4e3cca1 JH |
161 | if (pos < 0) |
162 | pos = -pos-1; | |
163 | while (pos < active_nr && !strcmp(active_cache[pos]->name, path)) | |
dbbce55b | 164 | remove_cache_entry_at(pos); |
197ee8c9 LT |
165 | return 0; |
166 | } | |
167 | ||
dbbce55b | 168 | int ce_same_name(struct cache_entry *a, struct cache_entry *b) |
7b937ca3 LT |
169 | { |
170 | int len = ce_namelen(a); | |
171 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
172 | } | |
173 | ||
0f1e4f04 JH |
174 | /* We may be in a situation where we already have path/file and path |
175 | * is being added, or we already have path and path/file is being | |
176 | * added. Either one would result in a nonsense tree that has path | |
177 | * twice when git-write-tree tries to write it out. Prevent it. | |
192268c1 JH |
178 | * |
179 | * If ok-to-replace is specified, we remove the conflicting entries | |
180 | * from the cache so the caller should recompute the insert position. | |
181 | * When this happens, we return non-zero. | |
0f1e4f04 | 182 | */ |
192268c1 JH |
183 | static int check_file_directory_conflict(const struct cache_entry *ce, |
184 | int ok_to_replace) | |
0f1e4f04 | 185 | { |
192268c1 | 186 | int pos, replaced = 0; |
0f1e4f04 JH |
187 | const char *path = ce->name; |
188 | int namelen = strlen(path); | |
189 | int stage = ce_stage(ce); | |
190 | char *pathbuf = xmalloc(namelen + 1); | |
191 | char *cp; | |
192 | ||
193 | memcpy(pathbuf, path, namelen + 1); | |
194 | ||
195 | /* | |
196 | * We are inserting path/file. Do they have path registered at | |
197 | * the same stage? We need to do this for all the levels of our | |
198 | * subpath. | |
199 | */ | |
200 | cp = pathbuf; | |
201 | while (1) { | |
202 | char *ep = strchr(cp, '/'); | |
8310c2c0 | 203 | int len; |
aa575f81 | 204 | if (!ep) |
0f1e4f04 JH |
205 | break; |
206 | *ep = 0; /* first cut it at slash */ | |
8310c2c0 | 207 | len = ep - pathbuf; |
0f1e4f04 | 208 | pos = cache_name_pos(pathbuf, |
972d1bb0 | 209 | ntohs(create_ce_flags(len, stage))); |
0f1e4f04 JH |
210 | if (0 <= pos) { |
211 | /* Our leading path component is registered as a file, | |
212 | * and we are trying to make it a directory. This is | |
213 | * bad. | |
214 | */ | |
192268c1 JH |
215 | if (!ok_to_replace) { |
216 | free(pathbuf); | |
217 | return -1; | |
218 | } | |
219 | fprintf(stderr, "removing file '%s' to replace it with a directory to create '%s'.\n", pathbuf, path); | |
dbbce55b | 220 | remove_cache_entry_at(pos); |
192268c1 | 221 | replaced = 1; |
0f1e4f04 JH |
222 | } |
223 | *ep = '/'; /* then restore it and go downwards */ | |
224 | cp = ep + 1; | |
225 | } | |
226 | free(pathbuf); | |
227 | ||
228 | /* Do we have an entry in the cache that makes our path a prefix | |
229 | * of it? That is, are we creating a file where they already expect | |
230 | * a directory there? | |
231 | */ | |
232 | pos = cache_name_pos(path, | |
972d1bb0 | 233 | ntohs(create_ce_flags(namelen, stage))); |
0f1e4f04 JH |
234 | |
235 | /* (0 <= pos) cannot happen because add_cache_entry() | |
236 | * should have taken care of that case. | |
237 | */ | |
238 | pos = -pos-1; | |
239 | ||
240 | /* pos would point at an existing entry that would come immediately | |
241 | * after our path. It could be the same as our path in higher stage, | |
242 | * or different path but in a lower stage. | |
243 | * | |
244 | * E.g. when we are inserting path at stage 2, | |
245 | * | |
246 | * 1 path | |
247 | * pos-> 3 path | |
192268c1 JH |
248 | * 2 path/file1 |
249 | * 3 path/file1 | |
250 | * 2 path/file2 | |
251 | * 2 patho | |
0f1e4f04 JH |
252 | * |
253 | * We need to examine pos, ignore it because it is at different | |
254 | * stage, examine next to find the path/file at stage 2, and | |
192268c1 JH |
255 | * complain. We need to do this until we are not the leading |
256 | * path of an existing entry anymore. | |
0f1e4f04 JH |
257 | */ |
258 | ||
259 | while (pos < active_nr) { | |
260 | struct cache_entry *other = active_cache[pos]; | |
261 | if (strncmp(other->name, path, namelen)) | |
262 | break; /* it is not our "subdirectory" anymore */ | |
192268c1 JH |
263 | if ((ce_stage(other) == stage) && |
264 | other->name[namelen] == '/') { | |
265 | if (!ok_to_replace) | |
266 | return -1; | |
267 | fprintf(stderr, "removing file '%s' under '%s' to be replaced with a file\n", other->name, path); | |
dbbce55b | 268 | remove_cache_entry_at(pos); |
192268c1 JH |
269 | replaced = 1; |
270 | continue; /* cycle without updating pos */ | |
271 | } | |
0f1e4f04 JH |
272 | pos++; |
273 | } | |
192268c1 | 274 | return replaced; |
0f1e4f04 JH |
275 | } |
276 | ||
192268c1 | 277 | int add_cache_entry(struct cache_entry *ce, int option) |
197ee8c9 LT |
278 | { |
279 | int pos; | |
192268c1 JH |
280 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
281 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; | |
972d1bb0 | 282 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
197ee8c9 LT |
283 | |
284 | /* existing match? Just replace it */ | |
76e7f4ec | 285 | if (pos >= 0) { |
ee267527 | 286 | active_cache_changed = 1; |
76e7f4ec | 287 | active_cache[pos] = ce; |
197ee8c9 LT |
288 | return 0; |
289 | } | |
76e7f4ec | 290 | pos = -pos-1; |
197ee8c9 | 291 | |
7b937ca3 LT |
292 | /* |
293 | * Inserting a merged entry ("stage 0") into the index | |
294 | * will always replace all non-merged entries.. | |
295 | */ | |
296 | if (pos < active_nr && ce_stage(ce) == 0) { | |
dbbce55b | 297 | while (ce_same_name(active_cache[pos], ce)) { |
7b937ca3 | 298 | ok_to_add = 1; |
dbbce55b | 299 | if (!remove_cache_entry_at(pos)) |
7b937ca3 LT |
300 | break; |
301 | } | |
302 | } | |
303 | ||
121481ab LT |
304 | if (!ok_to_add) |
305 | return -1; | |
306 | ||
192268c1 JH |
307 | if (check_file_directory_conflict(ce, ok_to_replace)) { |
308 | if (!ok_to_replace) | |
309 | return -1; | |
972d1bb0 | 310 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
192268c1 JH |
311 | pos = -pos-1; |
312 | } | |
0f1e4f04 | 313 | |
197ee8c9 LT |
314 | /* Make sure the array is big enough .. */ |
315 | if (active_nr == active_alloc) { | |
316 | active_alloc = alloc_nr(active_alloc); | |
812666c8 | 317 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
197ee8c9 LT |
318 | } |
319 | ||
320 | /* Add it in.. */ | |
321 | active_nr++; | |
322 | if (active_nr > pos) | |
323 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
324 | active_cache[pos] = ce; | |
ee267527 | 325 | active_cache_changed = 1; |
197ee8c9 LT |
326 | return 0; |
327 | } | |
328 | ||
e83c5163 LT |
329 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
330 | { | |
331 | SHA_CTX c; | |
332 | unsigned char sha1[20]; | |
333 | ||
ccc4feb5 | 334 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 335 | return error("bad signature"); |
ca9be054 LT |
336 | if (hdr->hdr_version != htonl(2)) |
337 | return error("bad index version"); | |
e83c5163 | 338 | SHA1_Init(&c); |
ca9be054 | 339 | SHA1_Update(&c, hdr, size - 20); |
e83c5163 | 340 | SHA1_Final(sha1, &c); |
ca9be054 LT |
341 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
342 | return error("bad index file sha1 signature"); | |
e83c5163 LT |
343 | return 0; |
344 | } | |
345 | ||
346 | int read_cache(void) | |
347 | { | |
348 | int fd, i; | |
349 | struct stat st; | |
350 | unsigned long size, offset; | |
351 | void *map; | |
352 | struct cache_header *hdr; | |
353 | ||
354 | errno = EBUSY; | |
355 | if (active_cache) | |
356 | return error("more than one cachefile"); | |
357 | errno = ENOENT; | |
bb233d69 | 358 | fd = open(get_index_file(), O_RDONLY); |
e83c5163 LT |
359 | if (fd < 0) |
360 | return (errno == ENOENT) ? 0 : error("open failed"); | |
361 | ||
19b2860c | 362 | size = 0; // avoid gcc warning |
e83c5163 LT |
363 | map = (void *)-1; |
364 | if (!fstat(fd, &st)) { | |
e83c5163 LT |
365 | size = st.st_size; |
366 | errno = EINVAL; | |
ca9be054 | 367 | if (size >= sizeof(struct cache_header) + 20) |
520fc241 | 368 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
e83c5163 LT |
369 | } |
370 | close(fd); | |
371 | if (-1 == (int)(long)map) | |
372 | return error("mmap failed"); | |
373 | ||
374 | hdr = map; | |
375 | if (verify_hdr(hdr, size) < 0) | |
376 | goto unmap; | |
377 | ||
ccc4feb5 | 378 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
379 | active_alloc = alloc_nr(active_nr); |
380 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
381 | ||
382 | offset = sizeof(*hdr); | |
ccc4feb5 | 383 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
384 | struct cache_entry *ce = map + offset; |
385 | offset = offset + ce_size(ce); | |
386 | active_cache[i] = ce; | |
387 | } | |
388 | return active_nr; | |
389 | ||
390 | unmap: | |
391 | munmap(map, size); | |
392 | errno = EINVAL; | |
393 | return error("verify header failed"); | |
394 | } | |
395 | ||
4990aadc | 396 | #define WRITE_BUFFER_SIZE 8192 |
bf0f910d | 397 | static unsigned char write_buffer[WRITE_BUFFER_SIZE]; |
4990aadc LT |
398 | static unsigned long write_buffer_len; |
399 | ||
ca9be054 | 400 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
401 | { |
402 | while (len) { | |
403 | unsigned int buffered = write_buffer_len; | |
404 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
405 | if (partial > len) | |
406 | partial = len; | |
407 | memcpy(write_buffer + buffered, data, partial); | |
408 | buffered += partial; | |
409 | if (buffered == WRITE_BUFFER_SIZE) { | |
ca9be054 | 410 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
4990aadc LT |
411 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
412 | return -1; | |
413 | buffered = 0; | |
414 | } | |
415 | write_buffer_len = buffered; | |
416 | len -= partial; | |
417 | data += partial; | |
418 | } | |
419 | return 0; | |
420 | } | |
421 | ||
ca9be054 | 422 | static int ce_flush(SHA_CTX *context, int fd) |
4990aadc LT |
423 | { |
424 | unsigned int left = write_buffer_len; | |
ca9be054 | 425 | |
4990aadc LT |
426 | if (left) { |
427 | write_buffer_len = 0; | |
ca9be054 | 428 | SHA1_Update(context, write_buffer, left); |
4990aadc | 429 | } |
ca9be054 LT |
430 | |
431 | /* Append the SHA1 signature at the end */ | |
432 | SHA1_Final(write_buffer + left, context); | |
433 | left += 20; | |
434 | if (write(fd, write_buffer, left) != left) | |
435 | return -1; | |
4990aadc LT |
436 | return 0; |
437 | } | |
438 | ||
197ee8c9 LT |
439 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
440 | { | |
441 | SHA_CTX c; | |
442 | struct cache_header hdr; | |
025a0709 JH |
443 | int i, removed; |
444 | ||
445 | for (i = removed = 0; i < entries; i++) | |
446 | if (!cache[i]->ce_mode) | |
447 | removed++; | |
197ee8c9 | 448 | |
ccc4feb5 | 449 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
ca9be054 | 450 | hdr.hdr_version = htonl(2); |
025a0709 | 451 | hdr.hdr_entries = htonl(entries - removed); |
197ee8c9 LT |
452 | |
453 | SHA1_Init(&c); | |
ca9be054 | 454 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
455 | return -1; |
456 | ||
457 | for (i = 0; i < entries; i++) { | |
458 | struct cache_entry *ce = cache[i]; | |
aa16021e LT |
459 | if (!ce->ce_mode) |
460 | continue; | |
ca9be054 | 461 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
197ee8c9 LT |
462 | return -1; |
463 | } | |
ca9be054 | 464 | return ce_flush(&c, newfd); |
197ee8c9 | 465 | } |