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];
104 * NOTE! This returns a statically allocated buffer, so you have to be
105 * careful about using it. Do a "strdup()" if you need to save the
108 char *sha1_file_name(const unsigned char *sha1
)
111 static char *name
, *base
;
114 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
115 int len
= strlen(sha1_file_directory
);
116 base
= xmalloc(len
+ 60);
117 memcpy(base
, sha1_file_directory
, len
);
118 memset(base
+len
, 0, 60);
121 name
= base
+ len
+ 1;
123 for (i
= 0; i
< 20; i
++) {
124 static char hex
[] = "0123456789abcdef";
125 unsigned int val
= sha1
[i
];
126 char *pos
= name
+ i
*2 + (i
> 0);
127 *pos
++ = hex
[val
>> 4];
128 *pos
= hex
[val
& 0xf];
133 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
136 unsigned char real_sha1
[20];
140 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
141 SHA1_Update(&c
, map
, size
);
142 SHA1_Final(real_sha1
, &c
);
143 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
146 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
148 char *filename
= sha1_file_name(sha1
);
153 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
155 /* See if it works without O_NOATIME */
156 switch (sha1_file_open_flag
) {
158 fd
= open(filename
, O_RDONLY
);
167 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
168 sha1_file_open_flag
= 0;
170 if (fstat(fd
, &st
) < 0) {
174 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
176 if (-1 == (int)(long)map
)
182 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
189 /* Get the data stream */
190 memset(&stream
, 0, sizeof(stream
));
191 stream
.next_in
= map
;
192 stream
.avail_in
= mapsize
;
193 stream
.next_out
= buffer
;
194 stream
.avail_out
= sizeof(buffer
);
196 inflateInit(&stream
);
197 ret
= inflate(&stream
, 0);
200 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
203 bytes
= strlen(buffer
) + 1;
204 buf
= xmalloc(*size
);
206 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
207 bytes
= stream
.total_out
- bytes
;
208 if (bytes
< *size
&& ret
== Z_OK
) {
209 stream
.next_out
= buf
+ bytes
;
210 stream
.avail_out
= *size
- bytes
;
211 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
218 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
220 unsigned long mapsize
;
223 map
= map_sha1_file(sha1
, &mapsize
);
225 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
226 munmap(map
, mapsize
);
232 void *read_object_with_reference(const unsigned char *sha1
,
233 const unsigned char *required_type
,
235 unsigned char *actual_sha1_return
)
240 unsigned char actual_sha1
[20];
242 memcpy(actual_sha1
, sha1
, 20);
245 const char *ref_type
= NULL
;
247 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
250 if (!strcmp(type
, required_type
)) {
252 if (actual_sha1_return
)
253 memcpy(actual_sha1_return
, actual_sha1
, 20);
256 /* Handle references */
257 else if (!strcmp(type
, "commit"))
259 else if (!strcmp(type
, "tag"))
260 ref_type
= "object ";
265 ref_length
= strlen(ref_type
);
267 if (memcmp(buffer
, ref_type
, ref_length
) ||
268 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
272 /* Now we have the ID of the referred-to object in
273 * actual_sha1. Check again. */
277 int write_sha1_file(char *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
282 unsigned char sha1
[20];
285 static char tmpfile
[PATH_MAX
];
289 /* Generate the header */
290 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
294 SHA1_Update(&c
, hdr
, hdrlen
);
295 SHA1_Update(&c
, buf
, len
);
296 SHA1_Final(sha1
, &c
);
299 memcpy(returnsha1
, sha1
, 20);
301 filename
= sha1_file_name(sha1
);
302 fd
= open(filename
, O_RDONLY
);
305 * FIXME!!! We might do collision checking here, but we'd
306 * need to uncompress the old file and check it. Later.
312 if (errno
!= ENOENT
) {
313 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
317 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
318 fd
= mkstemp(tmpfile
);
320 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
325 memset(&stream
, 0, sizeof(stream
));
326 deflateInit(&stream
, Z_BEST_COMPRESSION
);
327 size
= deflateBound(&stream
, len
+hdrlen
);
328 compressed
= xmalloc(size
);
331 stream
.next_out
= compressed
;
332 stream
.avail_out
= size
;
335 stream
.next_in
= hdr
;
336 stream
.avail_in
= hdrlen
;
337 while (deflate(&stream
, 0) == Z_OK
)
340 /* Then the data itself.. */
341 stream
.next_in
= buf
;
342 stream
.avail_in
= len
;
343 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
346 size
= stream
.total_out
;
348 if (write(fd
, compressed
, size
) != size
)
349 die("unable to write file");
353 ret
= link(tmpfile
, filename
);
358 * Coda hack - coda doesn't like cross-directory links,
359 * so we fall back to a rename, which will mean that it
360 * won't be able to check collisions, but that's not a
363 * When this succeeds, we just return 0. We have nothing
366 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
372 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
375 /* FIXME!!! Collision check here ? */
381 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
383 char *filename
= sha1_file_name(sha1
);
387 unsigned char real_sha1
[20];
393 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
396 return error("Couldn't open %s\n", filename
);
398 memset(&stream
, 0, sizeof(stream
));
400 inflateInit(&stream
);
406 size
= read(fd
, buf
, 4096);
411 return error("Connection closed?");
412 perror("Reading from connection");
415 write(local
, buf
, size
);
416 stream
.avail_in
= size
;
417 stream
.next_in
= buf
;
419 stream
.next_out
= discard
;
420 stream
.avail_out
= sizeof(discard
);
421 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
422 SHA1_Update(&c
, discard
, sizeof(discard
) -
424 } while (stream
.avail_in
&& ret
== Z_OK
);
426 } while (ret
== Z_OK
);
430 SHA1_Final(real_sha1
, &c
);
431 if (ret
!= Z_STREAM_END
) {
433 return error("File %s corrupted", sha1_to_hex(sha1
));
435 if (memcmp(sha1
, real_sha1
, 20)) {
437 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
443 int has_sha1_file(const unsigned char *sha1
)
445 char *filename
= sha1_file_name(sha1
);
448 if (!stat(filename
, &st
))
453 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
455 unsigned long size
= st
->st_size
;
461 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
463 if ((int)(long)buf
== -1)
466 ret
= write_sha1_file(buf
, size
, "blob", sha1
);