2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 const char *sha1_file_directory
= NULL
;
10 struct cache_entry
**active_cache
= NULL
;
11 unsigned int active_nr
= 0, active_alloc
= 0;
13 void usage(const char *err
)
15 fprintf(stderr
, "usage: %s\n", err
);
19 static void report(const char *prefix
, const char *err
, va_list params
)
21 fputs(prefix
, stderr
);
22 vfprintf(stderr
, err
, params
);
26 void die(const char *err
, ...)
30 va_start(params
, err
);
31 report("fatal: ", err
, params
);
36 int error(const char *err
, ...)
40 va_start(params
, err
);
41 report("error: ", err
, params
);
47 static unsigned hexval(char c
)
49 if (c
>= '0' && c
<= '9')
51 if (c
>= 'a' && c
<= 'f')
53 if (c
>= 'A' && c
<= 'F')
58 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
61 for (i
= 0; i
< 20; i
++) {
62 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
71 char * sha1_to_hex(const unsigned char *sha1
)
73 static char buffer
[50];
74 static const char hex
[] = "0123456789abcdef";
78 for (i
= 0; i
< 20; i
++) {
79 unsigned int val
= *sha1
++;
80 *buf
++ = hex
[val
>> 4];
81 *buf
++ = hex
[val
& 0xf];
87 * NOTE! This returns a statically allocated buffer, so you have to be
88 * careful about using it. Do a "strdup()" if you need to save the
91 char *sha1_file_name(const unsigned char *sha1
)
94 static char *name
, *base
;
97 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
98 int len
= strlen(sha1_file_directory
);
99 base
= malloc(len
+ 60);
100 memcpy(base
, sha1_file_directory
, len
);
101 memset(base
+len
, 0, 60);
104 name
= base
+ len
+ 1;
106 for (i
= 0; i
< 20; i
++) {
107 static char hex
[] = "0123456789abcdef";
108 unsigned int val
= sha1
[i
];
109 char *pos
= name
+ i
*2 + (i
> 0);
110 *pos
++ = hex
[val
>> 4];
111 *pos
= hex
[val
& 0xf];
116 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
)
118 unsigned char real_sha1
[20];
122 SHA1_Update(&c
, map
, size
);
123 SHA1_Final(real_sha1
, &c
);
124 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
127 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
129 char *filename
= sha1_file_name(sha1
);
130 int fd
= open(filename
, O_RDONLY
);
138 if (fstat(fd
, &st
) < 0) {
142 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
144 if (-1 == (int)(long)map
)
150 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
157 /* Get the data stream */
158 memset(&stream
, 0, sizeof(stream
));
159 stream
.next_in
= map
;
160 stream
.avail_in
= mapsize
;
161 stream
.next_out
= buffer
;
162 stream
.avail_out
= sizeof(buffer
);
164 inflateInit(&stream
);
165 ret
= inflate(&stream
, 0);
166 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
169 bytes
= strlen(buffer
) + 1;
174 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
175 bytes
= stream
.total_out
- bytes
;
176 if (bytes
< *size
&& ret
== Z_OK
) {
177 stream
.next_out
= buf
+ bytes
;
178 stream
.avail_out
= *size
- bytes
;
179 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
186 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
188 unsigned long mapsize
;
191 map
= map_sha1_file(sha1
, &mapsize
);
193 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
194 munmap(map
, mapsize
);
200 int write_sha1_file(char *buf
, unsigned len
, unsigned char *returnsha1
)
205 unsigned char sha1
[20];
209 memset(&stream
, 0, sizeof(stream
));
210 deflateInit(&stream
, Z_BEST_COMPRESSION
);
211 size
= deflateBound(&stream
, len
);
212 compressed
= malloc(size
);
215 stream
.next_in
= buf
;
216 stream
.avail_in
= len
;
217 stream
.next_out
= compressed
;
218 stream
.avail_out
= size
;
219 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
222 size
= stream
.total_out
;
226 SHA1_Update(&c
, compressed
, size
);
227 SHA1_Final(sha1
, &c
);
229 if (write_sha1_buffer(sha1
, compressed
, size
) < 0)
232 memcpy(returnsha1
, sha1
, 20);
236 static inline int collision_check(char *filename
, void *buf
, unsigned int size
)
238 #ifdef COLLISION_CHECK
240 int fd
= open(filename
, O_RDONLY
);
244 /* Unreadable object, or object went away? Strange. */
248 if (fstat(fd
, &st
) < 0 || size
!= st
.st_size
)
251 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
253 if (map
== MAP_FAILED
)
255 cmp
= memcmp(buf
, map
, size
);
263 int write_sha1_buffer(const unsigned char *sha1
, void *buf
, unsigned int size
)
265 char *filename
= sha1_file_name(sha1
);
268 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
272 if (collision_check(filename
, buf
, size
))
273 return error("SHA1 collision detected!"
274 " This is bad, bad, BAD!\a\n");
277 write(fd
, buf
, size
);
282 int cache_match_stat(struct cache_entry
*ce
, struct stat
*st
)
284 unsigned int changed
= 0;
286 /* nsec seems unreliable - not all filesystems support it, so
287 * as long as it is in the inode cache you get right nsec
288 * but after it gets flushed, you get zero nsec. */
289 if (ce
->mtime
.sec
!= (unsigned int)st
->st_mtim
.tv_sec
291 || ce
->mtime
.nsec
!= (unsigned int)st
->st_mtim
.tv_nsec
294 changed
|= MTIME_CHANGED
;
295 if (ce
->ctime
.sec
!= (unsigned int)st
->st_ctim
.tv_sec
297 || ce
->ctime
.nsec
!= (unsigned int)st
->st_ctim
.tv_nsec
300 changed
|= CTIME_CHANGED
;
301 if (ce
->st_uid
!= (unsigned int)st
->st_uid
||
302 ce
->st_gid
!= (unsigned int)st
->st_gid
)
303 changed
|= OWNER_CHANGED
;
304 if (ce
->st_mode
!= (unsigned int)st
->st_mode
)
305 changed
|= MODE_CHANGED
;
306 if (ce
->st_dev
!= (unsigned int)st
->st_dev
||
307 ce
->st_ino
!= (unsigned int)st
->st_ino
)
308 changed
|= INODE_CHANGED
;
309 if (ce
->st_size
!= (unsigned int)st
->st_size
)
310 changed
|= DATA_CHANGED
;
314 int cache_name_compare(const char *name1
, int len1
, const char *name2
, int len2
)
316 int len
= len1
< len2
? len1
: len2
;
319 cmp
= memcmp(name1
, name2
, len
);
329 int cache_name_pos(const char *name
, int namelen
)
335 while (last
> first
) {
336 int next
= (last
+ first
) >> 1;
337 struct cache_entry
*ce
= active_cache
[next
];
338 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ce
->namelen
);
350 int remove_file_from_cache(char *path
)
352 int pos
= cache_name_pos(path
, strlen(path
));
356 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
361 int add_cache_entry(struct cache_entry
*ce
, int ok_to_add
)
365 pos
= cache_name_pos(ce
->name
, ce
->namelen
);
367 /* existing match? Just replace it */
369 active_cache
[pos
] = ce
;
377 /* Make sure the array is big enough .. */
378 if (active_nr
== active_alloc
) {
379 active_alloc
= alloc_nr(active_alloc
);
380 active_cache
= realloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
386 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
387 active_cache
[pos
] = ce
;
391 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
394 unsigned char sha1
[20];
396 if (hdr
->signature
!= CACHE_SIGNATURE
)
397 return error("bad signature");
398 if (hdr
->version
!= 1)
399 return error("bad version");
401 SHA1_Update(&c
, hdr
, offsetof(struct cache_header
, sha1
));
402 SHA1_Update(&c
, hdr
+1, size
- sizeof(*hdr
));
403 SHA1_Final(sha1
, &c
);
404 if (memcmp(sha1
, hdr
->sha1
, 20))
405 return error("bad header sha1");
413 unsigned long size
, offset
;
415 struct cache_header
*hdr
;
419 return error("more than one cachefile");
421 sha1_file_directory
= getenv(DB_ENVIRONMENT
);
422 if (!sha1_file_directory
)
423 sha1_file_directory
= DEFAULT_DB_ENVIRONMENT
;
424 if (access(sha1_file_directory
, X_OK
) < 0)
425 return error("no access to SHA1 file directory");
426 fd
= open(".git/index", O_RDONLY
);
428 return (errno
== ENOENT
) ? 0 : error("open failed");
430 size
= 0; // avoid gcc warning
432 if (!fstat(fd
, &st
)) {
435 if (size
>= sizeof(struct cache_header
))
436 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
439 if (-1 == (int)(long)map
)
440 return error("mmap failed");
443 if (verify_hdr(hdr
, size
) < 0)
446 active_nr
= hdr
->entries
;
447 active_alloc
= alloc_nr(active_nr
);
448 active_cache
= calloc(active_alloc
, sizeof(struct cache_entry
*));
450 offset
= sizeof(*hdr
);
451 for (i
= 0; i
< hdr
->entries
; i
++) {
452 struct cache_entry
*ce
= map
+ offset
;
453 offset
= offset
+ ce_size(ce
);
454 active_cache
[i
] = ce
;
461 return error("verify header failed");
464 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
467 struct cache_header hdr
;
470 hdr
.signature
= CACHE_SIGNATURE
;
472 hdr
.entries
= entries
;
475 SHA1_Update(&c
, &hdr
, offsetof(struct cache_header
, sha1
));
476 for (i
= 0; i
< entries
; i
++) {
477 struct cache_entry
*ce
= cache
[i
];
478 int size
= ce_size(ce
);
479 SHA1_Update(&c
, ce
, size
);
481 SHA1_Final(hdr
.sha1
, &c
);
483 if (write(newfd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
486 for (i
= 0; i
< entries
; i
++) {
487 struct cache_entry
*ce
= cache
[i
];
488 int size
= ce_size(ce
);
489 if (write(newfd
, ce
, size
) != size
)