2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 struct cache_entry
**active_cache
= NULL
;
10 unsigned int active_nr
= 0, active_alloc
= 0, active_cache_changed
= 0;
13 * This only updates the "non-critical" parts of the directory
14 * cache, ie the parts that aren't tracked by GIT, and only used
15 * to validate the cache.
17 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
19 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
20 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
22 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
23 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
25 ce
->ce_dev
= htonl(st
->st_dev
);
26 ce
->ce_ino
= htonl(st
->st_ino
);
27 ce
->ce_uid
= htonl(st
->st_uid
);
28 ce
->ce_gid
= htonl(st
->st_gid
);
29 ce
->ce_size
= htonl(st
->st_size
);
32 int ce_match_stat(struct cache_entry
*ce
, struct stat
*st
)
34 unsigned int changed
= 0;
36 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
38 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
39 /* We consider only the owner x bit to be relevant for "mode changes" */
40 if (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
))
41 changed
|= MODE_CHANGED
;
44 changed
|= !S_ISLNK(st
->st_mode
) ? TYPE_CHANGED
: 0;
47 die("internal error: ce_mode is %o", ntohl(ce
->ce_mode
));
49 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
50 changed
|= MTIME_CHANGED
;
51 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
52 changed
|= CTIME_CHANGED
;
56 * nsec seems unreliable - not all filesystems support it, so
57 * as long as it is in the inode cache you get right nsec
58 * but after it gets flushed, you get zero nsec.
60 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
61 changed
|= MTIME_CHANGED
;
62 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
63 changed
|= CTIME_CHANGED
;
66 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
67 ce
->ce_gid
!= htonl(st
->st_gid
))
68 changed
|= OWNER_CHANGED
;
69 if (ce
->ce_dev
!= htonl(st
->st_dev
) ||
70 ce
->ce_ino
!= htonl(st
->st_ino
))
71 changed
|= INODE_CHANGED
;
72 if (ce
->ce_size
!= htonl(st
->st_size
))
73 changed
|= DATA_CHANGED
;
77 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
79 int len1
= flags1
& CE_NAMEMASK
;
80 int len2
= flags2
& CE_NAMEMASK
;
81 int len
= len1
< len2
? len1
: len2
;
84 cmp
= memcmp(name1
, name2
, len
);
98 int cache_name_pos(const char *name
, int namelen
)
104 while (last
> first
) {
105 int next
= (last
+ first
) >> 1;
106 struct cache_entry
*ce
= active_cache
[next
];
107 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, htons(ce
->ce_flags
));
119 /* Remove entry, return true if there are more entries to go.. */
120 int remove_cache_entry_at(int pos
)
122 active_cache_changed
= 1;
124 if (pos
>= active_nr
)
126 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
130 int remove_file_from_cache(char *path
)
132 int pos
= cache_name_pos(path
, strlen(path
));
135 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
136 remove_cache_entry_at(pos
);
140 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
142 int len
= ce_namelen(a
);
143 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
146 /* We may be in a situation where we already have path/file and path
147 * is being added, or we already have path and path/file is being
148 * added. Either one would result in a nonsense tree that has path
149 * twice when git-write-tree tries to write it out. Prevent it.
151 * If ok-to-replace is specified, we remove the conflicting entries
152 * from the cache so the caller should recompute the insert position.
153 * When this happens, we return non-zero.
155 static int check_file_directory_conflict(const struct cache_entry
*ce
,
158 int pos
, replaced
= 0;
159 const char *path
= ce
->name
;
160 int namelen
= strlen(path
);
161 int stage
= ce_stage(ce
);
162 char *pathbuf
= xmalloc(namelen
+ 1);
165 memcpy(pathbuf
, path
, namelen
+ 1);
168 * We are inserting path/file. Do they have path registered at
169 * the same stage? We need to do this for all the levels of our
174 char *ep
= strchr(cp
, '/');
177 *ep
= 0; /* first cut it at slash */
178 pos
= cache_name_pos(pathbuf
,
179 htons(create_ce_flags(ep
-cp
, stage
)));
181 /* Our leading path component is registered as a file,
182 * and we are trying to make it a directory. This is
185 if (!ok_to_replace
) {
189 fprintf(stderr
, "removing file '%s' to replace it with a directory to create '%s'.\n", pathbuf
, path
);
190 remove_cache_entry_at(pos
);
193 *ep
= '/'; /* then restore it and go downwards */
198 /* Do we have an entry in the cache that makes our path a prefix
199 * of it? That is, are we creating a file where they already expect
202 pos
= cache_name_pos(path
,
203 htons(create_ce_flags(namelen
, stage
)));
205 /* (0 <= pos) cannot happen because add_cache_entry()
206 * should have taken care of that case.
210 /* pos would point at an existing entry that would come immediately
211 * after our path. It could be the same as our path in higher stage,
212 * or different path but in a lower stage.
214 * E.g. when we are inserting path at stage 2,
223 * We need to examine pos, ignore it because it is at different
224 * stage, examine next to find the path/file at stage 2, and
225 * complain. We need to do this until we are not the leading
226 * path of an existing entry anymore.
229 while (pos
< active_nr
) {
230 struct cache_entry
*other
= active_cache
[pos
];
231 if (strncmp(other
->name
, path
, namelen
))
232 break; /* it is not our "subdirectory" anymore */
233 if ((ce_stage(other
) == stage
) &&
234 other
->name
[namelen
] == '/') {
237 fprintf(stderr
, "removing file '%s' under '%s' to be replaced with a file\n", other
->name
, path
);
238 remove_cache_entry_at(pos
);
240 continue; /* cycle without updating pos */
247 int add_cache_entry(struct cache_entry
*ce
, int option
)
250 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
251 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
252 pos
= cache_name_pos(ce
->name
, htons(ce
->ce_flags
));
254 /* existing match? Just replace it */
256 active_cache_changed
= 1;
257 active_cache
[pos
] = ce
;
263 * Inserting a merged entry ("stage 0") into the index
264 * will always replace all non-merged entries..
266 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
267 while (ce_same_name(active_cache
[pos
], ce
)) {
269 if (!remove_cache_entry_at(pos
))
277 if (check_file_directory_conflict(ce
, ok_to_replace
)) {
280 pos
= cache_name_pos(ce
->name
, htons(ce
->ce_flags
));
284 /* Make sure the array is big enough .. */
285 if (active_nr
== active_alloc
) {
286 active_alloc
= alloc_nr(active_alloc
);
287 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
293 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
294 active_cache
[pos
] = ce
;
295 active_cache_changed
= 1;
299 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
302 unsigned char sha1
[20];
304 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
305 return error("bad signature");
306 if (hdr
->hdr_version
!= htonl(2))
307 return error("bad index version");
309 SHA1_Update(&c
, hdr
, size
- 20);
310 SHA1_Final(sha1
, &c
);
311 if (memcmp(sha1
, (void *)hdr
+ size
- 20, 20))
312 return error("bad index file sha1 signature");
320 unsigned long size
, offset
;
322 struct cache_header
*hdr
;
326 return error("more than one cachefile");
328 fd
= open(get_index_file(), O_RDONLY
);
330 return (errno
== ENOENT
) ? 0 : error("open failed");
332 size
= 0; // avoid gcc warning
334 if (!fstat(fd
, &st
)) {
337 if (size
>= sizeof(struct cache_header
) + 20)
338 map
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
341 if (-1 == (int)(long)map
)
342 return error("mmap failed");
345 if (verify_hdr(hdr
, size
) < 0)
348 active_nr
= ntohl(hdr
->hdr_entries
);
349 active_alloc
= alloc_nr(active_nr
);
350 active_cache
= calloc(active_alloc
, sizeof(struct cache_entry
*));
352 offset
= sizeof(*hdr
);
353 for (i
= 0; i
< active_nr
; i
++) {
354 struct cache_entry
*ce
= map
+ offset
;
355 offset
= offset
+ ce_size(ce
);
356 active_cache
[i
] = ce
;
363 return error("verify header failed");
366 #define WRITE_BUFFER_SIZE 8192
367 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
368 static unsigned long write_buffer_len
;
370 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
373 unsigned int buffered
= write_buffer_len
;
374 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
377 memcpy(write_buffer
+ buffered
, data
, partial
);
379 if (buffered
== WRITE_BUFFER_SIZE
) {
380 SHA1_Update(context
, write_buffer
, WRITE_BUFFER_SIZE
);
381 if (write(fd
, write_buffer
, WRITE_BUFFER_SIZE
) != WRITE_BUFFER_SIZE
)
385 write_buffer_len
= buffered
;
392 static int ce_flush(SHA_CTX
*context
, int fd
)
394 unsigned int left
= write_buffer_len
;
397 write_buffer_len
= 0;
398 SHA1_Update(context
, write_buffer
, left
);
401 /* Append the SHA1 signature at the end */
402 SHA1_Final(write_buffer
+ left
, context
);
404 if (write(fd
, write_buffer
, left
) != left
)
409 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
412 struct cache_header hdr
;
415 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
416 hdr
.hdr_version
= htonl(2);
417 hdr
.hdr_entries
= htonl(entries
);
420 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
423 for (i
= 0; i
< entries
; i
++) {
424 struct cache_entry
*ce
= cache
[i
];
425 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
428 return ce_flush(&c
, newfd
);