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 base_name_compare(const char *name1
, int len1
, int mode1
,
78 const char *name2
, int len2
, int mode2
)
81 int len
= len1
< len2
? len1
: len2
;
84 cmp
= memcmp(name1
, name2
, len
);
89 if (!c1
&& S_ISDIR(mode1
))
91 if (!c2
&& S_ISDIR(mode2
))
93 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
96 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
98 int len1
= flags1
& CE_NAMEMASK
;
99 int len2
= flags2
& CE_NAMEMASK
;
100 int len
= len1
< len2
? len1
: len2
;
103 cmp
= memcmp(name1
, name2
, len
);
117 int cache_name_pos(const char *name
, int namelen
)
123 while (last
> first
) {
124 int next
= (last
+ first
) >> 1;
125 struct cache_entry
*ce
= active_cache
[next
];
126 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, htons(ce
->ce_flags
));
138 /* Remove entry, return true if there are more entries to go.. */
139 int remove_cache_entry_at(int pos
)
141 active_cache_changed
= 1;
143 if (pos
>= active_nr
)
145 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
149 int remove_file_from_cache(char *path
)
151 int pos
= cache_name_pos(path
, strlen(path
));
154 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
155 remove_cache_entry_at(pos
);
159 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
161 int len
= ce_namelen(a
);
162 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
165 /* We may be in a situation where we already have path/file and path
166 * is being added, or we already have path and path/file is being
167 * added. Either one would result in a nonsense tree that has path
168 * twice when git-write-tree tries to write it out. Prevent it.
170 * If ok-to-replace is specified, we remove the conflicting entries
171 * from the cache so the caller should recompute the insert position.
172 * When this happens, we return non-zero.
174 static int check_file_directory_conflict(const struct cache_entry
*ce
,
177 int pos
, replaced
= 0;
178 const char *path
= ce
->name
;
179 int namelen
= strlen(path
);
180 int stage
= ce_stage(ce
);
181 char *pathbuf
= xmalloc(namelen
+ 1);
184 memcpy(pathbuf
, path
, namelen
+ 1);
187 * We are inserting path/file. Do they have path registered at
188 * the same stage? We need to do this for all the levels of our
193 char *ep
= strchr(cp
, '/');
196 *ep
= 0; /* first cut it at slash */
197 pos
= cache_name_pos(pathbuf
,
198 htons(create_ce_flags(ep
-cp
, stage
)));
200 /* Our leading path component is registered as a file,
201 * and we are trying to make it a directory. This is
204 if (!ok_to_replace
) {
208 fprintf(stderr
, "removing file '%s' to replace it with a directory to create '%s'.\n", pathbuf
, path
);
209 remove_cache_entry_at(pos
);
212 *ep
= '/'; /* then restore it and go downwards */
217 /* Do we have an entry in the cache that makes our path a prefix
218 * of it? That is, are we creating a file where they already expect
221 pos
= cache_name_pos(path
,
222 htons(create_ce_flags(namelen
, stage
)));
224 /* (0 <= pos) cannot happen because add_cache_entry()
225 * should have taken care of that case.
229 /* pos would point at an existing entry that would come immediately
230 * after our path. It could be the same as our path in higher stage,
231 * or different path but in a lower stage.
233 * E.g. when we are inserting path at stage 2,
242 * We need to examine pos, ignore it because it is at different
243 * stage, examine next to find the path/file at stage 2, and
244 * complain. We need to do this until we are not the leading
245 * path of an existing entry anymore.
248 while (pos
< active_nr
) {
249 struct cache_entry
*other
= active_cache
[pos
];
250 if (strncmp(other
->name
, path
, namelen
))
251 break; /* it is not our "subdirectory" anymore */
252 if ((ce_stage(other
) == stage
) &&
253 other
->name
[namelen
] == '/') {
256 fprintf(stderr
, "removing file '%s' under '%s' to be replaced with a file\n", other
->name
, path
);
257 remove_cache_entry_at(pos
);
259 continue; /* cycle without updating pos */
266 int add_cache_entry(struct cache_entry
*ce
, int option
)
269 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
270 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
271 pos
= cache_name_pos(ce
->name
, htons(ce
->ce_flags
));
273 /* existing match? Just replace it */
275 active_cache_changed
= 1;
276 active_cache
[pos
] = ce
;
282 * Inserting a merged entry ("stage 0") into the index
283 * will always replace all non-merged entries..
285 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
286 while (ce_same_name(active_cache
[pos
], ce
)) {
288 if (!remove_cache_entry_at(pos
))
296 if (check_file_directory_conflict(ce
, ok_to_replace
)) {
299 pos
= cache_name_pos(ce
->name
, htons(ce
->ce_flags
));
303 /* Make sure the array is big enough .. */
304 if (active_nr
== active_alloc
) {
305 active_alloc
= alloc_nr(active_alloc
);
306 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
312 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
313 active_cache
[pos
] = ce
;
314 active_cache_changed
= 1;
318 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
321 unsigned char sha1
[20];
323 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
324 return error("bad signature");
325 if (hdr
->hdr_version
!= htonl(2))
326 return error("bad index version");
328 SHA1_Update(&c
, hdr
, size
- 20);
329 SHA1_Final(sha1
, &c
);
330 if (memcmp(sha1
, (void *)hdr
+ size
- 20, 20))
331 return error("bad index file sha1 signature");
339 unsigned long size
, offset
;
341 struct cache_header
*hdr
;
345 return error("more than one cachefile");
347 fd
= open(get_index_file(), O_RDONLY
);
349 return (errno
== ENOENT
) ? 0 : error("open failed");
351 size
= 0; // avoid gcc warning
353 if (!fstat(fd
, &st
)) {
356 if (size
>= sizeof(struct cache_header
) + 20)
357 map
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
360 if (-1 == (int)(long)map
)
361 return error("mmap failed");
364 if (verify_hdr(hdr
, size
) < 0)
367 active_nr
= ntohl(hdr
->hdr_entries
);
368 active_alloc
= alloc_nr(active_nr
);
369 active_cache
= calloc(active_alloc
, sizeof(struct cache_entry
*));
371 offset
= sizeof(*hdr
);
372 for (i
= 0; i
< active_nr
; i
++) {
373 struct cache_entry
*ce
= map
+ offset
;
374 offset
= offset
+ ce_size(ce
);
375 active_cache
[i
] = ce
;
382 return error("verify header failed");
385 #define WRITE_BUFFER_SIZE 8192
386 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
387 static unsigned long write_buffer_len
;
389 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
392 unsigned int buffered
= write_buffer_len
;
393 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
396 memcpy(write_buffer
+ buffered
, data
, partial
);
398 if (buffered
== WRITE_BUFFER_SIZE
) {
399 SHA1_Update(context
, write_buffer
, WRITE_BUFFER_SIZE
);
400 if (write(fd
, write_buffer
, WRITE_BUFFER_SIZE
) != WRITE_BUFFER_SIZE
)
404 write_buffer_len
= buffered
;
411 static int ce_flush(SHA_CTX
*context
, int fd
)
413 unsigned int left
= write_buffer_len
;
416 write_buffer_len
= 0;
417 SHA1_Update(context
, write_buffer
, left
);
420 /* Append the SHA1 signature at the end */
421 SHA1_Final(write_buffer
+ left
, context
);
423 if (write(fd
, write_buffer
, left
) != left
)
428 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
431 struct cache_header hdr
;
434 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
435 hdr
.hdr_version
= htonl(2);
436 hdr
.hdr_entries
= htonl(entries
);
439 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
442 for (i
= 0; i
< entries
; i
++) {
443 struct cache_entry
*ce
= cache
[i
];
444 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
447 return ce_flush(&c
, newfd
);