2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 const char *sha1_file_directory
= NULL
;
9 struct cache_entry
**active_cache
= NULL
;
10 unsigned int active_nr
= 0, active_alloc
= 0;
12 void usage(const char *err
, ...)
17 vfprintf(stderr
, err
, args
);
22 static unsigned hexval(char c
)
24 if (c
>= '0' && c
<= '9')
26 if (c
>= 'a' && c
<= 'f')
28 if (c
>= 'A' && c
<= 'F')
33 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
36 for (i
= 0; i
< 20; i
++) {
37 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
46 char * sha1_to_hex(const unsigned char *sha1
)
48 static char buffer
[50];
49 static const char hex
[] = "0123456789abcdef";
53 for (i
= 0; i
< 20; i
++) {
54 unsigned int val
= *sha1
++;
55 *buf
++ = hex
[val
>> 4];
56 *buf
++ = hex
[val
& 0xf];
62 * NOTE! This returns a statically allocated buffer, so you have to be
63 * careful about using it. Do a "strdup()" if you need to save the
66 char *sha1_file_name(const unsigned char *sha1
)
69 static char *name
, *base
;
72 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
73 int len
= strlen(sha1_file_directory
);
74 base
= malloc(len
+ 60);
75 memcpy(base
, sha1_file_directory
, len
);
76 memset(base
+len
, 0, 60);
79 name
= base
+ len
+ 1;
81 for (i
= 0; i
< 20; i
++) {
82 static char hex
[] = "0123456789abcdef";
83 unsigned int val
= sha1
[i
];
84 char *pos
= name
+ i
*2 + (i
> 0);
85 *pos
++ = hex
[val
>> 4];
86 *pos
= hex
[val
& 0xf];
91 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
)
93 unsigned char real_sha1
[20];
97 SHA1_Update(&c
, map
, size
);
98 SHA1_Final(real_sha1
, &c
);
99 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
102 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
104 char *filename
= sha1_file_name(sha1
);
105 int fd
= open(filename
, O_RDONLY
);
113 if (fstat(fd
, &st
) < 0) {
117 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
119 if (-1 == (int)(long)map
)
125 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
132 /* Get the data stream */
133 memset(&stream
, 0, sizeof(stream
));
134 stream
.next_in
= map
;
135 stream
.avail_in
= mapsize
;
136 stream
.next_out
= buffer
;
137 stream
.avail_out
= sizeof(buffer
);
139 inflateInit(&stream
);
140 ret
= inflate(&stream
, 0);
141 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
144 bytes
= strlen(buffer
) + 1;
149 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
150 bytes
= stream
.total_out
- bytes
;
151 if (bytes
< *size
&& ret
== Z_OK
) {
152 stream
.next_out
= buf
+ bytes
;
153 stream
.avail_out
= *size
- bytes
;
154 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
161 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
163 unsigned long mapsize
;
166 map
= map_sha1_file(sha1
, &mapsize
);
168 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
169 munmap(map
, mapsize
);
175 int write_sha1_file(char *buf
, unsigned len
, unsigned char *returnsha1
)
180 unsigned char sha1
[20];
184 memset(&stream
, 0, sizeof(stream
));
185 deflateInit(&stream
, Z_BEST_COMPRESSION
);
186 size
= deflateBound(&stream
, len
);
187 compressed
= malloc(size
);
190 stream
.next_in
= buf
;
191 stream
.avail_in
= len
;
192 stream
.next_out
= compressed
;
193 stream
.avail_out
= size
;
194 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
197 size
= stream
.total_out
;
201 SHA1_Update(&c
, compressed
, size
);
202 SHA1_Final(sha1
, &c
);
204 if (write_sha1_buffer(sha1
, compressed
, size
) < 0)
207 memcpy(returnsha1
, sha1
, 20);
211 int write_sha1_buffer(const unsigned char *sha1
, void *buf
, unsigned int size
)
213 char *filename
= sha1_file_name(sha1
);
216 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
218 return (errno
== EEXIST
) ? 0 : -1;
219 write(fd
, buf
, size
);
224 static int error(const char * string
)
226 fprintf(stderr
, "error: %s\n", string
);
230 int cache_match_stat(struct cache_entry
*ce
, struct stat
*st
)
232 unsigned int changed
= 0;
234 if (ce
->mtime
.sec
!= (unsigned int)st
->st_mtim
.tv_sec
||
235 ce
->mtime
.nsec
!= (unsigned int)st
->st_mtim
.tv_nsec
)
236 changed
|= MTIME_CHANGED
;
237 if (ce
->ctime
.sec
!= (unsigned int)st
->st_ctim
.tv_sec
||
238 ce
->ctime
.nsec
!= (unsigned int)st
->st_ctim
.tv_nsec
)
239 changed
|= CTIME_CHANGED
;
240 if (ce
->st_uid
!= (unsigned int)st
->st_uid
||
241 ce
->st_gid
!= (unsigned int)st
->st_gid
)
242 changed
|= OWNER_CHANGED
;
243 if (ce
->st_mode
!= (unsigned int)st
->st_mode
)
244 changed
|= MODE_CHANGED
;
245 if (ce
->st_dev
!= (unsigned int)st
->st_dev
||
246 ce
->st_ino
!= (unsigned int)st
->st_ino
)
247 changed
|= INODE_CHANGED
;
248 if (ce
->st_size
!= (unsigned int)st
->st_size
)
249 changed
|= DATA_CHANGED
;
253 int cache_name_compare(const char *name1
, int len1
, const char *name2
, int len2
)
255 int len
= len1
< len2
? len1
: len2
;
258 cmp
= memcmp(name1
, name2
, len
);
268 int cache_name_pos(const char *name
, int namelen
)
274 while (last
> first
) {
275 int next
= (last
+ first
) >> 1;
276 struct cache_entry
*ce
= active_cache
[next
];
277 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ce
->namelen
);
289 int remove_file_from_cache(char *path
)
291 int pos
= cache_name_pos(path
, strlen(path
));
296 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
- 1) * sizeof(struct cache_entry
*));
301 int add_cache_entry(struct cache_entry
*ce
, int ok_to_add
)
305 pos
= cache_name_pos(ce
->name
, ce
->namelen
);
307 /* existing match? Just replace it */
309 active_cache
[-pos
-1] = ce
;
316 /* Make sure the array is big enough .. */
317 if (active_nr
== active_alloc
) {
318 active_alloc
= alloc_nr(active_alloc
);
319 active_cache
= realloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
325 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
326 active_cache
[pos
] = ce
;
330 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
333 unsigned char sha1
[20];
335 if (hdr
->signature
!= CACHE_SIGNATURE
)
336 return error("bad signature");
337 if (hdr
->version
!= 1)
338 return error("bad version");
340 SHA1_Update(&c
, hdr
, offsetof(struct cache_header
, sha1
));
341 SHA1_Update(&c
, hdr
+1, size
- sizeof(*hdr
));
342 SHA1_Final(sha1
, &c
);
343 if (memcmp(sha1
, hdr
->sha1
, 20))
344 return error("bad header sha1");
352 unsigned long size
, offset
;
354 struct cache_header
*hdr
;
358 return error("more than one cachefile");
360 sha1_file_directory
= getenv(DB_ENVIRONMENT
);
361 if (!sha1_file_directory
)
362 sha1_file_directory
= DEFAULT_DB_ENVIRONMENT
;
363 if (access(sha1_file_directory
, X_OK
) < 0)
364 return error("no access to SHA1 file directory");
365 fd
= open(".dircache/index", O_RDONLY
);
367 return (errno
== ENOENT
) ? 0 : error("open failed");
369 size
= 0; // avoid gcc warning
371 if (!fstat(fd
, &st
)) {
374 if (size
>= sizeof(struct cache_header
))
375 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
378 if (-1 == (int)(long)map
)
379 return error("mmap failed");
382 if (verify_hdr(hdr
, size
) < 0)
385 active_nr
= hdr
->entries
;
386 active_alloc
= alloc_nr(active_nr
);
387 active_cache
= calloc(active_alloc
, sizeof(struct cache_entry
*));
389 offset
= sizeof(*hdr
);
390 for (i
= 0; i
< hdr
->entries
; i
++) {
391 struct cache_entry
*ce
= map
+ offset
;
392 offset
= offset
+ ce_size(ce
);
393 active_cache
[i
] = ce
;
400 return error("verify header failed");
403 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
406 struct cache_header hdr
;
409 hdr
.signature
= CACHE_SIGNATURE
;
411 hdr
.entries
= entries
;
414 SHA1_Update(&c
, &hdr
, offsetof(struct cache_header
, sha1
));
415 for (i
= 0; i
< entries
; i
++) {
416 struct cache_entry
*ce
= cache
[i
];
417 int size
= ce_size(ce
);
418 SHA1_Update(&c
, ce
, size
);
420 SHA1_Final(hdr
.sha1
, &c
);
422 if (write(newfd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
425 for (i
= 0; i
< entries
; i
++) {
426 struct cache_entry
*ce
= cache
[i
];
427 int size
= ce_size(ce
);
428 if (write(newfd
, ce
, size
) != size
)