[PATCH] Lift path length limits from git-tar-tree.
[git/git.git] / sha1_file.c
CommitLineData
0fcfd160
LT
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
144bde78
LT
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
20static unsigned int sha1_file_open_flag = O_NOATIME;
21
0fcfd160
LT
22static 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
33int 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
3c249c95
LT
46int 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
61int get_sha1(const char *str, unsigned char *sha1)
62{
63 static char pathname[PATH_MAX];
35ad3382
LT
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;
3c249c95
LT
74
75 if (!get_sha1_hex(str, sha1))
76 return 0;
35ad3382 77
35ad3382
LT
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
3c249c95
LT
85 return -1;
86}
87
0fcfd160
LT
88char * 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/*
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
106 * filename.
107 */
108char *sha1_file_name(const unsigned char *sha1)
109{
110 int i;
111 static char *name, *base;
112
113 if (!base) {
114 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
115 int len = strlen(sha1_file_directory);
812666c8 116 base = xmalloc(len + 60);
0fcfd160
LT
117 memcpy(base, sha1_file_directory, len);
118 memset(base+len, 0, 60);
119 base[len] = '/';
120 base[len+3] = '/';
121 name = base + len + 1;
122 }
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];
129 }
130 return base;
131}
132
d98b46f8 133int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
0fcfd160 134{
d98b46f8 135 char header[100];
0fcfd160
LT
136 unsigned char real_sha1[20];
137 SHA_CTX c;
138
139 SHA1_Init(&c);
d98b46f8 140 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
0fcfd160
LT
141 SHA1_Update(&c, map, size);
142 SHA1_Final(real_sha1, &c);
143 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
144}
145
146void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
147{
148 char *filename = sha1_file_name(sha1);
0fcfd160
LT
149 struct stat st;
150 void *map;
144bde78 151 int fd;
0fcfd160 152
144bde78 153 fd = open(filename, O_RDONLY | sha1_file_open_flag);
0fcfd160 154 if (fd < 0) {
144bde78
LT
155 /* See if it works without O_NOATIME */
156 switch (sha1_file_open_flag) {
157 default:
158 fd = open(filename, O_RDONLY);
159 if (fd >= 0)
160 break;
161 /* Fallthrough */
162 case 0:
163 perror(filename);
164 return NULL;
165 }
166
167 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
168 sha1_file_open_flag = 0;
0fcfd160
LT
169 }
170 if (fstat(fd, &st) < 0) {
171 close(fd);
172 return NULL;
173 }
174 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
175 close(fd);
176 if (-1 == (int)(long)map)
177 return NULL;
178 *size = st.st_size;
179 return map;
180}
181
182void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
183{
184 int ret, bytes;
185 z_stream stream;
186 char buffer[8192];
187 char *buf;
188
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);
195
196 inflateInit(&stream);
197 ret = inflate(&stream, 0);
a44c9a5e
LT
198 if (ret < Z_OK)
199 return NULL;
0fcfd160
LT
200 if (sscanf(buffer, "%10s %lu", type, size) != 2)
201 return NULL;
202
203 bytes = strlen(buffer) + 1;
812666c8 204 buf = xmalloc(*size);
0fcfd160
LT
205
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)
212 /* nothing */;
213 }
214 inflateEnd(&stream);
215 return buf;
216}
217
218void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
219{
220 unsigned long mapsize;
221 void *map, *buf;
222
223 map = map_sha1_file(sha1, &mapsize);
224 if (map) {
225 buf = unpack_sha1_file(map, mapsize, type, size);
226 munmap(map, mapsize);
227 return buf;
228 }
229 return NULL;
230}
231
40469ee9
JH
232void *read_object_with_reference(const unsigned char *sha1,
233 const unsigned char *required_type,
234 unsigned long *size,
235 unsigned char *actual_sha1_return)
f4913f91
JH
236{
237 char type[20];
238 void *buffer;
239 unsigned long isize;
40469ee9 240 unsigned char actual_sha1[20];
f4913f91 241
40469ee9
JH
242 memcpy(actual_sha1, sha1, 20);
243 while (1) {
244 int ref_length = -1;
245 const char *ref_type = NULL;
f4913f91 246
40469ee9
JH
247 buffer = read_sha1_file(actual_sha1, type, &isize);
248 if (!buffer)
249 return NULL;
250 if (!strcmp(type, required_type)) {
251 *size = isize;
252 if (actual_sha1_return)
253 memcpy(actual_sha1_return, actual_sha1, 20);
254 return buffer;
255 }
256 /* Handle references */
257 else if (!strcmp(type, "commit"))
258 ref_type = "tree ";
259 else if (!strcmp(type, "tag"))
260 ref_type = "object ";
261 else {
262 free(buffer);
263 return NULL;
264 }
265 ref_length = strlen(ref_type);
f4913f91 266
40469ee9
JH
267 if (memcmp(buffer, ref_type, ref_length) ||
268 get_sha1_hex(buffer + ref_length, actual_sha1)) {
269 free(buffer);
270 return NULL;
271 }
272 /* Now we have the ID of the referred-to object in
273 * actual_sha1. Check again. */
f4913f91 274 }
f4913f91
JH
275}
276
f5b913c9 277int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
0fcfd160
LT
278{
279 int size;
280 char *compressed;
281 z_stream stream;
282 unsigned char sha1[20];
283 SHA_CTX c;
706bc531 284 char *filename;
aac17941 285 static char tmpfile[PATH_MAX];
a44c9a5e 286 char hdr[50];
aac17941 287 int fd, hdrlen, ret;
a44c9a5e
LT
288
289 /* Generate the header */
f5b913c9 290 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
0fcfd160 291
d98b46f8
LT
292 /* Sha1.. */
293 SHA1_Init(&c);
a44c9a5e 294 SHA1_Update(&c, hdr, hdrlen);
d98b46f8
LT
295 SHA1_Update(&c, buf, len);
296 SHA1_Final(sha1, &c);
297
706bc531
LT
298 if (returnsha1)
299 memcpy(returnsha1, sha1, 20);
300
301 filename = sha1_file_name(sha1);
aac17941
LT
302 fd = open(filename, O_RDONLY);
303 if (fd >= 0) {
706bc531 304 /*
aac17941
LT
305 * FIXME!!! We might do collision checking here, but we'd
306 * need to uncompress the old file and check it. Later.
706bc531 307 */
aac17941 308 close(fd);
706bc531
LT
309 return 0;
310 }
311
aac17941
LT
312 if (errno != ENOENT) {
313 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
314 return -1;
315 }
316
317 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
318 fd = mkstemp(tmpfile);
319 if (fd < 0) {
320 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
321 return -1;
322 }
323
0fcfd160
LT
324 /* Set it up */
325 memset(&stream, 0, sizeof(stream));
326 deflateInit(&stream, Z_BEST_COMPRESSION);
a44c9a5e 327 size = deflateBound(&stream, len+hdrlen);
812666c8 328 compressed = xmalloc(size);
0fcfd160
LT
329
330 /* Compress it */
0fcfd160
LT
331 stream.next_out = compressed;
332 stream.avail_out = size;
a44c9a5e
LT
333
334 /* First header.. */
335 stream.next_in = hdr;
336 stream.avail_in = hdrlen;
337 while (deflate(&stream, 0) == Z_OK)
338 /* nothing */
339
340 /* Then the data itself.. */
341 stream.next_in = buf;
342 stream.avail_in = len;
0fcfd160
LT
343 while (deflate(&stream, Z_FINISH) == Z_OK)
344 /* nothing */;
345 deflateEnd(&stream);
346 size = stream.total_out;
347
706bc531
LT
348 if (write(fd, compressed, size) != size)
349 die("unable to write file");
aac17941 350 fchmod(fd, 0444);
706bc531 351 close(fd);
0fcfd160 352
aac17941 353 ret = link(tmpfile, filename);
a31c6d02 354 if (ret < 0) {
aac17941 355 ret = errno;
a31c6d02
LT
356
357 /*
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
361 * big deal.
362 *
363 * When this succeeds, we just return 0. We have nothing
364 * left to unlink.
365 */
366 if (ret == EXDEV && !rename(tmpfile, filename))
367 return 0;
368 }
aac17941
LT
369 unlink(tmpfile);
370 if (ret) {
371 if (ret != EEXIST) {
372 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
0fcfd160 373 return -1;
aac17941
LT
374 }
375 /* FIXME!!! Collision check here ? */
0fcfd160 376 }
aac17941 377
0fcfd160
LT
378 return 0;
379}
8237b185
DB
380
381int write_sha1_from_fd(const unsigned char *sha1, int fd)
382{
383 char *filename = sha1_file_name(sha1);
384
385 int local;
386 z_stream stream;
387 unsigned char real_sha1[20];
388 char buf[4096];
389 char discard[4096];
390 int ret;
391 SHA_CTX c;
392
393 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
394
395 if (local < 0)
396 return error("Couldn't open %s\n", filename);
397
398 memset(&stream, 0, sizeof(stream));
399
400 inflateInit(&stream);
401
402 SHA1_Init(&c);
403
404 do {
405 ssize_t size;
406 size = read(fd, buf, 4096);
407 if (size <= 0) {
408 close(local);
409 unlink(filename);
410 if (!size)
411 return error("Connection closed?");
412 perror("Reading from connection");
413 return -1;
414 }
415 write(local, buf, size);
416 stream.avail_in = size;
417 stream.next_in = buf;
418 do {
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) -
423 stream.avail_out);
424 } while (stream.avail_in && ret == Z_OK);
425
426 } while (ret == Z_OK);
427 inflateEnd(&stream);
428
429 close(local);
430 SHA1_Final(real_sha1, &c);
431 if (ret != Z_STREAM_END) {
432 unlink(filename);
433 return error("File %s corrupted", sha1_to_hex(sha1));
434 }
435 if (memcmp(sha1, real_sha1, 20)) {
436 unlink(filename);
437 return error("File %s has bad hash\n", sha1_to_hex(sha1));
438 }
439
440 return 0;
441}
442
443int has_sha1_file(const unsigned char *sha1)
444{
445 char *filename = sha1_file_name(sha1);
446 struct stat st;
447
448 if (!stat(filename, &st))
449 return 1;
450 return 0;
451}
74400e71
JH
452
453int index_fd(unsigned char *sha1, int fd, struct stat *st)
454{
74400e71 455 unsigned long size = st->st_size;
aac17941
LT
456 void *buf;
457 int ret;
74400e71 458
aac17941 459 buf = "";
74400e71 460 if (size)
aac17941 461 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
74400e71 462 close(fd);
aac17941 463 if ((int)(long)buf == -1)
74400e71
JH
464 return -1;
465
aac17941
LT
466 ret = write_sha1_file(buf, size, "blob", sha1);
467 if (size)
468 munmap(buf, size);
469 return ret;
74400e71 470}