]>
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); | |
20 | #ifdef NSEC | |
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 | ||
bdd4da59 | 53 | #ifdef 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; |
ccc4feb5 LT |
68 | if (ce->ce_dev != htonl(st->st_dev) || |
69 | ce->ce_ino != htonl(st->st_ino)) | |
734aab75 | 70 | changed |= INODE_CHANGED; |
ccc4feb5 | 71 | if (ce->ce_size != htonl(st->st_size)) |
734aab75 LT |
72 | changed |= DATA_CHANGED; |
73 | return changed; | |
74 | } | |
75 | ||
958ba6c9 LT |
76 | int base_name_compare(const char *name1, int len1, int mode1, |
77 | const char *name2, int len2, int mode2) | |
78 | { | |
79 | unsigned char c1, c2; | |
80 | int len = len1 < len2 ? len1 : len2; | |
81 | int cmp; | |
82 | ||
83 | cmp = memcmp(name1, name2, len); | |
84 | if (cmp) | |
85 | return cmp; | |
86 | c1 = name1[len]; | |
87 | c2 = name2[len]; | |
88 | if (!c1 && S_ISDIR(mode1)) | |
89 | c1 = '/'; | |
90 | if (!c2 && S_ISDIR(mode2)) | |
91 | c2 = '/'; | |
92 | return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
93 | } | |
94 | ||
95fd5bf8 | 95 | int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2) |
eb38c22f | 96 | { |
95fd5bf8 LT |
97 | int len1 = flags1 & CE_NAMEMASK; |
98 | int len2 = flags2 & CE_NAMEMASK; | |
eb38c22f LT |
99 | int len = len1 < len2 ? len1 : len2; |
100 | int cmp; | |
101 | ||
102 | cmp = memcmp(name1, name2, len); | |
103 | if (cmp) | |
104 | return cmp; | |
105 | if (len1 < len2) | |
106 | return -1; | |
107 | if (len1 > len2) | |
108 | return 1; | |
95fd5bf8 LT |
109 | if (flags1 < flags2) |
110 | return -1; | |
111 | if (flags1 > flags2) | |
112 | return 1; | |
eb38c22f LT |
113 | return 0; |
114 | } | |
115 | ||
116 | int cache_name_pos(const char *name, int namelen) | |
117 | { | |
118 | int first, last; | |
119 | ||
120 | first = 0; | |
121 | last = active_nr; | |
122 | while (last > first) { | |
123 | int next = (last + first) >> 1; | |
124 | struct cache_entry *ce = active_cache[next]; | |
95fd5bf8 | 125 | int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags)); |
eb38c22f | 126 | if (!cmp) |
76e7f4ec | 127 | return next; |
eb38c22f LT |
128 | if (cmp < 0) { |
129 | last = next; | |
130 | continue; | |
131 | } | |
132 | first = next+1; | |
133 | } | |
76e7f4ec | 134 | return -first-1; |
eb38c22f LT |
135 | } |
136 | ||
7b937ca3 | 137 | /* Remove entry, return true if there are more entries to go.. */ |
dbbce55b | 138 | int remove_cache_entry_at(int pos) |
7b937ca3 | 139 | { |
ee267527 | 140 | active_cache_changed = 1; |
7b937ca3 LT |
141 | active_nr--; |
142 | if (pos >= active_nr) | |
143 | return 0; | |
144 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); | |
145 | return 1; | |
146 | } | |
147 | ||
197ee8c9 LT |
148 | int remove_file_from_cache(char *path) |
149 | { | |
150 | int pos = cache_name_pos(path, strlen(path)); | |
c4e3cca1 JH |
151 | if (pos < 0) |
152 | pos = -pos-1; | |
153 | while (pos < active_nr && !strcmp(active_cache[pos]->name, path)) | |
dbbce55b | 154 | remove_cache_entry_at(pos); |
197ee8c9 LT |
155 | return 0; |
156 | } | |
157 | ||
dbbce55b | 158 | int ce_same_name(struct cache_entry *a, struct cache_entry *b) |
7b937ca3 LT |
159 | { |
160 | int len = ce_namelen(a); | |
161 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
162 | } | |
163 | ||
0f1e4f04 JH |
164 | /* We may be in a situation where we already have path/file and path |
165 | * is being added, or we already have path and path/file is being | |
166 | * added. Either one would result in a nonsense tree that has path | |
167 | * twice when git-write-tree tries to write it out. Prevent it. | |
192268c1 JH |
168 | * |
169 | * If ok-to-replace is specified, we remove the conflicting entries | |
170 | * from the cache so the caller should recompute the insert position. | |
171 | * When this happens, we return non-zero. | |
0f1e4f04 | 172 | */ |
192268c1 JH |
173 | static int check_file_directory_conflict(const struct cache_entry *ce, |
174 | int ok_to_replace) | |
0f1e4f04 | 175 | { |
192268c1 | 176 | int pos, replaced = 0; |
0f1e4f04 JH |
177 | const char *path = ce->name; |
178 | int namelen = strlen(path); | |
179 | int stage = ce_stage(ce); | |
180 | char *pathbuf = xmalloc(namelen + 1); | |
181 | char *cp; | |
182 | ||
183 | memcpy(pathbuf, path, namelen + 1); | |
184 | ||
185 | /* | |
186 | * We are inserting path/file. Do they have path registered at | |
187 | * the same stage? We need to do this for all the levels of our | |
188 | * subpath. | |
189 | */ | |
190 | cp = pathbuf; | |
191 | while (1) { | |
192 | char *ep = strchr(cp, '/'); | |
aa575f81 | 193 | if (!ep) |
0f1e4f04 JH |
194 | break; |
195 | *ep = 0; /* first cut it at slash */ | |
196 | pos = cache_name_pos(pathbuf, | |
197 | htons(create_ce_flags(ep-cp, stage))); | |
198 | if (0 <= pos) { | |
199 | /* Our leading path component is registered as a file, | |
200 | * and we are trying to make it a directory. This is | |
201 | * bad. | |
202 | */ | |
192268c1 JH |
203 | if (!ok_to_replace) { |
204 | free(pathbuf); | |
205 | return -1; | |
206 | } | |
207 | fprintf(stderr, "removing file '%s' to replace it with a directory to create '%s'.\n", pathbuf, path); | |
dbbce55b | 208 | remove_cache_entry_at(pos); |
192268c1 | 209 | replaced = 1; |
0f1e4f04 JH |
210 | } |
211 | *ep = '/'; /* then restore it and go downwards */ | |
212 | cp = ep + 1; | |
213 | } | |
214 | free(pathbuf); | |
215 | ||
216 | /* Do we have an entry in the cache that makes our path a prefix | |
217 | * of it? That is, are we creating a file where they already expect | |
218 | * a directory there? | |
219 | */ | |
220 | pos = cache_name_pos(path, | |
221 | htons(create_ce_flags(namelen, stage))); | |
222 | ||
223 | /* (0 <= pos) cannot happen because add_cache_entry() | |
224 | * should have taken care of that case. | |
225 | */ | |
226 | pos = -pos-1; | |
227 | ||
228 | /* pos would point at an existing entry that would come immediately | |
229 | * after our path. It could be the same as our path in higher stage, | |
230 | * or different path but in a lower stage. | |
231 | * | |
232 | * E.g. when we are inserting path at stage 2, | |
233 | * | |
234 | * 1 path | |
235 | * pos-> 3 path | |
192268c1 JH |
236 | * 2 path/file1 |
237 | * 3 path/file1 | |
238 | * 2 path/file2 | |
239 | * 2 patho | |
0f1e4f04 JH |
240 | * |
241 | * We need to examine pos, ignore it because it is at different | |
242 | * stage, examine next to find the path/file at stage 2, and | |
192268c1 JH |
243 | * complain. We need to do this until we are not the leading |
244 | * path of an existing entry anymore. | |
0f1e4f04 JH |
245 | */ |
246 | ||
247 | while (pos < active_nr) { | |
248 | struct cache_entry *other = active_cache[pos]; | |
249 | if (strncmp(other->name, path, namelen)) | |
250 | break; /* it is not our "subdirectory" anymore */ | |
192268c1 JH |
251 | if ((ce_stage(other) == stage) && |
252 | other->name[namelen] == '/') { | |
253 | if (!ok_to_replace) | |
254 | return -1; | |
255 | fprintf(stderr, "removing file '%s' under '%s' to be replaced with a file\n", other->name, path); | |
dbbce55b | 256 | remove_cache_entry_at(pos); |
192268c1 JH |
257 | replaced = 1; |
258 | continue; /* cycle without updating pos */ | |
259 | } | |
0f1e4f04 JH |
260 | pos++; |
261 | } | |
192268c1 | 262 | return replaced; |
0f1e4f04 JH |
263 | } |
264 | ||
192268c1 | 265 | int add_cache_entry(struct cache_entry *ce, int option) |
197ee8c9 LT |
266 | { |
267 | int pos; | |
192268c1 JH |
268 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
269 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; | |
95fd5bf8 | 270 | pos = cache_name_pos(ce->name, htons(ce->ce_flags)); |
197ee8c9 LT |
271 | |
272 | /* existing match? Just replace it */ | |
76e7f4ec | 273 | if (pos >= 0) { |
ee267527 | 274 | active_cache_changed = 1; |
76e7f4ec | 275 | active_cache[pos] = ce; |
197ee8c9 LT |
276 | return 0; |
277 | } | |
76e7f4ec | 278 | pos = -pos-1; |
197ee8c9 | 279 | |
7b937ca3 LT |
280 | /* |
281 | * Inserting a merged entry ("stage 0") into the index | |
282 | * will always replace all non-merged entries.. | |
283 | */ | |
284 | if (pos < active_nr && ce_stage(ce) == 0) { | |
dbbce55b | 285 | while (ce_same_name(active_cache[pos], ce)) { |
7b937ca3 | 286 | ok_to_add = 1; |
dbbce55b | 287 | if (!remove_cache_entry_at(pos)) |
7b937ca3 LT |
288 | break; |
289 | } | |
290 | } | |
291 | ||
121481ab LT |
292 | if (!ok_to_add) |
293 | return -1; | |
294 | ||
192268c1 JH |
295 | if (check_file_directory_conflict(ce, ok_to_replace)) { |
296 | if (!ok_to_replace) | |
297 | return -1; | |
298 | pos = cache_name_pos(ce->name, htons(ce->ce_flags)); | |
299 | pos = -pos-1; | |
300 | } | |
0f1e4f04 | 301 | |
197ee8c9 LT |
302 | /* Make sure the array is big enough .. */ |
303 | if (active_nr == active_alloc) { | |
304 | active_alloc = alloc_nr(active_alloc); | |
812666c8 | 305 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
197ee8c9 LT |
306 | } |
307 | ||
308 | /* Add it in.. */ | |
309 | active_nr++; | |
310 | if (active_nr > pos) | |
311 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
312 | active_cache[pos] = ce; | |
ee267527 | 313 | active_cache_changed = 1; |
197ee8c9 LT |
314 | return 0; |
315 | } | |
316 | ||
e83c5163 LT |
317 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
318 | { | |
319 | SHA_CTX c; | |
320 | unsigned char sha1[20]; | |
321 | ||
ccc4feb5 | 322 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 323 | return error("bad signature"); |
ca9be054 LT |
324 | if (hdr->hdr_version != htonl(2)) |
325 | return error("bad index version"); | |
e83c5163 | 326 | SHA1_Init(&c); |
ca9be054 | 327 | SHA1_Update(&c, hdr, size - 20); |
e83c5163 | 328 | SHA1_Final(sha1, &c); |
ca9be054 LT |
329 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
330 | return error("bad index file sha1 signature"); | |
e83c5163 LT |
331 | return 0; |
332 | } | |
333 | ||
334 | int read_cache(void) | |
335 | { | |
336 | int fd, i; | |
337 | struct stat st; | |
338 | unsigned long size, offset; | |
339 | void *map; | |
340 | struct cache_header *hdr; | |
341 | ||
342 | errno = EBUSY; | |
343 | if (active_cache) | |
344 | return error("more than one cachefile"); | |
345 | errno = ENOENT; | |
bb233d69 | 346 | fd = open(get_index_file(), O_RDONLY); |
e83c5163 LT |
347 | if (fd < 0) |
348 | return (errno == ENOENT) ? 0 : error("open failed"); | |
349 | ||
19b2860c | 350 | size = 0; // avoid gcc warning |
e83c5163 LT |
351 | map = (void *)-1; |
352 | if (!fstat(fd, &st)) { | |
e83c5163 LT |
353 | size = st.st_size; |
354 | errno = EINVAL; | |
ca9be054 | 355 | if (size >= sizeof(struct cache_header) + 20) |
520fc241 | 356 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
e83c5163 LT |
357 | } |
358 | close(fd); | |
359 | if (-1 == (int)(long)map) | |
360 | return error("mmap failed"); | |
361 | ||
362 | hdr = map; | |
363 | if (verify_hdr(hdr, size) < 0) | |
364 | goto unmap; | |
365 | ||
ccc4feb5 | 366 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
367 | active_alloc = alloc_nr(active_nr); |
368 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
369 | ||
370 | offset = sizeof(*hdr); | |
ccc4feb5 | 371 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
372 | struct cache_entry *ce = map + offset; |
373 | offset = offset + ce_size(ce); | |
374 | active_cache[i] = ce; | |
375 | } | |
376 | return active_nr; | |
377 | ||
378 | unmap: | |
379 | munmap(map, size); | |
380 | errno = EINVAL; | |
381 | return error("verify header failed"); | |
382 | } | |
383 | ||
4990aadc | 384 | #define WRITE_BUFFER_SIZE 8192 |
bf0f910d | 385 | static unsigned char write_buffer[WRITE_BUFFER_SIZE]; |
4990aadc LT |
386 | static unsigned long write_buffer_len; |
387 | ||
ca9be054 | 388 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
389 | { |
390 | while (len) { | |
391 | unsigned int buffered = write_buffer_len; | |
392 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
393 | if (partial > len) | |
394 | partial = len; | |
395 | memcpy(write_buffer + buffered, data, partial); | |
396 | buffered += partial; | |
397 | if (buffered == WRITE_BUFFER_SIZE) { | |
ca9be054 | 398 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
4990aadc LT |
399 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
400 | return -1; | |
401 | buffered = 0; | |
402 | } | |
403 | write_buffer_len = buffered; | |
404 | len -= partial; | |
405 | data += partial; | |
406 | } | |
407 | return 0; | |
408 | } | |
409 | ||
ca9be054 | 410 | static int ce_flush(SHA_CTX *context, int fd) |
4990aadc LT |
411 | { |
412 | unsigned int left = write_buffer_len; | |
ca9be054 | 413 | |
4990aadc LT |
414 | if (left) { |
415 | write_buffer_len = 0; | |
ca9be054 | 416 | SHA1_Update(context, write_buffer, left); |
4990aadc | 417 | } |
ca9be054 LT |
418 | |
419 | /* Append the SHA1 signature at the end */ | |
420 | SHA1_Final(write_buffer + left, context); | |
421 | left += 20; | |
422 | if (write(fd, write_buffer, left) != left) | |
423 | return -1; | |
4990aadc LT |
424 | return 0; |
425 | } | |
426 | ||
197ee8c9 LT |
427 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
428 | { | |
429 | SHA_CTX c; | |
430 | struct cache_header hdr; | |
431 | int i; | |
432 | ||
ccc4feb5 | 433 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
ca9be054 | 434 | hdr.hdr_version = htonl(2); |
ccc4feb5 | 435 | hdr.hdr_entries = htonl(entries); |
197ee8c9 LT |
436 | |
437 | SHA1_Init(&c); | |
ca9be054 | 438 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
439 | return -1; |
440 | ||
441 | for (i = 0; i < entries; i++) { | |
442 | struct cache_entry *ce = cache[i]; | |
ca9be054 | 443 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
197ee8c9 LT |
444 | return -1; |
445 | } | |
ca9be054 | 446 | return ce_flush(&c, newfd); |
197ee8c9 | 447 | } |