2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 struct cache_entry
**active_cache
= NULL
;
9 unsigned int active_nr
= 0, active_alloc
= 0, active_cache_changed
= 0;
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.
16 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
18 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
19 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
21 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
22 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
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
);
31 int ce_match_stat(struct cache_entry
*ce
, struct stat
*st
)
33 unsigned int changed
= 0;
35 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
37 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
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
;
43 changed
|= !S_ISLNK(st
->st_mode
) ? TYPE_CHANGED
: 0;
46 die("internal error: ce_mode is %o", ntohl(ce
->ce_mode
));
48 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
49 changed
|= MTIME_CHANGED
;
50 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
51 changed
|= CTIME_CHANGED
;
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.
59 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
60 changed
|= MTIME_CHANGED
;
61 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
62 changed
|= CTIME_CHANGED
;
65 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
66 ce
->ce_gid
!= htonl(st
->st_gid
))
67 changed
|= OWNER_CHANGED
;
68 if (ce
->ce_ino
!= htonl(st
->st_ino
))
69 changed
|= INODE_CHANGED
;
73 * st_dev breaks on network filesystems where different
74 * clients will have different views of what "device"
75 * the filesystem is on
77 if (ce
->ce_dev
!= htonl(st
->st_dev
))
78 changed
|= INODE_CHANGED
;
81 if (ce
->ce_size
!= htonl(st
->st_size
))
82 changed
|= DATA_CHANGED
;
86 int base_name_compare(const char *name1
, int len1
, int mode1
,
87 const char *name2
, int len2
, int mode2
)
90 int len
= len1
< len2
? len1
: len2
;
93 cmp
= memcmp(name1
, name2
, len
);
98 if (!c1
&& S_ISDIR(mode1
))
100 if (!c2
&& S_ISDIR(mode2
))
102 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
105 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
107 int len1
= flags1
& CE_NAMEMASK
;
108 int len2
= flags2
& CE_NAMEMASK
;
109 int len
= len1
< len2
? len1
: len2
;
112 cmp
= memcmp(name1
, name2
, len
);
126 int cache_name_pos(const char *name
, int namelen
)
132 while (last
> first
) {
133 int next
= (last
+ first
) >> 1;
134 struct cache_entry
*ce
= active_cache
[next
];
135 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
147 /* Remove entry, return true if there are more entries to go.. */
148 int remove_cache_entry_at(int pos
)
150 active_cache_changed
= 1;
152 if (pos
>= active_nr
)
154 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
158 int remove_file_from_cache(char *path
)
160 int pos
= cache_name_pos(path
, strlen(path
));
163 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
164 remove_cache_entry_at(pos
);
168 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
170 int len
= ce_namelen(a
);
171 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
175 * Do we have another file that has the beginning components being a
176 * proper superset of the name we're trying to add?
178 static int has_file_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
181 int len
= ce_namelen(ce
);
182 int stage
= ce_stage(ce
);
183 const char *name
= ce
->name
;
185 while (pos
< active_nr
) {
186 struct cache_entry
*p
= active_cache
[pos
++];
188 if (len
>= ce_namelen(p
))
190 if (memcmp(name
, p
->name
, len
))
192 if (ce_stage(p
) != stage
)
194 if (p
->name
[len
] != '/')
199 remove_cache_entry_at(--pos
);
205 * Do we have another file with a pathname that is a proper
206 * subset of the name we're trying to add?
208 static int has_dir_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
211 int stage
= ce_stage(ce
);
212 const char *name
= ce
->name
;
213 const char *slash
= name
+ ce_namelen(ce
);
221 if (slash
<= ce
->name
)
226 pos
= cache_name_pos(name
, ntohs(create_ce_flags(len
, stage
)));
231 remove_cache_entry_at(pos
);
236 * Trivial optimization: if we find an entry that
237 * already matches the sub-directory, then we know
238 * we're ok, and we can exit.
241 while (pos
< active_nr
) {
242 struct cache_entry
*p
= active_cache
[pos
];
243 if ((ce_namelen(p
) <= len
) ||
244 (p
->name
[len
] != '/') ||
245 memcmp(p
->name
, name
, len
))
246 break; /* not our subdirectory */
247 if (ce_stage(p
) == stage
)
248 /* p is at the same stage as our entry, and
249 * is a subdirectory of what we are looking
250 * at, so we cannot have conflicts at our
251 * level or anything shorter.
260 /* We may be in a situation where we already have path/file and path
261 * is being added, or we already have path and path/file is being
262 * added. Either one would result in a nonsense tree that has path
263 * twice when git-write-tree tries to write it out. Prevent it.
265 * If ok-to-replace is specified, we remove the conflicting entries
266 * from the cache so the caller should recompute the insert position.
267 * When this happens, we return non-zero.
269 static int check_file_directory_conflict(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
272 * We check if the path is a sub-path of a subsequent pathname
273 * first, since removing those will not change the position
276 int retval
= has_file_name(ce
, pos
, ok_to_replace
);
278 * Then check if the path might have a clashing sub-directory
281 return retval
+ has_dir_name(ce
, pos
, ok_to_replace
);
284 int add_cache_entry(struct cache_entry
*ce
, int option
)
287 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
288 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
289 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
290 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
292 /* existing match? Just replace it */
294 active_cache_changed
= 1;
295 active_cache
[pos
] = ce
;
301 * Inserting a merged entry ("stage 0") into the index
302 * will always replace all non-merged entries..
304 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
305 while (ce_same_name(active_cache
[pos
], ce
)) {
307 if (!remove_cache_entry_at(pos
))
315 if (!skip_df_check
&& check_file_directory_conflict(ce
, pos
, ok_to_replace
)) {
318 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
322 /* Make sure the array is big enough .. */
323 if (active_nr
== active_alloc
) {
324 active_alloc
= alloc_nr(active_alloc
);
325 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
331 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
332 active_cache
[pos
] = ce
;
333 active_cache_changed
= 1;
337 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
340 unsigned char sha1
[20];
342 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
343 return error("bad signature");
344 if (hdr
->hdr_version
!= htonl(2))
345 return error("bad index version");
347 SHA1_Update(&c
, hdr
, size
- 20);
348 SHA1_Final(sha1
, &c
);
349 if (memcmp(sha1
, (void *)hdr
+ size
- 20, 20))
350 return error("bad index file sha1 signature");
358 unsigned long size
, offset
;
360 struct cache_header
*hdr
;
364 return error("more than one cachefile");
366 fd
= open(get_index_file(), O_RDONLY
);
368 return (errno
== ENOENT
) ? 0 : error("open failed");
370 size
= 0; // avoid gcc warning
372 if (!fstat(fd
, &st
)) {
375 if (size
>= sizeof(struct cache_header
) + 20)
376 map
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
379 if (-1 == (int)(long)map
)
380 return error("mmap failed");
383 if (verify_hdr(hdr
, size
) < 0)
386 active_nr
= ntohl(hdr
->hdr_entries
);
387 active_alloc
= alloc_nr(active_nr
);
388 active_cache
= calloc(active_alloc
, sizeof(struct cache_entry
*));
390 offset
= sizeof(*hdr
);
391 for (i
= 0; i
< active_nr
; i
++) {
392 struct cache_entry
*ce
= map
+ offset
;
393 offset
= offset
+ ce_size(ce
);
394 active_cache
[i
] = ce
;
401 return error("verify header failed");
404 #define WRITE_BUFFER_SIZE 8192
405 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
406 static unsigned long write_buffer_len
;
408 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
411 unsigned int buffered
= write_buffer_len
;
412 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
415 memcpy(write_buffer
+ buffered
, data
, partial
);
417 if (buffered
== WRITE_BUFFER_SIZE
) {
418 SHA1_Update(context
, write_buffer
, WRITE_BUFFER_SIZE
);
419 if (write(fd
, write_buffer
, WRITE_BUFFER_SIZE
) != WRITE_BUFFER_SIZE
)
423 write_buffer_len
= buffered
;
430 static int ce_flush(SHA_CTX
*context
, int fd
)
432 unsigned int left
= write_buffer_len
;
435 write_buffer_len
= 0;
436 SHA1_Update(context
, write_buffer
, left
);
439 /* Append the SHA1 signature at the end */
440 SHA1_Final(write_buffer
+ left
, context
);
442 if (write(fd
, write_buffer
, left
) != left
)
447 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
450 struct cache_header hdr
;
453 for (i
= removed
= 0; i
< entries
; i
++)
454 if (!cache
[i
]->ce_mode
)
457 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
458 hdr
.hdr_version
= htonl(2);
459 hdr
.hdr_entries
= htonl(entries
- removed
);
462 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
465 for (i
= 0; i
< entries
; i
++) {
466 struct cache_entry
*ce
= cache
[i
];
469 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
472 return ce_flush(&c
, newfd
);