Code

d91e072f3e5480de3605fb3f1e581a938301bdfb
[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"
12 const char *sha1_file_directory = NULL;
14 #ifndef O_NOATIME
15 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
16 #define O_NOATIME 01000000
17 #else
18 #define O_NOATIME 0
19 #endif
20 #endif
22 static unsigned int sha1_file_open_flag = O_NOATIME;
24 static unsigned hexval(char c)
25 {
26         if (c >= '0' && c <= '9')
27                 return c - '0';
28         if (c >= 'a' && c <= 'f')
29                 return c - 'a' + 10;
30         if (c >= 'A' && c <= 'F')
31                 return c - 'A' + 10;
32         return ~0;
33 }
35 int get_sha1_hex(const char *hex, unsigned char *sha1)
36 {
37         int i;
38         for (i = 0; i < 20; i++) {
39                 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
40                 if (val & ~0xff)
41                         return -1;
42                 *sha1++ = val;
43                 hex += 2;
44         }
45         return 0;
46 }
48 int get_sha1_file(const char *path, unsigned char *result)
49 {
50         char buffer[60];
51         int fd = open(path, O_RDONLY);
52         int len;
54         if (fd < 0)
55                 return -1;
56         len = read(fd, buffer, sizeof(buffer));
57         close(fd);
58         if (len < 40)
59                 return -1;
60         return get_sha1_hex(buffer, result);
61 }
63 int get_sha1(const char *str, unsigned char *sha1)
64 {
65         static char pathname[PATH_MAX];
67         if (!get_sha1_hex(str, sha1))
68                 return 0;
69         if (!get_sha1_file(str, sha1))
70                 return 0;
71         snprintf(pathname, sizeof(pathname), ".git/%s", str);
72         if (!get_sha1_file(pathname, sha1))
73                 return 0;
74         return -1;
75 }
77 char * sha1_to_hex(const unsigned char *sha1)
78 {
79         static char buffer[50];
80         static const char hex[] = "0123456789abcdef";
81         char *buf = buffer;
82         int i;
84         for (i = 0; i < 20; i++) {
85                 unsigned int val = *sha1++;
86                 *buf++ = hex[val >> 4];
87                 *buf++ = hex[val & 0xf];
88         }
89         return buffer;
90 }
92 /*
93  * NOTE! This returns a statically allocated buffer, so you have to be
94  * careful about using it. Do a "strdup()" if you need to save the
95  * filename.
96  */
97 char *sha1_file_name(const unsigned char *sha1)
98 {
99         int i;
100         static char *name, *base;
102         if (!base) {
103                 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
104                 int len = strlen(sha1_file_directory);
105                 base = xmalloc(len + 60);
106                 memcpy(base, sha1_file_directory, len);
107                 memset(base+len, 0, 60);
108                 base[len] = '/';
109                 base[len+3] = '/';
110                 name = base + len + 1;
111         }
112         for (i = 0; i < 20; i++) {
113                 static char hex[] = "0123456789abcdef";
114                 unsigned int val = sha1[i];
115                 char *pos = name + i*2 + (i > 0);
116                 *pos++ = hex[val >> 4];
117                 *pos = hex[val & 0xf];
118         }
119         return base;
122 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
124         char header[100];
125         unsigned char real_sha1[20];
126         SHA_CTX c;
128         SHA1_Init(&c);
129         SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
130         SHA1_Update(&c, map, size);
131         SHA1_Final(real_sha1, &c);
132         return memcmp(sha1, real_sha1, 20) ? -1 : 0;
135 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
137         char *filename = sha1_file_name(sha1);
138         struct stat st;
139         void *map;
140         int fd;
142         fd = open(filename, O_RDONLY | sha1_file_open_flag);
143         if (fd < 0) {
144                 /* See if it works without O_NOATIME */
145                 switch (sha1_file_open_flag) {
146                 default:
147                         fd = open(filename, O_RDONLY);
148                         if (fd >= 0)
149                                 break;
150                 /* Fallthrough */
151                 case 0:
152                         perror(filename);
153                         return NULL;
154                 }
156                 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
157                 sha1_file_open_flag = 0;
158         }
159         if (fstat(fd, &st) < 0) {
160                 close(fd);
161                 return NULL;
162         }
163         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
164         close(fd);
165         if (-1 == (int)(long)map)
166                 return NULL;
167         *size = st.st_size;
168         return map;
171 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
173         int ret, bytes;
174         z_stream stream;
175         char buffer[8192];
176         char *buf;
178         /* Get the data stream */
179         memset(&stream, 0, sizeof(stream));
180         stream.next_in = map;
181         stream.avail_in = mapsize;
182         stream.next_out = buffer;
183         stream.avail_out = sizeof(buffer);
185         inflateInit(&stream);
186         ret = inflate(&stream, 0);
187         if (ret < Z_OK)
188                 return NULL;
189         if (sscanf(buffer, "%10s %lu", type, size) != 2)
190                 return NULL;
192         bytes = strlen(buffer) + 1;
193         buf = xmalloc(*size);
195         memcpy(buf, buffer + bytes, stream.total_out - bytes);
196         bytes = stream.total_out - bytes;
197         if (bytes < *size && ret == Z_OK) {
198                 stream.next_out = buf + bytes;
199                 stream.avail_out = *size - bytes;
200                 while (inflate(&stream, Z_FINISH) == Z_OK)
201                         /* nothing */;
202         }
203         inflateEnd(&stream);
204         return buf;
207 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
209         unsigned long mapsize;
210         void *map, *buf;
212         map = map_sha1_file(sha1, &mapsize);
213         if (map) {
214                 buf = unpack_sha1_file(map, mapsize, type, size);
215                 munmap(map, mapsize);
216                 return buf;
217         }
218         return NULL;
221 void *read_object_with_reference(const unsigned char *sha1,
222                                  const unsigned char *required_type,
223                                  unsigned long *size,
224                                  unsigned char *actual_sha1_return)
226         char type[20];
227         void *buffer;
228         unsigned long isize;
229         unsigned char actual_sha1[20];
231         memcpy(actual_sha1, sha1, 20);
232         while (1) {
233                 int ref_length = -1;
234                 const char *ref_type = NULL;
236                 buffer = read_sha1_file(actual_sha1, type, &isize);
237                 if (!buffer)
238                         return NULL;
239                 if (!strcmp(type, required_type)) {
240                         *size = isize;
241                         if (actual_sha1_return)
242                                 memcpy(actual_sha1_return, actual_sha1, 20);
243                         return buffer;
244                 }
245                 /* Handle references */
246                 else if (!strcmp(type, "commit"))
247                         ref_type = "tree ";
248                 else if (!strcmp(type, "tag"))
249                         ref_type = "object ";
250                 else {
251                         free(buffer);
252                         return NULL;
253                 }
254                 ref_length = strlen(ref_type);
256                 if (memcmp(buffer, ref_type, ref_length) ||
257                     get_sha1_hex(buffer + ref_length, actual_sha1)) {
258                         free(buffer);
259                         return NULL;
260                 }
261                 /* Now we have the ID of the referred-to object in
262                  * actual_sha1.  Check again. */
263         }
266 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
268         int size;
269         char *compressed;
270         z_stream stream;
271         unsigned char sha1[20];
272         SHA_CTX c;
273         char *filename;
274         char hdr[50];
275         int fd, hdrlen;
277         /* Generate the header */
278         hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
280         /* Sha1.. */
281         SHA1_Init(&c);
282         SHA1_Update(&c, hdr, hdrlen);
283         SHA1_Update(&c, buf, len);
284         SHA1_Final(sha1, &c);
286         if (returnsha1)
287                 memcpy(returnsha1, sha1, 20);
289         filename = sha1_file_name(sha1);
290         fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
291         if (fd < 0) {
292                 if (errno != EEXIST)
293                         return -1;
295                 /*
296                  * We might do collision checking here, but we'd need to
297                  * uncompress the old file and check it. Later.
298                  */
299                 return 0;
300         }
302         /* Set it up */
303         memset(&stream, 0, sizeof(stream));
304         deflateInit(&stream, Z_BEST_COMPRESSION);
305         size = deflateBound(&stream, len+hdrlen);
306         compressed = xmalloc(size);
308         /* Compress it */
309         stream.next_out = compressed;
310         stream.avail_out = size;
312         /* First header.. */
313         stream.next_in = hdr;
314         stream.avail_in = hdrlen;
315         while (deflate(&stream, 0) == Z_OK)
316                 /* nothing */
318         /* Then the data itself.. */
319         stream.next_in = buf;
320         stream.avail_in = len;
321         while (deflate(&stream, Z_FINISH) == Z_OK)
322                 /* nothing */;
323         deflateEnd(&stream);
324         size = stream.total_out;
326         if (write(fd, compressed, size) != size)
327                 die("unable to write file");
328         close(fd);
329                 
330         return 0;
333 static inline int collision_check(char *filename, void *buf, unsigned int size)
335 #ifdef COLLISION_CHECK
336         void *map;
337         int fd = open(filename, O_RDONLY);
338         struct stat st;
339         int cmp;
341         /* Unreadable object, or object went away? Strange. */
342         if (fd < 0)
343                 return -1;
345         if (fstat(fd, &st) < 0 || size != st.st_size)
346                 return -1;
348         map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
349         close(fd);
350         if (map == MAP_FAILED)
351                 return -1;
352         cmp = memcmp(buf, map, size);
353         munmap(map, size);
354         if (cmp)
355                 return -1;
356 #endif
357         return 0;
360 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
362         char *filename = sha1_file_name(sha1);
363         int fd;
365         fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
366         if (fd < 0) {
367                 if (errno != EEXIST)
368                         return -1;
369                 if (collision_check(filename, buf, size))
370                         return error("SHA1 collision detected!"
371                                         " This is bad, bad, BAD!\a\n");
372                 return 0;
373         }
374         write(fd, buf, size);
375         close(fd);
376         return 0;
379 int write_sha1_from_fd(const unsigned char *sha1, int fd)
381         char *filename = sha1_file_name(sha1);
383         int local;
384         z_stream stream;
385         unsigned char real_sha1[20];
386         char buf[4096];
387         char discard[4096];
388         int ret;
389         SHA_CTX c;
391         local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
393         if (local < 0)
394                 return error("Couldn't open %s\n", filename);
396         memset(&stream, 0, sizeof(stream));
398         inflateInit(&stream);
400         SHA1_Init(&c);
402         do {
403                 ssize_t size;
404                 size = read(fd, buf, 4096);
405                 if (size <= 0) {
406                         close(local);
407                         unlink(filename);
408                         if (!size)
409                                 return error("Connection closed?");
410                         perror("Reading from connection");
411                         return -1;
412                 }
413                 write(local, buf, size);
414                 stream.avail_in = size;
415                 stream.next_in = buf;
416                 do {
417                         stream.next_out = discard;
418                         stream.avail_out = sizeof(discard);
419                         ret = inflate(&stream, Z_SYNC_FLUSH);
420                         SHA1_Update(&c, discard, sizeof(discard) -
421                                     stream.avail_out);
422                 } while (stream.avail_in && ret == Z_OK);
423                 
424         } while (ret == Z_OK);
425         inflateEnd(&stream);
427         close(local);
428         SHA1_Final(real_sha1, &c);
429         if (ret != Z_STREAM_END) {
430                 unlink(filename);
431                 return error("File %s corrupted", sha1_to_hex(sha1));
432         }
433         if (memcmp(sha1, real_sha1, 20)) {
434                 unlink(filename);
435                 return error("File %s has bad hash\n", sha1_to_hex(sha1));
436         }
437         
438         return 0;
441 int has_sha1_file(const unsigned char *sha1)
443         char *filename = sha1_file_name(sha1);
444         struct stat st;
446         if (!stat(filename, &st))
447                 return 1;
448         return 0;