2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
13 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
14 #define O_NOATIME 01000000
20 static unsigned int sha1_file_open_flag
= O_NOATIME
;
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 int get_sha1_file(const char *path
, unsigned char *result
)
49 int fd
= open(path
, O_RDONLY
);
54 len
= read(fd
, buffer
, sizeof(buffer
));
58 return get_sha1_hex(buffer
, result
);
61 int get_sha1(const char *str
, unsigned char *sha1
)
63 static char pathname
[PATH_MAX
];
64 static const char *prefix
[] = {
75 if (!get_sha1_hex(str
, sha1
))
79 for (p
= prefix
; *p
; p
++) {
80 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s", gitdir
, *p
, str
);
81 if (!get_sha1_file(pathname
, sha1
))
88 char * sha1_to_hex(const unsigned char *sha1
)
90 static char buffer
[50];
91 static const char hex
[] = "0123456789abcdef";
95 for (i
= 0; i
< 20; i
++) {
96 unsigned int val
= *sha1
++;
97 *buf
++ = hex
[val
>> 4];
98 *buf
++ = hex
[val
& 0xf];
103 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
106 for (i
= 0; i
< 20; i
++) {
107 static char hex
[] = "0123456789abcdef";
108 unsigned int val
= sha1
[i
];
109 char *pos
= pathbuf
+ i
*2 + (i
> 0);
110 *pos
++ = hex
[val
>> 4];
111 *pos
= hex
[val
& 0xf];
116 * NOTE! This returns a statically allocated buffer, so you have to be
117 * careful about using it. Do a "strdup()" if you need to save the
120 * Also note that this returns the location for creating. Reading
121 * SHA1 file can happen from any alternate directory listed in the
122 * SHA1_FILE_DIRECTORIES environment variable if it is not found in
123 * the primary object database.
125 char *sha1_file_name(const unsigned char *sha1
)
127 static char *name
, *base
;
130 char *sha1_file_directory
= get_object_directory();
131 int len
= strlen(sha1_file_directory
);
132 base
= xmalloc(len
+ 60);
133 memcpy(base
, sha1_file_directory
, len
);
134 memset(base
+len
, 0, 60);
137 name
= base
+ len
+ 1;
139 fill_sha1_path(name
, sha1
);
143 static struct alternate_object_database
149 static void prepare_alt_odb(void)
153 const char *cp
, *last
;
155 const char *alt
= getenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
157 for (totlen
= pass
= 0; pass
< 2; pass
++) {
161 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
163 /* 43 = 40-byte + 2 '/' + terminating NUL */
164 int pfxlen
= cp
- last
;
165 int entlen
= pfxlen
+ 43;
169 alt_odb
[i
].base
= op
;
170 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
171 memcpy(op
, last
, pfxlen
);
172 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
178 while (*cp
&& *cp
== ':')
184 alt_odb
= buf
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
185 alt_odb
[i
].base
= alt_odb
[i
].name
= 0;
186 op
= (char*)(&alt_odb
[i
+1]);
190 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
193 char *name
= sha1_file_name(sha1
);
199 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
200 fill_sha1_path(name
, sha1
);
201 if (!stat(alt_odb
[i
].base
, st
))
202 return alt_odb
[i
].base
;
207 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
210 unsigned char real_sha1
[20];
214 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
215 SHA1_Update(&c
, map
, size
);
216 SHA1_Final(real_sha1
, &c
);
217 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
220 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
225 char *filename
= find_sha1_file(sha1
, &st
);
228 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
232 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
234 /* See if it works without O_NOATIME */
235 switch (sha1_file_open_flag
) {
237 fd
= open(filename
, O_RDONLY
);
246 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
247 sha1_file_open_flag
= 0;
249 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
251 if (-1 == (int)(long)map
)
257 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
264 /* Get the data stream */
265 memset(&stream
, 0, sizeof(stream
));
266 stream
.next_in
= map
;
267 stream
.avail_in
= mapsize
;
268 stream
.next_out
= buffer
;
269 stream
.avail_out
= sizeof(buffer
);
271 inflateInit(&stream
);
272 ret
= inflate(&stream
, 0);
275 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
278 bytes
= strlen(buffer
) + 1;
279 buf
= xmalloc(*size
);
281 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
282 bytes
= stream
.total_out
- bytes
;
283 if (bytes
< *size
&& ret
== Z_OK
) {
284 stream
.next_out
= buf
+ bytes
;
285 stream
.avail_out
= *size
- bytes
;
286 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
293 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
295 unsigned long mapsize
;
298 map
= map_sha1_file(sha1
, &mapsize
);
300 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
301 munmap(map
, mapsize
);
307 void *read_object_with_reference(const unsigned char *sha1
,
308 const unsigned char *required_type
,
310 unsigned char *actual_sha1_return
)
315 unsigned char actual_sha1
[20];
317 memcpy(actual_sha1
, sha1
, 20);
320 const char *ref_type
= NULL
;
322 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
325 if (!strcmp(type
, required_type
)) {
327 if (actual_sha1_return
)
328 memcpy(actual_sha1_return
, actual_sha1
, 20);
331 /* Handle references */
332 else if (!strcmp(type
, "commit"))
334 else if (!strcmp(type
, "tag"))
335 ref_type
= "object ";
340 ref_length
= strlen(ref_type
);
342 if (memcmp(buffer
, ref_type
, ref_length
) ||
343 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
347 /* Now we have the ID of the referred-to object in
348 * actual_sha1. Check again. */
352 int write_sha1_file(char *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
357 unsigned char sha1
[20];
360 static char tmpfile
[PATH_MAX
];
364 /* Generate the header */
365 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
369 SHA1_Update(&c
, hdr
, hdrlen
);
370 SHA1_Update(&c
, buf
, len
);
371 SHA1_Final(sha1
, &c
);
374 memcpy(returnsha1
, sha1
, 20);
376 filename
= sha1_file_name(sha1
);
377 fd
= open(filename
, O_RDONLY
);
380 * FIXME!!! We might do collision checking here, but we'd
381 * need to uncompress the old file and check it. Later.
387 if (errno
!= ENOENT
) {
388 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
392 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
394 fd
= mkstemp(tmpfile
);
396 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
401 memset(&stream
, 0, sizeof(stream
));
402 deflateInit(&stream
, Z_BEST_COMPRESSION
);
403 size
= deflateBound(&stream
, len
+hdrlen
);
404 compressed
= xmalloc(size
);
407 stream
.next_out
= compressed
;
408 stream
.avail_out
= size
;
411 stream
.next_in
= hdr
;
412 stream
.avail_in
= hdrlen
;
413 while (deflate(&stream
, 0) == Z_OK
)
416 /* Then the data itself.. */
417 stream
.next_in
= buf
;
418 stream
.avail_in
= len
;
419 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
422 size
= stream
.total_out
;
424 if (write(fd
, compressed
, size
) != size
)
425 die("unable to write file");
429 ret
= link(tmpfile
, filename
);
434 * Coda hack - coda doesn't like cross-directory links,
435 * so we fall back to a rename, which will mean that it
436 * won't be able to check collisions, but that's not a
439 * When this succeeds, we just return 0. We have nothing
442 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
448 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
451 /* FIXME!!! Collision check here ? */
457 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
459 char *filename
= sha1_file_name(sha1
);
463 unsigned char real_sha1
[20];
469 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
472 return error("Couldn't open %s\n", filename
);
474 memset(&stream
, 0, sizeof(stream
));
476 inflateInit(&stream
);
482 size
= read(fd
, buf
, 4096);
487 return error("Connection closed?");
488 perror("Reading from connection");
491 write(local
, buf
, size
);
492 stream
.avail_in
= size
;
493 stream
.next_in
= buf
;
495 stream
.next_out
= discard
;
496 stream
.avail_out
= sizeof(discard
);
497 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
498 SHA1_Update(&c
, discard
, sizeof(discard
) -
500 } while (stream
.avail_in
&& ret
== Z_OK
);
502 } while (ret
== Z_OK
);
506 SHA1_Final(real_sha1
, &c
);
507 if (ret
!= Z_STREAM_END
) {
509 return error("File %s corrupted", sha1_to_hex(sha1
));
511 if (memcmp(sha1
, real_sha1
, 20)) {
513 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
519 int has_sha1_file(const unsigned char *sha1
)
522 return !!find_sha1_file(sha1
, &st
);
525 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
527 unsigned long size
= st
->st_size
;
533 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
535 if ((int)(long)buf
== -1)
538 ret
= write_sha1_file(buf
, size
, "blob", sha1
);