[PATCH] diff.c: clean temporary files
[git/gitweb.git] / sha1_file.c
blobdb2880e389e556dd3a5eef02aa8a3bb235528057
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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)
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;
35 int get_sha1_hex(const char *hex, unsigned char *sha1)
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;
45 return 0;
48 char * sha1_to_hex(const unsigned char *sha1)
50 static char buffer[50];
51 static const char hex[] = "0123456789abcdef";
52 char *buf = buffer;
53 int i;
55 for (i = 0; i < 20; i++) {
56 unsigned int val = *sha1++;
57 *buf++ = hex[val >> 4];
58 *buf++ = hex[val & 0xf];
60 return buffer;
64 * NOTE! This returns a statically allocated buffer, so you have to be
65 * careful about using it. Do a "strdup()" if you need to save the
66 * filename.
68 char *sha1_file_name(const unsigned char *sha1)
70 int i;
71 static char *name, *base;
73 if (!base) {
74 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
75 int len = strlen(sha1_file_directory);
76 base = xmalloc(len + 60);
77 memcpy(base, sha1_file_directory, len);
78 memset(base+len, 0, 60);
79 base[len] = '/';
80 base[len+3] = '/';
81 name = base + len + 1;
83 for (i = 0; i < 20; i++) {
84 static char hex[] = "0123456789abcdef";
85 unsigned int val = sha1[i];
86 char *pos = name + i*2 + (i > 0);
87 *pos++ = hex[val >> 4];
88 *pos = hex[val & 0xf];
90 return base;
93 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
95 char header[100];
96 unsigned char real_sha1[20];
97 SHA_CTX c;
99 SHA1_Init(&c);
100 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
101 SHA1_Update(&c, map, size);
102 SHA1_Final(real_sha1, &c);
103 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
106 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
108 char *filename = sha1_file_name(sha1);
109 struct stat st;
110 void *map;
111 int fd;
113 fd = open(filename, O_RDONLY | sha1_file_open_flag);
114 if (fd < 0) {
115 /* See if it works without O_NOATIME */
116 switch (sha1_file_open_flag) {
117 default:
118 fd = open(filename, O_RDONLY);
119 if (fd >= 0)
120 break;
121 /* Fallthrough */
122 case 0:
123 perror(filename);
124 return NULL;
127 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
128 sha1_file_open_flag = 0;
130 if (fstat(fd, &st) < 0) {
131 close(fd);
132 return NULL;
134 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
135 close(fd);
136 if (-1 == (int)(long)map)
137 return NULL;
138 *size = st.st_size;
139 return map;
142 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
144 int ret, bytes;
145 z_stream stream;
146 char buffer[8192];
147 char *buf;
149 /* Get the data stream */
150 memset(&stream, 0, sizeof(stream));
151 stream.next_in = map;
152 stream.avail_in = mapsize;
153 stream.next_out = buffer;
154 stream.avail_out = sizeof(buffer);
156 inflateInit(&stream);
157 ret = inflate(&stream, 0);
158 if (ret < Z_OK)
159 return NULL;
160 if (sscanf(buffer, "%10s %lu", type, size) != 2)
161 return NULL;
163 bytes = strlen(buffer) + 1;
164 buf = xmalloc(*size);
166 memcpy(buf, buffer + bytes, stream.total_out - bytes);
167 bytes = stream.total_out - bytes;
168 if (bytes < *size && ret == Z_OK) {
169 stream.next_out = buf + bytes;
170 stream.avail_out = *size - bytes;
171 while (inflate(&stream, Z_FINISH) == Z_OK)
172 /* nothing */;
174 inflateEnd(&stream);
175 return buf;
178 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
180 unsigned long mapsize;
181 void *map, *buf;
183 map = map_sha1_file(sha1, &mapsize);
184 if (map) {
185 buf = unpack_sha1_file(map, mapsize, type, size);
186 munmap(map, mapsize);
187 return buf;
189 return NULL;
192 void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1,
193 unsigned long *size,
194 unsigned char *tree_sha1_return)
196 char type[20];
197 void *buffer;
198 unsigned long isize;
199 int was_commit = 0;
200 unsigned char tree_sha1[20];
202 buffer = read_sha1_file(sha1, type, &isize);
205 * We might have read a commit instead of a tree, in which case
206 * we parse out the tree_sha1 and attempt to read from there.
207 * (buffer + 5) is because the tree sha1 is always at offset 5
208 * in a commit record ("tree ").
210 if (buffer &&
211 !strcmp(type, "commit") &&
212 !get_sha1_hex(buffer + 5, tree_sha1)) {
213 free(buffer);
214 buffer = read_sha1_file(tree_sha1, type, &isize);
215 was_commit = 1;
219 * Now do we have something and if so is it a tree?
221 if (!buffer || strcmp(type, "tree")) {
222 free(buffer);
223 return NULL;
226 *size = isize;
227 if (tree_sha1_return)
228 memcpy(tree_sha1_return, was_commit ? tree_sha1 : sha1, 20);
229 return buffer;
232 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
234 int size;
235 char *compressed;
236 z_stream stream;
237 unsigned char sha1[20];
238 SHA_CTX c;
239 char *filename;
240 char hdr[50];
241 int fd, hdrlen;
243 /* Generate the header */
244 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
246 /* Sha1.. */
247 SHA1_Init(&c);
248 SHA1_Update(&c, hdr, hdrlen);
249 SHA1_Update(&c, buf, len);
250 SHA1_Final(sha1, &c);
252 if (returnsha1)
253 memcpy(returnsha1, sha1, 20);
255 filename = sha1_file_name(sha1);
256 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
257 if (fd < 0) {
258 if (errno != EEXIST)
259 return -1;
262 * We might do collision checking here, but we'd need to
263 * uncompress the old file and check it. Later.
265 return 0;
268 /* Set it up */
269 memset(&stream, 0, sizeof(stream));
270 deflateInit(&stream, Z_BEST_COMPRESSION);
271 size = deflateBound(&stream, len+hdrlen);
272 compressed = xmalloc(size);
274 /* Compress it */
275 stream.next_out = compressed;
276 stream.avail_out = size;
278 /* First header.. */
279 stream.next_in = hdr;
280 stream.avail_in = hdrlen;
281 while (deflate(&stream, 0) == Z_OK)
282 /* nothing */
284 /* Then the data itself.. */
285 stream.next_in = buf;
286 stream.avail_in = len;
287 while (deflate(&stream, Z_FINISH) == Z_OK)
288 /* nothing */;
289 deflateEnd(&stream);
290 size = stream.total_out;
292 if (write(fd, compressed, size) != size)
293 die("unable to write file");
294 close(fd);
296 return 0;
299 static inline int collision_check(char *filename, void *buf, unsigned int size)
301 #ifdef COLLISION_CHECK
302 void *map;
303 int fd = open(filename, O_RDONLY);
304 struct stat st;
305 int cmp;
307 /* Unreadable object, or object went away? Strange. */
308 if (fd < 0)
309 return -1;
311 if (fstat(fd, &st) < 0 || size != st.st_size)
312 return -1;
314 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
315 close(fd);
316 if (map == MAP_FAILED)
317 return -1;
318 cmp = memcmp(buf, map, size);
319 munmap(map, size);
320 if (cmp)
321 return -1;
322 #endif
323 return 0;
326 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
328 char *filename = sha1_file_name(sha1);
329 int fd;
331 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
332 if (fd < 0) {
333 if (errno != EEXIST)
334 return -1;
335 if (collision_check(filename, buf, size))
336 return error("SHA1 collision detected!"
337 " This is bad, bad, BAD!\a\n");
338 return 0;
340 write(fd, buf, size);
341 close(fd);
342 return 0;
345 int write_sha1_from_fd(const unsigned char *sha1, int fd)
347 char *filename = sha1_file_name(sha1);
349 int local;
350 z_stream stream;
351 unsigned char real_sha1[20];
352 char buf[4096];
353 char discard[4096];
354 int ret;
355 SHA_CTX c;
357 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
359 if (local < 0)
360 return error("Couldn't open %s\n", filename);
362 memset(&stream, 0, sizeof(stream));
364 inflateInit(&stream);
366 SHA1_Init(&c);
368 do {
369 ssize_t size;
370 size = read(fd, buf, 4096);
371 if (size <= 0) {
372 close(local);
373 unlink(filename);
374 if (!size)
375 return error("Connection closed?");
376 perror("Reading from connection");
377 return -1;
379 write(local, buf, size);
380 stream.avail_in = size;
381 stream.next_in = buf;
382 do {
383 stream.next_out = discard;
384 stream.avail_out = sizeof(discard);
385 ret = inflate(&stream, Z_SYNC_FLUSH);
386 SHA1_Update(&c, discard, sizeof(discard) -
387 stream.avail_out);
388 } while (stream.avail_in && ret == Z_OK);
390 } while (ret == Z_OK);
391 inflateEnd(&stream);
393 close(local);
394 SHA1_Final(real_sha1, &c);
395 if (ret != Z_STREAM_END) {
396 unlink(filename);
397 return error("File %s corrupted", sha1_to_hex(sha1));
399 if (memcmp(sha1, real_sha1, 20)) {
400 unlink(filename);
401 return error("File %s has bad hash\n", sha1_to_hex(sha1));
404 return 0;
407 int has_sha1_file(const unsigned char *sha1)
409 char *filename = sha1_file_name(sha1);
410 struct stat st;
412 if (!stat(filename, &st))
413 return 1;
414 return 0;