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