blob: 82a01887c261676eb39fe72b108bfa56041d71f5 [file] [log] [blame]
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001/*
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 */
Junio C Hamano1f688552005-06-27 03:35:33 -07009#include <sys/types.h>
10#include <dirent.h>
Linus Torvalds0fcfd162005-04-18 13:04:43 -070011#include "cache.h"
Junio C Hamano1f688552005-06-27 03:35:33 -070012#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070013#include "pack.h"
Linus Torvalds0fcfd162005-04-18 13:04:43 -070014
Linus Torvalds144bde72005-04-23 11:09:32 -070015#ifndef O_NOATIME
16#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
17#define O_NOATIME 01000000
18#else
19#define O_NOATIME 0
20#endif
21#endif
22
Junio C Hamano88cd6212005-09-30 14:02:47 -070023const unsigned char null_sha1[20] = { 0, };
24
Linus Torvalds144bde72005-04-23 11:09:32 -070025static unsigned int sha1_file_open_flag = O_NOATIME;
26
Linus Torvalds0fcfd162005-04-18 13:04:43 -070027static unsigned hexval(char c)
28{
29 if (c >= '0' && c <= '9')
30 return c - '0';
31 if (c >= 'a' && c <= 'f')
32 return c - 'a' + 10;
33 if (c >= 'A' && c <= 'F')
34 return c - 'A' + 10;
35 return ~0;
36}
37
38int get_sha1_hex(const char *hex, unsigned char *sha1)
39{
40 int i;
41 for (i = 0; i < 20; i++) {
42 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
43 if (val & ~0xff)
44 return -1;
45 *sha1++ = val;
46 hex += 2;
47 }
48 return 0;
49}
50
Junio C Hamanob2cb9422005-07-06 01:11:52 -070051int safe_create_leading_directories(char *path)
52{
53 char *pos = path;
Johannes Schindelin67ffdf42005-11-07 00:36:15 +010054 if (*pos == '/')
55 pos++;
Junio C Hamanob2cb9422005-07-06 01:11:52 -070056
57 while (pos) {
58 pos = strchr(pos, '/');
59 if (!pos)
60 break;
61 *pos = 0;
62 if (mkdir(path, 0777) < 0)
63 if (errno != EEXIST) {
64 *pos = '/';
65 return -1;
66 }
67 *pos++ = '/';
68 }
69 return 0;
70}
71
Linus Torvalds0fcfd162005-04-18 13:04:43 -070072char * sha1_to_hex(const unsigned char *sha1)
73{
74 static char buffer[50];
75 static const char hex[] = "0123456789abcdef";
76 char *buf = buffer;
77 int i;
78
79 for (i = 0; i < 20; i++) {
80 unsigned int val = *sha1++;
81 *buf++ = hex[val >> 4];
82 *buf++ = hex[val & 0xf];
83 }
84 return buffer;
85}
86
Junio C Hamanoace15342005-05-07 00:38:04 -070087static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
88{
89 int i;
90 for (i = 0; i < 20; i++) {
91 static char hex[] = "0123456789abcdef";
92 unsigned int val = sha1[i];
93 char *pos = pathbuf + i*2 + (i > 0);
94 *pos++ = hex[val >> 4];
95 *pos = hex[val & 0xf];
96 }
97}
98
Linus Torvalds0fcfd162005-04-18 13:04:43 -070099/*
100 * NOTE! This returns a statically allocated buffer, so you have to be
101 * careful about using it. Do a "strdup()" if you need to save the
102 * filename.
Junio C Hamanoace15342005-05-07 00:38:04 -0700103 *
104 * Also note that this returns the location for creating. Reading
105 * SHA1 file can happen from any alternate directory listed in the
Junio C Hamanod19938a2005-05-09 17:57:56 -0700106 * DB_ENVIRONMENT environment variable if it is not found in
Junio C Hamanoace15342005-05-07 00:38:04 -0700107 * the primary object database.
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700108 */
109char *sha1_file_name(const unsigned char *sha1)
110{
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700111 static char *name, *base;
112
113 if (!base) {
Junio C Hamanod19938a2005-05-09 17:57:56 -0700114 const char *sha1_file_directory = get_object_directory();
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700115 int len = strlen(sha1_file_directory);
Christopher Li812666c2005-04-26 12:00:58 -0700116 base = xmalloc(len + 60);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700117 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 }
Junio C Hamanoace15342005-05-07 00:38:04 -0700123 fill_sha1_path(name, sha1);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700124 return base;
125}
126
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400127char *sha1_pack_name(const unsigned char *sha1)
128{
129 static const char hex[] = "0123456789abcdef";
130 static char *name, *base, *buf;
131 int i;
132
133 if (!base) {
134 const char *sha1_file_directory = get_object_directory();
135 int len = strlen(sha1_file_directory);
136 base = xmalloc(len + 60);
137 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
138 name = base + len + 11;
139 }
140
141 buf = name;
142
143 for (i = 0; i < 20; i++) {
144 unsigned int val = *sha1++;
145 *buf++ = hex[val >> 4];
146 *buf++ = hex[val & 0xf];
147 }
148
149 return base;
150}
151
152char *sha1_pack_index_name(const unsigned char *sha1)
153{
154 static const char hex[] = "0123456789abcdef";
155 static char *name, *base, *buf;
156 int i;
157
158 if (!base) {
159 const char *sha1_file_directory = get_object_directory();
160 int len = strlen(sha1_file_directory);
161 base = xmalloc(len + 60);
162 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
163 name = base + len + 11;
164 }
165
166 buf = name;
167
168 for (i = 0; i < 20; i++) {
169 unsigned int val = *sha1++;
170 *buf++ = hex[val >> 4];
171 *buf++ = hex[val & 0xf];
172 }
173
174 return base;
175}
176
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700177struct alternate_object_database *alt_odb_list;
178static struct alternate_object_database **alt_odb_tail;
Junio C Hamanoace15342005-05-07 00:38:04 -0700179
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700180/*
181 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700182 *
183 * The variable alt_odb_list points at the list of struct
184 * alternate_object_database. The elements on this list come from
185 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
186 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
187 * whose contents is exactly in the same format as that environment
188 * variable. Its base points at a statically allocated buffer that
189 * contains "/the/directory/corresponding/to/.git/objects/...", while
190 * its name points just after the slash at the end of ".git/objects/"
191 * in the example above, and has enough space to hold 40-byte hex
192 * SHA1, an extra slash for the first level indirection, and the
193 * terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700194 */
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700195static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
196 const char *relative_base)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700197{
198 const char *cp, *last;
199 struct alternate_object_database *ent;
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700200 int base_len = -1;
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700201
202 last = alt;
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700203 while (last < ep) {
204 cp = last;
205 if (cp < ep && *cp == '#') {
206 while (cp < ep && *cp != sep)
207 cp++;
208 last = cp + 1;
209 continue;
210 }
211 for ( ; cp < ep && *cp != sep; cp++)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700212 ;
213 if (last != cp) {
214 /* 43 = 40-byte + 2 '/' + terminating NUL */
215 int pfxlen = cp - last;
216 int entlen = pfxlen + 43;
217
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700218 if (*last != '/' && relative_base) {
219 /* Relative alt-odb */
220 if (base_len < 0)
221 base_len = strlen(relative_base) + 1;
222 entlen += base_len;
223 pfxlen += base_len;
224 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700225 ent = xmalloc(sizeof(*ent) + entlen);
226 *alt_odb_tail = ent;
227 alt_odb_tail = &(ent->next);
228 ent->next = NULL;
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700229 if (*last != '/' && relative_base) {
230 memcpy(ent->base, relative_base, base_len - 1);
231 ent->base[base_len - 1] = '/';
232 memcpy(ent->base + base_len,
233 last, cp - last);
234 }
235 else
236 memcpy(ent->base, last, pfxlen);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700237 ent->name = ent->base + pfxlen + 1;
238 ent->base[pfxlen] = ent->base[pfxlen + 3] = '/';
239 ent->base[entlen-1] = 0;
240 }
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700241 while (cp < ep && *cp == sep)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700242 cp++;
243 last = cp;
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700244 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700245}
246
Junio C Hamano9a217f22005-06-28 14:56:57 -0700247void prepare_alt_odb(void)
Junio C Hamanoace15342005-05-07 00:38:04 -0700248{
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700249 char path[PATH_MAX];
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700250 char *map;
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700251 int fd;
252 struct stat st;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700253 char *alt;
254
Junio C Hamanoa9ab5862005-09-09 14:48:54 -0700255 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
Jason Riedyc7c81b32005-08-23 13:34:07 -0700256 if (!alt) alt = "";
Junio C Hamanoace15342005-05-07 00:38:04 -0700257
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700258 if (alt_odb_tail)
Junio C Hamano9a217f22005-06-28 14:56:57 -0700259 return;
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700260 alt_odb_tail = &alt_odb_list;
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700261 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700262
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700263 sprintf(path, "%s/info/alternates", get_object_directory());
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700264 fd = open(path, O_RDONLY);
265 if (fd < 0)
266 return;
267 if (fstat(fd, &st) || (st.st_size == 0)) {
268 close(fd);
269 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700270 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700271 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
272 close(fd);
273 if (map == MAP_FAILED)
274 return;
275
Junio C Hamanoccfd3e92005-09-13 00:05:22 -0700276 link_alt_odb_entries(map, map + st.st_size, '\n',
277 get_object_directory());
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700278 munmap(map, st.st_size);
Junio C Hamanoace15342005-05-07 00:38:04 -0700279}
280
281static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
282{
Junio C Hamanoace15342005-05-07 00:38:04 -0700283 char *name = sha1_file_name(sha1);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700284 struct alternate_object_database *alt;
Junio C Hamanoace15342005-05-07 00:38:04 -0700285
286 if (!stat(name, st))
287 return name;
Junio C Hamano9a217f22005-06-28 14:56:57 -0700288 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700289 for (alt = alt_odb_list; alt; alt = alt->next) {
290 name = alt->name;
Junio C Hamanoace15342005-05-07 00:38:04 -0700291 fill_sha1_path(name, sha1);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700292 if (!stat(alt->base, st))
293 return alt->base;
Junio C Hamanoace15342005-05-07 00:38:04 -0700294 }
295 return NULL;
296}
297
Junio C Hamano1f688552005-06-27 03:35:33 -0700298#define PACK_MAX_SZ (1<<26)
299static int pack_used_ctr;
300static unsigned long pack_mapped;
Junio C Hamano9a217f22005-06-28 14:56:57 -0700301struct packed_git *packed_git;
Junio C Hamano1f688552005-06-27 03:35:33 -0700302
Junio C Hamano1f688552005-06-27 03:35:33 -0700303static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
304 void **idx_map_)
305{
306 void *idx_map;
307 unsigned int *index;
308 unsigned long idx_size;
309 int nr, i;
310 int fd = open(path, O_RDONLY);
311 struct stat st;
312 if (fd < 0)
313 return -1;
314 if (fstat(fd, &st)) {
315 close(fd);
316 return -1;
317 }
318 idx_size = st.st_size;
319 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
320 close(fd);
321 if (idx_map == MAP_FAILED)
322 return -1;
323
324 index = idx_map;
Linus Torvalds4d235c82005-07-03 09:58:44 -0700325 *idx_map_ = idx_map;
326 *idx_size_ = idx_size;
Junio C Hamano1f688552005-06-27 03:35:33 -0700327
328 /* check index map */
Junio C Hamanof9253392005-06-29 02:51:27 -0700329 if (idx_size < 4*256 + 20 + 20)
Junio C Hamano1f688552005-06-27 03:35:33 -0700330 return error("index file too small");
331 nr = 0;
332 for (i = 0; i < 256; i++) {
333 unsigned int n = ntohl(index[i]);
334 if (n < nr)
335 return error("non-monotonic index");
336 nr = n;
337 }
338
339 /*
340 * Total size:
341 * - 256 index entries 4 bytes each
342 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
343 * - 20-byte SHA1 of the packfile
344 * - 20-byte SHA1 file checksum
345 */
346 if (idx_size != 4*256 + nr * 24 + 20 + 20)
347 return error("wrong index file size");
348
Junio C Hamano1f688552005-06-27 03:35:33 -0700349 return 0;
350}
351
Junio C Hamanof9253392005-06-29 02:51:27 -0700352static int unuse_one_packed_git(void)
Junio C Hamano1f688552005-06-27 03:35:33 -0700353{
Junio C Hamanof9253392005-06-29 02:51:27 -0700354 struct packed_git *p, *lru = NULL;
355
356 for (p = packed_git; p; p = p->next) {
357 if (p->pack_use_cnt || !p->pack_base)
358 continue;
359 if (!lru || p->pack_last_used < lru->pack_last_used)
360 lru = p;
361 }
362 if (!lru)
363 return 0;
364 munmap(lru->pack_base, lru->pack_size);
365 lru->pack_base = NULL;
366 return 1;
Junio C Hamano1f688552005-06-27 03:35:33 -0700367}
368
Junio C Hamanof9253392005-06-29 02:51:27 -0700369void unuse_packed_git(struct packed_git *p)
370{
371 p->pack_use_cnt--;
372}
373
374int use_packed_git(struct packed_git *p)
Junio C Hamano1f688552005-06-27 03:35:33 -0700375{
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400376 if (!p->pack_size) {
377 struct stat st;
378 // We created the struct before we had the pack
379 stat(p->pack_name, &st);
380 if (!S_ISREG(st.st_mode))
381 die("packfile %s not a regular file", p->pack_name);
382 p->pack_size = st.st_size;
383 }
Junio C Hamano1f688552005-06-27 03:35:33 -0700384 if (!p->pack_base) {
385 int fd;
386 struct stat st;
387 void *map;
388
389 pack_mapped += p->pack_size;
Junio C Hamanof9253392005-06-29 02:51:27 -0700390 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
391 ; /* nothing */
Junio C Hamano1f688552005-06-27 03:35:33 -0700392 fd = open(p->pack_name, O_RDONLY);
393 if (fd < 0)
Junio C Hamanof9253392005-06-29 02:51:27 -0700394 die("packfile %s cannot be opened", p->pack_name);
Junio C Hamano1f688552005-06-27 03:35:33 -0700395 if (fstat(fd, &st)) {
396 close(fd);
Junio C Hamanof9253392005-06-29 02:51:27 -0700397 die("packfile %s cannot be opened", p->pack_name);
Junio C Hamano1f688552005-06-27 03:35:33 -0700398 }
399 if (st.st_size != p->pack_size)
Junio C Hamanof9253392005-06-29 02:51:27 -0700400 die("packfile %s size mismatch.", p->pack_name);
Junio C Hamano1f688552005-06-27 03:35:33 -0700401 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
402 close(fd);
403 if (map == MAP_FAILED)
Junio C Hamanof9253392005-06-29 02:51:27 -0700404 die("packfile %s cannot be mapped.", p->pack_name);
Junio C Hamano1f688552005-06-27 03:35:33 -0700405 p->pack_base = map;
Junio C Hamanof9253392005-06-29 02:51:27 -0700406
407 /* Check if the pack file matches with the index file.
408 * this is cheap.
409 */
410 if (memcmp((char*)(p->index_base) + p->index_size - 40,
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400411 p->pack_base + p->pack_size - 20, 20)) {
412
Junio C Hamanof9253392005-06-29 02:51:27 -0700413 die("packfile %s does not match index.", p->pack_name);
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400414 }
Junio C Hamano1f688552005-06-27 03:35:33 -0700415 }
416 p->pack_last_used = pack_used_ctr++;
Junio C Hamanof9253392005-06-29 02:51:27 -0700417 p->pack_use_cnt++;
Junio C Hamano1f688552005-06-27 03:35:33 -0700418 return 0;
419}
420
Linus Torvalds9d835df2005-10-13 15:38:28 -0700421struct packed_git *add_packed_git(char *path, int path_len, int local)
Junio C Hamano1f688552005-06-27 03:35:33 -0700422{
423 struct stat st;
424 struct packed_git *p;
425 unsigned long idx_size;
426 void *idx_map;
Junio C Hamanoc0bbbb12005-11-15 12:51:02 -0800427 unsigned char sha1[20];
Junio C Hamano1f688552005-06-27 03:35:33 -0700428
429 if (check_packed_git_idx(path, &idx_size, &idx_map))
430 return NULL;
431
432 /* do we have a corresponding .pack file? */
433 strcpy(path + path_len - 4, ".pack");
434 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
435 munmap(idx_map, idx_size);
436 return NULL;
437 }
438 /* ok, it looks sane as far as we can check without
439 * actually mapping the pack file.
440 */
441 p = xmalloc(sizeof(*p) + path_len + 2);
442 strcpy(p->pack_name, path);
443 p->index_size = idx_size;
444 p->pack_size = st.st_size;
445 p->index_base = idx_map;
446 p->next = NULL;
Junio C Hamanod85a4fe2005-06-28 14:55:16 -0700447 p->pack_base = NULL;
Junio C Hamano1f688552005-06-27 03:35:33 -0700448 p->pack_last_used = 0;
Junio C Hamanof9253392005-06-29 02:51:27 -0700449 p->pack_use_cnt = 0;
Linus Torvalds9d835df2005-10-13 15:38:28 -0700450 p->pack_local = local;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100451 if (!get_sha1_hex(path + path_len - 40 - 4, sha1))
452 memcpy(p->sha1, sha1, 20);
Junio C Hamano1f688552005-06-27 03:35:33 -0700453 return p;
454}
455
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400456struct packed_git *parse_pack_index(unsigned char *sha1)
457{
Daniel Barkalowc508df52005-08-16 00:10:03 -0400458 char *path = sha1_pack_index_name(sha1);
459 return parse_pack_index_file(sha1, path);
460}
461
Peter Hagervall2ab141a2005-09-02 14:17:10 +0200462struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
Daniel Barkalowc508df52005-08-16 00:10:03 -0400463{
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400464 struct packed_git *p;
465 unsigned long idx_size;
466 void *idx_map;
Daniel Barkalowc508df52005-08-16 00:10:03 -0400467 char *path;
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400468
Daniel Barkalowc508df52005-08-16 00:10:03 -0400469 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400470 return NULL;
471
472 path = sha1_pack_name(sha1);
473
474 p = xmalloc(sizeof(*p) + strlen(path) + 2);
475 strcpy(p->pack_name, path);
476 p->index_size = idx_size;
477 p->pack_size = 0;
478 p->index_base = idx_map;
479 p->next = NULL;
480 p->pack_base = NULL;
481 p->pack_last_used = 0;
482 p->pack_use_cnt = 0;
483 memcpy(p->sha1, sha1, 20);
484 return p;
485}
486
487void install_packed_git(struct packed_git *pack)
488{
489 pack->next = packed_git;
490 packed_git = pack;
491}
492
Linus Torvalds9d835df2005-10-13 15:38:28 -0700493static void prepare_packed_git_one(char *objdir, int local)
Junio C Hamano1f688552005-06-27 03:35:33 -0700494{
495 char path[PATH_MAX];
496 int len;
497 DIR *dir;
498 struct dirent *de;
499
500 sprintf(path, "%s/pack", objdir);
501 len = strlen(path);
502 dir = opendir(path);
503 if (!dir)
504 return;
505 path[len++] = '/';
506 while ((de = readdir(dir)) != NULL) {
507 int namelen = strlen(de->d_name);
508 struct packed_git *p;
509
510 if (strcmp(de->d_name + namelen - 4, ".idx"))
511 continue;
512
513 /* we have .idx. Is it a file we can map? */
514 strcpy(path + len, de->d_name);
Linus Torvalds9d835df2005-10-13 15:38:28 -0700515 p = add_packed_git(path, len + namelen, local);
Junio C Hamano1f688552005-06-27 03:35:33 -0700516 if (!p)
517 continue;
518 p->next = packed_git;
519 packed_git = p;
520 }
Junio C Hamano5b35bcd2005-07-05 23:52:17 -0700521 closedir(dir);
Junio C Hamano1f688552005-06-27 03:35:33 -0700522}
523
Junio C Hamano9a217f22005-06-28 14:56:57 -0700524void prepare_packed_git(void)
Junio C Hamano1f688552005-06-27 03:35:33 -0700525{
Junio C Hamano1f688552005-06-27 03:35:33 -0700526 static int run_once = 0;
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700527 struct alternate_object_database *alt;
Junio C Hamano1f688552005-06-27 03:35:33 -0700528
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700529 if (run_once)
Junio C Hamano1f688552005-06-27 03:35:33 -0700530 return;
Linus Torvalds9d835df2005-10-13 15:38:28 -0700531 prepare_packed_git_one(get_object_directory(), 1);
Junio C Hamano9a217f22005-06-28 14:56:57 -0700532 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700533 for (alt = alt_odb_list; alt; alt = alt->next) {
534 alt->name[0] = 0;
Linus Torvalds9d835df2005-10-13 15:38:28 -0700535 prepare_packed_git_one(alt->base, 0);
Junio C Hamano1f688552005-06-27 03:35:33 -0700536 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700537 run_once = 1;
Junio C Hamano1f688552005-06-27 03:35:33 -0700538}
539
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400540int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700541{
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700542 char header[100];
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700543 unsigned char real_sha1[20];
544 SHA_CTX c;
545
546 SHA1_Init(&c);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700547 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700548 SHA1_Update(&c, map, size);
549 SHA1_Final(real_sha1, &c);
550 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
551}
552
Junio C Hamano1f688552005-06-27 03:35:33 -0700553static void *map_sha1_file_internal(const unsigned char *sha1,
Daniel Barkalowd5f1bef2005-07-10 18:27:02 -0400554 unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700555{
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700556 struct stat st;
557 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -0700558 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -0700559 char *filename = find_sha1_file(sha1, &st);
560
561 if (!filename) {
Junio C Hamanoace15342005-05-07 00:38:04 -0700562 return NULL;
563 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700564
Linus Torvalds144bde72005-04-23 11:09:32 -0700565 fd = open(filename, O_RDONLY | sha1_file_open_flag);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700566 if (fd < 0) {
Linus Torvalds144bde72005-04-23 11:09:32 -0700567 /* See if it works without O_NOATIME */
568 switch (sha1_file_open_flag) {
569 default:
570 fd = open(filename, O_RDONLY);
571 if (fd >= 0)
572 break;
573 /* Fallthrough */
574 case 0:
Linus Torvalds144bde72005-04-23 11:09:32 -0700575 return NULL;
576 }
577
Junio C Hamano1f688552005-06-27 03:35:33 -0700578 /* If it failed once, it will probably fail again.
579 * Stop using O_NOATIME
580 */
Linus Torvalds144bde72005-04-23 11:09:32 -0700581 sha1_file_open_flag = 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700582 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700583 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
584 close(fd);
Pavel Roskine35f9822005-07-29 10:49:14 -0400585 if (map == MAP_FAILED)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700586 return NULL;
587 *size = st.st_size;
588 return map;
589}
590
Linus Torvaldsc4483572005-06-01 17:54:59 -0700591int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
592{
593 /* Get the data stream */
594 memset(stream, 0, sizeof(*stream));
595 stream->next_in = map;
596 stream->avail_in = mapsize;
597 stream->next_out = buffer;
598 stream->avail_out = size;
599
600 inflateInit(stream);
601 return inflate(stream, 0);
602}
603
Linus Torvalds6da40162005-07-03 10:10:45 -0700604static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700605{
Linus Torvalds5180cac2005-06-02 07:57:25 -0700606 int bytes = strlen(buffer) + 1;
Mika Kukkonend565b342005-06-21 23:04:33 +0300607 unsigned char *buf = xmalloc(1+size);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700608
Linus Torvalds5180cac2005-06-02 07:57:25 -0700609 memcpy(buf, buffer + bytes, stream->total_out - bytes);
610 bytes = stream->total_out - bytes;
611 if (bytes < size) {
612 stream->next_out = buf + bytes;
613 stream->avail_out = size - bytes;
614 while (inflate(stream, Z_FINISH) == Z_OK)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700615 /* nothing */;
616 }
Linus Torvalds5180cac2005-06-02 07:57:25 -0700617 buf[size] = 0;
618 inflateEnd(stream);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700619 return buf;
620}
621
Linus Torvalds5180cac2005-06-02 07:57:25 -0700622/*
623 * We used to just use "sscanf()", but that's actually way
624 * too permissive for what we want to check. So do an anal
625 * object header parse by hand.
626 */
627int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
628{
629 int i;
630 unsigned long size;
631
632 /*
633 * The type can be at most ten bytes (including the
634 * terminating '\0' that we add), and is followed by
635 * a space.
636 */
637 i = 10;
638 for (;;) {
639 char c = *hdr++;
640 if (c == ' ')
641 break;
642 if (!--i)
643 return -1;
644 *type++ = c;
645 }
646 *type = 0;
647
648 /*
649 * The length must follow immediately, and be in canonical
650 * decimal format (ie "010" is not valid).
651 */
652 size = *hdr++ - '0';
653 if (size > 9)
654 return -1;
655 if (size) {
656 for (;;) {
657 unsigned long c = *hdr - '0';
658 if (c > 9)
659 break;
660 hdr++;
661 size = size * 10 + c;
662 }
663 }
664 *sizep = size;
665
666 /*
667 * The length must be followed by a zero byte
668 */
669 return *hdr ? -1 : 0;
670}
671
672void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
673{
674 int ret;
675 z_stream stream;
676 char hdr[8192];
677
678 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
679 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
680 return NULL;
681
682 return unpack_sha1_rest(&stream, hdr, *size);
683}
684
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700685/* forward declaration for a mutually recursive function */
686static int packed_object_info(struct pack_entry *entry,
687 char *type, unsigned long *sizep);
688
Junio C Hamano5db47c22005-06-27 23:58:08 -0700689static int packed_delta_info(unsigned char *base_sha1,
690 unsigned long delta_size,
691 unsigned long left,
692 char *type,
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700693 unsigned long *sizep,
694 struct packed_git *p)
Junio C Hamano5db47c22005-06-27 23:58:08 -0700695{
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700696 struct pack_entry base_ent;
697
Junio C Hamano5db47c22005-06-27 23:58:08 -0700698 if (left < 20)
699 die("truncated pack file");
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700700
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700701 /* The base entry _must_ be in the same pack */
702 if (!find_pack_entry_one(base_sha1, &base_ent, p))
703 die("failed to find delta-pack base object %s",
704 sha1_to_hex(base_sha1));
705
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700706 /* We choose to only get the type of the base object and
707 * ignore potentially corrupt pack file that expects the delta
708 * based on a base with a wrong size. This saves tons of
709 * inflate() calls.
710 */
711
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700712 if (packed_object_info(&base_ent, type, NULL))
Junio C Hamano5db47c22005-06-27 23:58:08 -0700713 die("cannot get info for delta-pack base");
714
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700715 if (sizep) {
716 const unsigned char *data;
717 unsigned char delta_head[64];
718 unsigned long result_size;
719 z_stream stream;
720 int st;
Junio C Hamano5db47c22005-06-27 23:58:08 -0700721
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700722 memset(&stream, 0, sizeof(stream));
Junio C Hamano5db47c22005-06-27 23:58:08 -0700723
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700724 data = stream.next_in = base_sha1 + 20;
725 stream.avail_in = left - 20;
726 stream.next_out = delta_head;
727 stream.avail_out = sizeof(delta_head);
Junio C Hamano5db47c22005-06-27 23:58:08 -0700728
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700729 inflateInit(&stream);
730 st = inflate(&stream, Z_FINISH);
731 inflateEnd(&stream);
732 if ((st != Z_STREAM_END) &&
733 stream.total_out != sizeof(delta_head))
734 die("delta data unpack-initial failed");
Junio C Hamano5db47c22005-06-27 23:58:08 -0700735
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700736 /* Examine the initial part of the delta to figure out
737 * the result size.
738 */
739 data = delta_head;
740 get_delta_hdr_size(&data); /* ignore base size */
741
742 /* Read the result size */
743 result_size = get_delta_hdr_size(&data);
744 *sizep = result_size;
745 }
Junio C Hamano5db47c22005-06-27 23:58:08 -0700746 return 0;
747}
748
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700749static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
750 enum object_type *type, unsigned long *sizep)
751{
Linus Torvalds01247d82005-06-28 22:15:57 -0700752 unsigned shift;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700753 unsigned char *pack, c;
754 unsigned long size;
755
756 if (offset >= p->pack_size)
757 die("object offset outside of pack file");
758
759 pack = p->pack_base + offset;
760 c = *pack++;
761 offset++;
762 *type = (c >> 4) & 7;
763 size = c & 15;
Linus Torvalds01247d82005-06-28 22:15:57 -0700764 shift = 4;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700765 while (c & 0x80) {
766 if (offset >= p->pack_size)
767 die("object offset outside of pack file");
768 c = *pack++;
769 offset++;
Linus Torvalds01247d82005-06-28 22:15:57 -0700770 size += (c & 0x7f) << shift;
771 shift += 7;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700772 }
773 *sizep = size;
774 return offset;
775}
776
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700777void packed_object_info_detail(struct pack_entry *e,
778 char *type,
779 unsigned long *size,
780 unsigned long *store_size,
781 int *delta_chain_length,
782 unsigned char *base_sha1)
783{
784 struct packed_git *p = e->p;
785 unsigned long offset, left;
786 unsigned char *pack;
787 enum object_type kind;
788
789 offset = unpack_object_header(p, e->offset, &kind, size);
790 pack = p->pack_base + offset;
791 left = p->pack_size - offset;
792 if (kind != OBJ_DELTA)
793 *delta_chain_length = 0;
794 else {
795 int chain_length = 0;
796 memcpy(base_sha1, pack, 20);
797 do {
798 struct pack_entry base_ent;
799 unsigned long junk;
800
801 find_pack_entry_one(pack, &base_ent, p);
802 offset = unpack_object_header(p, base_ent.offset,
803 &kind, &junk);
804 pack = p->pack_base + offset;
805 chain_length++;
806 } while (kind == OBJ_DELTA);
807 *delta_chain_length = chain_length;
808 }
809 switch (kind) {
810 case OBJ_COMMIT:
811 strcpy(type, "commit");
812 break;
813 case OBJ_TREE:
814 strcpy(type, "tree");
815 break;
816 case OBJ_BLOB:
817 strcpy(type, "blob");
818 break;
819 case OBJ_TAG:
820 strcpy(type, "tag");
821 break;
822 default:
Junio C Hamano264b16b2005-09-30 00:09:04 -0700823 die("corrupted pack file %s containing object of kind %d",
824 p->pack_name, kind);
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700825 }
826 *store_size = 0; /* notyet */
827}
828
Junio C Hamano1f688552005-06-27 03:35:33 -0700829static int packed_object_info(struct pack_entry *entry,
830 char *type, unsigned long *sizep)
831{
832 struct packed_git *p = entry->p;
833 unsigned long offset, size, left;
834 unsigned char *pack;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700835 enum object_type kind;
Junio C Hamanof9253392005-06-29 02:51:27 -0700836 int retval;
Junio C Hamano5db47c22005-06-27 23:58:08 -0700837
838 if (use_packed_git(p))
839 die("cannot map packed file");
840
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700841 offset = unpack_object_header(p, entry->offset, &kind, &size);
Junio C Hamano1f688552005-06-27 03:35:33 -0700842 pack = p->pack_base + offset;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700843 left = p->pack_size - offset;
844
845 switch (kind) {
846 case OBJ_DELTA:
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700847 retval = packed_delta_info(pack, size, left, type, sizep, p);
Junio C Hamanof9253392005-06-29 02:51:27 -0700848 unuse_packed_git(p);
849 return retval;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700850 case OBJ_COMMIT:
Junio C Hamano1f688552005-06-27 03:35:33 -0700851 strcpy(type, "commit");
852 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700853 case OBJ_TREE:
Junio C Hamano1f688552005-06-27 03:35:33 -0700854 strcpy(type, "tree");
855 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700856 case OBJ_BLOB:
Junio C Hamano1f688552005-06-27 03:35:33 -0700857 strcpy(type, "blob");
858 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700859 case OBJ_TAG:
Linus Torvaldsa69d0942005-06-28 09:58:23 -0700860 strcpy(type, "tag");
861 break;
Junio C Hamano1f688552005-06-27 03:35:33 -0700862 default:
Junio C Hamano264b16b2005-09-30 00:09:04 -0700863 die("corrupted pack file %s containing object of kind %d",
864 p->pack_name, kind);
Junio C Hamano1f688552005-06-27 03:35:33 -0700865 }
Junio C Hamanoc62266f2005-06-30 17:13:07 -0700866 if (sizep)
867 *sizep = size;
Junio C Hamanof9253392005-06-29 02:51:27 -0700868 unuse_packed_git(p);
Junio C Hamano1f688552005-06-27 03:35:33 -0700869 return 0;
870}
871
872/* forward declaration for a mutually recursive function */
873static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
874
875static void *unpack_delta_entry(unsigned char *base_sha1,
876 unsigned long delta_size,
877 unsigned long left,
878 char *type,
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700879 unsigned long *sizep,
880 struct packed_git *p)
Junio C Hamano1f688552005-06-27 03:35:33 -0700881{
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700882 struct pack_entry base_ent;
Junio C Hamano1f688552005-06-27 03:35:33 -0700883 void *data, *delta_data, *result, *base;
884 unsigned long data_size, result_size, base_size;
885 z_stream stream;
886 int st;
887
888 if (left < 20)
889 die("truncated pack file");
890 data = base_sha1 + 20;
891 data_size = left - 20;
892 delta_data = xmalloc(delta_size);
893
894 memset(&stream, 0, sizeof(stream));
895
896 stream.next_in = data;
897 stream.avail_in = data_size;
898 stream.next_out = delta_data;
899 stream.avail_out = delta_size;
900
901 inflateInit(&stream);
902 st = inflate(&stream, Z_FINISH);
903 inflateEnd(&stream);
904 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
905 die("delta data unpack failed");
906
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700907 /* The base entry _must_ be in the same pack */
908 if (!find_pack_entry_one(base_sha1, &base_ent, p))
909 die("failed to find delta-pack base object %s",
910 sha1_to_hex(base_sha1));
911 base = unpack_entry_gently(&base_ent, type, &base_size);
Junio C Hamano1f688552005-06-27 03:35:33 -0700912 if (!base)
913 die("failed to read delta-pack base object %s",
914 sha1_to_hex(base_sha1));
915 result = patch_delta(base, base_size,
916 delta_data, delta_size,
917 &result_size);
918 if (!result)
919 die("failed to apply delta");
920 free(delta_data);
921 free(base);
922 *sizep = result_size;
923 return result;
924}
925
926static void *unpack_non_delta_entry(unsigned char *data,
927 unsigned long size,
928 unsigned long left)
929{
930 int st;
931 z_stream stream;
Linus Torvalds4d235c82005-07-03 09:58:44 -0700932 unsigned char *buffer;
Junio C Hamano1f688552005-06-27 03:35:33 -0700933
934 buffer = xmalloc(size + 1);
935 buffer[size] = 0;
936 memset(&stream, 0, sizeof(stream));
937 stream.next_in = data;
938 stream.avail_in = left;
939 stream.next_out = buffer;
940 stream.avail_out = size;
941
942 inflateInit(&stream);
943 st = inflate(&stream, Z_FINISH);
944 inflateEnd(&stream);
945 if ((st != Z_STREAM_END) || stream.total_out != size) {
946 free(buffer);
947 return NULL;
948 }
949
950 return buffer;
951}
952
953static void *unpack_entry(struct pack_entry *entry,
954 char *type, unsigned long *sizep)
955{
956 struct packed_git *p = entry->p;
Junio C Hamanof9253392005-06-29 02:51:27 -0700957 void *retval;
Junio C Hamano1f688552005-06-27 03:35:33 -0700958
959 if (use_packed_git(p))
960 die("cannot map packed file");
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700961 retval = unpack_entry_gently(entry, type, sizep);
962 unuse_packed_git(p);
963 if (!retval)
Junio C Hamano264b16b2005-09-30 00:09:04 -0700964 die("corrupted pack file %s", p->pack_name);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700965 return retval;
966}
967
968/* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
969void *unpack_entry_gently(struct pack_entry *entry,
970 char *type, unsigned long *sizep)
971{
972 struct packed_git *p = entry->p;
973 unsigned long offset, size, left;
974 unsigned char *pack;
975 enum object_type kind;
976 void *retval;
Junio C Hamano1f688552005-06-27 03:35:33 -0700977
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700978 offset = unpack_object_header(p, entry->offset, &kind, &size);
Junio C Hamano1f688552005-06-27 03:35:33 -0700979 pack = p->pack_base + offset;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700980 left = p->pack_size - offset;
981 switch (kind) {
982 case OBJ_DELTA:
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700983 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
Junio C Hamanof9253392005-06-29 02:51:27 -0700984 return retval;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700985 case OBJ_COMMIT:
Junio C Hamano1f688552005-06-27 03:35:33 -0700986 strcpy(type, "commit");
987 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700988 case OBJ_TREE:
Junio C Hamano1f688552005-06-27 03:35:33 -0700989 strcpy(type, "tree");
990 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700991 case OBJ_BLOB:
Junio C Hamano1f688552005-06-27 03:35:33 -0700992 strcpy(type, "blob");
993 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700994 case OBJ_TAG:
Linus Torvaldsa69d0942005-06-28 09:58:23 -0700995 strcpy(type, "tag");
996 break;
Junio C Hamano1f688552005-06-27 03:35:33 -0700997 default:
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700998 return NULL;
Junio C Hamano1f688552005-06-27 03:35:33 -0700999 }
1000 *sizep = size;
Junio C Hamanof9253392005-06-29 02:51:27 -07001001 retval = unpack_non_delta_entry(pack, size, left);
Junio C Hamanof9253392005-06-29 02:51:27 -07001002 return retval;
Junio C Hamano1f688552005-06-27 03:35:33 -07001003}
1004
Junio C Hamano9a217f22005-06-28 14:56:57 -07001005int num_packed_objects(const struct packed_git *p)
1006{
Junio C Hamanof9253392005-06-29 02:51:27 -07001007 /* See check_packed_git_idx() */
Junio C Hamano9a217f22005-06-28 14:56:57 -07001008 return (p->index_size - 20 - 20 - 4*256) / 24;
1009}
1010
1011int nth_packed_object_sha1(const struct packed_git *p, int n,
1012 unsigned char* sha1)
1013{
1014 void *index = p->index_base + 256;
1015 if (n < 0 || num_packed_objects(p) <= n)
1016 return -1;
1017 memcpy(sha1, (index + 24 * n + 4), 20);
1018 return 0;
1019}
1020
Junio C Hamanof3bf9222005-06-30 17:15:39 -07001021int find_pack_entry_one(const unsigned char *sha1,
1022 struct pack_entry *e, struct packed_git *p)
Junio C Hamano1f688552005-06-27 03:35:33 -07001023{
Linus Torvalds4d235c82005-07-03 09:58:44 -07001024 unsigned int *level1_ofs = p->index_base;
Junio C Hamano1f688552005-06-27 03:35:33 -07001025 int hi = ntohl(level1_ofs[*sha1]);
1026 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1027 void *index = p->index_base + 256;
1028
1029 do {
1030 int mi = (lo + hi) / 2;
1031 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
1032 if (!cmp) {
1033 e->offset = ntohl(*((int*)(index + 24 * mi)));
1034 memcpy(e->sha1, sha1, 20);
1035 e->p = p;
1036 return 1;
1037 }
1038 if (cmp > 0)
1039 hi = mi;
1040 else
1041 lo = mi+1;
1042 } while (lo < hi);
1043 return 0;
1044}
1045
1046static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1047{
1048 struct packed_git *p;
1049 prepare_packed_git();
1050
1051 for (p = packed_git; p; p = p->next) {
Junio C Hamanof3bf9222005-06-30 17:15:39 -07001052 if (find_pack_entry_one(sha1, e, p))
Junio C Hamano1f688552005-06-27 03:35:33 -07001053 return 1;
1054 }
1055 return 0;
1056}
1057
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001058struct packed_git *find_sha1_pack(const unsigned char *sha1,
1059 struct packed_git *packs)
1060{
1061 struct packed_git *p;
1062 struct pack_entry e;
1063
1064 for (p = packs; p; p = p->next) {
1065 if (find_pack_entry_one(sha1, &e, p))
1066 return p;
1067 }
1068 return NULL;
1069
1070}
1071
Junio C Hamano36e4d742005-06-27 03:34:06 -07001072int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001073{
Junio C Hamano36e4d742005-06-27 03:34:06 -07001074 int status;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001075 unsigned long mapsize, size;
1076 void *map;
1077 z_stream stream;
Junio C Hamano36e4d742005-06-27 03:34:06 -07001078 char hdr[128];
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001079
Daniel Barkalowd5f1bef2005-07-10 18:27:02 -04001080 map = map_sha1_file_internal(sha1, &mapsize);
Junio C Hamano1f688552005-06-27 03:35:33 -07001081 if (!map) {
1082 struct pack_entry e;
1083
1084 if (!find_pack_entry(sha1, &e))
1085 return error("unable to find %s", sha1_to_hex(sha1));
Junio C Hamanoc62266f2005-06-30 17:13:07 -07001086 return packed_object_info(&e, type, sizep);
Junio C Hamano1f688552005-06-27 03:35:33 -07001087 }
Junio C Hamano36e4d742005-06-27 03:34:06 -07001088 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1089 status = error("unable to unpack %s header",
1090 sha1_to_hex(sha1));
1091 if (parse_sha1_header(hdr, type, &size) < 0)
1092 status = error("unable to parse %s header", sha1_to_hex(sha1));
Junio C Hamanoc4584ae2005-06-27 03:33:33 -07001093 else {
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001094 status = 0;
Junio C Hamanoc62266f2005-06-30 17:13:07 -07001095 if (sizep)
1096 *sizep = size;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001097 }
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001098 inflateEnd(&stream);
1099 munmap(map, mapsize);
1100 return status;
1101}
1102
Junio C Hamano1f688552005-06-27 03:35:33 -07001103static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1104{
1105 struct pack_entry e;
1106
1107 if (!find_pack_entry(sha1, &e)) {
1108 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1109 return NULL;
1110 }
1111 return unpack_entry(&e, type, size);
1112}
1113
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001114void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1115{
1116 unsigned long mapsize;
1117 void *map, *buf;
Junio C Hamanoab90ea52005-07-11 00:00:55 -07001118 struct pack_entry e;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001119
Junio C Hamanoab90ea52005-07-11 00:00:55 -07001120 if (find_pack_entry(sha1, &e))
1121 return read_packed_sha1(sha1, type, size);
Daniel Barkalowd5f1bef2005-07-10 18:27:02 -04001122 map = map_sha1_file_internal(sha1, &mapsize);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001123 if (map) {
1124 buf = unpack_sha1_file(map, mapsize, type, size);
1125 munmap(map, mapsize);
1126 return buf;
1127 }
Junio C Hamanoab90ea52005-07-11 00:00:55 -07001128 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001129}
1130
Junio C Hamano40469ee2005-04-28 16:42:27 -07001131void *read_object_with_reference(const unsigned char *sha1,
Brian Gerstbf0f9102005-05-18 08:14:09 -04001132 const char *required_type,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001133 unsigned long *size,
1134 unsigned char *actual_sha1_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001135{
1136 char type[20];
1137 void *buffer;
1138 unsigned long isize;
Junio C Hamano40469ee2005-04-28 16:42:27 -07001139 unsigned char actual_sha1[20];
Junio C Hamanof4913f92005-04-20 18:06:49 -07001140
Junio C Hamano40469ee2005-04-28 16:42:27 -07001141 memcpy(actual_sha1, sha1, 20);
1142 while (1) {
1143 int ref_length = -1;
1144 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001145
Junio C Hamano40469ee2005-04-28 16:42:27 -07001146 buffer = read_sha1_file(actual_sha1, type, &isize);
1147 if (!buffer)
1148 return NULL;
1149 if (!strcmp(type, required_type)) {
1150 *size = isize;
1151 if (actual_sha1_return)
1152 memcpy(actual_sha1_return, actual_sha1, 20);
1153 return buffer;
1154 }
1155 /* Handle references */
1156 else if (!strcmp(type, "commit"))
1157 ref_type = "tree ";
1158 else if (!strcmp(type, "tag"))
1159 ref_type = "object ";
1160 else {
1161 free(buffer);
1162 return NULL;
1163 }
1164 ref_length = strlen(ref_type);
1165
1166 if (memcmp(buffer, ref_type, ref_length) ||
1167 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1168 free(buffer);
1169 return NULL;
1170 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001171 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001172 /* Now we have the ID of the referred-to object in
1173 * actual_sha1. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001174 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001175}
1176
Bryan Larsen7672db22005-07-08 16:51:55 -07001177char *write_sha1_file_prepare(void *buf,
1178 unsigned long len,
1179 const char *type,
1180 unsigned char *sha1,
1181 unsigned char *hdr,
1182 int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001183{
1184 SHA_CTX c;
1185
1186 /* Generate the header */
1187 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1188
1189 /* Sha1.. */
1190 SHA1_Init(&c);
1191 SHA1_Update(&c, hdr, *hdrlen);
1192 SHA1_Update(&c, buf, len);
1193 SHA1_Final(sha1, &c);
1194
1195 return sha1_file_name(sha1);
1196}
1197
Linus Torvalds230f1322005-10-08 15:54:01 -07001198/*
1199 * Link the tempfile to the final place, possibly creating the
1200 * last directory level as you do so.
1201 *
1202 * Returns the errno on failure, 0 on success.
1203 */
1204static int link_temp_to_file(const char *tmpfile, char *filename)
1205{
1206 int ret;
1207
1208 if (!link(tmpfile, filename))
1209 return 0;
1210
1211 /*
1212 * Try to mkdir the last path component if that failed
1213 * with an ENOENT.
1214 *
1215 * Re-try the "link()" regardless of whether the mkdir
1216 * succeeds, since a race might mean that somebody
1217 * else succeeded.
1218 */
1219 ret = errno;
1220 if (ret == ENOENT) {
1221 char *dir = strrchr(filename, '/');
1222 if (dir) {
1223 *dir = 0;
1224 mkdir(filename, 0777);
1225 *dir = '/';
1226 if (!link(tmpfile, filename))
1227 return 0;
1228 ret = errno;
1229 }
1230 }
1231 return ret;
1232}
1233
1234/*
1235 * Move the just written object into its final resting place
1236 */
Junio C Hamanob721e012005-10-10 23:22:01 -07001237int move_temp_to_file(const char *tmpfile, char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001238{
1239 int ret = link_temp_to_file(tmpfile, filename);
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001240
1241 /*
1242 * Coda hack - coda doesn't like cross-directory links,
1243 * so we fall back to a rename, which will mean that it
1244 * won't be able to check collisions, but that's not a
1245 * big deal.
1246 *
1247 * The same holds for FAT formatted media.
1248 *
1249 * When this succeeds, we just return 0. We have nothing
1250 * left to unlink.
1251 */
1252 if (ret && ret != EEXIST) {
1253 if (!rename(tmpfile, filename))
Linus Torvalds230f1322005-10-08 15:54:01 -07001254 return 0;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001255 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001256 }
1257 unlink(tmpfile);
1258 if (ret) {
1259 if (ret != EEXIST) {
1260 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1261 return -1;
1262 }
1263 /* FIXME!!! Collision check here ? */
1264 }
1265
1266 return 0;
1267}
1268
Brian Gerstbf0f9102005-05-18 08:14:09 -04001269int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001270{
1271 int size;
Brian Gerstbf0f9102005-05-18 08:14:09 -04001272 unsigned char *compressed;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001273 z_stream stream;
1274 unsigned char sha1[20];
Linus Torvalds706bc532005-04-20 09:28:05 -07001275 char *filename;
Linus Torvaldsaac17942005-05-03 11:46:16 -07001276 static char tmpfile[PATH_MAX];
Brian Gerstbf0f9102005-05-18 08:14:09 -04001277 unsigned char hdr[50];
Linus Torvalds230f1322005-10-08 15:54:01 -07001278 int fd, hdrlen;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001279
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001280 /* Normally if we have it in the pack then we do not bother writing
1281 * it out into .git/objects/??/?{38} file.
1282 */
1283 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
Linus Torvalds706bc532005-04-20 09:28:05 -07001284 if (returnsha1)
1285 memcpy(returnsha1, sha1, 20);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001286 if (has_sha1_file(sha1))
1287 return 0;
Linus Torvaldsaac17942005-05-03 11:46:16 -07001288 fd = open(filename, O_RDONLY);
1289 if (fd >= 0) {
Linus Torvalds706bc532005-04-20 09:28:05 -07001290 /*
Linus Torvaldsaac17942005-05-03 11:46:16 -07001291 * FIXME!!! We might do collision checking here, but we'd
1292 * need to uncompress the old file and check it. Later.
Linus Torvalds706bc532005-04-20 09:28:05 -07001293 */
Linus Torvaldsaac17942005-05-03 11:46:16 -07001294 close(fd);
Linus Torvalds706bc532005-04-20 09:28:05 -07001295 return 0;
1296 }
1297
Linus Torvaldsaac17942005-05-03 11:46:16 -07001298 if (errno != ENOENT) {
1299 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1300 return -1;
1301 }
1302
1303 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
Junio C Hamanoace15342005-05-07 00:38:04 -07001304
Linus Torvaldsaac17942005-05-03 11:46:16 -07001305 fd = mkstemp(tmpfile);
1306 if (fd < 0) {
1307 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1308 return -1;
1309 }
1310
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001311 /* Set it up */
1312 memset(&stream, 0, sizeof(stream));
1313 deflateInit(&stream, Z_BEST_COMPRESSION);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001314 size = deflateBound(&stream, len+hdrlen);
Christopher Li812666c2005-04-26 12:00:58 -07001315 compressed = xmalloc(size);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001316
1317 /* Compress it */
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001318 stream.next_out = compressed;
1319 stream.avail_out = size;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001320
1321 /* First header.. */
1322 stream.next_in = hdr;
1323 stream.avail_in = hdrlen;
1324 while (deflate(&stream, 0) == Z_OK)
Thomas Glanzmann6ffcee82005-05-08 11:34:40 +02001325 /* nothing */;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001326
1327 /* Then the data itself.. */
1328 stream.next_in = buf;
1329 stream.avail_in = len;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001330 while (deflate(&stream, Z_FINISH) == Z_OK)
1331 /* nothing */;
1332 deflateEnd(&stream);
1333 size = stream.total_out;
1334
Linus Torvalds706bc532005-04-20 09:28:05 -07001335 if (write(fd, compressed, size) != size)
1336 die("unable to write file");
Linus Torvaldsaac17942005-05-03 11:46:16 -07001337 fchmod(fd, 0444);
Linus Torvalds706bc532005-04-20 09:28:05 -07001338 close(fd);
Junio C Hamano383f85b2005-05-07 01:27:00 -07001339 free(compressed);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001340
Linus Torvalds230f1322005-10-08 15:54:01 -07001341 return move_temp_to_file(tmpfile, filename);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001342}
Daniel Barkalow8237b182005-04-23 18:47:23 -07001343
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001344int write_sha1_to_fd(int fd, const unsigned char *sha1)
1345{
1346 ssize_t size;
1347 unsigned long objsize;
1348 int posn = 0;
Sergey Vlasovbfc66da2005-08-08 22:45:36 +04001349 void *map = map_sha1_file_internal(sha1, &objsize);
1350 void *buf = map;
1351 void *temp_obj = NULL;
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001352 z_stream stream;
Sergey Vlasovbfc66da2005-08-08 22:45:36 +04001353
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001354 if (!buf) {
1355 unsigned char *unpacked;
1356 unsigned long len;
1357 char type[20];
1358 char hdr[50];
1359 int hdrlen;
1360 // need to unpack and recompress it by itself
1361 unpacked = read_packed_sha1(sha1, type, &len);
1362
1363 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1364
1365 /* Set it up */
1366 memset(&stream, 0, sizeof(stream));
1367 deflateInit(&stream, Z_BEST_COMPRESSION);
1368 size = deflateBound(&stream, len + hdrlen);
Sergey Vlasovbfc66da2005-08-08 22:45:36 +04001369 temp_obj = buf = xmalloc(size);
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001370
1371 /* Compress it */
1372 stream.next_out = buf;
1373 stream.avail_out = size;
1374
1375 /* First header.. */
Linus Torvalds0ee19dc2005-07-10 15:43:54 -07001376 stream.next_in = (void *)hdr;
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001377 stream.avail_in = hdrlen;
1378 while (deflate(&stream, 0) == Z_OK)
1379 /* nothing */;
1380
1381 /* Then the data itself.. */
1382 stream.next_in = unpacked;
1383 stream.avail_in = len;
1384 while (deflate(&stream, Z_FINISH) == Z_OK)
1385 /* nothing */;
1386 deflateEnd(&stream);
Sergey Vlasovbfc66da2005-08-08 22:45:36 +04001387 free(unpacked);
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001388
1389 objsize = stream.total_out;
1390 }
1391
1392 do {
1393 size = write(fd, buf + posn, objsize - posn);
1394 if (size <= 0) {
1395 if (!size) {
1396 fprintf(stderr, "write closed");
1397 } else {
1398 perror("write ");
1399 }
1400 return -1;
1401 }
1402 posn += size;
1403 } while (posn < objsize);
Sergey Vlasovbfc66da2005-08-08 22:45:36 +04001404
1405 if (map)
1406 munmap(map, objsize);
1407 if (temp_obj)
1408 free(temp_obj);
1409
Daniel Barkalowa5eda522005-07-10 18:25:38 -04001410 return 0;
1411}
1412
barkalow@iabervon.org70b98292005-08-02 19:46:29 -04001413int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1414 size_t bufsize, size_t *bufposn)
Daniel Barkalow8237b182005-04-23 18:47:23 -07001415{
Linus Torvalds230f1322005-10-08 15:54:01 -07001416 char tmpfile[PATH_MAX];
Daniel Barkalow8237b182005-04-23 18:47:23 -07001417 int local;
1418 z_stream stream;
1419 unsigned char real_sha1[20];
Brian Gerstbf0f9102005-05-18 08:14:09 -04001420 unsigned char discard[4096];
Daniel Barkalow8237b182005-04-23 18:47:23 -07001421 int ret;
1422 SHA_CTX c;
1423
Linus Torvalds230f1322005-10-08 15:54:01 -07001424 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
Daniel Barkalow8237b182005-04-23 18:47:23 -07001425
Linus Torvalds230f1322005-10-08 15:54:01 -07001426 local = mkstemp(tmpfile);
Daniel Barkalow8237b182005-04-23 18:47:23 -07001427 if (local < 0)
Linus Torvalds230f1322005-10-08 15:54:01 -07001428 return error("Couldn't open %s for %s\n", tmpfile, sha1_to_hex(sha1));
Daniel Barkalow8237b182005-04-23 18:47:23 -07001429
1430 memset(&stream, 0, sizeof(stream));
1431
1432 inflateInit(&stream);
1433
1434 SHA1_Init(&c);
1435
1436 do {
1437 ssize_t size;
barkalow@iabervon.org70b98292005-08-02 19:46:29 -04001438 if (*bufposn) {
1439 stream.avail_in = *bufposn;
Pavel Roskin96ad15a2005-08-09 16:54:40 -04001440 stream.next_in = (unsigned char *) buffer;
barkalow@iabervon.org70b98292005-08-02 19:46:29 -04001441 do {
1442 stream.next_out = discard;
1443 stream.avail_out = sizeof(discard);
1444 ret = inflate(&stream, Z_SYNC_FLUSH);
1445 SHA1_Update(&c, discard, sizeof(discard) -
1446 stream.avail_out);
1447 } while (stream.avail_in && ret == Z_OK);
1448 write(local, buffer, *bufposn - stream.avail_in);
1449 memmove(buffer, buffer + *bufposn - stream.avail_in,
1450 stream.avail_in);
1451 *bufposn = stream.avail_in;
1452 if (ret != Z_OK)
1453 break;
1454 }
1455 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
Daniel Barkalow8237b182005-04-23 18:47:23 -07001456 if (size <= 0) {
1457 close(local);
Linus Torvalds230f1322005-10-08 15:54:01 -07001458 unlink(tmpfile);
Daniel Barkalow8237b182005-04-23 18:47:23 -07001459 if (!size)
1460 return error("Connection closed?");
1461 perror("Reading from connection");
1462 return -1;
1463 }
barkalow@iabervon.org70b98292005-08-02 19:46:29 -04001464 *bufposn += size;
1465 } while (1);
Daniel Barkalow8237b182005-04-23 18:47:23 -07001466 inflateEnd(&stream);
1467
1468 close(local);
1469 SHA1_Final(real_sha1, &c);
1470 if (ret != Z_STREAM_END) {
Linus Torvalds230f1322005-10-08 15:54:01 -07001471 unlink(tmpfile);
Daniel Barkalow8237b182005-04-23 18:47:23 -07001472 return error("File %s corrupted", sha1_to_hex(sha1));
1473 }
1474 if (memcmp(sha1, real_sha1, 20)) {
Linus Torvalds230f1322005-10-08 15:54:01 -07001475 unlink(tmpfile);
Daniel Barkalow8237b182005-04-23 18:47:23 -07001476 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1477 }
Linus Torvalds230f1322005-10-08 15:54:01 -07001478
1479 return move_temp_to_file(tmpfile, sha1_file_name(sha1));
Daniel Barkalow8237b182005-04-23 18:47:23 -07001480}
1481
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001482int has_pack_index(const unsigned char *sha1)
1483{
1484 struct stat st;
1485 if (stat(sha1_pack_index_name(sha1), &st))
1486 return 0;
1487 return 1;
1488}
1489
1490int has_pack_file(const unsigned char *sha1)
1491{
1492 struct stat st;
1493 if (stat(sha1_pack_name(sha1), &st))
1494 return 0;
1495 return 1;
1496}
1497
Linus Torvaldsdade09c2005-07-03 13:06:36 -07001498int has_sha1_pack(const unsigned char *sha1)
1499{
1500 struct pack_entry e;
1501 return find_pack_entry(sha1, &e);
1502}
1503
Daniel Barkalow8237b182005-04-23 18:47:23 -07001504int has_sha1_file(const unsigned char *sha1)
1505{
Daniel Barkalow8237b182005-04-23 18:47:23 -07001506 struct stat st;
Junio C Hamano1f688552005-06-27 03:35:33 -07001507 struct pack_entry e;
1508
Junio C Hamanoab90ea52005-07-11 00:00:55 -07001509 if (find_pack_entry(sha1, &e))
Junio C Hamano1f688552005-06-27 03:35:33 -07001510 return 1;
Junio C Hamanoab90ea52005-07-11 00:00:55 -07001511 return find_sha1_file(sha1, &st) ? 1 : 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07001512}
Junio C Hamano74400e72005-05-01 23:45:49 -07001513
Bryan Larsen7672db22005-07-08 16:51:55 -07001514int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
Junio C Hamano74400e72005-05-01 23:45:49 -07001515{
Junio C Hamano74400e72005-05-01 23:45:49 -07001516 unsigned long size = st->st_size;
Linus Torvaldsaac17942005-05-03 11:46:16 -07001517 void *buf;
1518 int ret;
Bryan Larsen7672db22005-07-08 16:51:55 -07001519 unsigned char hdr[50];
1520 int hdrlen;
Junio C Hamano74400e72005-05-01 23:45:49 -07001521
Linus Torvaldsaac17942005-05-03 11:46:16 -07001522 buf = "";
Junio C Hamano74400e72005-05-01 23:45:49 -07001523 if (size)
Linus Torvaldsaac17942005-05-03 11:46:16 -07001524 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano74400e72005-05-01 23:45:49 -07001525 close(fd);
Pavel Roskine35f9822005-07-29 10:49:14 -04001526 if (buf == MAP_FAILED)
Junio C Hamano74400e72005-05-01 23:45:49 -07001527 return -1;
1528
Bryan Larsen7672db22005-07-08 16:51:55 -07001529 if (!type)
1530 type = "blob";
1531 if (write_object)
1532 ret = write_sha1_file(buf, size, type, sha1);
1533 else {
1534 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1535 ret = 0;
1536 }
Linus Torvaldsaac17942005-05-03 11:46:16 -07001537 if (size)
1538 munmap(buf, size);
1539 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07001540}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001541
1542int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
1543{
1544 int fd;
1545 char *target;
1546
1547 switch (st->st_mode & S_IFMT) {
1548 case S_IFREG:
1549 fd = open(path, O_RDONLY);
1550 if (fd < 0)
1551 return error("open(\"%s\"): %s", path,
1552 strerror(errno));
1553 if (index_fd(sha1, fd, st, write_object, NULL) < 0)
1554 return error("%s: failed to insert into database",
1555 path);
1556 break;
1557 case S_IFLNK:
1558 target = xmalloc(st->st_size+1);
1559 if (readlink(path, target, st->st_size+1) != st->st_size) {
1560 char *errstr = strerror(errno);
1561 free(target);
1562 return error("readlink(\"%s\"): %s", path,
1563 errstr);
1564 }
1565 if (!write_object) {
1566 unsigned char hdr[50];
1567 int hdrlen;
1568 write_sha1_file_prepare(target, st->st_size, "blob",
1569 sha1, hdr, &hdrlen);
1570 } else if (write_sha1_file(target, st->st_size, "blob", sha1))
1571 return error("%s: failed to insert into database",
1572 path);
1573 free(target);
1574 break;
1575 default:
1576 return error("%s: unsupported file type", path);
1577 }
1578 return 0;
1579}