blob: b9c1fa3f1a0c24fa411d590987b60362cae6f943 [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 */
Linus Torvalds0fcfd162005-04-18 13:04:43 -07009#include "cache.h"
Michael Haggerty6eac50d2012-11-05 09:41:22 +010010#include "string-list.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020011#include "lockfile.h"
Junio C Hamano1f688552005-06-27 03:35:33 -070012#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070013#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020014#include "blob.h"
15#include "commit.h"
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -070016#include "run-command.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020017#include "tag.h"
18#include "tree.h"
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +070019#include "tree-walk.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070020#include "refs.h"
Nicolas Pitre70f5d5d2008-02-28 00:25:19 -050021#include "pack-revindex.h"
Junio C Hamano628522e2007-12-29 02:05:47 -080022#include "sha1-lookup.h"
Junio C Hamano568508e2011-10-28 14:48:40 -070023#include "bulk-checkin.h"
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +070024#include "streaming.h"
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070025#include "dir.h"
Jeff Kinga73cdd22016-07-29 00:09:46 -040026#include "mru.h"
Jeff King12d95ef2016-08-22 17:59:42 -040027#include "list.h"
René Scharfec4c6eff2016-09-13 19:54:42 +020028#include "mergesort.h"
Linus Torvalds0fcfd162005-04-18 13:04:43 -070029
Linus Torvalds144bde72005-04-23 11:09:32 -070030#ifndef O_NOATIME
31#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
32#define O_NOATIME 01000000
33#else
34#define O_NOATIME 0
35#endif
36#endif
37
Jonathan Nieder28bd70d2011-03-16 00:15:31 -050038#define SZ_FMT PRIuMAX
39static inline uintmax_t sz_fmt(size_t s) { return s; }
Pavel Roskine05db0f2007-01-09 23:07:11 -050040
David Rientjes96f1e582006-08-15 10:23:48 -070041const unsigned char null_sha1[20];
brian m. carlson3e56e722015-12-06 22:16:35 +000042const struct object_id null_oid;
Jacob Keller8576fde2016-08-31 16:27:18 -070043const struct object_id empty_tree_oid = {
44 EMPTY_TREE_SHA1_BIN_LITERAL
45};
46const struct object_id empty_blob_oid = {
47 EMPTY_BLOB_SHA1_BIN_LITERAL
48};
Junio C Hamano88cd6212005-09-30 14:02:47 -070049
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +070050/*
51 * This is meant to hold a *small* number of objects that you would
52 * want read_sha1_file() to be able to return, but yet you do not want
53 * to write them into the object store (e.g. a browse-only
54 * application).
55 */
56static struct cached_object {
57 unsigned char sha1[20];
58 enum object_type type;
59 void *buf;
60 unsigned long size;
61} *cached_objects;
62static int cached_object_nr, cached_object_alloc;
63
64static struct cached_object empty_tree = {
Jonathan Niederdab0d412011-02-07 02:17:27 -060065 EMPTY_TREE_SHA1_BIN_LITERAL,
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +070066 OBJ_TREE,
67 "",
68 0
69};
70
71static struct cached_object *find_cached_object(const unsigned char *sha1)
72{
73 int i;
74 struct cached_object *co = cached_objects;
75
76 for (i = 0; i < cached_object_nr; i++, co++) {
77 if (!hashcmp(co->sha1, sha1))
78 return co;
79 }
80 if (!hashcmp(sha1, empty_tree.sha1))
81 return &empty_tree;
82 return NULL;
83}
84
Junio C Hamano90a64642011-03-10 16:02:50 -080085int mkdir_in_gitdir(const char *path)
86{
87 if (mkdir(path, 0777)) {
88 int saved_errno = errno;
89 struct stat st;
90 struct strbuf sb = STRBUF_INIT;
91
92 if (errno != EEXIST)
93 return -1;
94 /*
95 * Are we looking at a path in a symlinked worktree
96 * whose original repository does not yet have it?
97 * e.g. .git/rr-cache pointing at its original
98 * repository in which the user hasn't performed any
99 * conflict resolution yet?
100 */
101 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
102 strbuf_readlink(&sb, path, st.st_size) ||
103 !is_absolute_path(sb.buf) ||
104 mkdir(sb.buf, 0777)) {
105 strbuf_release(&sb);
106 errno = saved_errno;
107 return -1;
108 }
109 strbuf_release(&sb);
110 }
111 return adjust_shared_perm(path);
112}
113
Michael Haggerty0be05212014-01-06 14:45:25 +0100114enum scld_error safe_create_leading_directories(char *path)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700115{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100116 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100117 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800118
Michael Haggerty0be05212014-01-06 14:45:25 +0100119 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100120 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100121 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100122
Michael Haggerty0f527402014-01-19 00:40:44 +0100123 while (*slash && !is_dir_sep(*slash))
124 slash++;
125
126 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700127 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100128
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100129 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100130 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100131 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100132 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700133 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100134
Michael Haggerty0f527402014-01-19 00:40:44 +0100135 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100136 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800137 if (!stat(path, &st)) {
138 /* path exists */
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100139 if (!S_ISDIR(st.st_mode))
Michael Haggerty0be05212014-01-06 14:45:25 +0100140 ret = SCLD_EXISTS;
Michael Haggerty53a39722014-01-06 14:45:19 +0100141 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400142 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100143 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400144 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100145 else if (errno == ENOENT)
146 /*
147 * Either mkdir() failed because
148 * somebody just pruned the containing
149 * directory, or stat() failed because
150 * the file that was in our way was
151 * just removed. Either way, inform
152 * the caller that it might be worth
153 * trying again:
154 */
155 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100156 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100157 ret = SCLD_FAILED;
Michael Haggerty53a39722014-01-06 14:45:19 +0100158 } else if (adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100159 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800160 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100161 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700162 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100163 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700164}
165
Michael Haggerty0be05212014-01-06 14:45:25 +0100166enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400167{
168 /* path points to cache entries, so xstrdup before messing with it */
169 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100170 enum scld_error result = safe_create_leading_directories(buf);
Jeff King8e21d632008-06-25 01:41:34 -0400171 free(buf);
172 return result;
173}
174
Junio C Hamanoace15342005-05-07 00:38:04 -0700175static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
176{
177 int i;
178 for (i = 0; i < 20; i++) {
179 static char hex[] = "0123456789abcdef";
180 unsigned int val = sha1[i];
181 char *pos = pathbuf + i*2 + (i > 0);
182 *pos++ = hex[val >> 4];
183 *pos = hex[val & 0xf];
184 }
185}
186
Michael Haggerty30d6c6e2014-02-21 17:32:05 +0100187const char *sha1_file_name(const unsigned char *sha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700188{
Jeff King560fb6a2010-05-22 02:59:42 -0400189 static char buf[PATH_MAX];
190 const char *objdir;
191 int len;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700192
Jeff King560fb6a2010-05-22 02:59:42 -0400193 objdir = get_object_directory();
194 len = strlen(objdir);
195
196 /* '/' + sha1(2) + '/' + sha1(38) + '\0' */
197 if (len + 43 > PATH_MAX)
198 die("insanely long object directory %s", objdir);
199 memcpy(buf, objdir, len);
200 buf[len] = '/';
201 buf[len+3] = '/';
202 buf[len+42] = '\0';
203 fill_sha1_path(buf + len + 1, sha1);
204 return buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700205}
206
Michael Haggertyd40d5352014-02-21 17:32:06 +0100207/*
208 * Return the name of the pack or index file with the specified sha1
209 * in its filename. *base and *name are scratch space that must be
210 * provided by the caller. which should be "pack" or "idx".
211 */
Heikki Orsila633f43e2008-05-24 01:43:55 +0300212static char *sha1_get_pack_name(const unsigned char *sha1,
Jeff Kingac5190c2015-09-24 17:07:34 -0400213 struct strbuf *buf,
214 const char *which)
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400215{
Jeff Kingac5190c2015-09-24 17:07:34 -0400216 strbuf_reset(buf);
217 strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
218 sha1_to_hex(sha1), which);
219 return buf->buf;
Heikki Orsila633f43e2008-05-24 01:43:55 +0300220}
221
222char *sha1_pack_name(const unsigned char *sha1)
223{
Jeff Kingac5190c2015-09-24 17:07:34 -0400224 static struct strbuf buf = STRBUF_INIT;
225 return sha1_get_pack_name(sha1, &buf, "pack");
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400226}
227
228char *sha1_pack_index_name(const unsigned char *sha1)
229{
Jeff Kingac5190c2015-09-24 17:07:34 -0400230 static struct strbuf buf = STRBUF_INIT;
231 return sha1_get_pack_name(sha1, &buf, "idx");
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -0400232}
233
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700234struct alternate_object_database *alt_odb_list;
235static struct alternate_object_database **alt_odb_tail;
Junio C Hamanoace15342005-05-07 00:38:04 -0700236
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700237/*
238 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700239 *
240 * The variable alt_odb_list points at the list of struct
241 * alternate_object_database. The elements on this list come from
242 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
243 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800244 * whose contents is similar to that environment variable but can be
245 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700246 * contains "/the/directory/corresponding/to/.git/objects/...", while
247 * its name points just after the slash at the end of ".git/objects/"
248 * in the example above, and has enough space to hold 40-byte hex
249 * SHA1, an extra slash for the first level indirection, and the
250 * terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700251 */
Ephrim Khong539e7502014-07-15 13:29:45 +0200252static int link_alt_odb_entry(const char *entry, const char *relative_base,
253 int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200254{
Martin Waitzc2f493a2006-05-07 20:19:21 +0200255 struct alternate_object_database *ent;
256 struct alternate_object_database *alt;
Jeff King50a6c8e2016-02-22 17:44:35 -0500257 size_t pfxlen, entlen;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800258 struct strbuf pathbuf = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200259
Johannes Sixt85dadc32007-11-13 21:05:00 +0100260 if (!is_absolute_path(entry) && relative_base) {
Hui Wang5bdf0a82011-09-07 18:37:47 +0800261 strbuf_addstr(&pathbuf, real_path(relative_base));
262 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200263 }
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100264 strbuf_addstr(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800265
266 normalize_path_copy(pathbuf.buf, pathbuf.buf);
267
268 pfxlen = strlen(pathbuf.buf);
269
270 /*
271 * The trailing slash after the directory name is given by
272 * this function at the end. Remove duplicates.
273 */
274 while (pfxlen && pathbuf.buf[pfxlen-1] == '/')
275 pfxlen -= 1;
276
Jeff King50a6c8e2016-02-22 17:44:35 -0500277 entlen = st_add(pfxlen, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
278 ent = xmalloc(st_add(sizeof(*ent), entlen));
Hui Wang5bdf0a82011-09-07 18:37:47 +0800279 memcpy(ent->base, pathbuf.buf, pfxlen);
280 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200281
282 ent->name = ent->base + pfxlen + 1;
283 ent->base[pfxlen + 3] = '/';
284 ent->base[pfxlen] = ent->base[entlen-1] = 0;
285
286 /* Detect cases where alternate disappeared */
Junio C Hamano90b4a712008-09-09 01:27:07 -0700287 if (!is_directory(ent->base)) {
Martin Waitzc2f493a2006-05-07 20:19:21 +0200288 error("object directory %s does not exist; "
289 "check .git/objects/info/alternates.",
290 ent->base);
291 free(ent);
292 return -1;
293 }
294
295 /* Prevent the common mistake of listing the same
296 * thing twice, or object directory itself.
297 */
298 for (alt = alt_odb_list; alt; alt = alt->next) {
René Scharfe80b47852014-07-01 20:00:01 +0200299 if (pfxlen == alt->name - alt->base - 1 &&
300 !memcmp(ent->base, alt->base, pfxlen)) {
Martin Waitzc2f493a2006-05-07 20:19:21 +0200301 free(ent);
302 return -1;
303 }
304 }
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +0700305 if (!fspathcmp(ent->base, normalized_objdir)) {
Martin Waitzc2f493a2006-05-07 20:19:21 +0200306 free(ent);
307 return -1;
308 }
309
310 /* add the alternate entry */
311 *alt_odb_tail = ent;
312 alt_odb_tail = &(ent->next);
313 ent->next = NULL;
314
315 /* recursively add alternates */
316 read_info_alternates(ent->base, depth + 1);
317
318 ent->base[pfxlen] = '/';
319
320 return 0;
321}
322
Michael Haggertyc5950162012-11-05 09:41:23 +0100323static void link_alt_odb_entries(const char *alt, int len, int sep,
Martin Waitzc2f493a2006-05-07 20:19:21 +0200324 const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700325{
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100326 struct string_list entries = STRING_LIST_INIT_NODUP;
327 char *alt_copy;
328 int i;
Ephrim Khong539e7502014-07-15 13:29:45 +0200329 struct strbuf objdirbuf = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200330
331 if (depth > 5) {
332 error("%s: ignoring alternate object stores, nesting too deep.",
333 relative_base);
334 return;
335 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700336
Junio C Hamanof6556512014-09-02 13:27:45 -0700337 strbuf_add_absolute_path(&objdirbuf, get_object_directory());
Ephrim Khong539e7502014-07-15 13:29:45 +0200338 normalize_path_copy(objdirbuf.buf, objdirbuf.buf);
339
Michael Haggertyc5950162012-11-05 09:41:23 +0100340 alt_copy = xmemdupz(alt, len);
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100341 string_list_split_in_place(&entries, alt_copy, sep, -1);
342 for (i = 0; i < entries.nr; i++) {
343 const char *entry = entries.items[i].string;
344 if (entry[0] == '\0' || entry[0] == '#')
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700345 continue;
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100346 if (!is_absolute_path(entry) && depth) {
347 error("%s: ignoring relative alternate object store %s",
348 relative_base, entry);
349 } else {
Ephrim Khong539e7502014-07-15 13:29:45 +0200350 link_alt_odb_entry(entry, relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700351 }
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700352 }
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100353 string_list_clear(&entries, 0);
354 free(alt_copy);
Ephrim Khong539e7502014-07-15 13:29:45 +0200355 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700356}
357
Heiko Voigt5e736332012-05-14 18:24:45 +0200358void read_info_alternates(const char * relative_base, int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700359{
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700360 char *map;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500361 size_t mapsz;
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700362 struct stat st;
Jeff King5015f012015-08-19 14:12:45 -0400363 char *path;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200364 int fd;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700365
Jeff King5015f012015-08-19 14:12:45 -0400366 path = xstrfmt("%s/info/alternates", relative_base);
Junio C Hamanof4e51682011-05-15 12:16:29 -0700367 fd = git_open_noatime(path);
Jeff King5015f012015-08-19 14:12:45 -0400368 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700369 if (fd < 0)
370 return;
371 if (fstat(fd, &st) || (st.st_size == 0)) {
372 close(fd);
373 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700374 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500375 mapsz = xsize_t(st.st_size);
376 map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700377 close(fd);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700378
Michael Haggertyc5950162012-11-05 09:41:23 +0100379 link_alt_odb_entries(map, mapsz, '\n', relative_base, depth);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200380
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500381 munmap(map, mapsz);
Junio C Hamanoace15342005-05-07 00:38:04 -0700382}
383
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400384void add_to_alternates_file(const char *reference)
385{
386 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
Jeff King77b9b1d2015-08-10 05:34:46 -0400387 char *alts = git_pathdup("objects/info/alternates");
388 FILE *in, *out;
389
390 hold_lock_file_for_update(lock, alts, LOCK_DIE_ON_ERROR);
391 out = fdopen_lock_file(lock, "w");
392 if (!out)
393 die_errno("unable to fdopen alternates lockfile");
394
395 in = fopen(alts, "r");
396 if (in) {
397 struct strbuf line = STRBUF_INIT;
398 int found = 0;
399
Junio C Hamano3f163962015-10-28 13:29:24 -0700400 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400401 if (!strcmp(reference, line.buf)) {
402 found = 1;
403 break;
404 }
405 fprintf_or_die(out, "%s\n", line.buf);
406 }
407
408 strbuf_release(&line);
409 fclose(in);
410
411 if (found) {
412 rollback_lock_file(lock);
413 lock = NULL;
414 }
415 }
416 else if (errno != ENOENT)
417 die_errno("unable to read alternates file");
418
419 if (lock) {
420 fprintf_or_die(out, "%s\n", reference);
421 if (commit_lock_file(lock))
422 die_errno("unable to move new alternates file into place");
423 if (alt_odb_tail)
424 link_alt_odb_entries(reference, strlen(reference), '\n', NULL, 0);
425 }
426 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400427}
428
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700429/*
430 * Compute the exact path an alternate is at and returns it. In case of
431 * error NULL is returned and the human readable error is added to `err`
432 * `path` may be relative and should point to $GITDIR.
433 * `err` must not be null.
434 */
435char *compute_alternate_path(const char *path, struct strbuf *err)
436{
437 char *ref_git = NULL;
438 const char *repo, *ref_git_s;
439 int seen_error = 0;
440
441 ref_git_s = real_path_if_valid(path);
442 if (!ref_git_s) {
443 seen_error = 1;
444 strbuf_addf(err, _("path '%s' does not exist"), path);
445 goto out;
446 } else
447 /*
448 * Beware: read_gitfile(), real_path() and mkpath()
449 * return static buffer
450 */
451 ref_git = xstrdup(ref_git_s);
452
453 repo = read_gitfile(ref_git);
454 if (!repo)
455 repo = read_gitfile(mkpath("%s/.git", ref_git));
456 if (repo) {
457 free(ref_git);
458 ref_git = xstrdup(repo);
459 }
460
461 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
462 char *ref_git_git = mkpathdup("%s/.git", ref_git);
463 free(ref_git);
464 ref_git = ref_git_git;
465 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
466 struct strbuf sb = STRBUF_INIT;
467 seen_error = 1;
468 if (get_common_dir(&sb, ref_git)) {
469 strbuf_addf(err,
470 _("reference repository '%s' as a linked "
471 "checkout is not supported yet."),
472 path);
473 goto out;
474 }
475
476 strbuf_addf(err, _("reference repository '%s' is not a "
477 "local repository."), path);
478 goto out;
479 }
480
481 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
482 strbuf_addf(err, _("reference repository '%s' is shallow"),
483 path);
484 seen_error = 1;
485 goto out;
486 }
487
488 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
489 strbuf_addf(err,
490 _("reference repository '%s' is grafted"),
491 path);
492 seen_error = 1;
493 goto out;
494 }
495
496out:
497 if (seen_error) {
498 free(ref_git);
499 ref_git = NULL;
500 }
501
502 return ref_git;
503}
504
Jeff Kingfe1b2262014-10-15 18:33:13 -0400505int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700506{
507 struct alternate_object_database *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400508 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700509
510 prepare_alt_odb();
Jeff Kingfe1b2262014-10-15 18:33:13 -0400511 for (ent = alt_odb_list; ent; ent = ent->next) {
512 r = fn(ent, cb);
513 if (r)
514 break;
515 }
516 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700517}
518
Martin Waitzc2f493a2006-05-07 20:19:21 +0200519void prepare_alt_odb(void)
520{
Timo Hirvonen554fe202006-06-28 12:04:39 +0300521 const char *alt;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200522
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400523 if (alt_odb_tail)
524 return;
525
Martin Waitzc2f493a2006-05-07 20:19:21 +0200526 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
527 if (!alt) alt = "";
528
Martin Waitzc2f493a2006-05-07 20:19:21 +0200529 alt_odb_tail = &alt_odb_list;
Michael Haggertyc5950162012-11-05 09:41:23 +0100530 link_alt_odb_entries(alt, strlen(alt), PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200531
532 read_info_alternates(get_object_directory(), 0);
533}
534
Jeff King3096b2e2015-07-08 16:33:52 -0400535/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400536static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700537{
Jeff King33d42212014-10-15 18:42:22 -0400538 struct utimbuf t;
539 t.actime = t.modtime = time(NULL);
540 return !utime(fn, &t);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600541}
Junio C Hamanoace15342005-05-07 00:38:04 -0700542
Jeff King3096b2e2015-07-08 16:33:52 -0400543/*
544 * All of the check_and_freshen functions return 1 if the file exists and was
545 * freshened (if freshening was requested), 0 otherwise. If they return
546 * 0, you should not assume that it is safe to skip a write of the object (it
547 * either does not exist on disk, or has a stale mtime and may be subject to
548 * pruning).
549 */
Jeff King33d42212014-10-15 18:42:22 -0400550static int check_and_freshen_file(const char *fn, int freshen)
551{
552 if (access(fn, F_OK))
553 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400554 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400555 return 0;
556 return 1;
557}
558
559static int check_and_freshen_local(const unsigned char *sha1, int freshen)
560{
561 return check_and_freshen_file(sha1_file_name(sha1), freshen);
562}
563
564static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600565{
566 struct alternate_object_database *alt;
Junio C Hamano9a217f22005-06-28 14:56:57 -0700567 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700568 for (alt = alt_odb_list; alt; alt = alt->next) {
Brandon Casey0f4dc142008-11-09 23:59:57 -0600569 fill_sha1_path(alt->name, sha1);
Jeff King33d42212014-10-15 18:42:22 -0400570 if (check_and_freshen_file(alt->base, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700571 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700572 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700573 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700574}
575
Jeff King33d42212014-10-15 18:42:22 -0400576static int check_and_freshen(const unsigned char *sha1, int freshen)
577{
578 return check_and_freshen_local(sha1, freshen) ||
579 check_and_freshen_nonlocal(sha1, freshen);
580}
581
582int has_loose_object_nonlocal(const unsigned char *sha1)
583{
584 return check_and_freshen_nonlocal(sha1, 0);
585}
586
Brandon Casey0f4dc142008-11-09 23:59:57 -0600587static int has_loose_object(const unsigned char *sha1)
588{
Jeff King33d42212014-10-15 18:42:22 -0400589 return check_and_freshen(sha1, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600590}
591
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500592static unsigned int pack_used_ctr;
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500593static unsigned int pack_mmap_calls;
594static unsigned int peak_pack_open_windows;
595static unsigned int pack_open_windows;
Shawn O. Pearcec7934302011-02-28 12:52:39 -0800596static unsigned int pack_open_fds;
597static unsigned int pack_max_fds;
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500598static size_t peak_pack_mapped;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500599static size_t pack_mapped;
Junio C Hamano9a217f22005-06-28 14:56:57 -0700600struct packed_git *packed_git;
Junio C Hamano1f688552005-06-27 03:35:33 -0700601
Jeff Kinga73cdd22016-07-29 00:09:46 -0400602static struct mru packed_git_mru_storage;
603struct mru *packed_git_mru = &packed_git_mru_storage;
604
Junio C Hamanob79d18c2007-06-13 01:22:51 -0700605void pack_report(void)
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500606{
607 fprintf(stderr,
Pavel Roskine05db0f2007-01-09 23:07:11 -0500608 "pack_report: getpagesize() = %10" SZ_FMT "\n"
609 "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
610 "pack_report: core.packedGitLimit = %10" SZ_FMT "\n",
Steffen Prohaska9e42d6a2007-11-21 21:27:19 +0100611 sz_fmt(getpagesize()),
612 sz_fmt(packed_git_window_size),
613 sz_fmt(packed_git_limit));
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500614 fprintf(stderr,
615 "pack_report: pack_used_ctr = %10u\n"
616 "pack_report: pack_mmap_calls = %10u\n"
617 "pack_report: pack_open_windows = %10u / %10u\n"
Pavel Roskine05db0f2007-01-09 23:07:11 -0500618 "pack_report: pack_mapped = "
619 "%10" SZ_FMT " / %10" SZ_FMT "\n",
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500620 pack_used_ctr,
621 pack_mmap_calls,
622 pack_open_windows, peak_pack_open_windows,
Steffen Prohaska9e42d6a2007-11-21 21:27:19 +0100623 sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500624}
625
Michael Haggertyd40d5352014-02-21 17:32:06 +0100626/*
627 * Open and mmap the index file at path, perform a couple of
628 * consistency checks, then record its information to p. Return 0 on
629 * success.
630 */
631static int check_packed_git_idx(const char *path, struct packed_git *p)
Junio C Hamano1f688552005-06-27 03:35:33 -0700632{
633 void *idx_map;
Nicolas Pitre42873072007-03-16 16:42:50 -0400634 struct pack_idx_header *hdr;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500635 size_t idx_size;
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400636 uint32_t version, nr, i, *index;
Junio C Hamanof4e51682011-05-15 12:16:29 -0700637 int fd = git_open_noatime(path);
Junio C Hamano1f688552005-06-27 03:35:33 -0700638 struct stat st;
Nicolas Pitre42873072007-03-16 16:42:50 -0400639
Junio C Hamano1f688552005-06-27 03:35:33 -0700640 if (fd < 0)
641 return -1;
642 if (fstat(fd, &st)) {
643 close(fd);
644 return -1;
645 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500646 idx_size = xsize_t(st.st_size);
Shawn O. Pearce2d884512007-03-06 20:44:11 -0500647 if (idx_size < 4 * 256 + 20 + 20) {
648 close(fd);
649 return error("index file %s is too small", path);
650 }
Shawn O. Pearcec4712e42006-12-24 00:47:23 -0500651 idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano1f688552005-06-27 03:35:33 -0700652 close(fd);
Junio C Hamano1f688552005-06-27 03:35:33 -0700653
Nicolas Pitre42873072007-03-16 16:42:50 -0400654 hdr = idx_map;
655 if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400656 version = ntohl(hdr->idx_version);
657 if (version < 2 || version > 2) {
658 munmap(idx_map, idx_size);
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100659 return error("index file %s is version %"PRIu32
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400660 " and is not supported by this binary"
661 " (try upgrading GIT to a newer version)",
662 path, version);
663 }
664 } else
665 version = 1;
Shawn O. Pearcedf1b0592007-01-17 20:43:57 -0500666
Junio C Hamano1f688552005-06-27 03:35:33 -0700667 nr = 0;
Nicolas Pitre42873072007-03-16 16:42:50 -0400668 index = idx_map;
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400669 if (version > 1)
670 index += 2; /* skip index header */
Junio C Hamano1f688552005-06-27 03:35:33 -0700671 for (i = 0; i < 256; i++) {
Shawn O. Pearce326bf392007-03-06 20:44:19 -0500672 uint32_t n = ntohl(index[i]);
Shawn O. Pearce2d884512007-03-06 20:44:11 -0500673 if (n < nr) {
674 munmap(idx_map, idx_size);
Shawn O. Pearcedf1b0592007-01-17 20:43:57 -0500675 return error("non-monotonic index %s", path);
Shawn O. Pearce2d884512007-03-06 20:44:11 -0500676 }
Junio C Hamano1f688552005-06-27 03:35:33 -0700677 nr = n;
678 }
679
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400680 if (version == 1) {
681 /*
682 * Total size:
683 * - 256 index entries 4 bytes each
684 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
685 * - 20-byte SHA1 of the packfile
686 * - 20-byte SHA1 file checksum
687 */
688 if (idx_size != 4*256 + nr * 24 + 20 + 20) {
689 munmap(idx_map, idx_size);
Luiz Fernando N. Capitulinoeef427a2007-08-14 16:42:37 -0300690 return error("wrong index v1 file size in %s", path);
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400691 }
692 } else if (version == 2) {
693 /*
694 * Minimum size:
695 * - 8 bytes of header
696 * - 256 index entries 4 bytes each
697 * - 20-byte sha1 entry * nr
698 * - 4-byte crc entry * nr
699 * - 4-byte offset entry * nr
700 * - 20-byte SHA1 of the packfile
701 * - 20-byte SHA1 file checksum
702 * And after the 4-byte offset table might be a
703 * variable sized table containing 8-byte entries
704 * for offsets larger than 2^31.
705 */
706 unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
Linus Torvalds1164f1e2007-06-26 14:34:02 -0700707 unsigned long max_size = min_size;
708 if (nr)
709 max_size += (nr - 1)*8;
710 if (idx_size < min_size || idx_size > max_size) {
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400711 munmap(idx_map, idx_size);
Luiz Fernando N. Capitulinoeef427a2007-08-14 16:42:37 -0300712 return error("wrong index v2 file size in %s", path);
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400713 }
Junio C Hamano7109c882007-10-29 11:53:55 -0700714 if (idx_size != min_size &&
715 /*
716 * make sure we can deal with large pack offsets.
717 * 31-bit signed offset won't be enough, neither
718 * 32-bit unsigned one will be.
719 */
720 (sizeof(off_t) <= 4)) {
721 munmap(idx_map, idx_size);
722 return error("pack too large for current definition of off_t in %s", path);
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400723 }
Shawn O. Pearce2d884512007-03-06 20:44:11 -0500724 }
Junio C Hamano1f688552005-06-27 03:35:33 -0700725
Nicolas Pitre74e34e12007-04-09 01:06:35 -0400726 p->index_version = version;
Nicolas Pitre42873072007-03-16 16:42:50 -0400727 p->index_data = idx_map;
728 p->index_size = idx_size;
Nicolas Pitre57059092007-04-09 01:06:28 -0400729 p->num_objects = nr;
Junio C Hamano1f688552005-06-27 03:35:33 -0700730 return 0;
731}
732
Shawn O. Pearcebc8e4782007-05-30 02:13:42 -0400733int open_pack_index(struct packed_git *p)
Shawn O. Pearced0798372007-05-26 01:24:19 -0400734{
735 char *idx_name;
Jeff King9ae97012015-09-24 17:07:09 -0400736 size_t len;
Shawn O. Pearced0798372007-05-26 01:24:19 -0400737 int ret;
738
739 if (p->index_data)
740 return 0;
741
Jeff King9ae97012015-09-24 17:07:09 -0400742 if (!strip_suffix(p->pack_name, ".pack", &len))
743 die("BUG: pack_name does not end in .pack");
744 idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
Shawn O. Pearced0798372007-05-26 01:24:19 -0400745 ret = check_packed_git_idx(idx_name, p);
746 free(idx_name);
747 return ret;
748}
749
Shawn O. Pearce11daf392006-12-23 02:34:44 -0500750static void scan_windows(struct packed_git *p,
751 struct packed_git **lru_p,
752 struct pack_window **lru_w,
753 struct pack_window **lru_l)
754{
755 struct pack_window *w, *w_l;
756
757 for (w_l = NULL, w = p->windows; w; w = w->next) {
758 if (!w->inuse_cnt) {
759 if (!*lru_w || w->last_used < (*lru_w)->last_used) {
760 *lru_p = p;
761 *lru_w = w;
762 *lru_l = w_l;
763 }
764 }
765 w_l = w;
766 }
767}
768
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700769static int unuse_one_window(struct packed_git *current)
Junio C Hamano1f688552005-06-27 03:35:33 -0700770{
Shawn O. Pearce54044bf2006-12-23 02:34:23 -0500771 struct packed_git *p, *lru_p = NULL;
Shawn O. Pearce11daf392006-12-23 02:34:44 -0500772 struct pack_window *lru_w = NULL, *lru_l = NULL;
Junio C Hamanof9253392005-06-29 02:51:27 -0700773
Shawn O. Pearce11daf392006-12-23 02:34:44 -0500774 if (current)
775 scan_windows(current, &lru_p, &lru_w, &lru_l);
776 for (p = packed_git; p; p = p->next)
777 scan_windows(p, &lru_p, &lru_w, &lru_l);
Shawn O. Pearce54044bf2006-12-23 02:34:23 -0500778 if (lru_p) {
779 munmap(lru_w->base, lru_w->len);
780 pack_mapped -= lru_w->len;
781 if (lru_l)
782 lru_l->next = lru_w->next;
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700783 else
Shawn O. Pearce54044bf2006-12-23 02:34:23 -0500784 lru_p->windows = lru_w->next;
Shawn O. Pearce54044bf2006-12-23 02:34:23 -0500785 free(lru_w);
Shawn O. Pearcea53128b2006-12-23 02:34:47 -0500786 pack_open_windows--;
Shawn O. Pearce54044bf2006-12-23 02:34:23 -0500787 return 1;
788 }
789 return 0;
Junio C Hamano1f688552005-06-27 03:35:33 -0700790}
791
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700792void release_pack_memory(size_t need)
Shawn O. Pearce97bfeb32006-12-24 00:47:19 -0500793{
794 size_t cur = pack_mapped;
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700795 while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
Shawn O. Pearce97bfeb32006-12-24 00:47:19 -0500796 ; /* nothing */
797}
798
Steffen Prohaska02710222014-08-26 17:23:23 +0200799static void mmap_limit_check(size_t length)
800{
801 static size_t limit = 0;
802 if (!limit) {
803 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
804 if (!limit)
805 limit = SIZE_MAX;
806 }
807 if (length > limit)
808 die("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX,
809 (uintmax_t)length, (uintmax_t)limit);
810}
811
Jeff King15708562015-05-28 03:56:15 -0400812void *xmmap_gently(void *start, size_t length,
813 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500814{
Steffen Prohaska02710222014-08-26 17:23:23 +0200815 void *ret;
816
817 mmap_limit_check(length);
818 ret = mmap(start, length, prot, flags, fd, offset);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500819 if (ret == MAP_FAILED) {
820 if (!length)
821 return NULL;
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700822 release_pack_memory(length);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500823 ret = mmap(start, length, prot, flags, fd, offset);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500824 }
825 return ret;
826}
827
Jeff King15708562015-05-28 03:56:15 -0400828void *xmmap(void *start, size_t length,
829 int prot, int flags, int fd, off_t offset)
830{
831 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
832 if (ret == MAP_FAILED)
Junio C Hamano9ca0aaf2015-05-27 13:30:29 -0700833 die_errno("mmap failed");
Jeff King15708562015-05-28 03:56:15 -0400834 return ret;
835}
836
Shawn O. Pearcec9ced052008-01-17 22:57:00 -0500837void close_pack_windows(struct packed_git *p)
838{
839 while (p->windows) {
840 struct pack_window *w = p->windows;
841
842 if (w->inuse_cnt)
843 die("pack '%s' still has open windows to it",
844 p->pack_name);
845 munmap(w->base, w->len);
846 pack_mapped -= w->len;
847 pack_open_windows--;
848 p->windows = w->next;
849 free(w);
850 }
851}
852
Johannes Schindelin71fe5d72015-10-05 22:30:24 +0200853static int close_pack_fd(struct packed_git *p)
854{
855 if (p->pack_fd < 0)
856 return 0;
857
858 close(p->pack_fd);
859 pack_open_fds--;
860 p->pack_fd = -1;
861
862 return 1;
863}
864
Johannes Schindelin38849a82015-10-06 15:18:34 +0200865static void close_pack(struct packed_git *p)
866{
867 close_pack_windows(p);
868 close_pack_fd(p);
869 close_pack_index(p);
870}
871
872void close_all_packs(void)
873{
874 struct packed_git *p;
875
876 for (p = packed_git; p; p = p->next)
877 if (p->do_not_close)
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200878 die("BUG: want to close pack marked 'do-not-close'");
Johannes Schindelin38849a82015-10-06 15:18:34 +0200879 else
880 close_pack(p);
881}
882
883
Brandon Casey88d0db52013-08-01 22:36:33 -0700884/*
885 * The LRU pack is the one with the oldest MRU window, preferring packs
886 * with no used windows, or the oldest mtime if it has no windows allocated.
887 */
888static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
889{
890 struct pack_window *w, *this_mru_w;
891 int has_windows_inuse = 0;
892
893 /*
894 * Reject this pack if it has windows and the previously selected
895 * one does not. If this pack does not have windows, reject
896 * it if the pack file is newer than the previously selected one.
897 */
898 if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
899 return;
900
901 for (w = this_mru_w = p->windows; w; w = w->next) {
902 /*
903 * Reject this pack if any of its windows are in use,
904 * but the previously selected pack did not have any
905 * inuse windows. Otherwise, record that this pack
906 * has windows in use.
907 */
908 if (w->inuse_cnt) {
909 if (*accept_windows_inuse)
910 has_windows_inuse = 1;
911 else
912 return;
913 }
914
915 if (w->last_used > this_mru_w->last_used)
916 this_mru_w = w;
917
918 /*
919 * Reject this pack if it has windows that have been
920 * used more recently than the previously selected pack.
921 * If the previously selected pack had windows inuse and
922 * we have not encountered a window in this pack that is
923 * inuse, skip this check since we prefer a pack with no
924 * inuse windows to one that has inuse windows.
925 */
926 if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
927 this_mru_w->last_used > (*mru_w)->last_used)
928 return;
929 }
930
931 /*
932 * Select this pack.
933 */
934 *mru_w = this_mru_w;
935 *lru_p = p;
936 *accept_windows_inuse = has_windows_inuse;
937}
938
939static int close_one_pack(void)
940{
941 struct packed_git *p, *lru_p = NULL;
942 struct pack_window *mru_w = NULL;
943 int accept_windows_inuse = 1;
944
945 for (p = packed_git; p; p = p->next) {
946 if (p->pack_fd == -1)
947 continue;
948 find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
949 }
950
Johannes Schindelin71fe5d72015-10-05 22:30:24 +0200951 if (lru_p)
952 return close_pack_fd(lru_p);
Brandon Casey88d0db52013-08-01 22:36:33 -0700953
954 return 0;
955}
956
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500957void unuse_pack(struct pack_window **w_cursor)
Junio C Hamanof9253392005-06-29 02:51:27 -0700958{
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500959 struct pack_window *w = *w_cursor;
960 if (w) {
961 w->inuse_cnt--;
962 *w_cursor = NULL;
963 }
Junio C Hamanof9253392005-06-29 02:51:27 -0700964}
965
Shawn O. Pearcefa5fc152010-04-19 07:23:06 -0700966void close_pack_index(struct packed_git *p)
967{
968 if (p->index_data) {
969 munmap((void *)p->index_data, p->index_size);
970 p->index_data = NULL;
971 }
972}
973
Joachim Schmitza0788262012-08-24 11:52:22 +0200974static unsigned int get_max_fd_limit(void)
975{
976#ifdef RLIMIT_NOFILE
Junio C Hamano491a8de2013-12-18 14:59:12 -0800977 {
978 struct rlimit lim;
Joachim Schmitza0788262012-08-24 11:52:22 +0200979
Junio C Hamano491a8de2013-12-18 14:59:12 -0800980 if (!getrlimit(RLIMIT_NOFILE, &lim))
981 return lim.rlim_cur;
982 }
983#endif
Joachim Schmitza0788262012-08-24 11:52:22 +0200984
Junio C Hamano491a8de2013-12-18 14:59:12 -0800985#ifdef _SC_OPEN_MAX
986 {
987 long open_max = sysconf(_SC_OPEN_MAX);
988 if (0 < open_max)
989 return open_max;
990 /*
991 * Otherwise, we got -1 for one of the two
992 * reasons:
993 *
994 * (1) sysconf() did not understand _SC_OPEN_MAX
995 * and signaled an error with -1; or
996 * (2) sysconf() said there is no limit.
997 *
998 * We _could_ clear errno before calling sysconf() to
999 * tell these two cases apart and return a huge number
1000 * in the latter case to let the caller cap it to a
1001 * value that is not so selfish, but letting the
1002 * fallback OPEN_MAX codepath take care of these cases
1003 * is a lot simpler.
1004 */
1005 }
1006#endif
1007
1008#ifdef OPEN_MAX
Joachim Schmitza0788262012-08-24 11:52:22 +02001009 return OPEN_MAX;
1010#else
1011 return 1; /* see the caller ;-) */
1012#endif
1013}
1014
Nicolas Pitrec74faea2008-12-09 14:26:52 -05001015/*
Shawn O. Pearce3cf8b462007-02-02 03:00:03 -05001016 * Do not call this directly as this leaks p->pack_fd on error return;
1017 * call open_packed_git() instead.
1018 */
1019static int open_packed_git_1(struct packed_git *p)
Junio C Hamano1f688552005-06-27 03:35:33 -07001020{
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001021 struct stat st;
1022 struct pack_header hdr;
1023 unsigned char sha1[20];
1024 unsigned char *idx_sha1;
Junio C Hamano2c039da2006-12-29 00:30:01 -08001025 long fd_flag;
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001026
Shawn O. Pearced0798372007-05-26 01:24:19 -04001027 if (!p->index_data && open_pack_index(p))
1028 return error("packfile %s index unavailable", p->pack_name);
1029
Shawn O. Pearcec7934302011-02-28 12:52:39 -08001030 if (!pack_max_fds) {
Joachim Schmitza0788262012-08-24 11:52:22 +02001031 unsigned int max_fds = get_max_fd_limit();
Shawn O. Pearcec7934302011-02-28 12:52:39 -08001032
1033 /* Save 3 for stdin/stdout/stderr, 22 for work */
1034 if (25 < max_fds)
1035 pack_max_fds = max_fds - 25;
1036 else
1037 pack_max_fds = 1;
1038 }
1039
Brandon Casey88d0db52013-08-01 22:36:33 -07001040 while (pack_max_fds <= pack_open_fds && close_one_pack())
Shawn O. Pearcec7934302011-02-28 12:52:39 -08001041 ; /* nothing */
1042
Junio C Hamanof4e51682011-05-15 12:16:29 -07001043 p->pack_fd = git_open_noatime(p->pack_name);
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001044 if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
Shawn O. Pearce072db272007-02-01 15:52:33 -05001045 return -1;
Shawn O. Pearcec7934302011-02-28 12:52:39 -08001046 pack_open_fds++;
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001047
1048 /* If we created the struct before we had the pack we lack size. */
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001049 if (!p->pack_size) {
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001050 if (!S_ISREG(st.st_mode))
Shawn O. Pearce072db272007-02-01 15:52:33 -05001051 return error("packfile %s not a regular file", p->pack_name);
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001052 p->pack_size = st.st_size;
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001053 } else if (p->pack_size != st.st_size)
Shawn O. Pearce072db272007-02-01 15:52:33 -05001054 return error("packfile %s size changed", p->pack_name);
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001055
Junio C Hamano2c039da2006-12-29 00:30:01 -08001056 /* We leave these file descriptors open with sliding mmap;
1057 * there is no point keeping them open across exec(), though.
1058 */
1059 fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
1060 if (fd_flag < 0)
Shawn O. Pearce072db272007-02-01 15:52:33 -05001061 return error("cannot determine file descriptor flags");
Junio C Hamano2c039da2006-12-29 00:30:01 -08001062 fd_flag |= FD_CLOEXEC;
1063 if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
Shawn O. Pearce072db272007-02-01 15:52:33 -05001064 return error("cannot set FD_CLOEXEC");
Junio C Hamano2c039da2006-12-29 00:30:01 -08001065
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001066 /* Verify we recognize this pack file format. */
Shawn O. Pearcee6e2bd62007-01-14 01:01:49 -05001067 if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
Shawn O. Pearce072db272007-02-01 15:52:33 -05001068 return error("file %s is far too short to be a packfile", p->pack_name);
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001069 if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
Shawn O. Pearce072db272007-02-01 15:52:33 -05001070 return error("file %s is not a GIT packfile", p->pack_name);
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001071 if (!pack_version_ok(hdr.hdr_version))
Ramsay Jones6e1c2342008-07-03 16:52:09 +01001072 return error("packfile %s is version %"PRIu32" and not"
1073 " supported (try upgrading GIT to a newer version)",
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001074 p->pack_name, ntohl(hdr.hdr_version));
1075
1076 /* Verify the pack matches its index. */
Nicolas Pitre57059092007-04-09 01:06:28 -04001077 if (p->num_objects != ntohl(hdr.hdr_entries))
Ramsay Jones6e1c2342008-07-03 16:52:09 +01001078 return error("packfile %s claims to have %"PRIu32" objects"
1079 " while index indicates %"PRIu32" objects",
Nicolas Pitre57059092007-04-09 01:06:28 -04001080 p->pack_name, ntohl(hdr.hdr_entries),
1081 p->num_objects);
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001082 if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
Shawn O. Pearce072db272007-02-01 15:52:33 -05001083 return error("end of packfile %s is unavailable", p->pack_name);
Shawn O. Pearcee6e2bd62007-01-14 01:01:49 -05001084 if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
Shawn O. Pearce072db272007-02-01 15:52:33 -05001085 return error("packfile %s signature is unavailable", p->pack_name);
Nicolas Pitre42873072007-03-16 16:42:50 -04001086 idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40;
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001087 if (hashcmp(sha1, idx_sha1))
Shawn O. Pearce072db272007-02-01 15:52:33 -05001088 return error("packfile %s does not match index", p->pack_name);
1089 return 0;
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001090}
1091
Shawn O. Pearce3cf8b462007-02-02 03:00:03 -05001092static int open_packed_git(struct packed_git *p)
1093{
1094 if (!open_packed_git_1(p))
1095 return 0;
Johannes Schindelin71fe5d72015-10-05 22:30:24 +02001096 close_pack_fd(p);
Shawn O. Pearce3cf8b462007-02-02 03:00:03 -05001097 return -1;
1098}
1099
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05001100static int in_window(struct pack_window *win, off_t offset)
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001101{
1102 /* We must promise at least 20 bytes (one hash) after the
1103 * offset is available from this window, otherwise the offset
1104 * is not actually in this window and a different window (which
1105 * has that one hash excess) must be used. This is to support
1106 * the object header and delta base parsing routines below.
1107 */
1108 off_t win_off = win->offset;
1109 return win_off <= offset
1110 && (offset + 20) <= (win_off + win->len);
1111}
1112
Felipe Contreras4b25d092009-05-01 12:06:36 +03001113unsigned char *use_pack(struct packed_git *p,
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001114 struct pack_window **w_cursor,
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05001115 off_t offset,
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001116 unsigned long *left)
Shawn O. Pearce9bc879c2006-12-23 02:34:01 -05001117{
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001118 struct pack_window *win = *w_cursor;
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001119
Felipe Contrerasa9d98a12009-02-24 23:59:05 +02001120 /* Since packfiles end in a hash of their content and it's
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001121 * pointless to ask for an offset into the middle of that
1122 * hash, and the in_window function above wouldn't match
1123 * don't allow an offset too close to the end of the file.
1124 */
Shawn O. Pearced131b7a2011-03-02 10:01:54 -08001125 if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
1126 die("packfile %s cannot be accessed", p->pack_name);
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001127 if (offset > (p->pack_size - 20))
1128 die("offset beyond end of packfile (truncated pack?)");
Jeff King13e0b0d2016-02-25 09:23:26 -05001129 if (offset < 0)
Nguyễn Thái Ngọc Duy7465feb2016-02-27 14:49:33 +07001130 die(_("offset before end of packfile (broken .idx?)"));
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001131
1132 if (!win || !in_window(win, offset)) {
1133 if (win)
1134 win->inuse_cnt--;
1135 for (win = p->windows; win; win = win->next) {
1136 if (in_window(win, offset))
1137 break;
1138 }
1139 if (!win) {
Alexandre Julliard78a28df2007-02-14 18:11:40 +01001140 size_t window_align = packed_git_window_size / 2;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001141 off_t len;
Shawn O. Pearced131b7a2011-03-02 10:01:54 -08001142
1143 if (p->pack_fd == -1 && open_packed_git(p))
1144 die("packfile %s cannot be accessed", p->pack_name);
1145
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001146 win = xcalloc(1, sizeof(*win));
Alexandre Julliard78a28df2007-02-14 18:11:40 +01001147 win->offset = (offset / window_align) * window_align;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001148 len = p->pack_size - win->offset;
1149 if (len > packed_git_window_size)
1150 len = packed_git_window_size;
1151 win->len = (size_t)len;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001152 pack_mapped += win->len;
Shawn O. Pearce11daf392006-12-23 02:34:44 -05001153 while (packed_git_limit < pack_mapped
Brandon Casey7c3ecb32013-07-31 12:51:37 -07001154 && unuse_one_window(p))
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001155 ; /* nothing */
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05001156 win->base = xmmap(NULL, win->len,
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001157 PROT_READ, MAP_PRIVATE,
1158 p->pack_fd, win->offset);
1159 if (win->base == MAP_FAILED)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001160 die_errno("packfile %s cannot be mapped",
1161 p->pack_name);
Shawn O. Pearced131b7a2011-03-02 10:01:54 -08001162 if (!win->offset && win->len == p->pack_size
Johannes Schindelin71fe5d72015-10-05 22:30:24 +02001163 && !p->do_not_close)
1164 close_pack_fd(p);
Shawn O. Pearcea53128b2006-12-23 02:34:47 -05001165 pack_mmap_calls++;
1166 pack_open_windows++;
1167 if (pack_mapped > peak_pack_mapped)
1168 peak_pack_mapped = pack_mapped;
1169 if (pack_open_windows > peak_pack_open_windows)
1170 peak_pack_open_windows = pack_open_windows;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001171 win->next = p->windows;
1172 p->windows = win;
1173 }
Junio C Hamano1f688552005-06-27 03:35:33 -07001174 }
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001175 if (win != *w_cursor) {
1176 win->last_used = pack_used_ctr++;
1177 win->inuse_cnt++;
1178 *w_cursor = win;
1179 }
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001180 offset -= win->offset;
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001181 if (left)
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001182 *left = win->len - xsize_t(offset);
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001183 return win->base + offset;
Junio C Hamano1f688552005-06-27 03:35:33 -07001184}
1185
Nicolas Pitre27d69a42008-06-24 18:58:06 -04001186static struct packed_git *alloc_packed_git(int extra)
1187{
Jeff King50a6c8e2016-02-22 17:44:35 -05001188 struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
Nicolas Pitre27d69a42008-06-24 18:58:06 -04001189 memset(p, 0, sizeof(*p));
1190 p->pack_fd = -1;
1191 return p;
1192}
1193
Jonathan Niedere0500292010-11-06 14:00:38 -05001194static void try_to_free_pack_memory(size_t size)
1195{
Brandon Casey7c3ecb32013-07-31 12:51:37 -07001196 release_pack_memory(size);
Jonathan Niedere0500292010-11-06 14:00:38 -05001197}
1198
Jeff King48bcc1c2015-09-24 17:06:55 -04001199struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
Junio C Hamano1f688552005-06-27 03:35:33 -07001200{
Jonathan Niedere0500292010-11-06 14:00:38 -05001201 static int have_set_try_to_free_routine;
Junio C Hamano1f688552005-06-27 03:35:33 -07001202 struct stat st;
Jeff King48bcc1c2015-09-24 17:06:55 -04001203 size_t alloc;
1204 struct packed_git *p;
Junio C Hamano1f688552005-06-27 03:35:33 -07001205
Jonathan Niedere0500292010-11-06 14:00:38 -05001206 if (!have_set_try_to_free_routine) {
1207 have_set_try_to_free_routine = 1;
1208 set_try_to_free_routine(try_to_free_pack_memory);
1209 }
1210
Nicolas Pitre42873072007-03-16 16:42:50 -04001211 /*
1212 * Make sure a corresponding .pack file exists and that
1213 * the index looks sane.
1214 */
Jeff King48bcc1c2015-09-24 17:06:55 -04001215 if (!strip_suffix_mem(path, &path_len, ".idx"))
Junio C Hamano1f688552005-06-27 03:35:33 -07001216 return NULL;
Jeff King48bcc1c2015-09-24 17:06:55 -04001217
1218 /*
1219 * ".pack" is long enough to hold any suffix we're adding (and
1220 * the use xsnprintf double-checks that)
1221 */
Jeff King50a6c8e2016-02-22 17:44:35 -05001222 alloc = st_add3(path_len, strlen(".pack"), 1);
Jeff King48bcc1c2015-09-24 17:06:55 -04001223 p = alloc_packed_git(alloc);
Nicolas Pitre42873072007-03-16 16:42:50 -04001224 memcpy(p->pack_name, path, path_len);
Brandon Casey8d259312008-11-12 11:59:03 -06001225
Jeff King48bcc1c2015-09-24 17:06:55 -04001226 xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
Brandon Casey8d259312008-11-12 11:59:03 -06001227 if (!access(p->pack_name, F_OK))
1228 p->pack_keep = 1;
1229
Jeff King48bcc1c2015-09-24 17:06:55 -04001230 xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
Shawn O. Pearced0798372007-05-26 01:24:19 -04001231 if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
Nicolas Pitre42873072007-03-16 16:42:50 -04001232 free(p);
Junio C Hamano1f688552005-06-27 03:35:33 -07001233 return NULL;
1234 }
Nicolas Pitre42873072007-03-16 16:42:50 -04001235
Junio C Hamano1f688552005-06-27 03:35:33 -07001236 /* ok, it looks sane as far as we can check without
1237 * actually mapping the pack file.
1238 */
Junio C Hamano1f688552005-06-27 03:35:33 -07001239 p->pack_size = st.st_size;
Linus Torvalds9d835df2005-10-13 15:38:28 -07001240 p->pack_local = local;
Junio C Hamanob8670922007-03-09 03:52:12 -08001241 p->mtime = st.st_mtime;
Nicolas Pitre42873072007-03-16 16:42:50 -04001242 if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
1243 hashclr(p->sha1);
Junio C Hamano1f688552005-06-27 03:35:33 -07001244 return p;
1245}
1246
Shawn O. Pearce7b644692010-04-19 07:23:08 -07001247struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001248{
Nicolas Pitre42873072007-03-16 16:42:50 -04001249 const char *path = sha1_pack_name(sha1);
Jeff King50a6c8e2016-02-22 17:44:35 -05001250 size_t alloc = st_add(strlen(path), 1);
Jeff Kingc7ab0ba2015-09-24 17:08:12 -04001251 struct packed_git *p = alloc_packed_git(alloc);
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001252
Jeff Kingc7ab0ba2015-09-24 17:08:12 -04001253 memcpy(p->pack_name, path, alloc); /* includes NUL */
Nicolas Pitre27d69a42008-06-24 18:58:06 -04001254 hashcpy(p->sha1, sha1);
Nicolas Pitre42873072007-03-16 16:42:50 -04001255 if (check_packed_git_idx(idx_path, p)) {
1256 free(p);
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001257 return NULL;
Nicolas Pitre42873072007-03-16 16:42:50 -04001258 }
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001259
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001260 return p;
1261}
1262
1263void install_packed_git(struct packed_git *pack)
1264{
Shawn O. Pearcec7934302011-02-28 12:52:39 -08001265 if (pack->pack_fd != -1)
1266 pack_open_fds++;
1267
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04001268 pack->next = packed_git;
1269 packed_git = pack;
1270}
1271
Junio C Hamano0a489b02015-08-13 13:02:52 -05001272void (*report_garbage)(unsigned seen_bits, const char *path);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001273
1274static void report_helper(const struct string_list *list,
1275 int seen_bits, int first, int last)
1276{
Junio C Hamano0a489b02015-08-13 13:02:52 -05001277 if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001278 return;
Junio C Hamano0a489b02015-08-13 13:02:52 -05001279
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001280 for (; first < last; first++)
Junio C Hamano0a489b02015-08-13 13:02:52 -05001281 report_garbage(seen_bits, list->items[first].string);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001282}
1283
1284static void report_pack_garbage(struct string_list *list)
1285{
1286 int i, baselen = -1, first = 0, seen_bits = 0;
1287
1288 if (!report_garbage)
1289 return;
1290
Michael Haggerty3383e192014-11-25 09:02:35 +01001291 string_list_sort(list);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001292
1293 for (i = 0; i < list->nr; i++) {
1294 const char *path = list->items[i].string;
1295 if (baselen != -1 &&
1296 strncmp(path, list->items[first].string, baselen)) {
1297 report_helper(list, seen_bits, first, i);
1298 baselen = -1;
1299 seen_bits = 0;
1300 }
1301 if (baselen == -1) {
1302 const char *dot = strrchr(path, '.');
1303 if (!dot) {
Junio C Hamano0a489b02015-08-13 13:02:52 -05001304 report_garbage(PACKDIR_FILE_GARBAGE, path);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001305 continue;
1306 }
1307 baselen = dot - path + 1;
1308 first = i;
1309 }
1310 if (!strcmp(path + baselen, "pack"))
1311 seen_bits |= 1;
1312 else if (!strcmp(path + baselen, "idx"))
1313 seen_bits |= 2;
1314 }
1315 report_helper(list, seen_bits, first, list->nr);
1316}
1317
Linus Torvalds9d835df2005-10-13 15:38:28 -07001318static void prepare_packed_git_one(char *objdir, int local)
Junio C Hamano1f688552005-06-27 03:35:33 -07001319{
René Scharfe880fb8d2014-06-30 12:55:52 -04001320 struct strbuf path = STRBUF_INIT;
1321 size_t dirnamelen;
Junio C Hamano1f688552005-06-27 03:35:33 -07001322 DIR *dir;
1323 struct dirent *de;
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001324 struct string_list garbage = STRING_LIST_INIT_DUP;
Junio C Hamano1f688552005-06-27 03:35:33 -07001325
René Scharfe880fb8d2014-06-30 12:55:52 -04001326 strbuf_addstr(&path, objdir);
1327 strbuf_addstr(&path, "/pack");
1328 dir = opendir(path.buf);
Carl Worthb5b16992006-02-17 16:14:52 -08001329 if (!dir) {
Junio C Hamano26125f62006-02-22 11:16:38 -08001330 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001331 error_errno("unable to open object pack directory: %s",
1332 path.buf);
René Scharfe880fb8d2014-06-30 12:55:52 -04001333 strbuf_release(&path);
Junio C Hamano1f688552005-06-27 03:35:33 -07001334 return;
Carl Worthb5b16992006-02-17 16:14:52 -08001335 }
René Scharfe880fb8d2014-06-30 12:55:52 -04001336 strbuf_addch(&path, '/');
1337 dirnamelen = path.len;
Junio C Hamano1f688552005-06-27 03:35:33 -07001338 while ((de = readdir(dir)) != NULL) {
Junio C Hamano1f688552005-06-27 03:35:33 -07001339 struct packed_git *p;
Jeff King47bf4b02014-06-30 13:04:03 -04001340 size_t base_len;
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001341
1342 if (is_dot_or_dotdot(de->d_name))
Jim Meyering9cb18f52007-07-03 12:40:20 +02001343 continue;
1344
René Scharfe880fb8d2014-06-30 12:55:52 -04001345 strbuf_setlen(&path, dirnamelen);
1346 strbuf_addstr(&path, de->d_name);
Nguyễn Thái Ngọc Duyd90906a2013-02-13 16:13:17 +07001347
Jeff King47bf4b02014-06-30 13:04:03 -04001348 base_len = path.len;
1349 if (strip_suffix_mem(path.buf, &base_len, ".idx")) {
Nguyễn Thái Ngọc Duyd90906a2013-02-13 16:13:17 +07001350 /* Don't reopen a pack we already have. */
1351 for (p = packed_git; p; p = p->next) {
Jeff King47bf4b02014-06-30 13:04:03 -04001352 size_t len;
1353 if (strip_suffix(p->pack_name, ".pack", &len) &&
1354 len == base_len &&
1355 !memcmp(p->pack_name, path.buf, len))
Nguyễn Thái Ngọc Duyd90906a2013-02-13 16:13:17 +07001356 break;
1357 }
1358 if (p == NULL &&
1359 /*
1360 * See if it really is a valid .idx file with
1361 * corresponding .pack file that we can map.
1362 */
René Scharfe880fb8d2014-06-30 12:55:52 -04001363 (p = add_packed_git(path.buf, path.len, local)) != NULL)
Nguyễn Thái Ngọc Duyd90906a2013-02-13 16:13:17 +07001364 install_packed_git(p);
Jeff King86f77802006-06-02 12:49:32 -04001365 }
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001366
1367 if (!report_garbage)
1368 continue;
1369
Jeff King2975c772014-06-30 12:58:25 -04001370 if (ends_with(de->d_name, ".idx") ||
1371 ends_with(de->d_name, ".pack") ||
1372 ends_with(de->d_name, ".bitmap") ||
1373 ends_with(de->d_name, ".keep"))
René Scharfe880fb8d2014-06-30 12:55:52 -04001374 string_list_append(&garbage, path.buf);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001375 else
Junio C Hamano0a489b02015-08-13 13:02:52 -05001376 report_garbage(PACKDIR_FILE_GARBAGE, path.buf);
Junio C Hamano1f688552005-06-27 03:35:33 -07001377 }
Junio C Hamano5b35bcd2005-07-05 23:52:17 -07001378 closedir(dir);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +07001379 report_pack_garbage(&garbage);
1380 string_list_clear(&garbage, 0);
René Scharfe880fb8d2014-06-30 12:55:52 -04001381 strbuf_release(&path);
Junio C Hamano1f688552005-06-27 03:35:33 -07001382}
1383
René Scharfec4c6eff2016-09-13 19:54:42 +02001384static void *get_next_packed_git(const void *p)
1385{
1386 return ((const struct packed_git *)p)->next;
1387}
1388
1389static void set_next_packed_git(void *p, void *next)
1390{
1391 ((struct packed_git *)p)->next = next;
1392}
1393
Junio C Hamanob8670922007-03-09 03:52:12 -08001394static int sort_pack(const void *a_, const void *b_)
1395{
René Scharfec4c6eff2016-09-13 19:54:42 +02001396 const struct packed_git *a = a_;
1397 const struct packed_git *b = b_;
Junio C Hamanob8670922007-03-09 03:52:12 -08001398 int st;
1399
1400 /*
1401 * Local packs tend to contain objects specific to our
1402 * variant of the project than remote ones. In addition,
1403 * remote ones could be on a network mounted filesystem.
1404 * Favor local ones for these reasons.
1405 */
1406 st = a->pack_local - b->pack_local;
1407 if (st)
1408 return -st;
1409
1410 /*
1411 * Younger packs tend to contain more recent objects,
1412 * and more recent objects tend to get accessed more
1413 * often.
1414 */
1415 if (a->mtime < b->mtime)
1416 return 1;
1417 else if (a->mtime == b->mtime)
1418 return 0;
1419 return -1;
1420}
1421
1422static void rearrange_packed_git(void)
1423{
René Scharfec4c6eff2016-09-13 19:54:42 +02001424 packed_git = llist_mergesort(packed_git, get_next_packed_git,
1425 set_next_packed_git, sort_pack);
Junio C Hamanob8670922007-03-09 03:52:12 -08001426}
1427
Jeff Kinga73cdd22016-07-29 00:09:46 -04001428static void prepare_packed_git_mru(void)
1429{
1430 struct packed_git *p;
1431
1432 mru_clear(packed_git_mru);
1433 for (p = packed_git; p; p = p->next)
1434 mru_append(packed_git_mru, p);
1435}
1436
Jeff King637cdd92006-06-02 11:32:23 -04001437static int prepare_packed_git_run_once = 0;
Junio C Hamano9a217f22005-06-28 14:56:57 -07001438void prepare_packed_git(void)
Junio C Hamano1f688552005-06-27 03:35:33 -07001439{
Junio C Hamanod5a63b92005-08-14 17:25:57 -07001440 struct alternate_object_database *alt;
Junio C Hamano1f688552005-06-27 03:35:33 -07001441
Jeff King637cdd92006-06-02 11:32:23 -04001442 if (prepare_packed_git_run_once)
Junio C Hamano1f688552005-06-27 03:35:33 -07001443 return;
Linus Torvalds9d835df2005-10-13 15:38:28 -07001444 prepare_packed_git_one(get_object_directory(), 1);
Junio C Hamano9a217f22005-06-28 14:56:57 -07001445 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -07001446 for (alt = alt_odb_list; alt; alt = alt->next) {
Junio C Hamano1494e032005-12-04 22:48:43 -08001447 alt->name[-1] = 0;
Linus Torvalds9d835df2005-10-13 15:38:28 -07001448 prepare_packed_git_one(alt->base, 0);
Junio C Hamano1494e032005-12-04 22:48:43 -08001449 alt->name[-1] = '/';
Junio C Hamano1f688552005-06-27 03:35:33 -07001450 }
Junio C Hamanob8670922007-03-09 03:52:12 -08001451 rearrange_packed_git();
Jeff Kinga73cdd22016-07-29 00:09:46 -04001452 prepare_packed_git_mru();
Jeff King637cdd92006-06-02 11:32:23 -04001453 prepare_packed_git_run_once = 1;
1454}
1455
Shawn Pearcefc04c412006-11-01 17:06:21 -05001456void reprepare_packed_git(void)
Jeff King637cdd92006-06-02 11:32:23 -04001457{
1458 prepare_packed_git_run_once = 0;
1459 prepare_packed_git();
Junio C Hamano1f688552005-06-27 03:35:33 -07001460}
1461
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04001462static void mark_bad_packed_object(struct packed_git *p,
1463 const unsigned char *sha1)
1464{
1465 unsigned i;
1466 for (i = 0; i < p->num_bad_objects; i++)
Jeff King50a6c8e2016-02-22 17:44:35 -05001467 if (!hashcmp(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04001468 return;
Jeff King50a6c8e2016-02-22 17:44:35 -05001469 p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
1470 st_mult(GIT_SHA1_RAWSZ,
1471 st_add(p->num_bad_objects, 1)));
1472 hashcpy(p->bad_object_sha1 + GIT_SHA1_RAWSZ * p->num_bad_objects, sha1);
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04001473 p->num_bad_objects++;
1474}
1475
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001476static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
Nicolas Pitreac939102008-07-14 21:46:48 -04001477{
1478 struct packed_git *p;
1479 unsigned i;
1480
1481 for (p = packed_git; p; p = p->next)
1482 for (i = 0; i < p->num_bad_objects; i++)
1483 if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001484 return p;
1485 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001486}
1487
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001488/*
1489 * With an in-core object data in "map", rehash it to make sure the
1490 * object name actually matches "sha1" to detect object corruption.
1491 * With "map" == NULL, try reading the object named with "sha1" using
1492 * the streaming interface and rehash it to do the same.
1493 */
1494int check_sha1_signature(const unsigned char *sha1, void *map,
1495 unsigned long size, const char *type)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001496{
1497 unsigned char real_sha1[20];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001498 enum object_type obj_type;
1499 struct git_istream *st;
1500 git_SHA_CTX c;
1501 char hdr[32];
1502 int hdrlen;
1503
1504 if (map) {
1505 hash_sha1_file(map, size, type, real_sha1);
1506 return hashcmp(sha1, real_sha1) ? -1 : 0;
1507 }
1508
1509 st = open_istream(sha1, &obj_type, &size, NULL);
1510 if (!st)
1511 return -1;
1512
1513 /* Generate the header */
Jeff Kingef1286d2015-09-24 17:06:42 -04001514 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001515
1516 /* Sha1.. */
1517 git_SHA1_Init(&c);
1518 git_SHA1_Update(&c, hdr, hdrlen);
1519 for (;;) {
1520 char buf[1024 * 16];
1521 ssize_t readlen = read_istream(st, buf, sizeof(buf));
1522
Jeff Kingf54fac52013-03-25 16:17:17 -04001523 if (readlen < 0) {
1524 close_istream(st);
1525 return -1;
1526 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001527 if (!readlen)
1528 break;
1529 git_SHA1_Update(&c, buf, readlen);
1530 }
1531 git_SHA1_Final(real_sha1, &c);
1532 close_istream(st);
David Rientjesa89fccd2006-08-17 11:54:57 -07001533 return hashcmp(sha1, real_sha1) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001534}
1535
Vicent Martiec73f582013-10-24 14:01:47 -04001536int git_open_noatime(const char *name)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001537{
1538 static int sha1_file_open_flag = O_NOATIME;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001539
Shawn O. Pearcef2e872a2010-11-01 15:54:21 -07001540 for (;;) {
Clemens Buchacherdff6f282015-08-04 10:24:29 +02001541 int fd;
1542
1543 errno = 0;
1544 fd = open(name, O_RDONLY | sha1_file_open_flag);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001545 if (fd >= 0)
Shawn O. Pearcef2e872a2010-11-01 15:54:21 -07001546 return fd;
1547
Shawn O. Pearcef2e872a2010-11-01 15:54:21 -07001548 /* Might the failure be due to O_NOATIME? */
1549 if (errno != ENOENT && sha1_file_open_flag) {
Linus Torvalds44d1c192008-06-14 11:32:37 -07001550 sha1_file_open_flag = 0;
Shawn O. Pearcef2e872a2010-11-01 15:54:21 -07001551 continue;
1552 }
1553
1554 return -1;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001555 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001556}
1557
Jeff King052fe5e2013-07-12 02:30:48 -04001558static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
1559{
Jeff King052fe5e2013-07-12 02:30:48 -04001560 struct alternate_object_database *alt;
1561
Michael Haggerty30d6c6e2014-02-21 17:32:05 +01001562 if (!lstat(sha1_file_name(sha1), st))
Jeff King052fe5e2013-07-12 02:30:48 -04001563 return 0;
1564
1565 prepare_alt_odb();
1566 errno = ENOENT;
1567 for (alt = alt_odb_list; alt; alt = alt->next) {
Michael Haggerty30d6c6e2014-02-21 17:32:05 +01001568 fill_sha1_path(alt->name, sha1);
Jeff King052fe5e2013-07-12 02:30:48 -04001569 if (!lstat(alt->base, st))
1570 return 0;
1571 }
1572
1573 return -1;
1574}
1575
Linus Torvalds44d1c192008-06-14 11:32:37 -07001576static int open_sha1_file(const unsigned char *sha1)
1577{
1578 int fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001579 struct alternate_object_database *alt;
Jeff Kingd6c8a052014-05-15 04:54:06 -04001580 int most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001581
Michael Haggerty30d6c6e2014-02-21 17:32:05 +01001582 fd = git_open_noatime(sha1_file_name(sha1));
Linus Torvalds44d1c192008-06-14 11:32:37 -07001583 if (fd >= 0)
1584 return fd;
Jeff Kingd6c8a052014-05-15 04:54:06 -04001585 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001586
1587 prepare_alt_odb();
Linus Torvalds44d1c192008-06-14 11:32:37 -07001588 for (alt = alt_odb_list; alt; alt = alt->next) {
Michael Haggerty30d6c6e2014-02-21 17:32:05 +01001589 fill_sha1_path(alt->name, sha1);
Junio C Hamanof4e51682011-05-15 12:16:29 -07001590 fd = git_open_noatime(alt->base);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001591 if (fd >= 0)
1592 return fd;
Jeff Kingd6c8a052014-05-15 04:54:06 -04001593 if (most_interesting_errno == ENOENT)
1594 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001595 }
Jeff Kingd6c8a052014-05-15 04:54:06 -04001596 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001597 return -1;
1598}
1599
Junio C Hamanof0270ef2011-05-14 19:42:10 -07001600void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001601{
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001602 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -07001603 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -07001604
Linus Torvalds44d1c192008-06-14 11:32:37 -07001605 fd = open_sha1_file(sha1);
1606 map = NULL;
1607 if (fd >= 0) {
1608 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001609
Linus Torvalds44d1c192008-06-14 11:32:37 -07001610 if (!fstat(fd, &st)) {
1611 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001612 if (!*size) {
1613 /* mmap() is forbidden on empty files */
1614 error("object file %s is empty", sha1_file_name(sha1));
1615 return NULL;
1616 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001617 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -07001618 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001619 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001620 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001621 return map;
1622}
1623
Nicolas Pitre09ded042008-10-29 19:02:46 -04001624unsigned long unpack_object_header_buffer(const unsigned char *buf,
1625 unsigned long len, enum object_type *type, unsigned long *sizep)
Junio C Hamanoad1ed5e2006-09-01 15:17:01 -07001626{
1627 unsigned shift;
Linus Torvalds48fb7de2009-06-17 17:22:27 -07001628 unsigned long size, c;
Junio C Hamanoad1ed5e2006-09-01 15:17:01 -07001629 unsigned long used = 0;
1630
1631 c = buf[used++];
1632 *type = (c >> 4) & 7;
1633 size = c & 15;
1634 shift = 4;
1635 while (c & 0x80) {
Pierre Habouzitf630cfd2009-07-22 23:34:34 +02001636 if (len <= used || bitsizeof(long) <= shift) {
Nicolas Pitre09ded042008-10-29 19:02:46 -04001637 error("bad object header");
Junio C Hamanoea4f9682011-10-27 11:42:57 -07001638 size = used = 0;
1639 break;
Nicolas Pitre09ded042008-10-29 19:02:46 -04001640 }
Junio C Hamanoad1ed5e2006-09-01 15:17:01 -07001641 c = buf[used++];
1642 size += (c & 0x7f) << shift;
1643 shift += 7;
1644 }
1645 *sizep = size;
1646 return used;
1647}
1648
Junio C Hamanoeb4f4072011-07-19 09:33:03 -07001649int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
Linus Torvaldsc4483572005-06-01 17:54:59 -07001650{
1651 /* Get the data stream */
1652 memset(stream, 0, sizeof(*stream));
1653 stream->next_in = map;
1654 stream->avail_in = mapsize;
1655 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -07001656 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -07001657
Linus Torvalds39c68542009-01-07 19:54:47 -08001658 git_inflate_init(stream);
Junio C Hamanocc5c54e2011-06-08 11:29:01 -07001659 return git_inflate(stream, 0);
Linus Torvaldsc4483572005-06-01 17:54:59 -07001660}
1661
Karthik Nayak46f03442015-05-03 19:59:59 +05301662static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1663 unsigned long mapsize, void *buffer,
1664 unsigned long bufsiz, struct strbuf *header)
1665{
1666 int status;
1667
1668 status = unpack_sha1_header(stream, map, mapsize, buffer, bufsiz);
1669
1670 /*
1671 * Check if entire header is unpacked in the first iteration.
1672 */
1673 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1674 return 0;
1675
1676 /*
1677 * buffer[0..bufsiz] was not large enough. Copy the partial
1678 * result out to header, and then append the result of further
1679 * reading the stream.
1680 */
1681 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1682 stream->next_out = buffer;
1683 stream->avail_out = bufsiz;
1684
1685 do {
1686 status = git_inflate(stream, 0);
1687 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1688 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1689 return 0;
1690 stream->next_out = buffer;
1691 stream->avail_out = bufsiz;
1692 } while (status != Z_STREAM_END);
1693 return -1;
1694}
1695
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001696static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001697{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001698 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001699 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001700 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001701 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001702
Linus Torvalds93821bd2006-07-11 12:48:08 -07001703 n = stream->total_out - bytes;
1704 if (n > size)
1705 n = size;
1706 memcpy(buf, (char *) buffer + bytes, n);
1707 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001708 if (bytes <= size) {
1709 /*
1710 * The above condition must be (bytes <= size), not
1711 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001712 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001713 * the input zlib stream may have bytes that express
1714 * "this concludes the stream", and we *do* want to
1715 * eat that input.
1716 *
1717 * Otherwise we would not be able to test that we
1718 * consumed all the input to reach the expected size;
1719 * we also want to check that zlib tells us that all
1720 * went well with status == Z_STREAM_END at the end.
1721 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001722 stream->next_out = buf + bytes;
1723 stream->avail_out = size - bytes;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001724 while (status == Z_OK)
Linus Torvalds39c68542009-01-07 19:54:47 -08001725 status = git_inflate(stream, Z_FINISH);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001726 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001727 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001728 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001729 return buf;
1730 }
1731
1732 if (status < 0)
1733 error("corrupt loose object '%s'", sha1_to_hex(sha1));
1734 else if (stream->avail_in)
1735 error("garbage at end of loose object '%s'",
1736 sha1_to_hex(sha1));
1737 free(buf);
1738 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001739}
1740
Linus Torvalds5180cac2005-06-02 07:57:25 -07001741/*
1742 * We used to just use "sscanf()", but that's actually way
1743 * too permissive for what we want to check. So do an anal
1744 * object header parse by hand.
1745 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301746static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1747 unsigned int flags)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001748{
Karthik Nayak46f03442015-05-03 19:59:59 +05301749 const char *type_buf = hdr;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001750 unsigned long size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301751 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001752
1753 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301754 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001755 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001756 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001757 for (;;) {
1758 char c = *hdr++;
1759 if (c == ' ')
1760 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301761 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001762 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301763
1764 type = type_from_string_gently(type_buf, type_len, 1);
1765 if (oi->typename)
1766 strbuf_add(oi->typename, type_buf, type_len);
1767 /*
1768 * Set type to 0 if its an unknown object and
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001769 * we're obtaining the type using '--allow-unknown-type'
Karthik Nayak46f03442015-05-03 19:59:59 +05301770 * option.
1771 */
1772 if ((flags & LOOKUP_UNKNOWN_OBJECT) && (type < 0))
1773 type = 0;
1774 else if (type < 0)
1775 die("invalid object type");
1776 if (oi->typep)
1777 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001778
1779 /*
1780 * The length must follow immediately, and be in canonical
1781 * decimal format (ie "010" is not valid).
1782 */
1783 size = *hdr++ - '0';
1784 if (size > 9)
1785 return -1;
1786 if (size) {
1787 for (;;) {
1788 unsigned long c = *hdr - '0';
1789 if (c > 9)
1790 break;
1791 hdr++;
1792 size = size * 10 + c;
1793 }
1794 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301795
1796 if (oi->sizep)
1797 *oi->sizep = size;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001798
1799 /*
1800 * The length must be followed by a zero byte
1801 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301802 return *hdr ? -1 : type;
1803}
1804
1805int parse_sha1_header(const char *hdr, unsigned long *sizep)
1806{
1807 struct object_info oi;
1808
1809 oi.sizep = sizep;
1810 oi.typename = NULL;
1811 oi.typep = NULL;
1812 return parse_sha1_header_extended(hdr, &oi, LOOKUP_REPLACE_OBJECT);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001813}
1814
Junio C Hamano7efbff72007-03-05 00:21:37 -08001815static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001816{
1817 int ret;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001818 git_zstream stream;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001819 char hdr[8192];
1820
1821 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
Nicolas Pitre21666f12007-02-26 14:55:59 -05001822 if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001823 return NULL;
1824
Junio C Hamano7efbff72007-03-05 00:21:37 -08001825 return unpack_sha1_rest(&stream, hdr, *size, sha1);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001826}
1827
Nicolas Pitre54dab522007-04-16 12:31:56 -04001828unsigned long get_size_from_delta(struct packed_git *p,
1829 struct pack_window **w_curs,
1830 off_t curpos)
1831{
1832 const unsigned char *data;
1833 unsigned char delta_head[20], *in;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001834 git_zstream stream;
Nicolas Pitre54dab522007-04-16 12:31:56 -04001835 int st;
1836
1837 memset(&stream, 0, sizeof(stream));
1838 stream.next_out = delta_head;
1839 stream.avail_out = sizeof(delta_head);
1840
Linus Torvalds39c68542009-01-07 19:54:47 -08001841 git_inflate_init(&stream);
Nicolas Pitre54dab522007-04-16 12:31:56 -04001842 do {
1843 in = use_pack(p, w_curs, curpos, &stream.avail_in);
1844 stream.next_in = in;
Linus Torvalds39c68542009-01-07 19:54:47 -08001845 st = git_inflate(&stream, Z_FINISH);
Nicolas Pitre54dab522007-04-16 12:31:56 -04001846 curpos += stream.next_in - in;
1847 } while ((st == Z_OK || st == Z_BUF_ERROR) &&
1848 stream.total_out < sizeof(delta_head));
Linus Torvalds39c68542009-01-07 19:54:47 -08001849 git_inflate_end(&stream);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001850 if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
1851 error("delta data unpack-initial failed");
1852 return 0;
1853 }
Nicolas Pitre54dab522007-04-16 12:31:56 -04001854
1855 /* Examine the initial part of the delta to figure out
1856 * the result size.
1857 */
1858 data = delta_head;
1859
1860 /* ignore base size */
1861 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1862
1863 /* Read the result size */
1864 return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1865}
1866
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05001867static off_t get_delta_base(struct packed_git *p,
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001868 struct pack_window **w_curs,
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05001869 off_t *curpos,
Nicolas Pitre21666f12007-02-26 14:55:59 -05001870 enum object_type type,
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05001871 off_t delta_obj_offset)
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001872{
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001873 unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05001874 off_t base_offset;
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001875
Shawn O. Pearce8d8a4ea2006-12-23 02:34:18 -05001876 /* use_pack() assured us we have [base_info, base_info + 20)
1877 * as a range that we can look at without walking off the
1878 * end of the mapped window. Its actually the hash size
1879 * that is assured. An OFS_DELTA longer than the hash size
1880 * is stupid, as then a REF_DELTA would be smaller to store.
1881 */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001882 if (type == OBJ_OFS_DELTA) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001883 unsigned used = 0;
1884 unsigned char c = base_info[used++];
1885 base_offset = c & 127;
1886 while (c & 128) {
1887 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -04001888 if (!base_offset || MSB(base_offset, 7))
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04001889 return 0; /* overflow */
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001890 c = base_info[used++];
1891 base_offset = (base_offset << 7) + (c & 127);
1892 }
1893 base_offset = delta_obj_offset - base_offset;
Nicolas Pitred8f32552008-10-29 19:02:45 -04001894 if (base_offset <= 0 || base_offset >= delta_obj_offset)
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04001895 return 0; /* out of bound */
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001896 *curpos += used;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001897 } else if (type == OBJ_REF_DELTA) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001898 /* The base entry _must_ be in the same pack */
1899 base_offset = find_pack_entry_one(base_info, p);
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001900 *curpos += 20;
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001901 } else
1902 die("I am totally screwed");
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001903 return base_offset;
Nicolas Pitreeb32d232006-09-21 00:06:49 -04001904}
1905
Jeff King5d642e72013-12-21 09:24:20 -05001906/*
1907 * Like get_delta_base above, but we return the sha1 instead of the pack
1908 * offset. This means it is cheaper for REF deltas (we do not have to do
1909 * the final object lookup), but more expensive for OFS deltas (we
1910 * have to load the revidx to convert the offset back into a sha1).
1911 */
1912static const unsigned char *get_delta_base_sha1(struct packed_git *p,
1913 struct pack_window **w_curs,
1914 off_t curpos,
1915 enum object_type type,
1916 off_t delta_obj_offset)
1917{
1918 if (type == OBJ_REF_DELTA) {
1919 unsigned char *base = use_pack(p, w_curs, curpos, NULL);
1920 return base;
1921 } else if (type == OBJ_OFS_DELTA) {
1922 struct revindex_entry *revidx;
1923 off_t base_offset = get_delta_base(p, w_curs, &curpos,
1924 type, delta_obj_offset);
1925
1926 if (!base_offset)
1927 return NULL;
1928
1929 revidx = find_pack_revindex(p, base_offset);
1930 if (!revidx)
1931 return NULL;
1932
1933 return nth_packed_object_sha1(p, revidx->nr);
1934 } else
1935 return NULL;
1936}
1937
Junio C Hamanof8c8abc2011-05-13 15:33:33 -07001938int unpack_object_header(struct packed_git *p,
1939 struct pack_window **w_curs,
1940 off_t *curpos,
1941 unsigned long *sizep)
Linus Torvaldsa733cb62005-06-28 14:21:02 -07001942{
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001943 unsigned char *base;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001944 unsigned long left;
Junio C Hamanoad1ed5e2006-09-01 15:17:01 -07001945 unsigned long used;
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001946 enum object_type type;
Linus Torvaldsa733cb62005-06-28 14:21:02 -07001947
Shawn O. Pearce8d8a4ea2006-12-23 02:34:18 -05001948 /* use_pack() assures us we have [base, base + 20) available
Jim Meyering0353a0c2011-04-13 17:39:40 +02001949 * as a range that we can look at. (Its actually the hash
Pavel Roskin3dff5372007-02-03 23:49:16 -05001950 * size that is assured.) With our object header encoding
Shawn O. Pearce8d8a4ea2006-12-23 02:34:18 -05001951 * the maximum deflated object size is 2^137, which is just
1952 * insane, so we know won't exceed what we have been given.
1953 */
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001954 base = use_pack(p, w_curs, *curpos, &left);
Nicolas Pitre09ded042008-10-29 19:02:46 -04001955 used = unpack_object_header_buffer(base, left, &type, sizep);
1956 if (!used) {
1957 type = OBJ_BAD;
1958 } else
1959 *curpos += used;
Junio C Hamanoad1ed5e2006-09-01 15:17:01 -07001960
Nicolas Pitre2b87c452007-02-26 14:55:56 -05001961 return type;
Linus Torvaldsa733cb62005-06-28 14:21:02 -07001962}
1963
Thomas Rast790d96c2013-03-25 19:07:39 +01001964static int retry_bad_packed_offset(struct packed_git *p, off_t obj_offset)
1965{
1966 int type;
1967 struct revindex_entry *revidx;
1968 const unsigned char *sha1;
1969 revidx = find_pack_revindex(p, obj_offset);
1970 if (!revidx)
1971 return OBJ_BAD;
1972 sha1 = nth_packed_object_sha1(p, revidx->nr);
1973 mark_bad_packed_object(p, sha1);
1974 type = sha1_object_info(sha1, NULL);
1975 if (type <= OBJ_NONE)
1976 return OBJ_BAD;
1977 return type;
1978}
1979
Thomas Rast790d96c2013-03-25 19:07:39 +01001980#define POI_STACK_PREALLOC 64
1981
Jeff King90191d32013-07-12 02:31:57 -04001982static enum object_type packed_to_object_type(struct packed_git *p,
1983 off_t obj_offset,
1984 enum object_type type,
1985 struct pack_window **w_curs,
1986 off_t curpos)
Junio C Hamano1f688552005-06-27 03:35:33 -07001987{
Thomas Rast790d96c2013-03-25 19:07:39 +01001988 off_t small_poi_stack[POI_STACK_PREALLOC];
1989 off_t *poi_stack = small_poi_stack;
1990 int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
Junio C Hamano5db47c22005-06-27 23:58:08 -07001991
Thomas Rast790d96c2013-03-25 19:07:39 +01001992 while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1993 off_t base_offset;
Jeff King90191d32013-07-12 02:31:57 -04001994 unsigned long size;
Thomas Rast790d96c2013-03-25 19:07:39 +01001995 /* Push the object we're going to leave behind */
1996 if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
1997 poi_stack_alloc = alloc_nr(poi_stack_nr);
Jeff Kingb32fa952016-02-22 17:44:25 -05001998 ALLOC_ARRAY(poi_stack, poi_stack_alloc);
Thomas Rast790d96c2013-03-25 19:07:39 +01001999 memcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr);
2000 } else {
2001 ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
2002 }
2003 poi_stack[poi_stack_nr++] = obj_offset;
2004 /* If parsing the base offset fails, just unwind */
Jeff King90191d32013-07-12 02:31:57 -04002005 base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
Thomas Rast790d96c2013-03-25 19:07:39 +01002006 if (!base_offset)
2007 goto unwind;
2008 curpos = obj_offset = base_offset;
Jeff King90191d32013-07-12 02:31:57 -04002009 type = unpack_object_header(p, w_curs, &curpos, &size);
Thomas Rast790d96c2013-03-25 19:07:39 +01002010 if (type <= OBJ_NONE) {
2011 /* If getting the base itself fails, we first
2012 * retry the base, otherwise unwind */
2013 type = retry_bad_packed_offset(p, base_offset);
2014 if (type > OBJ_NONE)
2015 goto out;
2016 goto unwind;
2017 }
2018 }
2019
Nicolas Pitre21666f12007-02-26 14:55:59 -05002020 switch (type) {
Thomas Rast790d96c2013-03-25 19:07:39 +01002021 case OBJ_BAD:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002022 case OBJ_COMMIT:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002023 case OBJ_TREE:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002024 case OBJ_BLOB:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002025 case OBJ_TAG:
Linus Torvaldsa69d0942005-06-28 09:58:23 -07002026 break;
Junio C Hamano1f688552005-06-27 03:35:33 -07002027 default:
Nicolas Pitre3d77d872008-10-29 19:02:47 -04002028 error("unknown object type %i at offset %"PRIuMAX" in %s",
2029 type, (uintmax_t)obj_offset, p->pack_name);
2030 type = OBJ_BAD;
Junio C Hamano1f688552005-06-27 03:35:33 -07002031 }
Thomas Rast790d96c2013-03-25 19:07:39 +01002032
2033out:
2034 if (poi_stack != small_poi_stack)
2035 free(poi_stack);
Nicolas Pitre21666f12007-02-26 14:55:59 -05002036 return type;
Thomas Rast790d96c2013-03-25 19:07:39 +01002037
2038unwind:
2039 while (poi_stack_nr) {
2040 obj_offset = poi_stack[--poi_stack_nr];
2041 type = retry_bad_packed_offset(p, obj_offset);
2042 if (type > OBJ_NONE)
2043 goto out;
2044 }
2045 type = OBJ_BAD;
2046 goto out;
Junio C Hamano1f688552005-06-27 03:35:33 -07002047}
2048
Junio C Hamano1f688552005-06-27 03:35:33 -07002049static int packed_object_info(struct packed_git *p, off_t obj_offset,
Jeff King23c339c2013-07-12 02:37:53 -04002050 struct object_info *oi)
Junio C Hamano1f688552005-06-27 03:35:33 -07002051{
2052 struct pack_window *w_curs = NULL;
2053 unsigned long size;
2054 off_t curpos = obj_offset;
2055 enum object_type type;
Junio C Hamano1f688552005-06-27 03:35:33 -07002056
Jeff King412916e2013-07-12 02:32:25 -04002057 /*
2058 * We always get the representation type, but only convert it to
2059 * a "real" type later if the caller is interested.
2060 */
Junio C Hamano1f688552005-06-27 03:35:33 -07002061 type = unpack_object_header(p, &w_curs, &curpos, &size);
2062
Jeff King23c339c2013-07-12 02:37:53 -04002063 if (oi->sizep) {
Junio C Hamano1f688552005-06-27 03:35:33 -07002064 if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
2065 off_t tmp_pos = curpos;
2066 off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
2067 type, obj_offset);
2068 if (!base_offset) {
2069 type = OBJ_BAD;
2070 goto out;
2071 }
Jeff King23c339c2013-07-12 02:37:53 -04002072 *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
2073 if (*oi->sizep == 0) {
Junio C Hamano1f688552005-06-27 03:35:33 -07002074 type = OBJ_BAD;
2075 goto out;
2076 }
2077 } else {
Jeff King23c339c2013-07-12 02:37:53 -04002078 *oi->sizep = size;
Junio C Hamano1f688552005-06-27 03:35:33 -07002079 }
2080 }
2081
Jeff King23c339c2013-07-12 02:37:53 -04002082 if (oi->disk_sizep) {
Jeff King161f00e2013-07-07 06:04:00 -04002083 struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
Jeff King23c339c2013-07-12 02:37:53 -04002084 *oi->disk_sizep = revidx[1].offset - obj_offset;
Jeff King161f00e2013-07-07 06:04:00 -04002085 }
2086
Jeff King23c339c2013-07-12 02:37:53 -04002087 if (oi->typep) {
2088 *oi->typep = packed_to_object_type(p, obj_offset, type, &w_curs, curpos);
2089 if (*oi->typep < 0) {
Jeff King412916e2013-07-12 02:32:25 -04002090 type = OBJ_BAD;
2091 goto out;
2092 }
2093 }
Junio C Hamano1f688552005-06-27 03:35:33 -07002094
Jeff King5d642e72013-12-21 09:24:20 -05002095 if (oi->delta_base_sha1) {
2096 if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
2097 const unsigned char *base;
2098
2099 base = get_delta_base_sha1(p, &w_curs, curpos,
2100 type, obj_offset);
2101 if (!base) {
2102 type = OBJ_BAD;
2103 goto out;
2104 }
2105
2106 hashcpy(oi->delta_base_sha1, base);
2107 } else
2108 hashclr(oi->delta_base_sha1);
2109 }
2110
Junio C Hamano1f688552005-06-27 03:35:33 -07002111out:
Junio C Hamano1f688552005-06-27 03:35:33 -07002112 unuse_pack(&w_curs);
2113 return type;
Junio C Hamano1f688552005-06-27 03:35:33 -07002114}
2115
Shawn Pearceeb950c12006-08-26 04:12:27 -04002116static void *unpack_compressed_entry(struct packed_git *p,
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05002117 struct pack_window **w_curs,
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05002118 off_t curpos,
Shawn Pearceeb950c12006-08-26 04:12:27 -04002119 unsigned long size)
Shawn Pearcede530aa2006-08-26 04:10:43 -04002120{
2121 int st;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07002122 git_zstream stream;
Shawn O. Pearce079afb12006-12-23 02:34:13 -05002123 unsigned char *buffer, *in;
Shawn Pearcede530aa2006-08-26 04:10:43 -04002124
Nguyễn Thái Ngọc Duy735efde2014-08-16 10:08:03 +07002125 buffer = xmallocz_gently(size);
2126 if (!buffer)
2127 return NULL;
Shawn Pearcede530aa2006-08-26 04:10:43 -04002128 memset(&stream, 0, sizeof(stream));
Shawn Pearcede530aa2006-08-26 04:10:43 -04002129 stream.next_out = buffer;
Junio C Hamano39eea7b2009-10-21 23:06:14 -07002130 stream.avail_out = size + 1;
Shawn Pearcede530aa2006-08-26 04:10:43 -04002131
Linus Torvalds39c68542009-01-07 19:54:47 -08002132 git_inflate_init(&stream);
Shawn O. Pearce079afb12006-12-23 02:34:13 -05002133 do {
Nicolas Pitre2b87c452007-02-26 14:55:56 -05002134 in = use_pack(p, w_curs, curpos, &stream.avail_in);
Shawn O. Pearce079afb12006-12-23 02:34:13 -05002135 stream.next_in = in;
Linus Torvalds39c68542009-01-07 19:54:47 -08002136 st = git_inflate(&stream, Z_FINISH);
Junio C Hamano39eea7b2009-10-21 23:06:14 -07002137 if (!stream.avail_out)
2138 break; /* the payload is larger than it should be */
Nicolas Pitre2b87c452007-02-26 14:55:56 -05002139 curpos += stream.next_in - in;
Shawn O. Pearce079afb12006-12-23 02:34:13 -05002140 } while (st == Z_OK || st == Z_BUF_ERROR);
Linus Torvalds39c68542009-01-07 19:54:47 -08002141 git_inflate_end(&stream);
Shawn Pearcede530aa2006-08-26 04:10:43 -04002142 if ((st != Z_STREAM_END) || stream.total_out != size) {
2143 free(buffer);
2144 return NULL;
2145 }
2146
2147 return buffer;
2148}
2149
Jeff King8261e1f2016-08-22 18:00:07 -04002150static struct hashmap delta_base_cache;
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04002151static size_t delta_base_cached;
Nicolas Pitre5e08ecb2007-03-19 16:31:04 -04002152
Jeff King12d95ef2016-08-22 17:59:42 -04002153static LIST_HEAD(delta_base_cache_lru);
Nicolas Pitre5e08ecb2007-03-19 16:31:04 -04002154
Jeff King8261e1f2016-08-22 18:00:07 -04002155struct delta_base_cache_key {
Linus Torvaldse5e01612007-03-17 12:44:06 -07002156 struct packed_git *p;
2157 off_t base_offset;
Jeff King8261e1f2016-08-22 18:00:07 -04002158};
2159
2160struct delta_base_cache_entry {
2161 struct hashmap hash;
2162 struct delta_base_cache_key key;
2163 struct list_head lru;
2164 void *data;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002165 unsigned long size;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002166 enum object_type type;
Jeff King8261e1f2016-08-22 18:00:07 -04002167};
Linus Torvaldse5e01612007-03-17 12:44:06 -07002168
Jeff King8261e1f2016-08-22 18:00:07 -04002169static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
Linus Torvaldse5e01612007-03-17 12:44:06 -07002170{
Jeff King8261e1f2016-08-22 18:00:07 -04002171 unsigned int hash;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002172
Jeff King8261e1f2016-08-22 18:00:07 -04002173 hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002174 hash += (hash >> 8) + (hash >> 16);
Jeff King8261e1f2016-08-22 18:00:07 -04002175 return hash;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002176}
2177
Thomas Rast84dd81c2013-03-27 21:03:41 +01002178static struct delta_base_cache_entry *
2179get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
Junio C Hamano5266d362011-05-13 13:20:43 -07002180{
Jeff King8261e1f2016-08-22 18:00:07 -04002181 struct hashmap_entry entry;
2182 struct delta_base_cache_key key;
2183
2184 if (!delta_base_cache.cmpfn)
2185 return NULL;
2186
2187 hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
2188 key.p = p;
2189 key.base_offset = base_offset;
2190 return hashmap_get(&delta_base_cache, &entry, &key);
Thomas Rast84dd81c2013-03-27 21:03:41 +01002191}
2192
Jeff King8261e1f2016-08-22 18:00:07 -04002193static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
2194 const struct delta_base_cache_key *b)
Thomas Rast84dd81c2013-03-27 21:03:41 +01002195{
Jeff King8261e1f2016-08-22 18:00:07 -04002196 return a->p == b->p && a->base_offset == b->base_offset;
2197}
2198
2199static int delta_base_cache_hash_cmp(const void *va, const void *vb,
2200 const void *vkey)
2201{
2202 const struct delta_base_cache_entry *a = va, *b = vb;
2203 const struct delta_base_cache_key *key = vkey;
2204 if (key)
2205 return !delta_base_cache_key_eq(&a->key, key);
2206 else
2207 return !delta_base_cache_key_eq(&a->key, &b->key);
Junio C Hamano5266d362011-05-13 13:20:43 -07002208}
2209
Thomas Rast84dd81c2013-03-27 21:03:41 +01002210static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
2211{
Jeff King8261e1f2016-08-22 18:00:07 -04002212 return !!get_delta_base_cache_entry(p, base_offset);
Thomas Rast84dd81c2013-03-27 21:03:41 +01002213}
2214
Jeff King4a5397c2016-08-22 17:57:53 -04002215/*
2216 * Remove the entry from the cache, but do _not_ free the associated
2217 * entry data. The caller takes ownership of the "data" buffer, and
2218 * should copy out any fields it wants before detaching.
2219 */
2220static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
Thomas Rast84dd81c2013-03-27 21:03:41 +01002221{
Jeff King8261e1f2016-08-22 18:00:07 -04002222 hashmap_remove(&delta_base_cache, ent, &ent->key);
Jeff King12d95ef2016-08-22 17:59:42 -04002223 list_del(&ent->lru);
Thomas Rast84dd81c2013-03-27 21:03:41 +01002224 delta_base_cached -= ent->size;
Jeff King8261e1f2016-08-22 18:00:07 -04002225 free(ent);
Thomas Rast84dd81c2013-03-27 21:03:41 +01002226}
2227
Linus Torvalds62f255a2007-03-17 12:42:15 -07002228static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
Jeff King85fe35a2016-08-22 17:57:45 -04002229 unsigned long *base_size, enum object_type *type)
Linus Torvalds62f255a2007-03-17 12:42:15 -07002230{
Thomas Rast84dd81c2013-03-27 21:03:41 +01002231 struct delta_base_cache_entry *ent;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002232
Thomas Rast84dd81c2013-03-27 21:03:41 +01002233 ent = get_delta_base_cache_entry(p, base_offset);
Jeff King8261e1f2016-08-22 18:00:07 -04002234 if (!ent)
Miklos Vajna749bc582008-10-09 02:11:24 +02002235 return unpack_entry(p, base_offset, type, base_size);
Linus Torvaldse5e01612007-03-17 12:44:06 -07002236
Linus Torvaldse5e01612007-03-17 12:44:06 -07002237 *type = ent->type;
2238 *base_size = ent->size;
Jeff King85fe35a2016-08-22 17:57:45 -04002239 return xmemdupz(ent->data, ent->size);
Linus Torvalds62f255a2007-03-17 12:42:15 -07002240}
2241
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04002242static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
2243{
Jeff King8261e1f2016-08-22 18:00:07 -04002244 free(ent->data);
2245 detach_delta_base_cache_entry(ent);
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04002246}
2247
Shawn O. Pearce3d20c632009-02-10 13:36:12 -08002248void clear_delta_base_cache(void)
2249{
Jeff King8261e1f2016-08-22 18:00:07 -04002250 struct hashmap_iter iter;
2251 struct delta_base_cache_entry *entry;
2252 for (entry = hashmap_iter_first(&delta_base_cache, &iter);
2253 entry;
2254 entry = hashmap_iter_next(&iter)) {
2255 release_delta_base_cache(entry);
2256 }
Shawn O. Pearce3d20c632009-02-10 13:36:12 -08002257}
2258
Linus Torvalds62f255a2007-03-17 12:42:15 -07002259static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
2260 void *base, unsigned long base_size, enum object_type type)
2261{
Jeff King8261e1f2016-08-22 18:00:07 -04002262 struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
Jeff King4b92bae2016-09-12 12:46:17 -04002263 struct list_head *lru, *tmp;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002264
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04002265 delta_base_cached += base_size;
Nicolas Pitre5e08ecb2007-03-19 16:31:04 -04002266
Jeff King4b92bae2016-09-12 12:46:17 -04002267 list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
Jeff King12d95ef2016-08-22 17:59:42 -04002268 struct delta_base_cache_entry *f =
2269 list_entry(lru, struct delta_base_cache_entry, lru);
2270 if (delta_base_cached <= delta_base_cache_limit)
2271 break;
Nicolas Pitre5e08ecb2007-03-19 16:31:04 -04002272 release_delta_base_cache(f);
2273 }
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04002274
Jeff King8261e1f2016-08-22 18:00:07 -04002275 ent->key.p = p;
2276 ent->key.base_offset = base_offset;
Linus Torvaldse5e01612007-03-17 12:44:06 -07002277 ent->type = type;
2278 ent->data = base;
2279 ent->size = base_size;
Jeff King12d95ef2016-08-22 17:59:42 -04002280 list_add_tail(&ent->lru, &delta_base_cache_lru);
Jeff King8261e1f2016-08-22 18:00:07 -04002281
2282 if (!delta_base_cache.cmpfn)
2283 hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, 0);
2284 hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
2285 hashmap_add(&delta_base_cache, ent);
Linus Torvalds62f255a2007-03-17 12:42:15 -07002286}
2287
Christian Couderc2c5b272009-01-12 18:42:24 +01002288static void *read_object(const unsigned char *sha1, enum object_type *type,
2289 unsigned long *size);
2290
Junio C Hamano5f443242011-07-06 19:08:55 -07002291static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
2292{
Karsten Blees67dc5982014-07-12 02:01:38 +02002293 static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
2294 trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
2295 p->pack_name, (uintmax_t)obj_offset);
Junio C Hamano5f443242011-07-06 19:08:55 -07002296}
2297
Nicolas Pitre0e8189e2008-10-31 11:31:08 -04002298int do_check_packed_object_crc;
2299
Thomas Rastabe601b2013-03-27 21:03:42 +01002300#define UNPACK_ENTRY_STACK_PREALLOC 64
2301struct unpack_entry_stack_ent {
2302 off_t obj_offset;
2303 off_t curpos;
2304 unsigned long size;
2305};
2306
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05002307void *unpack_entry(struct packed_git *p, off_t obj_offset,
Thomas Rastabe601b2013-03-27 21:03:42 +01002308 enum object_type *final_type, unsigned long *final_size)
Junio C Hamanof3bf9222005-06-30 17:15:39 -07002309{
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05002310 struct pack_window *w_curs = NULL;
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05002311 off_t curpos = obj_offset;
Thomas Rastabe601b2013-03-27 21:03:42 +01002312 void *data = NULL;
2313 unsigned long size;
2314 enum object_type type;
2315 struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
2316 struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
2317 int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
2318 int base_from_cache = 0;
Junio C Hamano1f688552005-06-27 03:35:33 -07002319
Karsten Blees67dc5982014-07-12 02:01:38 +02002320 write_pack_access_log(p, obj_offset);
Junio C Hamano5f443242011-07-06 19:08:55 -07002321
Thomas Rastabe601b2013-03-27 21:03:42 +01002322 /* PHASE 1: drill down to the innermost base object */
2323 for (;;) {
2324 off_t base_offset;
2325 int i;
2326 struct delta_base_cache_entry *ent;
2327
Nguyễn Thái Ngọc Duy77965f82013-09-13 18:03:00 +07002328 ent = get_delta_base_cache_entry(p, curpos);
Jeff King8261e1f2016-08-22 18:00:07 -04002329 if (ent) {
Nguyễn Thái Ngọc Duy77965f82013-09-13 18:03:00 +07002330 type = ent->type;
2331 data = ent->data;
2332 size = ent->size;
Jeff King4a5397c2016-08-22 17:57:53 -04002333 detach_delta_base_cache_entry(ent);
Nguyễn Thái Ngọc Duy77965f82013-09-13 18:03:00 +07002334 base_from_cache = 1;
2335 break;
2336 }
2337
Thomas Rastabe601b2013-03-27 21:03:42 +01002338 if (do_check_packed_object_crc && p->index_version > 1) {
2339 struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
Nguyễn Thái Ngọc Duy211c61c2016-07-05 19:05:54 +02002340 off_t len = revidx[1].offset - obj_offset;
Thomas Rastabe601b2013-03-27 21:03:42 +01002341 if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
2342 const unsigned char *sha1 =
2343 nth_packed_object_sha1(p, revidx->nr);
2344 error("bad packed object CRC for %s",
2345 sha1_to_hex(sha1));
2346 mark_bad_packed_object(p, sha1);
2347 unuse_pack(&w_curs);
2348 return NULL;
2349 }
Nicolas Pitre0e8189e2008-10-31 11:31:08 -04002350 }
Thomas Rastabe601b2013-03-27 21:03:42 +01002351
Thomas Rastabe601b2013-03-27 21:03:42 +01002352 type = unpack_object_header(p, &w_curs, &curpos, &size);
2353 if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
2354 break;
2355
2356 base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
2357 if (!base_offset) {
2358 error("failed to validate delta base reference "
2359 "at offset %"PRIuMAX" from %s",
2360 (uintmax_t)curpos, p->pack_name);
2361 /* bail to phase 2, in hopes of recovery */
2362 data = NULL;
2363 break;
2364 }
2365
2366 /* push object, proceed to base */
2367 if (delta_stack_nr >= delta_stack_alloc
2368 && delta_stack == small_delta_stack) {
2369 delta_stack_alloc = alloc_nr(delta_stack_nr);
Jeff Kingb32fa952016-02-22 17:44:25 -05002370 ALLOC_ARRAY(delta_stack, delta_stack_alloc);
Thomas Rastabe601b2013-03-27 21:03:42 +01002371 memcpy(delta_stack, small_delta_stack,
2372 sizeof(*delta_stack)*delta_stack_nr);
2373 } else {
2374 ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
2375 }
2376 i = delta_stack_nr++;
2377 delta_stack[i].obj_offset = obj_offset;
2378 delta_stack[i].curpos = curpos;
2379 delta_stack[i].size = size;
2380
2381 curpos = obj_offset = base_offset;
Nicolas Pitre0e8189e2008-10-31 11:31:08 -04002382 }
2383
Thomas Rastabe601b2013-03-27 21:03:42 +01002384 /* PHASE 2: handle the base */
2385 switch (type) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -04002386 case OBJ_OFS_DELTA:
2387 case OBJ_REF_DELTA:
Thomas Rastabe601b2013-03-27 21:03:42 +01002388 if (data)
Johannes Schindelinef1177d12016-07-26 18:05:50 +02002389 die("BUG: unpack_entry: left loop at a valid delta");
Shawn O. Pearce4d703a12006-12-23 02:33:25 -05002390 break;
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002391 case OBJ_COMMIT:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002392 case OBJ_TREE:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002393 case OBJ_BLOB:
Linus Torvaldsa733cb62005-06-28 14:21:02 -07002394 case OBJ_TAG:
Thomas Rastabe601b2013-03-27 21:03:42 +01002395 if (!base_from_cache)
2396 data = unpack_compressed_entry(p, &w_curs, curpos, size);
Shawn O. Pearce4d703a12006-12-23 02:33:25 -05002397 break;
Junio C Hamano1f688552005-06-27 03:35:33 -07002398 default:
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04002399 data = NULL;
2400 error("unknown object type %i at offset %"PRIuMAX" in %s",
Thomas Rastabe601b2013-03-27 21:03:42 +01002401 type, (uintmax_t)obj_offset, p->pack_name);
Junio C Hamano1f688552005-06-27 03:35:33 -07002402 }
Thomas Rastabe601b2013-03-27 21:03:42 +01002403
2404 /* PHASE 3: apply deltas in order */
2405
2406 /* invariants:
2407 * 'data' holds the base data, or NULL if there was corruption
2408 */
2409 while (delta_stack_nr) {
2410 void *delta_data;
2411 void *base = data;
2412 unsigned long delta_size, base_size = size;
2413 int i;
2414
2415 data = NULL;
2416
2417 if (base)
2418 add_delta_base_cache(p, obj_offset, base, base_size, type);
2419
2420 if (!base) {
2421 /*
2422 * We're probably in deep shit, but let's try to fetch
2423 * the required base anyway from another pack or loose.
2424 * This is costly but should happen only in the presence
2425 * of a corrupted pack, and is better than failing outright.
2426 */
2427 struct revindex_entry *revidx;
2428 const unsigned char *base_sha1;
2429 revidx = find_pack_revindex(p, obj_offset);
2430 if (revidx) {
2431 base_sha1 = nth_packed_object_sha1(p, revidx->nr);
2432 error("failed to read delta base object %s"
2433 " at offset %"PRIuMAX" from %s",
2434 sha1_to_hex(base_sha1), (uintmax_t)obj_offset,
2435 p->pack_name);
2436 mark_bad_packed_object(p, base_sha1);
2437 base = read_object(base_sha1, &type, &base_size);
2438 }
2439 }
2440
2441 i = --delta_stack_nr;
2442 obj_offset = delta_stack[i].obj_offset;
2443 curpos = delta_stack[i].curpos;
2444 delta_size = delta_stack[i].size;
2445
2446 if (!base)
2447 continue;
2448
2449 delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
2450
2451 if (!delta_data) {
2452 error("failed to unpack compressed delta "
2453 "at offset %"PRIuMAX" from %s",
2454 (uintmax_t)curpos, p->pack_name);
Thomas Rastabe601b2013-03-27 21:03:42 +01002455 data = NULL;
2456 continue;
2457 }
2458
2459 data = patch_delta(base, base_size,
2460 delta_data, delta_size,
2461 &size);
Jeff King1ee886c2013-06-14 17:53:34 -04002462
2463 /*
2464 * We could not apply the delta; warn the user, but keep going.
2465 * Our failure will be noticed either in the next iteration of
2466 * the loop, or if this is the final delta, in the caller when
2467 * we return NULL. Those code paths will take care of making
2468 * a more explicit warning and retrying with another copy of
2469 * the object.
2470 */
Thomas Rastabe601b2013-03-27 21:03:42 +01002471 if (!data)
Jeff King1ee886c2013-06-14 17:53:34 -04002472 error("failed to apply delta");
Thomas Rastabe601b2013-03-27 21:03:42 +01002473
Felipe Contreras4b8f7722013-05-30 08:56:21 -05002474 free(delta_data);
Thomas Rastabe601b2013-03-27 21:03:42 +01002475 }
2476
2477 *final_type = type;
2478 *final_size = size;
2479
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05002480 unuse_pack(&w_curs);
Nguyễn Thái Ngọc Duy019d1e62014-02-21 06:47:47 +07002481
2482 if (delta_stack != small_delta_stack)
2483 free(delta_stack);
2484
Nicolas Pitre21666f12007-02-26 14:55:59 -05002485 return data;
Junio C Hamano1f688552005-06-27 03:35:33 -07002486}
2487
Shawn O. Pearced0798372007-05-26 01:24:19 -04002488const unsigned char *nth_packed_object_sha1(struct packed_git *p,
Nicolas Pitred72308e2007-04-04 16:49:04 -04002489 uint32_t n)
Junio C Hamano9a217f22005-06-28 14:56:57 -07002490{
Nicolas Pitre42873072007-03-16 16:42:50 -04002491 const unsigned char *index = p->index_data;
Shawn O. Pearced0798372007-05-26 01:24:19 -04002492 if (!index) {
2493 if (open_pack_index(p))
2494 return NULL;
2495 index = p->index_data;
2496 }
Nicolas Pitre57059092007-04-09 01:06:28 -04002497 if (n >= p->num_objects)
Nicolas Pitred72308e2007-04-04 16:49:04 -04002498 return NULL;
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002499 index += 4 * 256;
2500 if (p->index_version == 1) {
2501 return index + 24 * n + 4;
2502 } else {
2503 index += 8;
2504 return index + 20 * n;
2505 }
2506}
2507
Jeff King47fe3f62016-02-25 09:22:52 -05002508void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
2509{
2510 const unsigned char *ptr = vptr;
2511 const unsigned char *start = p->index_data;
2512 const unsigned char *end = start + p->index_size;
2513 if (ptr < start)
Nguyễn Thái Ngọc Duy7465feb2016-02-27 14:49:33 +07002514 die(_("offset before start of pack index for %s (corrupt index?)"),
Jeff King47fe3f62016-02-25 09:22:52 -05002515 p->pack_name);
2516 /* No need to check for underflow; .idx files must be at least 8 bytes */
2517 if (ptr >= end - 8)
Nguyễn Thái Ngọc Duy7465feb2016-02-27 14:49:33 +07002518 die(_("offset beyond end of pack index for %s (truncated index?)"),
Jeff King47fe3f62016-02-25 09:22:52 -05002519 p->pack_name);
2520}
2521
Nicolas Pitre99093232008-06-24 23:17:12 -04002522off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002523{
2524 const unsigned char *index = p->index_data;
2525 index += 4 * 256;
2526 if (p->index_version == 1) {
2527 return ntohl(*((uint32_t *)(index + 24 * n)));
2528 } else {
2529 uint32_t off;
2530 index += 8 + p->num_objects * (20 + 4);
2531 off = ntohl(*((uint32_t *)(index + 4 * n)));
2532 if (!(off & 0x80000000))
2533 return off;
2534 index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
Jeff King47fe3f62016-02-25 09:22:52 -05002535 check_pack_index_ptr(p, index);
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002536 return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
2537 ntohl(*((uint32_t *)(index + 4)));
2538 }
Junio C Hamano9a217f22005-06-28 14:56:57 -07002539}
2540
Shawn O. Pearcec4001d92007-03-06 20:44:30 -05002541off_t find_pack_entry_one(const unsigned char *sha1,
Nicolas Pitre43057302006-09-21 00:05:37 -04002542 struct packed_git *p)
Junio C Hamano1f688552005-06-27 03:35:33 -07002543{
Nicolas Pitre42873072007-03-16 16:42:50 -04002544 const uint32_t *level1_ofs = p->index_data;
Nicolas Pitre42873072007-03-16 16:42:50 -04002545 const unsigned char *index = p->index_data;
Junio C Hamano628522e2007-12-29 02:05:47 -08002546 unsigned hi, lo, stride;
2547 static int use_lookup = -1;
2548 static int debug_lookup = -1;
2549
2550 if (debug_lookup < 0)
2551 debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
Nicolas Pitre42873072007-03-16 16:42:50 -04002552
Shawn O. Pearced0798372007-05-26 01:24:19 -04002553 if (!index) {
2554 if (open_pack_index(p))
2555 return 0;
2556 level1_ofs = p->index_data;
2557 index = p->index_data;
2558 }
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002559 if (p->index_version > 1) {
2560 level1_ofs += 2;
2561 index += 8;
2562 }
Nicolas Pitre42873072007-03-16 16:42:50 -04002563 index += 4 * 256;
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002564 hi = ntohl(level1_ofs[*sha1]);
2565 lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
Junio C Hamano628522e2007-12-29 02:05:47 -08002566 if (p->index_version > 1) {
2567 stride = 20;
2568 } else {
2569 stride = 24;
2570 index += 4;
2571 }
2572
2573 if (debug_lookup)
Ramsay Jones6e1c2342008-07-03 16:52:09 +01002574 printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
Junio C Hamano628522e2007-12-29 02:05:47 -08002575 sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
2576
2577 if (use_lookup < 0)
2578 use_lookup = !!getenv("GIT_USE_LOOKUP");
2579 if (use_lookup) {
2580 int pos = sha1_entry_pos(index, stride, 0,
2581 lo, hi, p->num_objects, sha1);
2582 if (pos < 0)
2583 return 0;
2584 return nth_packed_object_offset(p, pos);
2585 }
Junio C Hamano1f688552005-06-27 03:35:33 -07002586
2587 do {
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002588 unsigned mi = (lo + hi) / 2;
Junio C Hamano628522e2007-12-29 02:05:47 -08002589 int cmp = hashcmp(index + mi * stride, sha1);
2590
2591 if (debug_lookup)
2592 printf("lo %u hi %u rg %u mi %u\n",
2593 lo, hi, hi - lo, mi);
Nicolas Pitre43057302006-09-21 00:05:37 -04002594 if (!cmp)
Nicolas Pitre74e34e12007-04-09 01:06:35 -04002595 return nth_packed_object_offset(p, mi);
Junio C Hamano1f688552005-06-27 03:35:33 -07002596 if (cmp > 0)
2597 hi = mi;
2598 else
2599 lo = mi+1;
2600 } while (lo < hi);
2601 return 0;
2602}
2603
Jeff King4c080182011-10-14 14:03:48 -04002604int is_pack_valid(struct packed_git *p)
Shawn O. Pearced131b7a2011-03-02 10:01:54 -08002605{
2606 /* An already open pack is known to be valid. */
2607 if (p->pack_fd != -1)
2608 return 1;
2609
2610 /* If the pack has one window completely covering the
2611 * file size, the pack is known to be valid even if
2612 * the descriptor is not currently open.
2613 */
2614 if (p->windows) {
2615 struct pack_window *w = p->windows;
2616
2617 if (!w->offset && w->len == p->pack_size)
2618 return 1;
2619 }
2620
2621 /* Force the pack to open to prove its valid. */
2622 return !open_packed_git(p);
2623}
2624
Nguyễn Thái Ngọc Duy95099732012-02-01 20:48:54 +07002625static int fill_pack_entry(const unsigned char *sha1,
2626 struct pack_entry *e,
2627 struct packed_git *p)
2628{
2629 off_t offset;
2630
2631 if (p->num_bad_objects) {
2632 unsigned i;
2633 for (i = 0; i < p->num_bad_objects; i++)
2634 if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
2635 return 0;
2636 }
2637
2638 offset = find_pack_entry_one(sha1, p);
2639 if (!offset)
2640 return 0;
2641
2642 /*
2643 * We are about to tell the caller where they can locate the
2644 * requested object. We better make sure the packfile is
2645 * still here and can be accessed before supplying that
2646 * answer, as it may have been deleted since the index was
2647 * loaded!
2648 */
Jeff King319b6782015-03-30 20:47:38 -04002649 if (!is_pack_valid(p))
Nguyễn Thái Ngọc Duy95099732012-02-01 20:48:54 +07002650 return 0;
Nguyễn Thái Ngọc Duy95099732012-02-01 20:48:54 +07002651 e->offset = offset;
2652 e->p = p;
2653 hashcpy(e->sha1, sha1);
2654 return 1;
2655}
2656
Michael Haggertyd40d5352014-02-21 17:32:06 +01002657/*
2658 * Iff a pack file contains the object named by sha1, return true and
2659 * store its location to e.
2660 */
Brandon Casey4d6acb72009-03-19 22:47:54 -05002661static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
Junio C Hamano1f688552005-06-27 03:35:33 -07002662{
Jeff Kinga73cdd22016-07-29 00:09:46 -04002663 struct mru_entry *p;
Nicolas Pitre43057302006-09-21 00:05:37 -04002664
Junio C Hamano1f688552005-06-27 03:35:33 -07002665 prepare_packed_git();
Nicolas Pitref7c22cc2007-05-30 22:48:13 -04002666 if (!packed_git)
2667 return 0;
Junio C Hamano1f688552005-06-27 03:35:33 -07002668
Jeff Kinga73cdd22016-07-29 00:09:46 -04002669 for (p = packed_git_mru->head; p; p = p->next) {
2670 if (fill_pack_entry(sha1, e, p->item)) {
2671 mru_mark(packed_git_mru, p);
Michael Haggerty1b1005d2014-02-21 17:32:04 +01002672 return 1;
2673 }
Nguyễn Thái Ngọc Duyc01f51c2012-02-01 20:48:55 +07002674 }
Junio C Hamano1f688552005-06-27 03:35:33 -07002675 return 0;
2676}
2677
Junio C Hamanoa6080a02007-06-07 00:04:01 -07002678struct packed_git *find_sha1_pack(const unsigned char *sha1,
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04002679 struct packed_git *packs)
2680{
2681 struct packed_git *p;
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04002682
2683 for (p = packs; p; p = p->next) {
Nicolas Pitre43057302006-09-21 00:05:37 -04002684 if (find_pack_entry_one(sha1, p))
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04002685 return p;
2686 }
2687 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002688
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04002689}
2690
Jeff King052fe5e2013-07-12 02:30:48 -04002691static int sha1_loose_object_info(const unsigned char *sha1,
Karthik Nayak46f03442015-05-03 19:59:59 +05302692 struct object_info *oi,
2693 int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07002694{
Karthik Nayak46f03442015-05-03 19:59:59 +05302695 int status = 0;
2696 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07002697 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07002698 git_zstream stream;
Nicolas Pitred65a16f2007-02-26 14:55:55 -05002699 char hdr[32];
Karthik Nayak46f03442015-05-03 19:59:59 +05302700 struct strbuf hdrbuf = STRBUF_INIT;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07002701
Jeff King5d642e72013-12-21 09:24:20 -05002702 if (oi->delta_base_sha1)
2703 hashclr(oi->delta_base_sha1);
2704
Jeff King052fe5e2013-07-12 02:30:48 -04002705 /*
2706 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08002707 * need to look inside the object at all. Note that we
2708 * do not optimize out the stat call, even if the
2709 * caller doesn't care about the disk-size, since our
2710 * return value implicitly indicates whether the
2711 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04002712 */
Karthik Nayak46f03442015-05-03 19:59:59 +05302713 if (!oi->typep && !oi->typename && !oi->sizep) {
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08002714 struct stat st;
2715 if (stat_sha1_file(sha1, &st) < 0)
2716 return -1;
2717 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04002718 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04002719 return 0;
2720 }
2721
Junio C Hamanobb6b8e42006-07-17 15:04:47 -07002722 map = map_sha1_file(sha1, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002723 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02002724 return -1;
Jeff King23c339c2013-07-12 02:37:53 -04002725 if (oi->disk_sizep)
2726 *oi->disk_sizep = mapsize;
Karthik Nayak46f03442015-05-03 19:59:59 +05302727 if ((flags & LOOKUP_UNKNOWN_OBJECT)) {
2728 if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
2729 status = error("unable to unpack %s header with --allow-unknown-type",
2730 sha1_to_hex(sha1));
2731 } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
Junio C Hamano36e4d742005-06-27 03:34:06 -07002732 status = error("unable to unpack %s header",
2733 sha1_to_hex(sha1));
Karthik Nayak46f03442015-05-03 19:59:59 +05302734 if (status < 0)
2735 ; /* Do nothing */
2736 else if (hdrbuf.len) {
2737 if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
2738 status = error("unable to parse %s header with --allow-unknown-type",
2739 sha1_to_hex(sha1));
2740 } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
Junio C Hamano36e4d742005-06-27 03:34:06 -07002741 status = error("unable to parse %s header", sha1_to_hex(sha1));
Linus Torvalds39c68542009-01-07 19:54:47 -08002742 git_inflate_end(&stream);
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07002743 munmap(map, mapsize);
Karthik Nayak46f03442015-05-03 19:59:59 +05302744 if (status && oi->typep)
Jeff King23c339c2013-07-12 02:37:53 -04002745 *oi->typep = status;
Karthik Nayak46f03442015-05-03 19:59:59 +05302746 strbuf_release(&hdrbuf);
Jeff King052fe5e2013-07-12 02:30:48 -04002747 return 0;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07002748}
2749
Christian Couderde7b5d62013-12-11 08:46:07 +01002750int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002751{
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07002752 struct cached_object *co;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002753 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04002754 int rtype;
Karthik Nayak46f03442015-05-03 19:59:59 +05302755 enum object_type real_type;
Christian Couder1f7117e2013-12-11 08:46:09 +01002756 const unsigned char *real = lookup_replace_object_extended(sha1, flags);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002757
Christian Couder1f7117e2013-12-11 08:46:09 +01002758 co = find_cached_object(real);
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07002759 if (co) {
Jeff King5b086402013-07-12 02:34:57 -04002760 if (oi->typep)
2761 *(oi->typep) = co->type;
Junio C Hamano9a490592011-05-12 15:51:38 -07002762 if (oi->sizep)
2763 *(oi->sizep) = co->size;
Jeff King161f00e2013-07-07 06:04:00 -04002764 if (oi->disk_sizep)
2765 *(oi->disk_sizep) = 0;
Jeff King5d642e72013-12-21 09:24:20 -05002766 if (oi->delta_base_sha1)
2767 hashclr(oi->delta_base_sha1);
Karthik Nayak46f03442015-05-03 19:59:59 +05302768 if (oi->typename)
2769 strbuf_addstr(oi->typename, typename(co->type));
Junio C Hamano9a490592011-05-12 15:51:38 -07002770 oi->whence = OI_CACHED;
Jeff King5b086402013-07-12 02:34:57 -04002771 return 0;
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07002772 }
2773
Christian Couder1f7117e2013-12-11 08:46:09 +01002774 if (!find_pack_entry(real, &e)) {
Steven Grimmddd63e62008-08-05 13:08:41 -07002775 /* Most likely it's a loose object. */
Karthik Nayak46f03442015-05-03 19:59:59 +05302776 if (!sha1_loose_object_info(real, oi, flags)) {
Junio C Hamano9a490592011-05-12 15:51:38 -07002777 oi->whence = OI_LOOSE;
Jeff King5b086402013-07-12 02:34:57 -04002778 return 0;
Junio C Hamano9a490592011-05-12 15:51:38 -07002779 }
Steven Grimmddd63e62008-08-05 13:08:41 -07002780
2781 /* Not a loose object; someone else may have just packed it. */
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002782 reprepare_packed_git();
Christian Couder1f7117e2013-12-11 08:46:09 +01002783 if (!find_pack_entry(real, &e))
Jeff King052fe5e2013-07-12 02:30:48 -04002784 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002785 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04002786
Karthik Nayak46f03442015-05-03 19:59:59 +05302787 /*
2788 * packed_object_info() does not follow the delta chain to
2789 * find out the real type, unless it is given oi->typep.
2790 */
2791 if (oi->typename && !oi->typep)
2792 oi->typep = &real_type;
2793
Jeff King23c339c2013-07-12 02:37:53 -04002794 rtype = packed_object_info(e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04002795 if (rtype < 0) {
Christian Couder1f7117e2013-12-11 08:46:09 +01002796 mark_bad_packed_object(e.p, real);
Karthik Nayak46f03442015-05-03 19:59:59 +05302797 if (oi->typep == &real_type)
2798 oi->typep = NULL;
Christian Couder1f7117e2013-12-11 08:46:09 +01002799 return sha1_object_info_extended(real, oi, 0);
Junio C Hamano5266d362011-05-13 13:20:43 -07002800 } else if (in_delta_base_cache(e.p, e.offset)) {
2801 oi->whence = OI_DBCACHED;
Junio C Hamano9a490592011-05-12 15:51:38 -07002802 } else {
2803 oi->whence = OI_PACKED;
2804 oi->u.packed.offset = e.offset;
2805 oi->u.packed.pack = e.p;
2806 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
2807 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04002808 }
Karthik Nayak46f03442015-05-03 19:59:59 +05302809 if (oi->typename)
2810 strbuf_addstr(oi->typename, typename(*oi->typep));
2811 if (oi->typep == &real_type)
2812 oi->typep = NULL;
Nicolas Pitre3d77d872008-10-29 19:02:47 -04002813
Jeff King5b086402013-07-12 02:34:57 -04002814 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01002815}
2816
Christian Couder3fc0dca2013-10-27 00:34:30 +02002817/* returns enum object_type or negative */
Junio C Hamano9a490592011-05-12 15:51:38 -07002818int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
2819{
Jeff King5b086402013-07-12 02:34:57 -04002820 enum object_type type;
Ramsay Jonesd099b712013-07-18 21:25:50 +01002821 struct object_info oi = {NULL};
Junio C Hamano9a490592011-05-12 15:51:38 -07002822
Jeff King5b086402013-07-12 02:34:57 -04002823 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07002824 oi.sizep = sizep;
Christian Couderde7b5d62013-12-11 08:46:07 +01002825 if (sha1_object_info_extended(sha1, &oi, LOOKUP_REPLACE_OBJECT) < 0)
Jeff King5b086402013-07-12 02:34:57 -04002826 return -1;
2827 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07002828}
2829
Nicolas Pitre21666f12007-02-26 14:55:59 -05002830static void *read_packed_sha1(const unsigned char *sha1,
2831 enum object_type *type, unsigned long *size)
Junio C Hamano1f688552005-06-27 03:35:33 -07002832{
2833 struct pack_entry e;
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04002834 void *data;
Junio C Hamano1f688552005-06-27 03:35:33 -07002835
Junio C Hamanocd673c12009-02-27 23:15:53 -08002836 if (!find_pack_entry(sha1, &e))
Junio C Hamano1f688552005-06-27 03:35:33 -07002837 return NULL;
Jeff King85fe35a2016-08-22 17:57:45 -04002838 data = cache_or_unpack_entry(e.p, e.offset, size, type);
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04002839 if (!data) {
2840 /*
2841 * We're probably in deep shit, but let's try to fetch
2842 * the required object anyway from another pack or loose.
2843 * This should happen only in the presence of a corrupted
2844 * pack, and is better than failing outright.
2845 */
2846 error("failed to read object %s at offset %"PRIuMAX" from %s",
2847 sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
2848 mark_bad_packed_object(e.p, sha1);
Nicolas Pitreac939102008-07-14 21:46:48 -04002849 data = read_object(sha1, type, size);
Nicolas Pitre8eca0b42008-06-23 21:23:39 -04002850 }
2851 return data;
Junio C Hamano1f688552005-06-27 03:35:33 -07002852}
2853
Nicolas Pitre21666f12007-02-26 14:55:59 -05002854int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
2855 unsigned char *sha1)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002856{
2857 struct cached_object *co;
2858
Nicolas Pitre21666f12007-02-26 14:55:59 -05002859 hash_sha1_file(buf, len, typename(type), sha1);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002860 if (has_sha1_file(sha1) || find_cached_object(sha1))
2861 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04002862 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002863 co = &cached_objects[cached_object_nr++];
2864 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002865 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08002866 co->buf = xmalloc(len);
2867 memcpy(co->buf, buf, len);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002868 hashcpy(co->sha1, sha1);
2869 return 0;
2870}
2871
Christian Couderc2c5b272009-01-12 18:42:24 +01002872static void *read_object(const unsigned char *sha1, enum object_type *type,
2873 unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002874{
2875 unsigned long mapsize;
2876 void *map, *buf;
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002877 struct cached_object *co;
2878
2879 co = find_cached_object(sha1);
2880 if (co) {
Nicolas Pitre21666f12007-02-26 14:55:59 -05002881 *type = co->type;
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002882 *size = co->size;
Pierre Habouzit182af832007-09-16 00:32:36 +02002883 return xmemdupz(co->buf, co->size);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08002884 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002885
Peter Eriksen8276c002007-01-22 21:29:45 +01002886 buf = read_packed_sha1(sha1, type, size);
2887 if (buf)
2888 return buf;
Junio C Hamanobb6b8e42006-07-17 15:04:47 -07002889 map = map_sha1_file(sha1, &mapsize);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002890 if (map) {
Junio C Hamano7efbff72007-03-05 00:21:37 -08002891 buf = unpack_sha1_file(map, mapsize, type, size, sha1);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002892 munmap(map, mapsize);
2893 return buf;
2894 }
Jeff King637cdd92006-06-02 11:32:23 -04002895 reprepare_packed_git();
Peter Eriksen8276c002007-01-22 21:29:45 +01002896 return read_packed_sha1(sha1, type, size);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002897}
2898
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002899/*
2900 * This function dies on corrupt objects; the callers who want to
2901 * deal with them should arrange to call read_object() and give error
2902 * messages themselves.
2903 */
Junio C Hamano5bf29b92011-05-15 12:54:54 -07002904void *read_sha1_file_extended(const unsigned char *sha1,
2905 enum object_type *type,
2906 unsigned long *size,
2907 unsigned flag)
Nicolas Pitreac939102008-07-14 21:46:48 -04002908{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07002909 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002910 const struct packed_git *p;
Christian Couderbf93eea2013-12-11 08:46:06 +01002911 const unsigned char *repl = lookup_replace_object_extended(sha1, flag);
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002912
Junio C Hamano3ba7a062010-10-28 11:13:06 -07002913 errno = 0;
2914 data = read_object(repl, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07002915 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002916 return data;
Christian Couder68095572009-01-23 10:06:53 +01002917
Björn Steinbrink25f3af32011-01-20 21:12:20 +01002918 if (errno && errno != ENOENT)
Junio C Hamano3ba7a062010-10-28 11:13:06 -07002919 die_errno("failed to read object %s", sha1_to_hex(sha1));
2920
Christian Couder68095572009-01-23 10:06:53 +01002921 /* die if we replaced an object with one that does not exist */
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002922 if (repl != sha1)
Christian Couder68095572009-01-23 10:06:53 +01002923 die("replacement %s not found for %s",
2924 sha1_to_hex(repl), sha1_to_hex(sha1));
2925
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002926 if (has_loose_object(repl)) {
Michael Haggerty30d6c6e2014-02-21 17:32:05 +01002927 const char *path = sha1_file_name(sha1);
2928
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002929 die("loose object %s (stored in %s) is corrupt",
2930 sha1_to_hex(repl), path);
Ævar Arnfjörð Bjarmasone8b15e62010-06-10 12:47:01 +00002931 }
Christian Couder68095572009-01-23 10:06:53 +01002932
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002933 if ((p = has_packed_and_bad(repl)) != NULL)
2934 die("packed object %s (stored in %s) is corrupt",
2935 sha1_to_hex(repl), p->pack_name);
Christian Couderf5552ae2009-01-23 10:07:01 +01002936
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07002937 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04002938}
2939
Junio C Hamano40469ee2005-04-28 16:42:27 -07002940void *read_object_with_reference(const unsigned char *sha1,
Nicolas Pitre21666f12007-02-26 14:55:59 -05002941 const char *required_type_name,
Junio C Hamano40469ee2005-04-28 16:42:27 -07002942 unsigned long *size,
2943 unsigned char *actual_sha1_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07002944{
Nicolas Pitre21666f12007-02-26 14:55:59 -05002945 enum object_type type, required_type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07002946 void *buffer;
2947 unsigned long isize;
Junio C Hamano40469ee2005-04-28 16:42:27 -07002948 unsigned char actual_sha1[20];
Junio C Hamanof4913f92005-04-20 18:06:49 -07002949
Nicolas Pitre21666f12007-02-26 14:55:59 -05002950 required_type = type_from_string(required_type_name);
Shawn Pearcee7024962006-08-23 02:49:00 -04002951 hashcpy(actual_sha1, sha1);
Junio C Hamano40469ee2005-04-28 16:42:27 -07002952 while (1) {
2953 int ref_length = -1;
2954 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07002955
Nicolas Pitre21666f12007-02-26 14:55:59 -05002956 buffer = read_sha1_file(actual_sha1, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07002957 if (!buffer)
2958 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002959 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07002960 *size = isize;
2961 if (actual_sha1_return)
Shawn Pearcee7024962006-08-23 02:49:00 -04002962 hashcpy(actual_sha1_return, actual_sha1);
Junio C Hamano40469ee2005-04-28 16:42:27 -07002963 return buffer;
2964 }
2965 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05002966 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07002967 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05002968 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07002969 ref_type = "object ";
2970 else {
2971 free(buffer);
2972 return NULL;
2973 }
2974 ref_length = strlen(ref_type);
2975
Martin Koegler50974ec2008-02-18 21:47:52 +01002976 if (ref_length + 40 > isize ||
2977 memcmp(buffer, ref_type, ref_length) ||
Florian Forster1d7f1712006-06-18 17:18:09 +02002978 get_sha1_hex((char *) buffer + ref_length, actual_sha1)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07002979 free(buffer);
2980 return NULL;
2981 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04002982 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07002983 /* Now we have the ID of the referred-to object in
2984 * actual_sha1. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07002985 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07002986}
2987
Nicolas Pitrece9fbf12007-03-20 16:02:09 -04002988static void write_sha1_file_prepare(const void *buf, unsigned long len,
Rene Scharfe972a9152006-10-15 14:02:03 +02002989 const char *type, unsigned char *sha1,
Nicolas Pitred65a16f2007-02-26 14:55:55 -05002990 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07002991{
Nicolas Pitre9126f002008-10-01 14:05:20 -04002992 git_SHA_CTX c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07002993
2994 /* Generate the header */
Jeff Kingef1286d2015-09-24 17:06:42 -04002995 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07002996
2997 /* Sha1.. */
Nicolas Pitre9126f002008-10-01 14:05:20 -04002998 git_SHA1_Init(&c);
2999 git_SHA1_Update(&c, hdr, *hdrlen);
3000 git_SHA1_Update(&c, buf, len);
3001 git_SHA1_Final(sha1, &c);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07003002}
3003
Linus Torvalds230f1322005-10-08 15:54:01 -07003004/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07003005 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07003006 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07003007int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07003008{
Thomas Raste32c0a92008-09-19 00:24:46 +02003009 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07003010
Johannes Schindelin348df162009-04-28 00:32:25 +02003011 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02003012 goto try_rename;
3013 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02003014 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07003015
3016 /*
3017 * Coda hack - coda doesn't like cross-directory links,
3018 * so we fall back to a rename, which will mean that it
3019 * won't be able to check collisions, but that's not a
3020 * big deal.
3021 *
3022 * The same holds for FAT formatted media.
3023 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07003024 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07003025 * left to unlink.
3026 */
3027 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02003028 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07003029 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07003030 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02003031 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07003032 }
Alex Riesen691f1a22009-04-29 23:22:56 +02003033 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07003034 if (ret) {
3035 if (ret != EEXIST) {
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003036 return error_errno("unable to write sha1 filename %s", filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07003037 }
3038 /* FIXME!!! Collision check here ? */
3039 }
3040
Junio C Hamano3be1f182009-03-27 23:14:39 -07003041out:
Matthieu Moy5256b002010-02-22 23:32:16 +01003042 if (adjust_shared_perm(filename))
Junio C Hamano5a688fe2009-03-25 16:19:36 -07003043 return error("unable to set permission to '%s'", filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07003044 return 0;
3045}
3046
Linus Torvalds4d548152006-05-24 08:30:54 -07003047static int write_buffer(int fd, const void *buf, size_t len)
3048{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08003049 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003050 return error_errno("file write error");
Linus Torvalds4d548152006-05-24 08:30:54 -07003051 return 0;
3052}
3053
Nicolas Pitrece9fbf12007-03-20 16:02:09 -04003054int hash_sha1_file(const void *buf, unsigned long len, const char *type,
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02003055 unsigned char *sha1)
3056{
Nicolas Pitred65a16f2007-02-26 14:55:55 -05003057 char hdr[32];
Jeff Kingef1286d2015-09-24 17:06:42 -04003058 int hdrlen = sizeof(hdr);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02003059 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
3060 return 0;
3061}
3062
Linus Torvaldse9039dd2008-06-10 18:47:18 -07003063/* Finalize a file on disk, and close it. */
3064static void close_sha1_file(int fd)
3065{
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -07003066 if (fsync_object_files)
3067 fsync_or_die(fd, "sha1 file");
Linus Torvaldse9039dd2008-06-10 18:47:18 -07003068 if (close(fd) != 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02003069 die_errno("error when closing sha1 file");
Linus Torvaldse9039dd2008-06-10 18:47:18 -07003070}
3071
Linus Torvalds5723fe72008-06-14 10:50:12 -07003072/* Size of directory component, including the ending '/' */
3073static inline int directory_size(const char *filename)
3074{
3075 const char *s = strrchr(filename, '/');
3076 if (!s)
3077 return 0;
3078 return s - filename + 1;
3079}
3080
3081/*
3082 * This creates a temporary file in the same directory as the final
3083 * 'filename'
3084 *
3085 * We want to avoid cross-directory filename renames, because those
3086 * can have problems on various filesystems (FAT, NFS, Coda).
3087 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04003088static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07003089{
3090 int fd, dirlen = directory_size(filename);
3091
Jeff Kingd4b3d112015-09-24 17:07:49 -04003092 strbuf_reset(tmp);
3093 strbuf_add(tmp, filename, dirlen);
3094 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
3095 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05003096 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04003097 /*
3098 * Make sure the directory exists; note that the contents
3099 * of the buffer are undefined after mkstemp returns an
3100 * error, so we have to rewrite the whole buffer from
3101 * scratch.
3102 */
3103 strbuf_reset(tmp);
3104 strbuf_add(tmp, filename, dirlen - 1);
3105 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01003106 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04003107 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07003108 return -1;
3109
3110 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04003111 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
3112 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07003113 }
3114 return fd;
3115}
3116
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003117static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
Jeff Kingc00e6572010-04-01 20:03:18 -04003118 const void *buf, unsigned long len, time_t mtime)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07003119{
Jeff King915308b2009-01-29 00:56:34 -05003120 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05003121 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07003122 git_zstream stream;
Nicolas Pitre748af442010-02-21 15:48:06 -05003123 git_SHA_CTX c;
3124 unsigned char parano_sha1[20];
Jeff Kingd4b3d112015-09-24 17:07:49 -04003125 static struct strbuf tmp_file = STRBUF_INIT;
Michael Haggerty30d6c6e2014-02-21 17:32:05 +01003126 const char *filename = sha1_file_name(sha1);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07003127
Jeff Kingd4b3d112015-09-24 17:07:49 -04003128 fd = create_tmpfile(&tmp_file, filename);
Linus Torvaldsaac17942005-05-03 11:46:16 -07003129 if (fd < 0) {
Sam Vilain35243572008-11-14 20:19:34 +13003130 if (errno == EACCES)
Pete Wyckoff82247e92012-04-29 20:28:45 -04003131 return error("insufficient permission for adding an object to repository database %s", get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01003132 else
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003133 return error_errno("unable to create temporary file");
Linus Torvaldsaac17942005-05-03 11:46:16 -07003134 }
3135
Linus Torvalds0fcfd162005-04-18 13:04:43 -07003136 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003137 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07003138 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05003139 stream.avail_out = sizeof(compressed);
Nicolas Pitre748af442010-02-21 15:48:06 -05003140 git_SHA1_Init(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07003141
3142 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05003143 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07003144 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003145 while (git_deflate(&stream, 0) == Z_OK)
3146 ; /* nothing */
Nicolas Pitre748af442010-02-21 15:48:06 -05003147 git_SHA1_Update(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07003148
3149 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04003150 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07003151 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05003152 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05003153 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003154 ret = git_deflate(&stream, Z_FINISH);
Nicolas Pitre748af442010-02-21 15:48:06 -05003155 git_SHA1_Update(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05003156 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
3157 die("unable to write sha1 file");
3158 stream.next_out = compressed;
3159 stream.avail_out = sizeof(compressed);
3160 } while (ret == Z_OK);
3161
Linus Torvaldsac54c272007-03-20 11:38:34 -07003162 if (ret != Z_STREAM_END)
3163 die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003164 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07003165 if (ret != Z_OK)
3166 die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
Nicolas Pitre748af442010-02-21 15:48:06 -05003167 git_SHA1_Final(parano_sha1, &c);
3168 if (hashcmp(sha1, parano_sha1) != 0)
3169 die("confused by unstable object source data for %s", sha1_to_hex(sha1));
Linus Torvaldsac54c272007-03-20 11:38:34 -07003170
Linus Torvaldse9039dd2008-06-10 18:47:18 -07003171 close_sha1_file(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07003172
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003173 if (mtime) {
3174 struct utimbuf utb;
3175 utb.actime = mtime;
3176 utb.modtime = mtime;
Jeff Kingd4b3d112015-09-24 17:07:49 -04003177 if (utime(tmp_file.buf, &utb) < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003178 warning_errno("failed utime() on %s", tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003179 }
3180
Jeff Kingd4b3d112015-09-24 17:07:49 -04003181 return finalize_object_file(tmp_file.buf, filename);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07003182}
Daniel Barkalow8237b182005-04-23 18:47:23 -07003183
Jeff King33d42212014-10-15 18:42:22 -04003184static int freshen_loose_object(const unsigned char *sha1)
3185{
3186 return check_and_freshen(sha1, 1);
3187}
3188
3189static int freshen_packed_object(const unsigned char *sha1)
3190{
3191 struct pack_entry e;
Jeff Kingee1c6c32015-04-20 15:55:00 -04003192 if (!find_pack_entry(sha1, &e))
3193 return 0;
3194 if (e.p->freshened)
3195 return 1;
3196 if (!freshen_file(e.p->pack_name))
3197 return 0;
3198 e.p->freshened = 1;
3199 return 1;
Jeff King33d42212014-10-15 18:42:22 -04003200}
3201
Junio C Hamano1427a7f2015-05-04 11:08:10 -07003202int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003203{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003204 char hdr[32];
Jeff Kingef1286d2015-09-24 17:06:42 -04003205 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003206
3207 /* Normally if we have it in the pack then we do not bother writing
3208 * it out into .git/objects/??/?{38} file.
3209 */
3210 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
Jeff Kingb5f52f32015-04-20 15:54:03 -04003211 if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003212 return 0;
3213 return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
3214}
3215
Eric Sunshine0c3db672015-05-04 03:25:15 -04003216int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
3217 unsigned char *sha1, unsigned flags)
3218{
3219 char *header;
3220 int hdrlen, status = 0;
3221
3222 /* type string, SP, %lu of the length plus NUL must fit this */
Jeff Kingef1286d2015-09-24 17:06:42 -04003223 hdrlen = strlen(type) + 32;
3224 header = xmalloc(hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04003225 write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
3226
3227 if (!(flags & HASH_WRITE_OBJECT))
3228 goto cleanup;
Junio C Hamano051086b2015-05-11 14:23:59 -07003229 if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
Eric Sunshine0c3db672015-05-04 03:25:15 -04003230 goto cleanup;
3231 status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
3232
3233cleanup:
3234 free(header);
3235 return status;
3236}
3237
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003238int force_object_loose(const unsigned char *sha1, time_t mtime)
3239{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003240 void *buf;
3241 unsigned long len;
3242 enum object_type type;
3243 char hdr[32];
3244 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02003245 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003246
Linus Torvaldsc529d752008-06-14 11:43:01 -07003247 if (has_loose_object(sha1))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003248 return 0;
3249 buf = read_packed_sha1(sha1, &type, &len);
3250 if (!buf)
3251 return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
Jeff Kingef1286d2015-09-24 17:06:42 -04003252 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02003253 ret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
3254 free(buf);
3255
3256 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04003257}
3258
barkalow@iabervon.orgbf592c52005-07-31 20:53:44 -04003259int has_pack_index(const unsigned char *sha1)
3260{
3261 struct stat st;
3262 if (stat(sha1_pack_index_name(sha1), &st))
3263 return 0;
3264 return 1;
3265}
3266
Junio C Hamanocd673c12009-02-27 23:15:53 -08003267int has_sha1_pack(const unsigned char *sha1)
Linus Torvaldsdade09c2005-07-03 13:06:36 -07003268{
3269 struct pack_entry e;
Junio C Hamanocd673c12009-02-27 23:15:53 -08003270 return find_pack_entry(sha1, &e);
3271}
3272
Jeff King0eeb0772015-06-09 13:24:37 -04003273int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07003274{
Junio C Hamano1f688552005-06-27 03:35:33 -07003275 struct pack_entry e;
3276
Junio C Hamanocd673c12009-02-27 23:15:53 -08003277 if (find_pack_entry(sha1, &e))
Junio C Hamano1f688552005-06-27 03:35:33 -07003278 return 1;
Jeff King45e8a742013-08-30 15:14:13 -04003279 if (has_loose_object(sha1))
3280 return 1;
Jeff King0eeb0772015-06-09 13:24:37 -04003281 if (flags & HAS_SHA1_QUICK)
3282 return 0;
Jeff King45e8a742013-08-30 15:14:13 -04003283 reprepare_packed_git();
3284 return find_pack_entry(sha1, &e);
Daniel Barkalow8237b182005-04-23 18:47:23 -07003285}
Junio C Hamano74400e72005-05-01 23:45:49 -07003286
brian m. carlsonb419aa22015-11-10 02:22:19 +00003287int has_object_file(const struct object_id *oid)
3288{
3289 return has_sha1_file(oid->hash);
3290}
3291
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07003292static void check_tree(const void *buf, size_t size)
3293{
3294 struct tree_desc desc;
3295 struct name_entry entry;
3296
3297 init_tree_desc(&desc, buf, size);
3298 while (tree_entry(&desc, &entry))
3299 /* do nothing
3300 * tree_entry() will die() on malformed entries */
3301 ;
3302}
3303
3304static void check_commit(const void *buf, size_t size)
3305{
3306 struct commit c;
3307 memset(&c, 0, sizeof(c));
3308 if (parse_commit_buffer(&c, buf, size))
3309 die("corrupt commit");
3310}
3311
3312static void check_tag(const void *buf, size_t size)
3313{
3314 struct tag t;
3315 memset(&t, 0, sizeof(t));
3316 if (parse_tag_buffer(&t, buf, size))
3317 die("corrupt tag");
3318}
3319
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003320static int index_mem(unsigned char *sha1, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003321 enum object_type type,
3322 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02003323{
Linus Torvalds6c510be2007-02-13 11:07:23 -08003324 int ret, re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003325 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07003326
Bryan Larsen7672db22005-07-08 16:51:55 -07003327 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08003328 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003329
3330 /*
3331 * Convert blobs to git internal format
3332 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003333 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003334 struct strbuf nbuf = STRBUF_INIT;
Steffen Prohaska21e5ad52008-02-06 12:25:58 +01003335 if (convert_to_git(path, buf, size, &nbuf,
Ramkumar Ramachandra5e12e782011-11-15 22:29:39 +05303336 write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02003337 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08003338 re_allocated = 1;
3339 }
3340 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003341 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07003342 if (type == OBJ_TREE)
3343 check_tree(buf, size);
3344 if (type == OBJ_COMMIT)
3345 check_commit(buf, size);
3346 if (type == OBJ_TAG)
3347 check_tag(buf, size);
3348 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08003349
Bryan Larsen7672db22005-07-08 16:51:55 -07003350 if (write_object)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08003351 ret = write_sha1_file(buf, size, typename(type), sha1);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02003352 else
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08003353 ret = hash_sha1_file(buf, size, typename(type), sha1);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003354 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08003355 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003356 return ret;
3357}
3358
Steffen Prohaska9035d752014-08-26 17:23:25 +02003359static int index_stream_convert_blob(unsigned char *sha1, int fd,
3360 const char *path, unsigned flags)
3361{
3362 int ret;
3363 const int write_object = flags & HASH_WRITE_OBJECT;
3364 struct strbuf sbuf = STRBUF_INIT;
3365
3366 assert(path);
3367 assert(would_convert_to_git_filter_fd(path));
3368
3369 convert_to_git_filter_fd(path, fd, &sbuf,
3370 write_object ? safe_crlf : SAFE_CRLF_FALSE);
3371
3372 if (write_object)
3373 ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
3374 sha1);
3375 else
3376 ret = hash_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
3377 sha1);
3378 strbuf_release(&sbuf);
3379 return ret;
3380}
3381
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003382static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
3383 const char *path, unsigned flags)
3384{
3385 struct strbuf sbuf = STRBUF_INIT;
3386 int ret;
3387
3388 if (strbuf_read(&sbuf, fd, 4096) >= 0)
3389 ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
3390 else
3391 ret = -1;
3392 strbuf_release(&sbuf);
3393 return ret;
3394}
3395
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03003396#define SMALL_FILE_SIZE (32*1024)
3397
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003398static int index_core(unsigned char *sha1, int fd, size_t size,
3399 enum object_type type, const char *path,
3400 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003401{
3402 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003403
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003404 if (!size) {
Jim Hillf6a1e1e2015-05-17 17:41:45 -07003405 ret = index_mem(sha1, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03003406 } else if (size <= SMALL_FILE_SIZE) {
3407 char *buf = xmalloc(size);
3408 if (size == read_in_full(fd, buf, size))
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003409 ret = index_mem(sha1, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03003410 else
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003411 ret = error_errno("short read");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03003412 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04003413 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003414 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003415 ret = index_mem(sha1, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07003416 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04003417 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003418 return ret;
3419}
3420
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07003421/*
Junio C Hamano568508e2011-10-28 14:48:40 -07003422 * This creates one packfile per large blob unless bulk-checkin
3423 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07003424 *
3425 * This also bypasses the usual "convert-to-git" dance, and that is on
3426 * purpose. We could write a streaming version of the converting
3427 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05003428 * (or equivalent in-core API described above). However, that is
3429 * somewhat complicated, as we do not know the size of the filter
3430 * result, which we need to know beforehand when writing a git object.
3431 * Since the primary motivation for trying to stream from the working
3432 * tree file and to avoid mmaping it in core is to deal with large
3433 * binary blobs, they generally do not want to get any conversion, and
3434 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07003435 */
3436static int index_stream(unsigned char *sha1, int fd, size_t size,
3437 enum object_type type, const char *path,
3438 unsigned flags)
3439{
Junio C Hamano568508e2011-10-28 14:48:40 -07003440 return index_bulk_checkin(sha1, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07003441}
3442
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003443int index_fd(unsigned char *sha1, int fd, struct stat *st,
3444 enum object_type type, const char *path, unsigned flags)
3445{
3446 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003447
Steffen Prohaska9079ab72014-09-21 12:03:26 +02003448 /*
3449 * Call xsize_t() only when needed to avoid potentially unnecessary
3450 * die() for large files.
3451 */
Steffen Prohaska9035d752014-08-26 17:23:25 +02003452 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
3453 ret = index_stream_convert_blob(sha1, fd, path, flags);
3454 else if (!S_ISREG(st->st_mode))
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07003455 ret = index_pipe(sha1, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02003456 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Steffen Prohaska7ce7c762014-08-21 18:05:08 +02003457 (path && would_convert_to_git(path)))
Steffen Prohaska9079ab72014-09-21 12:03:26 +02003458 ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
3459 flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07003460 else
Steffen Prohaska9079ab72014-09-21 12:03:26 +02003461 ret = index_stream(sha1, fd, xsize_t(st->st_size), type, path,
3462 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04003463 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07003464 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07003465}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003466
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003467int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003468{
3469 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08003470 struct strbuf sb = STRBUF_INIT;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003471
3472 switch (st->st_mode & S_IFMT) {
3473 case S_IFREG:
3474 fd = open(path, O_RDONLY);
3475 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003476 return error_errno("open(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003477 if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003478 return error("%s: failed to insert into database",
3479 path);
3480 break;
3481 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003482 if (strbuf_readlink(&sb, path, st->st_size))
3483 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07003484 if (!(flags & HASH_WRITE_OBJECT))
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08003485 hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
3486 else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003487 return error("%s: failed to insert into database",
3488 path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08003489 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003490 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07003491 case S_IFDIR:
3492 return resolve_gitlink_ref(path, "HEAD", sha1);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07003493 default:
3494 return error("%s: unsupported file type", path);
3495 }
3496 return 0;
3497}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08003498
3499int read_pack_header(int fd, struct pack_header *header)
3500{
Heikki Orsilac697ad12008-05-03 16:27:26 +03003501 if (read_in_full(fd, header, sizeof(*header)) < sizeof(*header))
3502 /* "eof before pack header was fully read" */
3503 return PH_ERROR_EOF;
3504
Junio C Hamanoa69e5422007-01-22 21:55:18 -08003505 if (header->hdr_signature != htonl(PACK_SIGNATURE))
3506 /* "protocol error (pack signature mismatch detected)" */
3507 return PH_ERROR_PACK_SIGNATURE;
3508 if (!pack_version_ok(header->hdr_version))
3509 /* "protocol error (pack version unsupported)" */
3510 return PH_ERROR_PROTOCOL;
3511 return 0;
3512}
Jeff King40d52ff2010-04-01 20:05:23 -04003513
3514void assert_sha1_type(const unsigned char *sha1, enum object_type expect)
3515{
3516 enum object_type type = sha1_object_info(sha1, NULL);
3517 if (type < 0)
3518 die("%s is not a valid object", sha1_to_hex(sha1));
3519 if (type != expect)
3520 die("%s is not a valid '%s' object", sha1_to_hex(sha1),
3521 typename(expect));
3522}
Jeff King27e1e222014-10-15 18:38:55 -04003523
3524static int for_each_file_in_obj_subdir(int subdir_nr,
3525 struct strbuf *path,
3526 each_loose_object_fn obj_cb,
3527 each_loose_cruft_fn cruft_cb,
3528 each_loose_subdir_fn subdir_cb,
3529 void *data)
3530{
3531 size_t baselen = path->len;
3532 DIR *dir = opendir(path->buf);
3533 struct dirent *de;
3534 int r = 0;
3535
3536 if (!dir) {
3537 if (errno == ENOENT)
3538 return 0;
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07003539 return error_errno("unable to open %s", path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04003540 }
3541
3542 while ((de = readdir(dir))) {
3543 if (is_dot_or_dotdot(de->d_name))
3544 continue;
3545
3546 strbuf_setlen(path, baselen);
3547 strbuf_addf(path, "/%s", de->d_name);
3548
3549 if (strlen(de->d_name) == 38) {
3550 char hex[41];
3551 unsigned char sha1[20];
3552
3553 snprintf(hex, sizeof(hex), "%02x%s",
3554 subdir_nr, de->d_name);
3555 if (!get_sha1_hex(hex, sha1)) {
3556 if (obj_cb) {
3557 r = obj_cb(sha1, path->buf, data);
3558 if (r)
3559 break;
3560 }
3561 continue;
3562 }
3563 }
3564
3565 if (cruft_cb) {
3566 r = cruft_cb(de->d_name, path->buf, data);
3567 if (r)
3568 break;
3569 }
3570 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02003571 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04003572
Johannes Sixt094c7e62015-08-12 19:43:01 +02003573 strbuf_setlen(path, baselen);
Jeff King27e1e222014-10-15 18:38:55 -04003574 if (!r && subdir_cb)
3575 r = subdir_cb(subdir_nr, path->buf, data);
3576
Jeff King27e1e222014-10-15 18:38:55 -04003577 return r;
3578}
3579
Jeff Kinge6f875e2015-02-08 20:13:22 -05003580int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04003581 each_loose_object_fn obj_cb,
3582 each_loose_cruft_fn cruft_cb,
3583 each_loose_subdir_fn subdir_cb,
3584 void *data)
3585{
Jeff Kinge6f875e2015-02-08 20:13:22 -05003586 size_t baselen = path->len;
Jeff King27e1e222014-10-15 18:38:55 -04003587 int r = 0;
3588 int i;
3589
Jeff King27e1e222014-10-15 18:38:55 -04003590 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05003591 strbuf_addf(path, "/%02x", i);
3592 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04003593 subdir_cb, data);
Jeff Kinge6f875e2015-02-08 20:13:22 -05003594 strbuf_setlen(path, baselen);
Jeff King27e1e222014-10-15 18:38:55 -04003595 if (r)
3596 break;
3597 }
3598
Jeff Kinge6f875e2015-02-08 20:13:22 -05003599 return r;
3600}
3601
3602int for_each_loose_file_in_objdir(const char *path,
3603 each_loose_object_fn obj_cb,
3604 each_loose_cruft_fn cruft_cb,
3605 each_loose_subdir_fn subdir_cb,
3606 void *data)
3607{
3608 struct strbuf buf = STRBUF_INIT;
3609 int r;
3610
3611 strbuf_addstr(&buf, path);
3612 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
3613 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04003614 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05003615
Jeff King27e1e222014-10-15 18:38:55 -04003616 return r;
3617}
Jeff King660c8892014-10-15 18:41:21 -04003618
3619struct loose_alt_odb_data {
3620 each_loose_object_fn *cb;
3621 void *data;
3622};
3623
3624static int loose_from_alt_odb(struct alternate_object_database *alt,
3625 void *vdata)
3626{
3627 struct loose_alt_odb_data *data = vdata;
Jonathon Mahb0a42642015-02-08 20:15:39 -05003628 struct strbuf buf = STRBUF_INIT;
3629 int r;
3630
3631 /* copy base not including trailing '/' */
3632 strbuf_add(&buf, alt->base, alt->name - alt->base - 1);
3633 r = for_each_loose_file_in_objdir_buf(&buf,
3634 data->cb, NULL, NULL,
3635 data->data);
3636 strbuf_release(&buf);
3637 return r;
Jeff King660c8892014-10-15 18:41:21 -04003638}
3639
Jeff King1385bb72015-03-27 07:32:41 -04003640int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags)
Jeff King660c8892014-10-15 18:41:21 -04003641{
3642 struct loose_alt_odb_data alt;
3643 int r;
3644
3645 r = for_each_loose_file_in_objdir(get_object_directory(),
3646 cb, NULL, NULL, data);
3647 if (r)
3648 return r;
3649
Jeff King1385bb72015-03-27 07:32:41 -04003650 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
3651 return 0;
3652
Jeff King660c8892014-10-15 18:41:21 -04003653 alt.cb = cb;
3654 alt.data = data;
3655 return foreach_alt_odb(loose_from_alt_odb, &alt);
3656}
3657
3658static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
3659{
3660 uint32_t i;
3661 int r = 0;
3662
3663 for (i = 0; i < p->num_objects; i++) {
3664 const unsigned char *sha1 = nth_packed_object_sha1(p, i);
3665
3666 if (!sha1)
3667 return error("unable to get sha1 of object %u in %s",
3668 i, p->pack_name);
3669
3670 r = cb(sha1, p, i, data);
3671 if (r)
3672 break;
3673 }
3674 return r;
3675}
3676
Jeff King1385bb72015-03-27 07:32:41 -04003677int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
Jeff King660c8892014-10-15 18:41:21 -04003678{
3679 struct packed_git *p;
3680 int r = 0;
Jeff Kingf813e9e2015-06-22 06:40:50 -04003681 int pack_errors = 0;
Jeff King660c8892014-10-15 18:41:21 -04003682
3683 prepare_packed_git();
3684 for (p = packed_git; p; p = p->next) {
Jeff King1385bb72015-03-27 07:32:41 -04003685 if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
3686 continue;
Jeff Kingf813e9e2015-06-22 06:40:50 -04003687 if (open_pack_index(p)) {
3688 pack_errors = 1;
3689 continue;
3690 }
Jeff King660c8892014-10-15 18:41:21 -04003691 r = for_each_object_in_pack(p, cb, data);
3692 if (r)
3693 break;
3694 }
Jeff Kingf813e9e2015-06-22 06:40:50 -04003695 return r ? r : pack_errors;
Jeff King660c8892014-10-15 18:41:21 -04003696}