]>
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 | ||
c0fd1f51 LT |
174 | int ce_path_match(const struct cache_entry *ce, const char **pathspec) |
175 | { | |
176 | const char *match, *name; | |
177 | int len; | |
178 | ||
179 | if (!pathspec) | |
180 | return 1; | |
181 | ||
182 | len = ce_namelen(ce); | |
183 | name = ce->name; | |
184 | while ((match = *pathspec++) != NULL) { | |
185 | int matchlen = strlen(match); | |
186 | if (matchlen > len) | |
187 | continue; | |
188 | if (memcmp(name, match, matchlen)) | |
189 | continue; | |
190 | if (matchlen && name[matchlen-1] == '/') | |
191 | return 1; | |
192 | if (name[matchlen] == '/' || !name[matchlen]) | |
193 | return 1; | |
194 | } | |
195 | return 0; | |
196 | } | |
197 | ||
12676608 LT |
198 | /* |
199 | * Do we have another file that has the beginning components being a | |
200 | * proper superset of the name we're trying to add? | |
0f1e4f04 | 201 | */ |
12676608 | 202 | static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace) |
0f1e4f04 | 203 | { |
12676608 LT |
204 | int retval = 0; |
205 | int len = ce_namelen(ce); | |
b155725d | 206 | int stage = ce_stage(ce); |
12676608 | 207 | const char *name = ce->name; |
0f1e4f04 | 208 | |
12676608 LT |
209 | while (pos < active_nr) { |
210 | struct cache_entry *p = active_cache[pos++]; | |
0f1e4f04 | 211 | |
12676608 | 212 | if (len >= ce_namelen(p)) |
0f1e4f04 | 213 | break; |
12676608 LT |
214 | if (memcmp(name, p->name, len)) |
215 | break; | |
b155725d JH |
216 | if (ce_stage(p) != stage) |
217 | continue; | |
12676608 LT |
218 | if (p->name[len] != '/') |
219 | continue; | |
12676608 LT |
220 | retval = -1; |
221 | if (!ok_to_replace) | |
222 | break; | |
223 | remove_cache_entry_at(--pos); | |
0f1e4f04 | 224 | } |
12676608 LT |
225 | return retval; |
226 | } | |
0f1e4f04 | 227 | |
12676608 LT |
228 | /* |
229 | * Do we have another file with a pathname that is a proper | |
230 | * subset of the name we're trying to add? | |
231 | */ | |
232 | static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace) | |
233 | { | |
234 | int retval = 0; | |
b155725d | 235 | int stage = ce_stage(ce); |
12676608 LT |
236 | const char *name = ce->name; |
237 | const char *slash = name + ce_namelen(ce); | |
0f1e4f04 | 238 | |
12676608 LT |
239 | for (;;) { |
240 | int len; | |
0f1e4f04 | 241 | |
12676608 LT |
242 | for (;;) { |
243 | if (*--slash == '/') | |
244 | break; | |
245 | if (slash <= ce->name) | |
246 | return retval; | |
247 | } | |
248 | len = slash - name; | |
0f1e4f04 | 249 | |
b155725d | 250 | pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage))); |
12676608 LT |
251 | if (pos >= 0) { |
252 | retval = -1; | |
253 | if (ok_to_replace) | |
254 | break; | |
dbbce55b | 255 | remove_cache_entry_at(pos); |
12676608 LT |
256 | continue; |
257 | } | |
258 | ||
259 | /* | |
260 | * Trivial optimization: if we find an entry that | |
261 | * already matches the sub-directory, then we know | |
b155725d | 262 | * we're ok, and we can exit. |
12676608 LT |
263 | */ |
264 | pos = -pos-1; | |
b155725d | 265 | while (pos < active_nr) { |
12676608 | 266 | struct cache_entry *p = active_cache[pos]; |
b155725d JH |
267 | if ((ce_namelen(p) <= len) || |
268 | (p->name[len] != '/') || | |
269 | memcmp(p->name, name, len)) | |
270 | break; /* not our subdirectory */ | |
271 | if (ce_stage(p) == stage) | |
272 | /* p is at the same stage as our entry, and | |
273 | * is a subdirectory of what we are looking | |
274 | * at, so we cannot have conflicts at our | |
275 | * level or anything shorter. | |
276 | */ | |
277 | return retval; | |
278 | pos++; | |
192268c1 | 279 | } |
0f1e4f04 | 280 | } |
12676608 LT |
281 | return retval; |
282 | } | |
283 | ||
284 | /* We may be in a situation where we already have path/file and path | |
285 | * is being added, or we already have path and path/file is being | |
286 | * added. Either one would result in a nonsense tree that has path | |
287 | * twice when git-write-tree tries to write it out. Prevent it. | |
288 | * | |
289 | * If ok-to-replace is specified, we remove the conflicting entries | |
290 | * from the cache so the caller should recompute the insert position. | |
291 | * When this happens, we return non-zero. | |
292 | */ | |
293 | static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace) | |
294 | { | |
295 | /* | |
296 | * We check if the path is a sub-path of a subsequent pathname | |
297 | * first, since removing those will not change the position | |
298 | * in the array | |
299 | */ | |
300 | int retval = has_file_name(ce, pos, ok_to_replace); | |
301 | /* | |
302 | * Then check if the path might have a clashing sub-directory | |
303 | * before it. | |
304 | */ | |
305 | return retval + has_dir_name(ce, pos, ok_to_replace); | |
0f1e4f04 JH |
306 | } |
307 | ||
192268c1 | 308 | int add_cache_entry(struct cache_entry *ce, int option) |
197ee8c9 LT |
309 | { |
310 | int pos; | |
192268c1 JH |
311 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
312 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; | |
b155725d | 313 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
972d1bb0 | 314 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
197ee8c9 LT |
315 | |
316 | /* existing match? Just replace it */ | |
76e7f4ec | 317 | if (pos >= 0) { |
ee267527 | 318 | active_cache_changed = 1; |
76e7f4ec | 319 | active_cache[pos] = ce; |
197ee8c9 LT |
320 | return 0; |
321 | } | |
76e7f4ec | 322 | pos = -pos-1; |
197ee8c9 | 323 | |
7b937ca3 LT |
324 | /* |
325 | * Inserting a merged entry ("stage 0") into the index | |
326 | * will always replace all non-merged entries.. | |
327 | */ | |
328 | if (pos < active_nr && ce_stage(ce) == 0) { | |
dbbce55b | 329 | while (ce_same_name(active_cache[pos], ce)) { |
7b937ca3 | 330 | ok_to_add = 1; |
dbbce55b | 331 | if (!remove_cache_entry_at(pos)) |
7b937ca3 LT |
332 | break; |
333 | } | |
334 | } | |
335 | ||
121481ab LT |
336 | if (!ok_to_add) |
337 | return -1; | |
338 | ||
b155725d | 339 | if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) { |
192268c1 JH |
340 | if (!ok_to_replace) |
341 | return -1; | |
972d1bb0 | 342 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
192268c1 JH |
343 | pos = -pos-1; |
344 | } | |
0f1e4f04 | 345 | |
197ee8c9 LT |
346 | /* Make sure the array is big enough .. */ |
347 | if (active_nr == active_alloc) { | |
348 | active_alloc = alloc_nr(active_alloc); | |
812666c8 | 349 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
197ee8c9 LT |
350 | } |
351 | ||
352 | /* Add it in.. */ | |
353 | active_nr++; | |
354 | if (active_nr > pos) | |
355 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
356 | active_cache[pos] = ce; | |
ee267527 | 357 | active_cache_changed = 1; |
197ee8c9 LT |
358 | return 0; |
359 | } | |
360 | ||
e83c5163 LT |
361 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
362 | { | |
363 | SHA_CTX c; | |
364 | unsigned char sha1[20]; | |
365 | ||
ccc4feb5 | 366 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 367 | return error("bad signature"); |
ca9be054 LT |
368 | if (hdr->hdr_version != htonl(2)) |
369 | return error("bad index version"); | |
e83c5163 | 370 | SHA1_Init(&c); |
ca9be054 | 371 | SHA1_Update(&c, hdr, size - 20); |
e83c5163 | 372 | SHA1_Final(sha1, &c); |
ca9be054 LT |
373 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
374 | return error("bad index file sha1 signature"); | |
e83c5163 LT |
375 | return 0; |
376 | } | |
377 | ||
378 | int read_cache(void) | |
379 | { | |
380 | int fd, i; | |
381 | struct stat st; | |
382 | unsigned long size, offset; | |
383 | void *map; | |
384 | struct cache_header *hdr; | |
385 | ||
386 | errno = EBUSY; | |
387 | if (active_cache) | |
388 | return error("more than one cachefile"); | |
389 | errno = ENOENT; | |
bb233d69 | 390 | fd = open(get_index_file(), O_RDONLY); |
e83c5163 LT |
391 | if (fd < 0) |
392 | return (errno == ENOENT) ? 0 : error("open failed"); | |
393 | ||
19b2860c | 394 | size = 0; // avoid gcc warning |
e83c5163 LT |
395 | map = (void *)-1; |
396 | if (!fstat(fd, &st)) { | |
e83c5163 LT |
397 | size = st.st_size; |
398 | errno = EINVAL; | |
ca9be054 | 399 | if (size >= sizeof(struct cache_header) + 20) |
520fc241 | 400 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
e83c5163 LT |
401 | } |
402 | close(fd); | |
403 | if (-1 == (int)(long)map) | |
404 | return error("mmap failed"); | |
405 | ||
406 | hdr = map; | |
407 | if (verify_hdr(hdr, size) < 0) | |
408 | goto unmap; | |
409 | ||
ccc4feb5 | 410 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
411 | active_alloc = alloc_nr(active_nr); |
412 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
413 | ||
414 | offset = sizeof(*hdr); | |
ccc4feb5 | 415 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
416 | struct cache_entry *ce = map + offset; |
417 | offset = offset + ce_size(ce); | |
418 | active_cache[i] = ce; | |
419 | } | |
420 | return active_nr; | |
421 | ||
422 | unmap: | |
423 | munmap(map, size); | |
424 | errno = EINVAL; | |
425 | return error("verify header failed"); | |
426 | } | |
427 | ||
4990aadc | 428 | #define WRITE_BUFFER_SIZE 8192 |
bf0f910d | 429 | static unsigned char write_buffer[WRITE_BUFFER_SIZE]; |
4990aadc LT |
430 | static unsigned long write_buffer_len; |
431 | ||
ca9be054 | 432 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
433 | { |
434 | while (len) { | |
435 | unsigned int buffered = write_buffer_len; | |
436 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
437 | if (partial > len) | |
438 | partial = len; | |
439 | memcpy(write_buffer + buffered, data, partial); | |
440 | buffered += partial; | |
441 | if (buffered == WRITE_BUFFER_SIZE) { | |
ca9be054 | 442 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
4990aadc LT |
443 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
444 | return -1; | |
445 | buffered = 0; | |
446 | } | |
447 | write_buffer_len = buffered; | |
448 | len -= partial; | |
449 | data += partial; | |
450 | } | |
451 | return 0; | |
452 | } | |
453 | ||
ca9be054 | 454 | static int ce_flush(SHA_CTX *context, int fd) |
4990aadc LT |
455 | { |
456 | unsigned int left = write_buffer_len; | |
ca9be054 | 457 | |
4990aadc LT |
458 | if (left) { |
459 | write_buffer_len = 0; | |
ca9be054 | 460 | SHA1_Update(context, write_buffer, left); |
4990aadc | 461 | } |
ca9be054 LT |
462 | |
463 | /* Append the SHA1 signature at the end */ | |
464 | SHA1_Final(write_buffer + left, context); | |
465 | left += 20; | |
466 | if (write(fd, write_buffer, left) != left) | |
467 | return -1; | |
4990aadc LT |
468 | return 0; |
469 | } | |
470 | ||
197ee8c9 LT |
471 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
472 | { | |
473 | SHA_CTX c; | |
474 | struct cache_header hdr; | |
025a0709 JH |
475 | int i, removed; |
476 | ||
477 | for (i = removed = 0; i < entries; i++) | |
478 | if (!cache[i]->ce_mode) | |
479 | removed++; | |
197ee8c9 | 480 | |
ccc4feb5 | 481 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
ca9be054 | 482 | hdr.hdr_version = htonl(2); |
025a0709 | 483 | hdr.hdr_entries = htonl(entries - removed); |
197ee8c9 LT |
484 | |
485 | SHA1_Init(&c); | |
ca9be054 | 486 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
487 | return -1; |
488 | ||
489 | for (i = 0; i < entries; i++) { | |
490 | struct cache_entry *ce = cache[i]; | |
aa16021e LT |
491 | if (!ce->ce_mode) |
492 | continue; | |
ca9be054 | 493 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
197ee8c9 LT |
494 | return -1; |
495 | } | |
ca9be054 | 496 | return ce_flush(&c, newfd); |
197ee8c9 | 497 | } |