Introduce SHA1_FILE_DIRECTORIES to support multiple object databases.
[git.git] / sha1_file.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  *
6  * This handles basic git sha1 object files - packing, unpacking,
7  * creation etc.
8  */
9 #include <stdarg.h>
10 #include "cache.h"
11
12 #ifndef O_NOATIME
13 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
14 #define O_NOATIME 01000000
15 #else
16 #define O_NOATIME 0
17 #endif
18 #endif
19
20 static unsigned int sha1_file_open_flag = O_NOATIME;
21
22 static unsigned hexval(char c)
23 {
24         if (c >= '0' && c <= '9')
25                 return c - '0';
26         if (c >= 'a' && c <= 'f')
27                 return c - 'a' + 10;
28         if (c >= 'A' && c <= 'F')
29                 return c - 'A' + 10;
30         return ~0;
31 }
32
33 int get_sha1_hex(const char *hex, unsigned char *sha1)
34 {
35         int i;
36         for (i = 0; i < 20; i++) {
37                 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
38                 if (val & ~0xff)
39                         return -1;
40                 *sha1++ = val;
41                 hex += 2;
42         }
43         return 0;
44 }
45
46 int get_sha1_file(const char *path, unsigned char *result)
47 {
48         char buffer[60];
49         int fd = open(path, O_RDONLY);
50         int len;
51
52         if (fd < 0)
53                 return -1;
54         len = read(fd, buffer, sizeof(buffer));
55         close(fd);
56         if (len < 40)
57                 return -1;
58         return get_sha1_hex(buffer, result);
59 }
60
61 int get_sha1(const char *str, unsigned char *sha1)
62 {
63         static char pathname[PATH_MAX];
64         static const char *prefix[] = {
65                 "",
66                 "refs",
67                 "refs/tags",
68                 "refs/heads",
69                 "refs/snap",
70                 NULL
71         };
72         const char *gitdir;
73         const char **p;
74
75         if (!get_sha1_hex(str, sha1))
76                 return 0;
77
78         gitdir = ".git";
79         for (p = prefix; *p; p++) {
80                 snprintf(pathname, sizeof(pathname), "%s/%s/%s", gitdir, *p, str);
81                 if (!get_sha1_file(pathname, sha1))
82                         return 0;
83         }
84
85         return -1;
86 }
87
88 char * sha1_to_hex(const unsigned char *sha1)
89 {
90         static char buffer[50];
91         static const char hex[] = "0123456789abcdef";
92         char *buf = buffer;
93         int i;
94
95         for (i = 0; i < 20; i++) {
96                 unsigned int val = *sha1++;
97                 *buf++ = hex[val >> 4];
98                 *buf++ = hex[val & 0xf];
99         }
100         return buffer;
101 }
102
103 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
104 {
105         int i;
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];
112         }
113 }
114
115 /*
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
118  * filename.
119  *
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.
124  */
125 char *sha1_file_name(const unsigned char *sha1)
126 {
127         static char *name, *base;
128
129         if (!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);
135                 base[len] = '/';
136                 base[len+3] = '/';
137                 name = base + len + 1;
138         }
139         fill_sha1_path(name, sha1);
140         return base;
141 }
142
143 static struct alternate_object_database
144 {
145         char *base;
146         char *name;
147 } *alt_odb;
148
149 static void prepare_alt_odb(void)
150 {
151         int pass, totlen, i;
152         void *buf;
153         const char *cp, *last;
154         char *op = 0;
155         const char *alt = getenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
156
157         for (totlen = pass = 0; pass < 2; pass++) {
158                 last = alt;
159                 i = 0;
160                 do {
161                         cp = strchr(last, ':') ? : last + strlen(last);
162                         if (last != cp) {
163                                 /* 43 = 40-byte + 2 '/' + terminating NUL */
164                                 int pfxlen = cp - last;
165                                 int entlen = pfxlen + 43;
166                                 if (pass == 0)
167                                         totlen += entlen;
168                                 else {
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] = '/';
173                                         op[entlen-1] = 0;
174                                         op += entlen;
175                                 }
176                                 i++;
177                         }
178                         while (*cp && *cp == ':')
179                                 cp++;
180                         last = cp;
181                 } while (*cp);
182                 if (pass)
183                         break;
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]);
187         }
188 }
189
190 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
191 {
192         int i;
193         char *name = sha1_file_name(sha1);
194
195         if (!stat(name, st))
196                 return name;
197         if (!alt_odb)
198                 prepare_alt_odb();
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;
203         }
204         return NULL;
205 }
206
207 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
208 {
209         char header[100];
210         unsigned char real_sha1[20];
211         SHA_CTX c;
212
213         SHA1_Init(&c);
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;
218 }
219
220 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
221 {
222         struct stat st;
223         void *map;
224         int fd;
225         char *filename = find_sha1_file(sha1, &st);
226
227         if (!filename) {
228                 error("cannot map sha1 file %s", sha1_to_hex(sha1));
229                 return NULL;
230         }
231
232         fd = open(filename, O_RDONLY | sha1_file_open_flag);
233         if (fd < 0) {
234                 /* See if it works without O_NOATIME */
235                 switch (sha1_file_open_flag) {
236                 default:
237                         fd = open(filename, O_RDONLY);
238                         if (fd >= 0)
239                                 break;
240                 /* Fallthrough */
241                 case 0:
242                         perror(filename);
243                         return NULL;
244                 }
245
246                 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
247                 sha1_file_open_flag = 0;
248         }
249         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
250         close(fd);
251         if (-1 == (int)(long)map)
252                 return NULL;
253         *size = st.st_size;
254         return map;
255 }
256
257 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
258 {
259         int ret, bytes;
260         z_stream stream;
261         char buffer[8192];
262         char *buf;
263
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);
270
271         inflateInit(&stream);
272         ret = inflate(&stream, 0);
273         if (ret < Z_OK)
274                 return NULL;
275         if (sscanf(buffer, "%10s %lu", type, size) != 2)
276                 return NULL;
277
278         bytes = strlen(buffer) + 1;
279         buf = xmalloc(*size);
280
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)
287                         /* nothing */;
288         }
289         inflateEnd(&stream);
290         return buf;
291 }
292
293 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
294 {
295         unsigned long mapsize;
296         void *map, *buf;
297
298         map = map_sha1_file(sha1, &mapsize);
299         if (map) {
300                 buf = unpack_sha1_file(map, mapsize, type, size);
301                 munmap(map, mapsize);
302                 return buf;
303         }
304         return NULL;
305 }
306
307 void *read_object_with_reference(const unsigned char *sha1,
308                                  const unsigned char *required_type,
309                                  unsigned long *size,
310                                  unsigned char *actual_sha1_return)
311 {
312         char type[20];
313         void *buffer;
314         unsigned long isize;
315         unsigned char actual_sha1[20];
316
317         memcpy(actual_sha1, sha1, 20);
318         while (1) {
319                 int ref_length = -1;
320                 const char *ref_type = NULL;
321
322                 buffer = read_sha1_file(actual_sha1, type, &isize);
323                 if (!buffer)
324                         return NULL;
325                 if (!strcmp(type, required_type)) {
326                         *size = isize;
327                         if (actual_sha1_return)
328                                 memcpy(actual_sha1_return, actual_sha1, 20);
329                         return buffer;
330                 }
331                 /* Handle references */
332                 else if (!strcmp(type, "commit"))
333                         ref_type = "tree ";
334                 else if (!strcmp(type, "tag"))
335                         ref_type = "object ";
336                 else {
337                         free(buffer);
338                         return NULL;
339                 }
340                 ref_length = strlen(ref_type);
341
342                 if (memcmp(buffer, ref_type, ref_length) ||
343                     get_sha1_hex(buffer + ref_length, actual_sha1)) {
344                         free(buffer);
345                         return NULL;
346                 }
347                 /* Now we have the ID of the referred-to object in
348                  * actual_sha1.  Check again. */
349         }
350 }
351
352 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
353 {
354         int size;
355         char *compressed;
356         z_stream stream;
357         unsigned char sha1[20];
358         SHA_CTX c;
359         char *filename;
360         static char tmpfile[PATH_MAX];
361         char hdr[50];
362         int fd, hdrlen, ret;
363
364         /* Generate the header */
365         hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
366
367         /* Sha1.. */
368         SHA1_Init(&c);
369         SHA1_Update(&c, hdr, hdrlen);
370         SHA1_Update(&c, buf, len);
371         SHA1_Final(sha1, &c);
372
373         if (returnsha1)
374                 memcpy(returnsha1, sha1, 20);
375
376         filename = sha1_file_name(sha1);
377         fd = open(filename, O_RDONLY);
378         if (fd >= 0) {
379                 /*
380                  * FIXME!!! We might do collision checking here, but we'd
381                  * need to uncompress the old file and check it. Later.
382                  */
383                 close(fd);
384                 return 0;
385         }
386
387         if (errno != ENOENT) {
388                 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
389                 return -1;
390         }
391
392         snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
393
394         fd = mkstemp(tmpfile);
395         if (fd < 0) {
396                 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
397                 return -1;
398         }
399
400         /* Set it up */
401         memset(&stream, 0, sizeof(stream));
402         deflateInit(&stream, Z_BEST_COMPRESSION);
403         size = deflateBound(&stream, len+hdrlen);
404         compressed = xmalloc(size);
405
406         /* Compress it */
407         stream.next_out = compressed;
408         stream.avail_out = size;
409
410         /* First header.. */
411         stream.next_in = hdr;
412         stream.avail_in = hdrlen;
413         while (deflate(&stream, 0) == Z_OK)
414                 /* nothing */
415
416         /* Then the data itself.. */
417         stream.next_in = buf;
418         stream.avail_in = len;
419         while (deflate(&stream, Z_FINISH) == Z_OK)
420                 /* nothing */;
421         deflateEnd(&stream);
422         size = stream.total_out;
423
424         if (write(fd, compressed, size) != size)
425                 die("unable to write file");
426         fchmod(fd, 0444);
427         close(fd);
428
429         ret = link(tmpfile, filename);
430         if (ret < 0) {
431                 ret = errno;
432
433                 /*
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
437                  * big deal.
438                  *
439                  * When this succeeds, we just return 0. We have nothing
440                  * left to unlink.
441                  */
442                 if (ret == EXDEV && !rename(tmpfile, filename))
443                         return 0;
444         }
445         unlink(tmpfile);
446         if (ret) {
447                 if (ret != EEXIST) {
448                         fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
449                         return -1;
450                 }
451                 /* FIXME!!! Collision check here ? */
452         }
453
454         return 0;
455 }
456
457 int write_sha1_from_fd(const unsigned char *sha1, int fd)
458 {
459         char *filename = sha1_file_name(sha1);
460
461         int local;
462         z_stream stream;
463         unsigned char real_sha1[20];
464         char buf[4096];
465         char discard[4096];
466         int ret;
467         SHA_CTX c;
468
469         local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
470
471         if (local < 0)
472                 return error("Couldn't open %s\n", filename);
473
474         memset(&stream, 0, sizeof(stream));
475
476         inflateInit(&stream);
477
478         SHA1_Init(&c);
479
480         do {
481                 ssize_t size;
482                 size = read(fd, buf, 4096);
483                 if (size <= 0) {
484                         close(local);
485                         unlink(filename);
486                         if (!size)
487                                 return error("Connection closed?");
488                         perror("Reading from connection");
489                         return -1;
490                 }
491                 write(local, buf, size);
492                 stream.avail_in = size;
493                 stream.next_in = buf;
494                 do {
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) -
499                                     stream.avail_out);
500                 } while (stream.avail_in && ret == Z_OK);
501                 
502         } while (ret == Z_OK);
503         inflateEnd(&stream);
504
505         close(local);
506         SHA1_Final(real_sha1, &c);
507         if (ret != Z_STREAM_END) {
508                 unlink(filename);
509                 return error("File %s corrupted", sha1_to_hex(sha1));
510         }
511         if (memcmp(sha1, real_sha1, 20)) {
512                 unlink(filename);
513                 return error("File %s has bad hash\n", sha1_to_hex(sha1));
514         }
515         
516         return 0;
517 }
518
519 int has_sha1_file(const unsigned char *sha1)
520 {
521         struct stat st;
522         return !!find_sha1_file(sha1, &st);
523 }
524
525 int index_fd(unsigned char *sha1, int fd, struct stat *st)
526 {
527         unsigned long size = st->st_size;
528         void *buf;
529         int ret;
530
531         buf = "";
532         if (size)
533                 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
534         close(fd);
535         if ((int)(long)buf == -1)
536                 return -1;
537
538         ret = write_sha1_file(buf, size, "blob", sha1);
539         if (size)
540                 munmap(buf, size);
541         return ret;
542 }