2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
14 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
15 #define O_NOATIME 01000000
21 static unsigned int sha1_file_open_flag
= O_NOATIME
;
23 static unsigned hexval(char c
)
25 if (c
>= '0' && c
<= '9')
27 if (c
>= 'a' && c
<= 'f')
29 if (c
>= 'A' && c
<= 'F')
34 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
37 for (i
= 0; i
< 20; i
++) {
38 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
47 int get_sha1_file(const char *path
, unsigned char *result
)
50 int fd
= open(path
, O_RDONLY
);
55 len
= read(fd
, buffer
, sizeof(buffer
));
59 return get_sha1_hex(buffer
, result
);
62 int get_sha1(const char *str
, unsigned char *sha1
)
64 static char pathname
[PATH_MAX
];
65 static const char *prefix
[] = {
76 if (!get_sha1_hex(str
, sha1
))
80 for (p
= prefix
; *p
; p
++) {
81 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s", gitdir
, *p
, str
);
82 if (!get_sha1_file(pathname
, sha1
))
89 char * sha1_to_hex(const unsigned char *sha1
)
91 static char buffer
[50];
92 static const char hex
[] = "0123456789abcdef";
96 for (i
= 0; i
< 20; i
++) {
97 unsigned int val
= *sha1
++;
98 *buf
++ = hex
[val
>> 4];
99 *buf
++ = hex
[val
& 0xf];
104 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
107 for (i
= 0; i
< 20; i
++) {
108 static char hex
[] = "0123456789abcdef";
109 unsigned int val
= sha1
[i
];
110 char *pos
= pathbuf
+ i
*2 + (i
> 0);
111 *pos
++ = hex
[val
>> 4];
112 *pos
= hex
[val
& 0xf];
117 * NOTE! This returns a statically allocated buffer, so you have to be
118 * careful about using it. Do a "strdup()" if you need to save the
121 * Also note that this returns the location for creating. Reading
122 * SHA1 file can happen from any alternate directory listed in the
123 * SHA1_FILE_DIRECTORIES environment variable if it is not found in
124 * the primary object database.
126 char *sha1_file_name(const unsigned char *sha1
)
128 static char *name
, *base
;
131 char *sha1_file_directory
= get_object_directory();
132 int len
= strlen(sha1_file_directory
);
133 base
= xmalloc(len
+ 60);
134 memcpy(base
, sha1_file_directory
, len
);
135 memset(base
+len
, 0, 60);
138 name
= base
+ len
+ 1;
140 fill_sha1_path(name
, sha1
);
144 static struct alternate_object_database
150 static void prepare_alt_odb(void)
154 const char *cp
, *last
;
156 const char *alt
= getenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
158 for (totlen
= pass
= 0; pass
< 2; pass
++) {
162 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
164 /* 43 = 40-byte + 2 '/' + terminating NUL */
165 int pfxlen
= cp
- last
;
166 int entlen
= pfxlen
+ 43;
170 alt_odb
[i
].base
= op
;
171 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
172 memcpy(op
, last
, pfxlen
);
173 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
179 while (*cp
&& *cp
== ':')
185 alt_odb
= buf
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
186 alt_odb
[i
].base
= alt_odb
[i
].name
= 0;
187 op
= (char*)(&alt_odb
[i
+1]);
191 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
194 char *name
= sha1_file_name(sha1
);
200 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
201 fill_sha1_path(name
, sha1
);
202 if (!stat(alt_odb
[i
].base
, st
))
203 return alt_odb
[i
].base
;
208 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
211 unsigned char real_sha1
[20];
215 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
216 SHA1_Update(&c
, map
, size
);
217 SHA1_Final(real_sha1
, &c
);
218 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
221 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
226 char *filename
= find_sha1_file(sha1
, &st
);
229 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
233 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
235 /* See if it works without O_NOATIME */
236 switch (sha1_file_open_flag
) {
238 fd
= open(filename
, O_RDONLY
);
247 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
248 sha1_file_open_flag
= 0;
250 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
252 if (-1 == (int)(long)map
)
258 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
265 /* Get the data stream */
266 memset(&stream
, 0, sizeof(stream
));
267 stream
.next_in
= map
;
268 stream
.avail_in
= mapsize
;
269 stream
.next_out
= buffer
;
270 stream
.avail_out
= sizeof(buffer
);
272 inflateInit(&stream
);
273 ret
= inflate(&stream
, 0);
276 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
279 bytes
= strlen(buffer
) + 1;
280 buf
= xmalloc(*size
);
282 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
283 bytes
= stream
.total_out
- bytes
;
284 if (bytes
< *size
&& ret
== Z_OK
) {
285 stream
.next_out
= buf
+ bytes
;
286 stream
.avail_out
= *size
- bytes
;
287 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
294 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
296 unsigned long mapsize
;
299 map
= map_sha1_file(sha1
, &mapsize
);
301 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
302 munmap(map
, mapsize
);
308 void *read_object_with_reference(const unsigned char *sha1
,
309 const unsigned char *required_type
,
311 unsigned char *actual_sha1_return
)
316 unsigned char actual_sha1
[20];
318 memcpy(actual_sha1
, sha1
, 20);
321 const char *ref_type
= NULL
;
323 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
326 if (!strcmp(type
, required_type
)) {
328 if (actual_sha1_return
)
329 memcpy(actual_sha1_return
, actual_sha1
, 20);
332 /* Handle references */
333 else if (!strcmp(type
, "commit"))
335 else if (!strcmp(type
, "tag"))
336 ref_type
= "object ";
341 ref_length
= strlen(ref_type
);
343 if (memcmp(buffer
, ref_type
, ref_length
) ||
344 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
348 /* Now we have the ID of the referred-to object in
349 * actual_sha1. Check again. */
353 int write_sha1_file(char *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
358 unsigned char sha1
[20];
361 static char tmpfile
[PATH_MAX
];
365 /* Generate the header */
366 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
370 SHA1_Update(&c
, hdr
, hdrlen
);
371 SHA1_Update(&c
, buf
, len
);
372 SHA1_Final(sha1
, &c
);
375 memcpy(returnsha1
, sha1
, 20);
377 filename
= sha1_file_name(sha1
);
378 fd
= open(filename
, O_RDONLY
);
381 * FIXME!!! We might do collision checking here, but we'd
382 * need to uncompress the old file and check it. Later.
388 if (errno
!= ENOENT
) {
389 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
393 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
395 fd
= mkstemp(tmpfile
);
397 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
402 memset(&stream
, 0, sizeof(stream
));
403 deflateInit(&stream
, Z_BEST_COMPRESSION
);
404 size
= deflateBound(&stream
, len
+hdrlen
);
405 compressed
= xmalloc(size
);
408 stream
.next_out
= compressed
;
409 stream
.avail_out
= size
;
412 stream
.next_in
= hdr
;
413 stream
.avail_in
= hdrlen
;
414 while (deflate(&stream
, 0) == Z_OK
)
417 /* Then the data itself.. */
418 stream
.next_in
= buf
;
419 stream
.avail_in
= len
;
420 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
423 size
= stream
.total_out
;
425 if (write(fd
, compressed
, size
) != size
)
426 die("unable to write file");
431 ret
= link(tmpfile
, filename
);
436 * Coda hack - coda doesn't like cross-directory links,
437 * so we fall back to a rename, which will mean that it
438 * won't be able to check collisions, but that's not a
441 * When this succeeds, we just return 0. We have nothing
444 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
450 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
453 /* FIXME!!! Collision check here ? */
459 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
461 char *filename
= sha1_file_name(sha1
);
465 unsigned char real_sha1
[20];
471 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
474 return error("Couldn't open %s\n", filename
);
476 memset(&stream
, 0, sizeof(stream
));
478 inflateInit(&stream
);
484 size
= read(fd
, buf
, 4096);
489 return error("Connection closed?");
490 perror("Reading from connection");
493 write(local
, buf
, size
);
494 stream
.avail_in
= size
;
495 stream
.next_in
= buf
;
497 stream
.next_out
= discard
;
498 stream
.avail_out
= sizeof(discard
);
499 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
500 SHA1_Update(&c
, discard
, sizeof(discard
) -
502 } while (stream
.avail_in
&& ret
== Z_OK
);
504 } while (ret
== Z_OK
);
508 SHA1_Final(real_sha1
, &c
);
509 if (ret
!= Z_STREAM_END
) {
511 return error("File %s corrupted", sha1_to_hex(sha1
));
513 if (memcmp(sha1
, real_sha1
, 20)) {
515 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
521 int has_sha1_file(const unsigned char *sha1
)
524 return !!find_sha1_file(sha1
, &st
);
527 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
529 unsigned long size
= st
->st_size
;
535 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
537 if ((int)(long)buf
== -1)
540 ret
= write_sha1_file(buf
, size
, "blob", sha1
);