Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | * |
| 6 | * This handles basic git sha1 object files - packing, unpacking, |
| 7 | * creation etc. |
| 8 | */ |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 9 | #include "cache.h" |
Michael Haggerty | 6eac50d | 2012-11-05 09:41:22 +0100 | [diff] [blame] | 10 | #include "string-list.h" |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 11 | #include "delta.h" |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 12 | #include "pack.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 13 | #include "blob.h" |
| 14 | #include "commit.h" |
Junio C Hamano | 4dd1fbc | 2011-05-08 01:47:35 -0700 | [diff] [blame] | 15 | #include "run-command.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 16 | #include "tag.h" |
| 17 | #include "tree.h" |
Nguyễn Thái Ngọc Duy | c879daa | 2011-02-05 17:52:21 +0700 | [diff] [blame] | 18 | #include "tree-walk.h" |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 19 | #include "refs.h" |
Nicolas Pitre | 70f5d5d | 2008-02-28 00:25:19 -0500 | [diff] [blame] | 20 | #include "pack-revindex.h" |
Junio C Hamano | 628522e | 2007-12-29 02:05:47 -0800 | [diff] [blame] | 21 | #include "sha1-lookup.h" |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 22 | #include "bulk-checkin.h" |
Nguyễn Thái Ngọc Duy | 090ea12 | 2012-03-07 17:54:18 +0700 | [diff] [blame] | 23 | #include "streaming.h" |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 24 | #include "dir.h" |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 144bde7 | 2005-04-23 11:09:32 -0700 | [diff] [blame] | 26 | #ifndef O_NOATIME |
| 27 | #if defined(__linux__) && (defined(__i386__) || defined(__PPC__)) |
| 28 | #define O_NOATIME 01000000 |
| 29 | #else |
| 30 | #define O_NOATIME 0 |
| 31 | #endif |
| 32 | #endif |
| 33 | |
Jonathan Nieder | 28bd70d | 2011-03-16 00:15:31 -0500 | [diff] [blame] | 34 | #define SZ_FMT PRIuMAX |
| 35 | static inline uintmax_t sz_fmt(size_t s) { return s; } |
Pavel Roskin | e05db0f | 2007-01-09 23:07:11 -0500 | [diff] [blame] | 36 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 37 | const unsigned char null_sha1[20]; |
Junio C Hamano | 88cd621 | 2005-09-30 14:02:47 -0700 | [diff] [blame] | 38 | |
Nguyễn Thái Ngọc Duy | c597ba8 | 2011-02-05 21:03:01 +0700 | [diff] [blame] | 39 | /* |
| 40 | * This is meant to hold a *small* number of objects that you would |
| 41 | * want read_sha1_file() to be able to return, but yet you do not want |
| 42 | * to write them into the object store (e.g. a browse-only |
| 43 | * application). |
| 44 | */ |
| 45 | static struct cached_object { |
| 46 | unsigned char sha1[20]; |
| 47 | enum object_type type; |
| 48 | void *buf; |
| 49 | unsigned long size; |
| 50 | } *cached_objects; |
| 51 | static int cached_object_nr, cached_object_alloc; |
| 52 | |
| 53 | static struct cached_object empty_tree = { |
Jonathan Nieder | dab0d41 | 2011-02-07 02:17:27 -0600 | [diff] [blame] | 54 | EMPTY_TREE_SHA1_BIN_LITERAL, |
Nguyễn Thái Ngọc Duy | c597ba8 | 2011-02-05 21:03:01 +0700 | [diff] [blame] | 55 | OBJ_TREE, |
| 56 | "", |
| 57 | 0 |
| 58 | }; |
| 59 | |
Michael Haggerty | 1b1005d | 2014-02-21 17:32:04 +0100 | [diff] [blame] | 60 | /* |
| 61 | * A pointer to the last packed_git in which an object was found. |
| 62 | * When an object is sought, we look in this packfile first, because |
| 63 | * objects that are looked up at similar times are often in the same |
| 64 | * packfile as one another. |
| 65 | */ |
Nguyễn Thái Ngọc Duy | c01f51c | 2012-02-01 20:48:55 +0700 | [diff] [blame] | 66 | static struct packed_git *last_found_pack; |
| 67 | |
Nguyễn Thái Ngọc Duy | c597ba8 | 2011-02-05 21:03:01 +0700 | [diff] [blame] | 68 | static struct cached_object *find_cached_object(const unsigned char *sha1) |
| 69 | { |
| 70 | int i; |
| 71 | struct cached_object *co = cached_objects; |
| 72 | |
| 73 | for (i = 0; i < cached_object_nr; i++, co++) { |
| 74 | if (!hashcmp(co->sha1, sha1)) |
| 75 | return co; |
| 76 | } |
| 77 | if (!hashcmp(sha1, empty_tree.sha1)) |
| 78 | return &empty_tree; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Junio C Hamano | 90a6464 | 2011-03-10 16:02:50 -0800 | [diff] [blame] | 82 | int mkdir_in_gitdir(const char *path) |
| 83 | { |
| 84 | if (mkdir(path, 0777)) { |
| 85 | int saved_errno = errno; |
| 86 | struct stat st; |
| 87 | struct strbuf sb = STRBUF_INIT; |
| 88 | |
| 89 | if (errno != EEXIST) |
| 90 | return -1; |
| 91 | /* |
| 92 | * Are we looking at a path in a symlinked worktree |
| 93 | * whose original repository does not yet have it? |
| 94 | * e.g. .git/rr-cache pointing at its original |
| 95 | * repository in which the user hasn't performed any |
| 96 | * conflict resolution yet? |
| 97 | */ |
| 98 | if (lstat(path, &st) || !S_ISLNK(st.st_mode) || |
| 99 | strbuf_readlink(&sb, path, st.st_size) || |
| 100 | !is_absolute_path(sb.buf) || |
| 101 | mkdir(sb.buf, 0777)) { |
| 102 | strbuf_release(&sb); |
| 103 | errno = saved_errno; |
| 104 | return -1; |
| 105 | } |
| 106 | strbuf_release(&sb); |
| 107 | } |
| 108 | return adjust_shared_perm(path); |
| 109 | } |
| 110 | |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 111 | enum scld_error safe_create_leading_directories(char *path) |
Junio C Hamano | b2cb942 | 2005-07-06 01:11:52 -0700 | [diff] [blame] | 112 | { |
Michael Haggerty | 26c8ae2 | 2014-01-06 14:45:22 +0100 | [diff] [blame] | 113 | char *next_component = path + offset_1st_component(path); |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 114 | enum scld_error ret = SCLD_OK; |
Jason Riedy | 67d4221 | 2006-02-09 17:56:13 -0800 | [diff] [blame] | 115 | |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 116 | while (ret == SCLD_OK && next_component) { |
Michael Haggerty | f050233 | 2014-01-06 14:45:20 +0100 | [diff] [blame] | 117 | struct stat st; |
Michael Haggerty | 0f52740 | 2014-01-19 00:40:44 +0100 | [diff] [blame] | 118 | char *slash = next_component, slash_character; |
Michael Haggerty | f050233 | 2014-01-06 14:45:20 +0100 | [diff] [blame] | 119 | |
Michael Haggerty | 0f52740 | 2014-01-19 00:40:44 +0100 | [diff] [blame] | 120 | while (*slash && !is_dir_sep(*slash)) |
| 121 | slash++; |
| 122 | |
| 123 | if (!*slash) |
Junio C Hamano | b2cb942 | 2005-07-06 01:11:52 -0700 | [diff] [blame] | 124 | break; |
Michael Haggerty | bf10cf7 | 2014-01-06 14:45:23 +0100 | [diff] [blame] | 125 | |
Michael Haggerty | 26c8ae2 | 2014-01-06 14:45:22 +0100 | [diff] [blame] | 126 | next_component = slash + 1; |
Michael Haggerty | 0f52740 | 2014-01-19 00:40:44 +0100 | [diff] [blame] | 127 | while (is_dir_sep(*next_component)) |
Michael Haggerty | bf10cf7 | 2014-01-06 14:45:23 +0100 | [diff] [blame] | 128 | next_component++; |
Michael Haggerty | 26c8ae2 | 2014-01-06 14:45:22 +0100 | [diff] [blame] | 129 | if (!*next_component) |
Junio C Hamano | 5f0bdf5 | 2008-09-02 14:10:15 -0700 | [diff] [blame] | 130 | break; |
Michael Haggerty | 831651f | 2014-01-06 14:45:21 +0100 | [diff] [blame] | 131 | |
Michael Haggerty | 0f52740 | 2014-01-19 00:40:44 +0100 | [diff] [blame] | 132 | slash_character = *slash; |
Michael Haggerty | 831651f | 2014-01-06 14:45:21 +0100 | [diff] [blame] | 133 | *slash = '\0'; |
Jason Riedy | 67d4221 | 2006-02-09 17:56:13 -0800 | [diff] [blame] | 134 | if (!stat(path, &st)) { |
| 135 | /* path exists */ |
Michael Haggerty | 9e6f885 | 2014-01-06 14:45:24 +0100 | [diff] [blame] | 136 | if (!S_ISDIR(st.st_mode)) |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 137 | ret = SCLD_EXISTS; |
Michael Haggerty | 53a3972 | 2014-01-06 14:45:19 +0100 | [diff] [blame] | 138 | } else if (mkdir(path, 0777)) { |
Steven Walter | 928734d | 2013-03-17 10:09:27 -0400 | [diff] [blame] | 139 | if (errno == EEXIST && |
Michael Haggerty | 9e6f885 | 2014-01-06 14:45:24 +0100 | [diff] [blame] | 140 | !stat(path, &st) && S_ISDIR(st.st_mode)) |
Steven Walter | 928734d | 2013-03-17 10:09:27 -0400 | [diff] [blame] | 141 | ; /* somebody created it since we checked */ |
Michael Haggerty | 18d37e8 | 2014-01-06 14:45:27 +0100 | [diff] [blame] | 142 | else if (errno == ENOENT) |
| 143 | /* |
| 144 | * Either mkdir() failed because |
| 145 | * somebody just pruned the containing |
| 146 | * directory, or stat() failed because |
| 147 | * the file that was in our way was |
| 148 | * just removed. Either way, inform |
| 149 | * the caller that it might be worth |
| 150 | * trying again: |
| 151 | */ |
| 152 | ret = SCLD_VANISHED; |
Michael Haggerty | 9e6f885 | 2014-01-06 14:45:24 +0100 | [diff] [blame] | 153 | else |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 154 | ret = SCLD_FAILED; |
Michael Haggerty | 53a3972 | 2014-01-06 14:45:19 +0100 | [diff] [blame] | 155 | } else if (adjust_shared_perm(path)) { |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 156 | ret = SCLD_PERMS; |
Jason Riedy | 67d4221 | 2006-02-09 17:56:13 -0800 | [diff] [blame] | 157 | } |
Michael Haggerty | 0f52740 | 2014-01-19 00:40:44 +0100 | [diff] [blame] | 158 | *slash = slash_character; |
Junio C Hamano | b2cb942 | 2005-07-06 01:11:52 -0700 | [diff] [blame] | 159 | } |
Michael Haggerty | 9e6f885 | 2014-01-06 14:45:24 +0100 | [diff] [blame] | 160 | return ret; |
Junio C Hamano | b2cb942 | 2005-07-06 01:11:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 163 | enum scld_error safe_create_leading_directories_const(const char *path) |
Jeff King | 8e21d63 | 2008-06-25 01:41:34 -0400 | [diff] [blame] | 164 | { |
| 165 | /* path points to cache entries, so xstrdup before messing with it */ |
| 166 | char *buf = xstrdup(path); |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 167 | enum scld_error result = safe_create_leading_directories(buf); |
Jeff King | 8e21d63 | 2008-06-25 01:41:34 -0400 | [diff] [blame] | 168 | free(buf); |
| 169 | return result; |
| 170 | } |
| 171 | |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 172 | static void fill_sha1_path(char *pathbuf, const unsigned char *sha1) |
| 173 | { |
| 174 | int i; |
| 175 | for (i = 0; i < 20; i++) { |
| 176 | static char hex[] = "0123456789abcdef"; |
| 177 | unsigned int val = sha1[i]; |
| 178 | char *pos = pathbuf + i*2 + (i > 0); |
| 179 | *pos++ = hex[val >> 4]; |
| 180 | *pos = hex[val & 0xf]; |
| 181 | } |
| 182 | } |
| 183 | |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 184 | const char *sha1_file_name(const unsigned char *sha1) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 185 | { |
Jeff King | 560fb6a | 2010-05-22 02:59:42 -0400 | [diff] [blame] | 186 | static char buf[PATH_MAX]; |
| 187 | const char *objdir; |
| 188 | int len; |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 189 | |
Jeff King | 560fb6a | 2010-05-22 02:59:42 -0400 | [diff] [blame] | 190 | objdir = get_object_directory(); |
| 191 | len = strlen(objdir); |
| 192 | |
| 193 | /* '/' + sha1(2) + '/' + sha1(38) + '\0' */ |
| 194 | if (len + 43 > PATH_MAX) |
| 195 | die("insanely long object directory %s", objdir); |
| 196 | memcpy(buf, objdir, len); |
| 197 | buf[len] = '/'; |
| 198 | buf[len+3] = '/'; |
| 199 | buf[len+42] = '\0'; |
| 200 | fill_sha1_path(buf + len + 1, sha1); |
| 201 | return buf; |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Michael Haggerty | d40d535 | 2014-02-21 17:32:06 +0100 | [diff] [blame] | 204 | /* |
| 205 | * Return the name of the pack or index file with the specified sha1 |
| 206 | * in its filename. *base and *name are scratch space that must be |
| 207 | * provided by the caller. which should be "pack" or "idx". |
| 208 | */ |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 209 | static char *sha1_get_pack_name(const unsigned char *sha1, |
Junio C Hamano | 6eec46b | 2008-05-28 09:47:43 -0700 | [diff] [blame] | 210 | char **name, char **base, const char *which) |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 211 | { |
| 212 | static const char hex[] = "0123456789abcdef"; |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 213 | char *buf; |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 214 | int i; |
| 215 | |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 216 | if (!*base) { |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 217 | const char *sha1_file_directory = get_object_directory(); |
| 218 | int len = strlen(sha1_file_directory); |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 219 | *base = xmalloc(len + 60); |
Junio C Hamano | 6eec46b | 2008-05-28 09:47:43 -0700 | [diff] [blame] | 220 | sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.%s", |
| 221 | sha1_file_directory, which); |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 222 | *name = *base + len + 11; |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 225 | buf = *name; |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 226 | |
| 227 | for (i = 0; i < 20; i++) { |
| 228 | unsigned int val = *sha1++; |
| 229 | *buf++ = hex[val >> 4]; |
| 230 | *buf++ = hex[val & 0xf]; |
| 231 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 232 | |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 233 | return *base; |
| 234 | } |
| 235 | |
| 236 | char *sha1_pack_name(const unsigned char *sha1) |
| 237 | { |
| 238 | static char *name, *base; |
| 239 | |
Junio C Hamano | 6eec46b | 2008-05-28 09:47:43 -0700 | [diff] [blame] | 240 | return sha1_get_pack_name(sha1, &name, &base, "pack"); |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | char *sha1_pack_index_name(const unsigned char *sha1) |
| 244 | { |
Heikki Orsila | 633f43e | 2008-05-24 01:43:55 +0300 | [diff] [blame] | 245 | static char *name, *base; |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 246 | |
Junio C Hamano | 6eec46b | 2008-05-28 09:47:43 -0700 | [diff] [blame] | 247 | return sha1_get_pack_name(sha1, &name, &base, "idx"); |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 250 | struct alternate_object_database *alt_odb_list; |
| 251 | static struct alternate_object_database **alt_odb_tail; |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 252 | |
Junio C Hamano | ddd5d05 | 2005-05-08 13:51:13 -0700 | [diff] [blame] | 253 | /* |
| 254 | * Prepare alternate object database registry. |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 255 | * |
| 256 | * The variable alt_odb_list points at the list of struct |
| 257 | * alternate_object_database. The elements on this list come from |
| 258 | * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT |
| 259 | * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates, |
Junio C Hamano | 1494e03 | 2005-12-04 22:48:43 -0800 | [diff] [blame] | 260 | * whose contents is similar to that environment variable but can be |
| 261 | * LF separated. Its base points at a statically allocated buffer that |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 262 | * contains "/the/directory/corresponding/to/.git/objects/...", while |
| 263 | * its name points just after the slash at the end of ".git/objects/" |
| 264 | * in the example above, and has enough space to hold 40-byte hex |
| 265 | * SHA1, an extra slash for the first level indirection, and the |
| 266 | * terminating NUL. |
Junio C Hamano | ddd5d05 | 2005-05-08 13:51:13 -0700 | [diff] [blame] | 267 | */ |
Ephrim Khong | 539e750 | 2014-07-15 13:29:45 +0200 | [diff] [blame] | 268 | static int link_alt_odb_entry(const char *entry, const char *relative_base, |
| 269 | int depth, const char *normalized_objdir) |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 270 | { |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 271 | struct alternate_object_database *ent; |
| 272 | struct alternate_object_database *alt; |
Hui Wang | 5bdf0a8 | 2011-09-07 18:37:47 +0800 | [diff] [blame] | 273 | int pfxlen, entlen; |
| 274 | struct strbuf pathbuf = STRBUF_INIT; |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 275 | |
Johannes Sixt | 85dadc3 | 2007-11-13 21:05:00 +0100 | [diff] [blame] | 276 | if (!is_absolute_path(entry) && relative_base) { |
Hui Wang | 5bdf0a8 | 2011-09-07 18:37:47 +0800 | [diff] [blame] | 277 | strbuf_addstr(&pathbuf, real_path(relative_base)); |
| 278 | strbuf_addch(&pathbuf, '/'); |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 279 | } |
Michael Haggerty | 6eac50d | 2012-11-05 09:41:22 +0100 | [diff] [blame] | 280 | strbuf_addstr(&pathbuf, entry); |
Hui Wang | 5bdf0a8 | 2011-09-07 18:37:47 +0800 | [diff] [blame] | 281 | |
| 282 | normalize_path_copy(pathbuf.buf, pathbuf.buf); |
| 283 | |
| 284 | pfxlen = strlen(pathbuf.buf); |
| 285 | |
| 286 | /* |
| 287 | * The trailing slash after the directory name is given by |
| 288 | * this function at the end. Remove duplicates. |
| 289 | */ |
| 290 | while (pfxlen && pathbuf.buf[pfxlen-1] == '/') |
| 291 | pfxlen -= 1; |
| 292 | |
| 293 | entlen = pfxlen + 43; /* '/' + 2 hex + '/' + 38 hex + NUL */ |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 294 | ent = xmalloc(sizeof(*ent) + entlen); |
Hui Wang | 5bdf0a8 | 2011-09-07 18:37:47 +0800 | [diff] [blame] | 295 | memcpy(ent->base, pathbuf.buf, pfxlen); |
| 296 | strbuf_release(&pathbuf); |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 297 | |
| 298 | ent->name = ent->base + pfxlen + 1; |
| 299 | ent->base[pfxlen + 3] = '/'; |
| 300 | ent->base[pfxlen] = ent->base[entlen-1] = 0; |
| 301 | |
| 302 | /* Detect cases where alternate disappeared */ |
Junio C Hamano | 90b4a71 | 2008-09-09 01:27:07 -0700 | [diff] [blame] | 303 | if (!is_directory(ent->base)) { |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 304 | error("object directory %s does not exist; " |
| 305 | "check .git/objects/info/alternates.", |
| 306 | ent->base); |
| 307 | free(ent); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | /* Prevent the common mistake of listing the same |
| 312 | * thing twice, or object directory itself. |
| 313 | */ |
| 314 | for (alt = alt_odb_list; alt; alt = alt->next) { |
René Scharfe | 80b4785 | 2014-07-01 20:00:01 +0200 | [diff] [blame] | 315 | if (pfxlen == alt->name - alt->base - 1 && |
| 316 | !memcmp(ent->base, alt->base, pfxlen)) { |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 317 | free(ent); |
| 318 | return -1; |
| 319 | } |
| 320 | } |
Ephrim Khong | 539e750 | 2014-07-15 13:29:45 +0200 | [diff] [blame] | 321 | if (!strcmp_icase(ent->base, normalized_objdir)) { |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 322 | free(ent); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | /* add the alternate entry */ |
| 327 | *alt_odb_tail = ent; |
| 328 | alt_odb_tail = &(ent->next); |
| 329 | ent->next = NULL; |
| 330 | |
| 331 | /* recursively add alternates */ |
| 332 | read_info_alternates(ent->base, depth + 1); |
| 333 | |
| 334 | ent->base[pfxlen] = '/'; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Michael Haggerty | c595016 | 2012-11-05 09:41:23 +0100 | [diff] [blame] | 339 | static void link_alt_odb_entries(const char *alt, int len, int sep, |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 340 | const char *relative_base, int depth) |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 341 | { |
Michael Haggerty | 6eac50d | 2012-11-05 09:41:22 +0100 | [diff] [blame] | 342 | struct string_list entries = STRING_LIST_INIT_NODUP; |
| 343 | char *alt_copy; |
| 344 | int i; |
Ephrim Khong | 539e750 | 2014-07-15 13:29:45 +0200 | [diff] [blame] | 345 | struct strbuf objdirbuf = STRBUF_INIT; |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 346 | |
| 347 | if (depth > 5) { |
| 348 | error("%s: ignoring alternate object stores, nesting too deep.", |
| 349 | relative_base); |
| 350 | return; |
| 351 | } |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 352 | |
Junio C Hamano | f655651 | 2014-09-02 13:27:45 -0700 | [diff] [blame] | 353 | strbuf_add_absolute_path(&objdirbuf, get_object_directory()); |
Ephrim Khong | 539e750 | 2014-07-15 13:29:45 +0200 | [diff] [blame] | 354 | normalize_path_copy(objdirbuf.buf, objdirbuf.buf); |
| 355 | |
Michael Haggerty | c595016 | 2012-11-05 09:41:23 +0100 | [diff] [blame] | 356 | alt_copy = xmemdupz(alt, len); |
Michael Haggerty | 6eac50d | 2012-11-05 09:41:22 +0100 | [diff] [blame] | 357 | string_list_split_in_place(&entries, alt_copy, sep, -1); |
| 358 | for (i = 0; i < entries.nr; i++) { |
| 359 | const char *entry = entries.items[i].string; |
| 360 | if (entry[0] == '\0' || entry[0] == '#') |
Junio C Hamano | 9577e7e | 2005-08-16 18:22:05 -0700 | [diff] [blame] | 361 | continue; |
Michael Haggerty | 6eac50d | 2012-11-05 09:41:22 +0100 | [diff] [blame] | 362 | if (!is_absolute_path(entry) && depth) { |
| 363 | error("%s: ignoring relative alternate object store %s", |
| 364 | relative_base, entry); |
| 365 | } else { |
Ephrim Khong | 539e750 | 2014-07-15 13:29:45 +0200 | [diff] [blame] | 366 | link_alt_odb_entry(entry, relative_base, depth, objdirbuf.buf); |
Junio C Hamano | 9577e7e | 2005-08-16 18:22:05 -0700 | [diff] [blame] | 367 | } |
Junio C Hamano | 9577e7e | 2005-08-16 18:22:05 -0700 | [diff] [blame] | 368 | } |
Michael Haggerty | 6eac50d | 2012-11-05 09:41:22 +0100 | [diff] [blame] | 369 | string_list_clear(&entries, 0); |
| 370 | free(alt_copy); |
Ephrim Khong | 539e750 | 2014-07-15 13:29:45 +0200 | [diff] [blame] | 371 | strbuf_release(&objdirbuf); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Heiko Voigt | 5e73633 | 2012-05-14 18:24:45 +0200 | [diff] [blame] | 374 | void read_info_alternates(const char * relative_base, int depth) |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 375 | { |
Junio C Hamano | 9577e7e | 2005-08-16 18:22:05 -0700 | [diff] [blame] | 376 | char *map; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 377 | size_t mapsz; |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 378 | struct stat st; |
Jim Meyering | 9cb18f5 | 2007-07-03 12:40:20 +0200 | [diff] [blame] | 379 | const char alt_file_name[] = "info/alternates"; |
| 380 | /* Given that relative_base is no longer than PATH_MAX, |
| 381 | ensure that "path" has enough space to append "/", the |
| 382 | file name, "info/alternates", and a trailing NUL. */ |
| 383 | char path[PATH_MAX + 1 + sizeof alt_file_name]; |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 384 | int fd; |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 385 | |
Jim Meyering | 9cb18f5 | 2007-07-03 12:40:20 +0200 | [diff] [blame] | 386 | sprintf(path, "%s/%s", relative_base, alt_file_name); |
Junio C Hamano | f4e5168 | 2011-05-15 12:16:29 -0700 | [diff] [blame] | 387 | fd = git_open_noatime(path); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 388 | if (fd < 0) |
| 389 | return; |
| 390 | if (fstat(fd, &st) || (st.st_size == 0)) { |
| 391 | close(fd); |
| 392 | return; |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 393 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 394 | mapsz = xsize_t(st.st_size); |
| 395 | map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 396 | close(fd); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 397 | |
Michael Haggerty | c595016 | 2012-11-05 09:41:23 +0100 | [diff] [blame] | 398 | link_alt_odb_entries(map, mapsz, '\n', relative_base, depth); |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 399 | |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 400 | munmap(map, mapsz); |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Daniel Barkalow | bef70b2 | 2008-04-17 19:32:30 -0400 | [diff] [blame] | 403 | void add_to_alternates_file(const char *reference) |
| 404 | { |
| 405 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 406 | int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR); |
Junio C Hamano | e6baf4a | 2011-08-22 18:05:16 -0700 | [diff] [blame] | 407 | char *alt = mkpath("%s\n", reference); |
Daniel Barkalow | bef70b2 | 2008-04-17 19:32:30 -0400 | [diff] [blame] | 408 | write_or_die(fd, alt, strlen(alt)); |
| 409 | if (commit_lock_file(lock)) |
| 410 | die("could not close alternates file"); |
| 411 | if (alt_odb_tail) |
Michael Haggerty | c595016 | 2012-11-05 09:41:23 +0100 | [diff] [blame] | 412 | link_alt_odb_entries(alt, strlen(alt), '\n', NULL, 0); |
Daniel Barkalow | bef70b2 | 2008-04-17 19:32:30 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Jeff King | fe1b226 | 2014-10-15 18:33:13 -0400 | [diff] [blame] | 415 | int foreach_alt_odb(alt_odb_fn fn, void *cb) |
Junio C Hamano | d79796b | 2008-09-09 01:27:10 -0700 | [diff] [blame] | 416 | { |
| 417 | struct alternate_object_database *ent; |
Jeff King | fe1b226 | 2014-10-15 18:33:13 -0400 | [diff] [blame] | 418 | int r = 0; |
Junio C Hamano | d79796b | 2008-09-09 01:27:10 -0700 | [diff] [blame] | 419 | |
| 420 | prepare_alt_odb(); |
Jeff King | fe1b226 | 2014-10-15 18:33:13 -0400 | [diff] [blame] | 421 | for (ent = alt_odb_list; ent; ent = ent->next) { |
| 422 | r = fn(ent, cb); |
| 423 | if (r) |
| 424 | break; |
| 425 | } |
| 426 | return r; |
Junio C Hamano | d79796b | 2008-09-09 01:27:10 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 429 | void prepare_alt_odb(void) |
| 430 | { |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 431 | const char *alt; |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 432 | |
Shawn O. Pearce | 7dc24aa | 2007-05-26 01:24:40 -0400 | [diff] [blame] | 433 | if (alt_odb_tail) |
| 434 | return; |
| 435 | |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 436 | alt = getenv(ALTERNATE_DB_ENVIRONMENT); |
| 437 | if (!alt) alt = ""; |
| 438 | |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 439 | alt_odb_tail = &alt_odb_list; |
Michael Haggerty | c595016 | 2012-11-05 09:41:23 +0100 | [diff] [blame] | 440 | link_alt_odb_entries(alt, strlen(alt), PATH_SEP, NULL, 0); |
Martin Waitz | c2f493a | 2006-05-07 20:19:21 +0200 | [diff] [blame] | 441 | |
| 442 | read_info_alternates(get_object_directory(), 0); |
| 443 | } |
| 444 | |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 445 | static int freshen_file(const char *fn) |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 446 | { |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 447 | struct utimbuf t; |
| 448 | t.actime = t.modtime = time(NULL); |
| 449 | return !utime(fn, &t); |
Brandon Casey | 0f4dc14 | 2008-11-09 23:59:57 -0600 | [diff] [blame] | 450 | } |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 451 | |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 452 | static int check_and_freshen_file(const char *fn, int freshen) |
| 453 | { |
| 454 | if (access(fn, F_OK)) |
| 455 | return 0; |
| 456 | if (freshen && freshen_file(fn)) |
| 457 | return 0; |
| 458 | return 1; |
| 459 | } |
| 460 | |
| 461 | static int check_and_freshen_local(const unsigned char *sha1, int freshen) |
| 462 | { |
| 463 | return check_and_freshen_file(sha1_file_name(sha1), freshen); |
| 464 | } |
| 465 | |
| 466 | static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen) |
Brandon Casey | 0f4dc14 | 2008-11-09 23:59:57 -0600 | [diff] [blame] | 467 | { |
| 468 | struct alternate_object_database *alt; |
Junio C Hamano | 9a217f2 | 2005-06-28 14:56:57 -0700 | [diff] [blame] | 469 | prepare_alt_odb(); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 470 | for (alt = alt_odb_list; alt; alt = alt->next) { |
Brandon Casey | 0f4dc14 | 2008-11-09 23:59:57 -0600 | [diff] [blame] | 471 | fill_sha1_path(alt->name, sha1); |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 472 | if (check_and_freshen_file(alt->base, freshen)) |
Linus Torvalds | c529d75 | 2008-06-14 11:43:01 -0700 | [diff] [blame] | 473 | return 1; |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 474 | } |
Linus Torvalds | c529d75 | 2008-06-14 11:43:01 -0700 | [diff] [blame] | 475 | return 0; |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 478 | static int check_and_freshen(const unsigned char *sha1, int freshen) |
| 479 | { |
| 480 | return check_and_freshen_local(sha1, freshen) || |
| 481 | check_and_freshen_nonlocal(sha1, freshen); |
| 482 | } |
| 483 | |
| 484 | int has_loose_object_nonlocal(const unsigned char *sha1) |
| 485 | { |
| 486 | return check_and_freshen_nonlocal(sha1, 0); |
| 487 | } |
| 488 | |
Brandon Casey | 0f4dc14 | 2008-11-09 23:59:57 -0600 | [diff] [blame] | 489 | static int has_loose_object(const unsigned char *sha1) |
| 490 | { |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 491 | return check_and_freshen(sha1, 0); |
Brandon Casey | 0f4dc14 | 2008-11-09 23:59:57 -0600 | [diff] [blame] | 492 | } |
| 493 | |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 494 | static unsigned int pack_used_ctr; |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 495 | static unsigned int pack_mmap_calls; |
| 496 | static unsigned int peak_pack_open_windows; |
| 497 | static unsigned int pack_open_windows; |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 498 | static unsigned int pack_open_fds; |
| 499 | static unsigned int pack_max_fds; |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 500 | static size_t peak_pack_mapped; |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 501 | static size_t pack_mapped; |
Junio C Hamano | 9a217f2 | 2005-06-28 14:56:57 -0700 | [diff] [blame] | 502 | struct packed_git *packed_git; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 503 | |
Junio C Hamano | b79d18c | 2007-06-13 01:22:51 -0700 | [diff] [blame] | 504 | void pack_report(void) |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 505 | { |
| 506 | fprintf(stderr, |
Pavel Roskin | e05db0f | 2007-01-09 23:07:11 -0500 | [diff] [blame] | 507 | "pack_report: getpagesize() = %10" SZ_FMT "\n" |
| 508 | "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n" |
| 509 | "pack_report: core.packedGitLimit = %10" SZ_FMT "\n", |
Steffen Prohaska | 9e42d6a | 2007-11-21 21:27:19 +0100 | [diff] [blame] | 510 | sz_fmt(getpagesize()), |
| 511 | sz_fmt(packed_git_window_size), |
| 512 | sz_fmt(packed_git_limit)); |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 513 | fprintf(stderr, |
| 514 | "pack_report: pack_used_ctr = %10u\n" |
| 515 | "pack_report: pack_mmap_calls = %10u\n" |
| 516 | "pack_report: pack_open_windows = %10u / %10u\n" |
Pavel Roskin | e05db0f | 2007-01-09 23:07:11 -0500 | [diff] [blame] | 517 | "pack_report: pack_mapped = " |
| 518 | "%10" SZ_FMT " / %10" SZ_FMT "\n", |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 519 | pack_used_ctr, |
| 520 | pack_mmap_calls, |
| 521 | pack_open_windows, peak_pack_open_windows, |
Steffen Prohaska | 9e42d6a | 2007-11-21 21:27:19 +0100 | [diff] [blame] | 522 | sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped)); |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Michael Haggerty | d40d535 | 2014-02-21 17:32:06 +0100 | [diff] [blame] | 525 | /* |
| 526 | * Open and mmap the index file at path, perform a couple of |
| 527 | * consistency checks, then record its information to p. Return 0 on |
| 528 | * success. |
| 529 | */ |
| 530 | static int check_packed_git_idx(const char *path, struct packed_git *p) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 531 | { |
| 532 | void *idx_map; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 533 | struct pack_idx_header *hdr; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 534 | size_t idx_size; |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 535 | uint32_t version, nr, i, *index; |
Junio C Hamano | f4e5168 | 2011-05-15 12:16:29 -0700 | [diff] [blame] | 536 | int fd = git_open_noatime(path); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 537 | struct stat st; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 538 | |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 539 | if (fd < 0) |
| 540 | return -1; |
| 541 | if (fstat(fd, &st)) { |
| 542 | close(fd); |
| 543 | return -1; |
| 544 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 545 | idx_size = xsize_t(st.st_size); |
Shawn O. Pearce | 2d88451 | 2007-03-06 20:44:11 -0500 | [diff] [blame] | 546 | if (idx_size < 4 * 256 + 20 + 20) { |
| 547 | close(fd); |
| 548 | return error("index file %s is too small", path); |
| 549 | } |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 550 | idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 551 | close(fd); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 552 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 553 | hdr = idx_map; |
| 554 | if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) { |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 555 | version = ntohl(hdr->idx_version); |
| 556 | if (version < 2 || version > 2) { |
| 557 | munmap(idx_map, idx_size); |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 558 | return error("index file %s is version %"PRIu32 |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 559 | " and is not supported by this binary" |
| 560 | " (try upgrading GIT to a newer version)", |
| 561 | path, version); |
| 562 | } |
| 563 | } else |
| 564 | version = 1; |
Shawn O. Pearce | df1b059 | 2007-01-17 20:43:57 -0500 | [diff] [blame] | 565 | |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 566 | nr = 0; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 567 | index = idx_map; |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 568 | if (version > 1) |
| 569 | index += 2; /* skip index header */ |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 570 | for (i = 0; i < 256; i++) { |
Shawn O. Pearce | 326bf39 | 2007-03-06 20:44:19 -0500 | [diff] [blame] | 571 | uint32_t n = ntohl(index[i]); |
Shawn O. Pearce | 2d88451 | 2007-03-06 20:44:11 -0500 | [diff] [blame] | 572 | if (n < nr) { |
| 573 | munmap(idx_map, idx_size); |
Shawn O. Pearce | df1b059 | 2007-01-17 20:43:57 -0500 | [diff] [blame] | 574 | return error("non-monotonic index %s", path); |
Shawn O. Pearce | 2d88451 | 2007-03-06 20:44:11 -0500 | [diff] [blame] | 575 | } |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 576 | nr = n; |
| 577 | } |
| 578 | |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 579 | if (version == 1) { |
| 580 | /* |
| 581 | * Total size: |
| 582 | * - 256 index entries 4 bytes each |
| 583 | * - 24-byte entries * nr (20-byte sha1 + 4-byte offset) |
| 584 | * - 20-byte SHA1 of the packfile |
| 585 | * - 20-byte SHA1 file checksum |
| 586 | */ |
| 587 | if (idx_size != 4*256 + nr * 24 + 20 + 20) { |
| 588 | munmap(idx_map, idx_size); |
Luiz Fernando N. Capitulino | eef427a | 2007-08-14 16:42:37 -0300 | [diff] [blame] | 589 | return error("wrong index v1 file size in %s", path); |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 590 | } |
| 591 | } else if (version == 2) { |
| 592 | /* |
| 593 | * Minimum size: |
| 594 | * - 8 bytes of header |
| 595 | * - 256 index entries 4 bytes each |
| 596 | * - 20-byte sha1 entry * nr |
| 597 | * - 4-byte crc entry * nr |
| 598 | * - 4-byte offset entry * nr |
| 599 | * - 20-byte SHA1 of the packfile |
| 600 | * - 20-byte SHA1 file checksum |
| 601 | * And after the 4-byte offset table might be a |
| 602 | * variable sized table containing 8-byte entries |
| 603 | * for offsets larger than 2^31. |
| 604 | */ |
| 605 | unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20; |
Linus Torvalds | 1164f1e | 2007-06-26 14:34:02 -0700 | [diff] [blame] | 606 | unsigned long max_size = min_size; |
| 607 | if (nr) |
| 608 | max_size += (nr - 1)*8; |
| 609 | if (idx_size < min_size || idx_size > max_size) { |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 610 | munmap(idx_map, idx_size); |
Luiz Fernando N. Capitulino | eef427a | 2007-08-14 16:42:37 -0300 | [diff] [blame] | 611 | return error("wrong index v2 file size in %s", path); |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 612 | } |
Junio C Hamano | 7109c88 | 2007-10-29 11:53:55 -0700 | [diff] [blame] | 613 | if (idx_size != min_size && |
| 614 | /* |
| 615 | * make sure we can deal with large pack offsets. |
| 616 | * 31-bit signed offset won't be enough, neither |
| 617 | * 32-bit unsigned one will be. |
| 618 | */ |
| 619 | (sizeof(off_t) <= 4)) { |
| 620 | munmap(idx_map, idx_size); |
| 621 | return error("pack too large for current definition of off_t in %s", path); |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 622 | } |
Shawn O. Pearce | 2d88451 | 2007-03-06 20:44:11 -0500 | [diff] [blame] | 623 | } |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 624 | |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 625 | p->index_version = version; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 626 | p->index_data = idx_map; |
| 627 | p->index_size = idx_size; |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 628 | p->num_objects = nr; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Shawn O. Pearce | bc8e478 | 2007-05-30 02:13:42 -0400 | [diff] [blame] | 632 | int open_pack_index(struct packed_git *p) |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 633 | { |
| 634 | char *idx_name; |
| 635 | int ret; |
| 636 | |
| 637 | if (p->index_data) |
| 638 | return 0; |
| 639 | |
| 640 | idx_name = xstrdup(p->pack_name); |
| 641 | strcpy(idx_name + strlen(idx_name) - strlen(".pack"), ".idx"); |
| 642 | ret = check_packed_git_idx(idx_name, p); |
| 643 | free(idx_name); |
| 644 | return ret; |
| 645 | } |
| 646 | |
Shawn O. Pearce | 11daf39 | 2006-12-23 02:34:44 -0500 | [diff] [blame] | 647 | static void scan_windows(struct packed_git *p, |
| 648 | struct packed_git **lru_p, |
| 649 | struct pack_window **lru_w, |
| 650 | struct pack_window **lru_l) |
| 651 | { |
| 652 | struct pack_window *w, *w_l; |
| 653 | |
| 654 | for (w_l = NULL, w = p->windows; w; w = w->next) { |
| 655 | if (!w->inuse_cnt) { |
| 656 | if (!*lru_w || w->last_used < (*lru_w)->last_used) { |
| 657 | *lru_p = p; |
| 658 | *lru_w = w; |
| 659 | *lru_l = w_l; |
| 660 | } |
| 661 | } |
| 662 | w_l = w; |
| 663 | } |
| 664 | } |
| 665 | |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 666 | static int unuse_one_window(struct packed_git *current) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 667 | { |
Shawn O. Pearce | 54044bf | 2006-12-23 02:34:23 -0500 | [diff] [blame] | 668 | struct packed_git *p, *lru_p = NULL; |
Shawn O. Pearce | 11daf39 | 2006-12-23 02:34:44 -0500 | [diff] [blame] | 669 | struct pack_window *lru_w = NULL, *lru_l = NULL; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 670 | |
Shawn O. Pearce | 11daf39 | 2006-12-23 02:34:44 -0500 | [diff] [blame] | 671 | if (current) |
| 672 | scan_windows(current, &lru_p, &lru_w, &lru_l); |
| 673 | for (p = packed_git; p; p = p->next) |
| 674 | scan_windows(p, &lru_p, &lru_w, &lru_l); |
Shawn O. Pearce | 54044bf | 2006-12-23 02:34:23 -0500 | [diff] [blame] | 675 | if (lru_p) { |
| 676 | munmap(lru_w->base, lru_w->len); |
| 677 | pack_mapped -= lru_w->len; |
| 678 | if (lru_l) |
| 679 | lru_l->next = lru_w->next; |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 680 | else |
Shawn O. Pearce | 54044bf | 2006-12-23 02:34:23 -0500 | [diff] [blame] | 681 | lru_p->windows = lru_w->next; |
Shawn O. Pearce | 54044bf | 2006-12-23 02:34:23 -0500 | [diff] [blame] | 682 | free(lru_w); |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 683 | pack_open_windows--; |
Shawn O. Pearce | 54044bf | 2006-12-23 02:34:23 -0500 | [diff] [blame] | 684 | return 1; |
| 685 | } |
| 686 | return 0; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 689 | void release_pack_memory(size_t need) |
Shawn O. Pearce | 97bfeb3 | 2006-12-24 00:47:19 -0500 | [diff] [blame] | 690 | { |
| 691 | size_t cur = pack_mapped; |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 692 | while (need >= (cur - pack_mapped) && unuse_one_window(NULL)) |
Shawn O. Pearce | 97bfeb3 | 2006-12-24 00:47:19 -0500 | [diff] [blame] | 693 | ; /* nothing */ |
| 694 | } |
| 695 | |
Steffen Prohaska | 0271022 | 2014-08-26 17:23:23 +0200 | [diff] [blame] | 696 | static void mmap_limit_check(size_t length) |
| 697 | { |
| 698 | static size_t limit = 0; |
| 699 | if (!limit) { |
| 700 | limit = git_env_ulong("GIT_MMAP_LIMIT", 0); |
| 701 | if (!limit) |
| 702 | limit = SIZE_MAX; |
| 703 | } |
| 704 | if (length > limit) |
| 705 | die("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX, |
| 706 | (uintmax_t)length, (uintmax_t)limit); |
| 707 | } |
| 708 | |
Jonathan Nieder | 58ecbd5 | 2010-11-06 06:44:11 -0500 | [diff] [blame] | 709 | void *xmmap(void *start, size_t length, |
| 710 | int prot, int flags, int fd, off_t offset) |
| 711 | { |
Steffen Prohaska | 0271022 | 2014-08-26 17:23:23 +0200 | [diff] [blame] | 712 | void *ret; |
| 713 | |
| 714 | mmap_limit_check(length); |
| 715 | ret = mmap(start, length, prot, flags, fd, offset); |
Jonathan Nieder | 58ecbd5 | 2010-11-06 06:44:11 -0500 | [diff] [blame] | 716 | if (ret == MAP_FAILED) { |
| 717 | if (!length) |
| 718 | return NULL; |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 719 | release_pack_memory(length); |
Jonathan Nieder | 58ecbd5 | 2010-11-06 06:44:11 -0500 | [diff] [blame] | 720 | ret = mmap(start, length, prot, flags, fd, offset); |
| 721 | if (ret == MAP_FAILED) |
| 722 | die_errno("Out of memory? mmap failed"); |
| 723 | } |
| 724 | return ret; |
| 725 | } |
| 726 | |
Shawn O. Pearce | c9ced05 | 2008-01-17 22:57:00 -0500 | [diff] [blame] | 727 | void close_pack_windows(struct packed_git *p) |
| 728 | { |
| 729 | while (p->windows) { |
| 730 | struct pack_window *w = p->windows; |
| 731 | |
| 732 | if (w->inuse_cnt) |
| 733 | die("pack '%s' still has open windows to it", |
| 734 | p->pack_name); |
| 735 | munmap(w->base, w->len); |
| 736 | pack_mapped -= w->len; |
| 737 | pack_open_windows--; |
| 738 | p->windows = w->next; |
| 739 | free(w); |
| 740 | } |
| 741 | } |
| 742 | |
Brandon Casey | 88d0db5 | 2013-08-01 22:36:33 -0700 | [diff] [blame] | 743 | /* |
| 744 | * The LRU pack is the one with the oldest MRU window, preferring packs |
| 745 | * with no used windows, or the oldest mtime if it has no windows allocated. |
| 746 | */ |
| 747 | static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse) |
| 748 | { |
| 749 | struct pack_window *w, *this_mru_w; |
| 750 | int has_windows_inuse = 0; |
| 751 | |
| 752 | /* |
| 753 | * Reject this pack if it has windows and the previously selected |
| 754 | * one does not. If this pack does not have windows, reject |
| 755 | * it if the pack file is newer than the previously selected one. |
| 756 | */ |
| 757 | if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime)) |
| 758 | return; |
| 759 | |
| 760 | for (w = this_mru_w = p->windows; w; w = w->next) { |
| 761 | /* |
| 762 | * Reject this pack if any of its windows are in use, |
| 763 | * but the previously selected pack did not have any |
| 764 | * inuse windows. Otherwise, record that this pack |
| 765 | * has windows in use. |
| 766 | */ |
| 767 | if (w->inuse_cnt) { |
| 768 | if (*accept_windows_inuse) |
| 769 | has_windows_inuse = 1; |
| 770 | else |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | if (w->last_used > this_mru_w->last_used) |
| 775 | this_mru_w = w; |
| 776 | |
| 777 | /* |
| 778 | * Reject this pack if it has windows that have been |
| 779 | * used more recently than the previously selected pack. |
| 780 | * If the previously selected pack had windows inuse and |
| 781 | * we have not encountered a window in this pack that is |
| 782 | * inuse, skip this check since we prefer a pack with no |
| 783 | * inuse windows to one that has inuse windows. |
| 784 | */ |
| 785 | if (*mru_w && *accept_windows_inuse == has_windows_inuse && |
| 786 | this_mru_w->last_used > (*mru_w)->last_used) |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Select this pack. |
| 792 | */ |
| 793 | *mru_w = this_mru_w; |
| 794 | *lru_p = p; |
| 795 | *accept_windows_inuse = has_windows_inuse; |
| 796 | } |
| 797 | |
| 798 | static int close_one_pack(void) |
| 799 | { |
| 800 | struct packed_git *p, *lru_p = NULL; |
| 801 | struct pack_window *mru_w = NULL; |
| 802 | int accept_windows_inuse = 1; |
| 803 | |
| 804 | for (p = packed_git; p; p = p->next) { |
| 805 | if (p->pack_fd == -1) |
| 806 | continue; |
| 807 | find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse); |
| 808 | } |
| 809 | |
| 810 | if (lru_p) { |
| 811 | close(lru_p->pack_fd); |
| 812 | pack_open_fds--; |
| 813 | lru_p->pack_fd = -1; |
| 814 | return 1; |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 820 | void unuse_pack(struct pack_window **w_cursor) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 821 | { |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 822 | struct pack_window *w = *w_cursor; |
| 823 | if (w) { |
| 824 | w->inuse_cnt--; |
| 825 | *w_cursor = NULL; |
| 826 | } |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Shawn O. Pearce | fa5fc15 | 2010-04-19 07:23:06 -0700 | [diff] [blame] | 829 | void close_pack_index(struct packed_git *p) |
| 830 | { |
| 831 | if (p->index_data) { |
| 832 | munmap((void *)p->index_data, p->index_size); |
| 833 | p->index_data = NULL; |
| 834 | } |
| 835 | } |
| 836 | |
Shawn O. Pearce | 3cf8b46 | 2007-02-02 03:00:03 -0500 | [diff] [blame] | 837 | /* |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 838 | * This is used by git-repack in case a newly created pack happens to |
| 839 | * contain the same set of objects as an existing one. In that case |
| 840 | * the resulting file might be different even if its name would be the |
| 841 | * same. It is best to close any reference to the old pack before it is |
Justin Lebar | 0168990 | 2014-03-31 15:11:46 -0700 | [diff] [blame] | 842 | * replaced on disk. Of course no index pointers or windows for given pack |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 843 | * must subsist at this point. If ever objects from this pack are requested |
| 844 | * again, the new version of the pack will be reinitialized through |
| 845 | * reprepare_packed_git(). |
| 846 | */ |
| 847 | void free_pack_by_name(const char *pack_name) |
| 848 | { |
| 849 | struct packed_git *p, **pp = &packed_git; |
| 850 | |
| 851 | while (*pp) { |
| 852 | p = *pp; |
| 853 | if (strcmp(pack_name, p->pack_name) == 0) { |
Shawn O. Pearce | fa3a0c9 | 2009-02-11 10:15:30 -0800 | [diff] [blame] | 854 | clear_delta_base_cache(); |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 855 | close_pack_windows(p); |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 856 | if (p->pack_fd != -1) { |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 857 | close(p->pack_fd); |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 858 | pack_open_fds--; |
| 859 | } |
Shawn O. Pearce | fa5fc15 | 2010-04-19 07:23:06 -0700 | [diff] [blame] | 860 | close_pack_index(p); |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 861 | free(p->bad_object_sha1); |
| 862 | *pp = p->next; |
Nguyễn Thái Ngọc Duy | c01f51c | 2012-02-01 20:48:55 +0700 | [diff] [blame] | 863 | if (last_found_pack == p) |
| 864 | last_found_pack = NULL; |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 865 | free(p); |
| 866 | return; |
| 867 | } |
| 868 | pp = &p->next; |
| 869 | } |
| 870 | } |
| 871 | |
Joachim Schmitz | a078826 | 2012-08-24 11:52:22 +0200 | [diff] [blame] | 872 | static unsigned int get_max_fd_limit(void) |
| 873 | { |
| 874 | #ifdef RLIMIT_NOFILE |
Junio C Hamano | 491a8de | 2013-12-18 14:59:12 -0800 | [diff] [blame] | 875 | { |
| 876 | struct rlimit lim; |
Joachim Schmitz | a078826 | 2012-08-24 11:52:22 +0200 | [diff] [blame] | 877 | |
Junio C Hamano | 491a8de | 2013-12-18 14:59:12 -0800 | [diff] [blame] | 878 | if (!getrlimit(RLIMIT_NOFILE, &lim)) |
| 879 | return lim.rlim_cur; |
| 880 | } |
| 881 | #endif |
Joachim Schmitz | a078826 | 2012-08-24 11:52:22 +0200 | [diff] [blame] | 882 | |
Junio C Hamano | 491a8de | 2013-12-18 14:59:12 -0800 | [diff] [blame] | 883 | #ifdef _SC_OPEN_MAX |
| 884 | { |
| 885 | long open_max = sysconf(_SC_OPEN_MAX); |
| 886 | if (0 < open_max) |
| 887 | return open_max; |
| 888 | /* |
| 889 | * Otherwise, we got -1 for one of the two |
| 890 | * reasons: |
| 891 | * |
| 892 | * (1) sysconf() did not understand _SC_OPEN_MAX |
| 893 | * and signaled an error with -1; or |
| 894 | * (2) sysconf() said there is no limit. |
| 895 | * |
| 896 | * We _could_ clear errno before calling sysconf() to |
| 897 | * tell these two cases apart and return a huge number |
| 898 | * in the latter case to let the caller cap it to a |
| 899 | * value that is not so selfish, but letting the |
| 900 | * fallback OPEN_MAX codepath take care of these cases |
| 901 | * is a lot simpler. |
| 902 | */ |
| 903 | } |
| 904 | #endif |
| 905 | |
| 906 | #ifdef OPEN_MAX |
Joachim Schmitz | a078826 | 2012-08-24 11:52:22 +0200 | [diff] [blame] | 907 | return OPEN_MAX; |
| 908 | #else |
| 909 | return 1; /* see the caller ;-) */ |
| 910 | #endif |
| 911 | } |
| 912 | |
Nicolas Pitre | c74faea | 2008-12-09 14:26:52 -0500 | [diff] [blame] | 913 | /* |
Shawn O. Pearce | 3cf8b46 | 2007-02-02 03:00:03 -0500 | [diff] [blame] | 914 | * Do not call this directly as this leaks p->pack_fd on error return; |
| 915 | * call open_packed_git() instead. |
| 916 | */ |
| 917 | static int open_packed_git_1(struct packed_git *p) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 918 | { |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 919 | struct stat st; |
| 920 | struct pack_header hdr; |
| 921 | unsigned char sha1[20]; |
| 922 | unsigned char *idx_sha1; |
Junio C Hamano | 2c039da | 2006-12-29 00:30:01 -0800 | [diff] [blame] | 923 | long fd_flag; |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 924 | |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 925 | if (!p->index_data && open_pack_index(p)) |
| 926 | return error("packfile %s index unavailable", p->pack_name); |
| 927 | |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 928 | if (!pack_max_fds) { |
Joachim Schmitz | a078826 | 2012-08-24 11:52:22 +0200 | [diff] [blame] | 929 | unsigned int max_fds = get_max_fd_limit(); |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 930 | |
| 931 | /* Save 3 for stdin/stdout/stderr, 22 for work */ |
| 932 | if (25 < max_fds) |
| 933 | pack_max_fds = max_fds - 25; |
| 934 | else |
| 935 | pack_max_fds = 1; |
| 936 | } |
| 937 | |
Brandon Casey | 88d0db5 | 2013-08-01 22:36:33 -0700 | [diff] [blame] | 938 | while (pack_max_fds <= pack_open_fds && close_one_pack()) |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 939 | ; /* nothing */ |
| 940 | |
Junio C Hamano | f4e5168 | 2011-05-15 12:16:29 -0700 | [diff] [blame] | 941 | p->pack_fd = git_open_noatime(p->pack_name); |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 942 | if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 943 | return -1; |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 944 | pack_open_fds++; |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 945 | |
| 946 | /* If we created the struct before we had the pack we lack size. */ |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 947 | if (!p->pack_size) { |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 948 | if (!S_ISREG(st.st_mode)) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 949 | return error("packfile %s not a regular file", p->pack_name); |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 950 | p->pack_size = st.st_size; |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 951 | } else if (p->pack_size != st.st_size) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 952 | return error("packfile %s size changed", p->pack_name); |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 953 | |
Junio C Hamano | 2c039da | 2006-12-29 00:30:01 -0800 | [diff] [blame] | 954 | /* We leave these file descriptors open with sliding mmap; |
| 955 | * there is no point keeping them open across exec(), though. |
| 956 | */ |
| 957 | fd_flag = fcntl(p->pack_fd, F_GETFD, 0); |
| 958 | if (fd_flag < 0) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 959 | return error("cannot determine file descriptor flags"); |
Junio C Hamano | 2c039da | 2006-12-29 00:30:01 -0800 | [diff] [blame] | 960 | fd_flag |= FD_CLOEXEC; |
| 961 | if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 962 | return error("cannot set FD_CLOEXEC"); |
Junio C Hamano | 2c039da | 2006-12-29 00:30:01 -0800 | [diff] [blame] | 963 | |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 964 | /* Verify we recognize this pack file format. */ |
Shawn O. Pearce | e6e2bd6 | 2007-01-14 01:01:49 -0500 | [diff] [blame] | 965 | if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 966 | return error("file %s is far too short to be a packfile", p->pack_name); |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 967 | if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 968 | return error("file %s is not a GIT packfile", p->pack_name); |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 969 | if (!pack_version_ok(hdr.hdr_version)) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 970 | return error("packfile %s is version %"PRIu32" and not" |
| 971 | " supported (try upgrading GIT to a newer version)", |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 972 | p->pack_name, ntohl(hdr.hdr_version)); |
| 973 | |
| 974 | /* Verify the pack matches its index. */ |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 975 | if (p->num_objects != ntohl(hdr.hdr_entries)) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 976 | return error("packfile %s claims to have %"PRIu32" objects" |
| 977 | " while index indicates %"PRIu32" objects", |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 978 | p->pack_name, ntohl(hdr.hdr_entries), |
| 979 | p->num_objects); |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 980 | if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 981 | return error("end of packfile %s is unavailable", p->pack_name); |
Shawn O. Pearce | e6e2bd6 | 2007-01-14 01:01:49 -0500 | [diff] [blame] | 982 | if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1)) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 983 | return error("packfile %s signature is unavailable", p->pack_name); |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 984 | idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40; |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 985 | if (hashcmp(sha1, idx_sha1)) |
Shawn O. Pearce | 072db27 | 2007-02-01 15:52:33 -0500 | [diff] [blame] | 986 | return error("packfile %s does not match index", p->pack_name); |
| 987 | return 0; |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 988 | } |
| 989 | |
Shawn O. Pearce | 3cf8b46 | 2007-02-02 03:00:03 -0500 | [diff] [blame] | 990 | static int open_packed_git(struct packed_git *p) |
| 991 | { |
| 992 | if (!open_packed_git_1(p)) |
| 993 | return 0; |
| 994 | if (p->pack_fd != -1) { |
| 995 | close(p->pack_fd); |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 996 | pack_open_fds--; |
Shawn O. Pearce | 3cf8b46 | 2007-02-02 03:00:03 -0500 | [diff] [blame] | 997 | p->pack_fd = -1; |
| 998 | } |
| 999 | return -1; |
| 1000 | } |
| 1001 | |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1002 | static int in_window(struct pack_window *win, off_t offset) |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1003 | { |
| 1004 | /* We must promise at least 20 bytes (one hash) after the |
| 1005 | * offset is available from this window, otherwise the offset |
| 1006 | * is not actually in this window and a different window (which |
| 1007 | * has that one hash excess) must be used. This is to support |
| 1008 | * the object header and delta base parsing routines below. |
| 1009 | */ |
| 1010 | off_t win_off = win->offset; |
| 1011 | return win_off <= offset |
| 1012 | && (offset + 20) <= (win_off + win->len); |
| 1013 | } |
| 1014 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1015 | unsigned char *use_pack(struct packed_git *p, |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1016 | struct pack_window **w_cursor, |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1017 | off_t offset, |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 1018 | unsigned long *left) |
Shawn O. Pearce | 9bc879c | 2006-12-23 02:34:01 -0500 | [diff] [blame] | 1019 | { |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1020 | struct pack_window *win = *w_cursor; |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1021 | |
Felipe Contreras | a9d98a1 | 2009-02-24 23:59:05 +0200 | [diff] [blame] | 1022 | /* Since packfiles end in a hash of their content and it's |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1023 | * pointless to ask for an offset into the middle of that |
| 1024 | * hash, and the in_window function above wouldn't match |
| 1025 | * don't allow an offset too close to the end of the file. |
| 1026 | */ |
Shawn O. Pearce | d131b7a | 2011-03-02 10:01:54 -0800 | [diff] [blame] | 1027 | if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p)) |
| 1028 | die("packfile %s cannot be accessed", p->pack_name); |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1029 | if (offset > (p->pack_size - 20)) |
| 1030 | die("offset beyond end of packfile (truncated pack?)"); |
| 1031 | |
| 1032 | if (!win || !in_window(win, offset)) { |
| 1033 | if (win) |
| 1034 | win->inuse_cnt--; |
| 1035 | for (win = p->windows; win; win = win->next) { |
| 1036 | if (in_window(win, offset)) |
| 1037 | break; |
| 1038 | } |
| 1039 | if (!win) { |
Alexandre Julliard | 78a28df | 2007-02-14 18:11:40 +0100 | [diff] [blame] | 1040 | size_t window_align = packed_git_window_size / 2; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1041 | off_t len; |
Shawn O. Pearce | d131b7a | 2011-03-02 10:01:54 -0800 | [diff] [blame] | 1042 | |
| 1043 | if (p->pack_fd == -1 && open_packed_git(p)) |
| 1044 | die("packfile %s cannot be accessed", p->pack_name); |
| 1045 | |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1046 | win = xcalloc(1, sizeof(*win)); |
Alexandre Julliard | 78a28df | 2007-02-14 18:11:40 +0100 | [diff] [blame] | 1047 | win->offset = (offset / window_align) * window_align; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1048 | len = p->pack_size - win->offset; |
| 1049 | if (len > packed_git_window_size) |
| 1050 | len = packed_git_window_size; |
| 1051 | win->len = (size_t)len; |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1052 | pack_mapped += win->len; |
Shawn O. Pearce | 11daf39 | 2006-12-23 02:34:44 -0500 | [diff] [blame] | 1053 | while (packed_git_limit < pack_mapped |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 1054 | && unuse_one_window(p)) |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1055 | ; /* nothing */ |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 1056 | win->base = xmmap(NULL, win->len, |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1057 | PROT_READ, MAP_PRIVATE, |
| 1058 | p->pack_fd, win->offset); |
| 1059 | if (win->base == MAP_FAILED) |
Shawn O. Pearce | 73b4e4b | 2006-12-23 02:34:41 -0500 | [diff] [blame] | 1060 | die("packfile %s cannot be mapped: %s", |
| 1061 | p->pack_name, |
| 1062 | strerror(errno)); |
Shawn O. Pearce | d131b7a | 2011-03-02 10:01:54 -0800 | [diff] [blame] | 1063 | if (!win->offset && win->len == p->pack_size |
| 1064 | && !p->do_not_close) { |
| 1065 | close(p->pack_fd); |
| 1066 | pack_open_fds--; |
| 1067 | p->pack_fd = -1; |
| 1068 | } |
Shawn O. Pearce | a53128b | 2006-12-23 02:34:47 -0500 | [diff] [blame] | 1069 | pack_mmap_calls++; |
| 1070 | pack_open_windows++; |
| 1071 | if (pack_mapped > peak_pack_mapped) |
| 1072 | peak_pack_mapped = pack_mapped; |
| 1073 | if (pack_open_windows > peak_pack_open_windows) |
| 1074 | peak_pack_open_windows = pack_open_windows; |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1075 | win->next = p->windows; |
| 1076 | p->windows = win; |
| 1077 | } |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1078 | } |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1079 | if (win != *w_cursor) { |
| 1080 | win->last_used = pack_used_ctr++; |
| 1081 | win->inuse_cnt++; |
| 1082 | *w_cursor = win; |
| 1083 | } |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 1084 | offset -= win->offset; |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1085 | if (left) |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1086 | *left = win->len - xsize_t(offset); |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1087 | return win->base + offset; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Nicolas Pitre | 27d69a4 | 2008-06-24 18:58:06 -0400 | [diff] [blame] | 1090 | static struct packed_git *alloc_packed_git(int extra) |
| 1091 | { |
| 1092 | struct packed_git *p = xmalloc(sizeof(*p) + extra); |
| 1093 | memset(p, 0, sizeof(*p)); |
| 1094 | p->pack_fd = -1; |
| 1095 | return p; |
| 1096 | } |
| 1097 | |
Jonathan Nieder | e050029 | 2010-11-06 14:00:38 -0500 | [diff] [blame] | 1098 | static void try_to_free_pack_memory(size_t size) |
| 1099 | { |
Brandon Casey | 7c3ecb3 | 2013-07-31 12:51:37 -0700 | [diff] [blame] | 1100 | release_pack_memory(size); |
Jonathan Nieder | e050029 | 2010-11-06 14:00:38 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1103 | struct packed_git *add_packed_git(const char *path, int path_len, int local) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1104 | { |
Jonathan Nieder | e050029 | 2010-11-06 14:00:38 -0500 | [diff] [blame] | 1105 | static int have_set_try_to_free_routine; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1106 | struct stat st; |
Nicolas Pitre | 27d69a4 | 2008-06-24 18:58:06 -0400 | [diff] [blame] | 1107 | struct packed_git *p = alloc_packed_git(path_len + 2); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1108 | |
Jonathan Nieder | e050029 | 2010-11-06 14:00:38 -0500 | [diff] [blame] | 1109 | if (!have_set_try_to_free_routine) { |
| 1110 | have_set_try_to_free_routine = 1; |
| 1111 | set_try_to_free_routine(try_to_free_pack_memory); |
| 1112 | } |
| 1113 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1114 | /* |
| 1115 | * Make sure a corresponding .pack file exists and that |
| 1116 | * the index looks sane. |
| 1117 | */ |
| 1118 | path_len -= strlen(".idx"); |
Nicolas Pitre | 27d69a4 | 2008-06-24 18:58:06 -0400 | [diff] [blame] | 1119 | if (path_len < 1) { |
| 1120 | free(p); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1121 | return NULL; |
Nicolas Pitre | 27d69a4 | 2008-06-24 18:58:06 -0400 | [diff] [blame] | 1122 | } |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1123 | memcpy(p->pack_name, path, path_len); |
Brandon Casey | 8d25931 | 2008-11-12 11:59:03 -0600 | [diff] [blame] | 1124 | |
| 1125 | strcpy(p->pack_name + path_len, ".keep"); |
| 1126 | if (!access(p->pack_name, F_OK)) |
| 1127 | p->pack_keep = 1; |
| 1128 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1129 | strcpy(p->pack_name + path_len, ".pack"); |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 1130 | if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) { |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1131 | free(p); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1132 | return NULL; |
| 1133 | } |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1134 | |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1135 | /* ok, it looks sane as far as we can check without |
| 1136 | * actually mapping the pack file. |
| 1137 | */ |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1138 | p->pack_size = st.st_size; |
Linus Torvalds | 9d835df | 2005-10-13 15:38:28 -0700 | [diff] [blame] | 1139 | p->pack_local = local; |
Junio C Hamano | b867092 | 2007-03-09 03:52:12 -0800 | [diff] [blame] | 1140 | p->mtime = st.st_mtime; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1141 | if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1)) |
| 1142 | hashclr(p->sha1); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1143 | return p; |
| 1144 | } |
| 1145 | |
Shawn O. Pearce | 7b64469 | 2010-04-19 07:23:08 -0700 | [diff] [blame] | 1146 | struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path) |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 1147 | { |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1148 | const char *path = sha1_pack_name(sha1); |
Nicolas Pitre | 27d69a4 | 2008-06-24 18:58:06 -0400 | [diff] [blame] | 1149 | struct packed_git *p = alloc_packed_git(strlen(path) + 1); |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 1150 | |
Nicolas Pitre | 27d69a4 | 2008-06-24 18:58:06 -0400 | [diff] [blame] | 1151 | strcpy(p->pack_name, path); |
| 1152 | hashcpy(p->sha1, sha1); |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1153 | if (check_packed_git_idx(idx_path, p)) { |
| 1154 | free(p); |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 1155 | return NULL; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 1156 | } |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 1157 | |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 1158 | return p; |
| 1159 | } |
| 1160 | |
| 1161 | void install_packed_git(struct packed_git *pack) |
| 1162 | { |
Shawn O. Pearce | c793430 | 2011-02-28 12:52:39 -0800 | [diff] [blame] | 1163 | if (pack->pack_fd != -1) |
| 1164 | pack_open_fds++; |
| 1165 | |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 1166 | pack->next = packed_git; |
| 1167 | packed_git = pack; |
| 1168 | } |
| 1169 | |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1170 | void (*report_garbage)(const char *desc, const char *path); |
| 1171 | |
| 1172 | static void report_helper(const struct string_list *list, |
| 1173 | int seen_bits, int first, int last) |
| 1174 | { |
| 1175 | const char *msg; |
| 1176 | switch (seen_bits) { |
| 1177 | case 0: |
Justin Lebar | 235e8d5 | 2014-03-31 15:11:47 -0700 | [diff] [blame] | 1178 | msg = "no corresponding .idx or .pack"; |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1179 | break; |
| 1180 | case 1: |
| 1181 | msg = "no corresponding .idx"; |
| 1182 | break; |
| 1183 | case 2: |
| 1184 | msg = "no corresponding .pack"; |
| 1185 | break; |
| 1186 | default: |
| 1187 | return; |
| 1188 | } |
| 1189 | for (; first < last; first++) |
| 1190 | report_garbage(msg, list->items[first].string); |
| 1191 | } |
| 1192 | |
| 1193 | static void report_pack_garbage(struct string_list *list) |
| 1194 | { |
| 1195 | int i, baselen = -1, first = 0, seen_bits = 0; |
| 1196 | |
| 1197 | if (!report_garbage) |
| 1198 | return; |
| 1199 | |
| 1200 | sort_string_list(list); |
| 1201 | |
| 1202 | for (i = 0; i < list->nr; i++) { |
| 1203 | const char *path = list->items[i].string; |
| 1204 | if (baselen != -1 && |
| 1205 | strncmp(path, list->items[first].string, baselen)) { |
| 1206 | report_helper(list, seen_bits, first, i); |
| 1207 | baselen = -1; |
| 1208 | seen_bits = 0; |
| 1209 | } |
| 1210 | if (baselen == -1) { |
| 1211 | const char *dot = strrchr(path, '.'); |
| 1212 | if (!dot) { |
| 1213 | report_garbage("garbage found", path); |
| 1214 | continue; |
| 1215 | } |
| 1216 | baselen = dot - path + 1; |
| 1217 | first = i; |
| 1218 | } |
| 1219 | if (!strcmp(path + baselen, "pack")) |
| 1220 | seen_bits |= 1; |
| 1221 | else if (!strcmp(path + baselen, "idx")) |
| 1222 | seen_bits |= 2; |
| 1223 | } |
| 1224 | report_helper(list, seen_bits, first, list->nr); |
| 1225 | } |
| 1226 | |
Linus Torvalds | 9d835df | 2005-10-13 15:38:28 -0700 | [diff] [blame] | 1227 | static void prepare_packed_git_one(char *objdir, int local) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1228 | { |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1229 | struct strbuf path = STRBUF_INIT; |
| 1230 | size_t dirnamelen; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1231 | DIR *dir; |
| 1232 | struct dirent *de; |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1233 | struct string_list garbage = STRING_LIST_INIT_DUP; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1234 | |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1235 | strbuf_addstr(&path, objdir); |
| 1236 | strbuf_addstr(&path, "/pack"); |
| 1237 | dir = opendir(path.buf); |
Carl Worth | b5b1699 | 2006-02-17 16:14:52 -0800 | [diff] [blame] | 1238 | if (!dir) { |
Junio C Hamano | 26125f6 | 2006-02-22 11:16:38 -0800 | [diff] [blame] | 1239 | if (errno != ENOENT) |
Junio C Hamano | bd2afde | 2006-02-22 17:47:10 -0800 | [diff] [blame] | 1240 | error("unable to open object pack directory: %s: %s", |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1241 | path.buf, strerror(errno)); |
| 1242 | strbuf_release(&path); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1243 | return; |
Carl Worth | b5b1699 | 2006-02-17 16:14:52 -0800 | [diff] [blame] | 1244 | } |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1245 | strbuf_addch(&path, '/'); |
| 1246 | dirnamelen = path.len; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1247 | while ((de = readdir(dir)) != NULL) { |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1248 | struct packed_git *p; |
Jeff King | 47bf4b0 | 2014-06-30 13:04:03 -0400 | [diff] [blame] | 1249 | size_t base_len; |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1250 | |
| 1251 | if (is_dot_or_dotdot(de->d_name)) |
Jim Meyering | 9cb18f5 | 2007-07-03 12:40:20 +0200 | [diff] [blame] | 1252 | continue; |
| 1253 | |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1254 | strbuf_setlen(&path, dirnamelen); |
| 1255 | strbuf_addstr(&path, de->d_name); |
Nguyễn Thái Ngọc Duy | d90906a | 2013-02-13 16:13:17 +0700 | [diff] [blame] | 1256 | |
Jeff King | 47bf4b0 | 2014-06-30 13:04:03 -0400 | [diff] [blame] | 1257 | base_len = path.len; |
| 1258 | if (strip_suffix_mem(path.buf, &base_len, ".idx")) { |
Nguyễn Thái Ngọc Duy | d90906a | 2013-02-13 16:13:17 +0700 | [diff] [blame] | 1259 | /* Don't reopen a pack we already have. */ |
| 1260 | for (p = packed_git; p; p = p->next) { |
Jeff King | 47bf4b0 | 2014-06-30 13:04:03 -0400 | [diff] [blame] | 1261 | size_t len; |
| 1262 | if (strip_suffix(p->pack_name, ".pack", &len) && |
| 1263 | len == base_len && |
| 1264 | !memcmp(p->pack_name, path.buf, len)) |
Nguyễn Thái Ngọc Duy | d90906a | 2013-02-13 16:13:17 +0700 | [diff] [blame] | 1265 | break; |
| 1266 | } |
| 1267 | if (p == NULL && |
| 1268 | /* |
| 1269 | * See if it really is a valid .idx file with |
| 1270 | * corresponding .pack file that we can map. |
| 1271 | */ |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1272 | (p = add_packed_git(path.buf, path.len, local)) != NULL) |
Nguyễn Thái Ngọc Duy | d90906a | 2013-02-13 16:13:17 +0700 | [diff] [blame] | 1273 | install_packed_git(p); |
Jeff King | 86f7780 | 2006-06-02 12:49:32 -0400 | [diff] [blame] | 1274 | } |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1275 | |
| 1276 | if (!report_garbage) |
| 1277 | continue; |
| 1278 | |
Jeff King | 2975c77 | 2014-06-30 12:58:25 -0400 | [diff] [blame] | 1279 | if (ends_with(de->d_name, ".idx") || |
| 1280 | ends_with(de->d_name, ".pack") || |
| 1281 | ends_with(de->d_name, ".bitmap") || |
| 1282 | ends_with(de->d_name, ".keep")) |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1283 | string_list_append(&garbage, path.buf); |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1284 | else |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1285 | report_garbage("garbage found", path.buf); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1286 | } |
Junio C Hamano | 5b35bcd | 2005-07-05 23:52:17 -0700 | [diff] [blame] | 1287 | closedir(dir); |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 1288 | report_pack_garbage(&garbage); |
| 1289 | string_list_clear(&garbage, 0); |
René Scharfe | 880fb8d | 2014-06-30 12:55:52 -0400 | [diff] [blame] | 1290 | strbuf_release(&path); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Junio C Hamano | b867092 | 2007-03-09 03:52:12 -0800 | [diff] [blame] | 1293 | static int sort_pack(const void *a_, const void *b_) |
| 1294 | { |
| 1295 | struct packed_git *a = *((struct packed_git **)a_); |
| 1296 | struct packed_git *b = *((struct packed_git **)b_); |
| 1297 | int st; |
| 1298 | |
| 1299 | /* |
| 1300 | * Local packs tend to contain objects specific to our |
| 1301 | * variant of the project than remote ones. In addition, |
| 1302 | * remote ones could be on a network mounted filesystem. |
| 1303 | * Favor local ones for these reasons. |
| 1304 | */ |
| 1305 | st = a->pack_local - b->pack_local; |
| 1306 | if (st) |
| 1307 | return -st; |
| 1308 | |
| 1309 | /* |
| 1310 | * Younger packs tend to contain more recent objects, |
| 1311 | * and more recent objects tend to get accessed more |
| 1312 | * often. |
| 1313 | */ |
| 1314 | if (a->mtime < b->mtime) |
| 1315 | return 1; |
| 1316 | else if (a->mtime == b->mtime) |
| 1317 | return 0; |
| 1318 | return -1; |
| 1319 | } |
| 1320 | |
| 1321 | static void rearrange_packed_git(void) |
| 1322 | { |
| 1323 | struct packed_git **ary, *p; |
| 1324 | int i, n; |
| 1325 | |
| 1326 | for (n = 0, p = packed_git; p; p = p->next) |
| 1327 | n++; |
| 1328 | if (n < 2) |
| 1329 | return; |
| 1330 | |
| 1331 | /* prepare an array of packed_git for easier sorting */ |
| 1332 | ary = xcalloc(n, sizeof(struct packed_git *)); |
| 1333 | for (n = 0, p = packed_git; p; p = p->next) |
| 1334 | ary[n++] = p; |
| 1335 | |
| 1336 | qsort(ary, n, sizeof(struct packed_git *), sort_pack); |
| 1337 | |
| 1338 | /* link them back again */ |
| 1339 | for (i = 0; i < n - 1; i++) |
| 1340 | ary[i]->next = ary[i + 1]; |
| 1341 | ary[n - 1]->next = NULL; |
| 1342 | packed_git = ary[0]; |
| 1343 | |
| 1344 | free(ary); |
| 1345 | } |
| 1346 | |
Jeff King | 637cdd9 | 2006-06-02 11:32:23 -0400 | [diff] [blame] | 1347 | static int prepare_packed_git_run_once = 0; |
Junio C Hamano | 9a217f2 | 2005-06-28 14:56:57 -0700 | [diff] [blame] | 1348 | void prepare_packed_git(void) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1349 | { |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 1350 | struct alternate_object_database *alt; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1351 | |
Jeff King | 637cdd9 | 2006-06-02 11:32:23 -0400 | [diff] [blame] | 1352 | if (prepare_packed_git_run_once) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1353 | return; |
Linus Torvalds | 9d835df | 2005-10-13 15:38:28 -0700 | [diff] [blame] | 1354 | prepare_packed_git_one(get_object_directory(), 1); |
Junio C Hamano | 9a217f2 | 2005-06-28 14:56:57 -0700 | [diff] [blame] | 1355 | prepare_alt_odb(); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 1356 | for (alt = alt_odb_list; alt; alt = alt->next) { |
Junio C Hamano | 1494e03 | 2005-12-04 22:48:43 -0800 | [diff] [blame] | 1357 | alt->name[-1] = 0; |
Linus Torvalds | 9d835df | 2005-10-13 15:38:28 -0700 | [diff] [blame] | 1358 | prepare_packed_git_one(alt->base, 0); |
Junio C Hamano | 1494e03 | 2005-12-04 22:48:43 -0800 | [diff] [blame] | 1359 | alt->name[-1] = '/'; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1360 | } |
Junio C Hamano | b867092 | 2007-03-09 03:52:12 -0800 | [diff] [blame] | 1361 | rearrange_packed_git(); |
Jeff King | 637cdd9 | 2006-06-02 11:32:23 -0400 | [diff] [blame] | 1362 | prepare_packed_git_run_once = 1; |
| 1363 | } |
| 1364 | |
Shawn Pearce | fc04c41 | 2006-11-01 17:06:21 -0500 | [diff] [blame] | 1365 | void reprepare_packed_git(void) |
Jeff King | 637cdd9 | 2006-06-02 11:32:23 -0400 | [diff] [blame] | 1366 | { |
| 1367 | prepare_packed_git_run_once = 0; |
| 1368 | prepare_packed_git(); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 1371 | static void mark_bad_packed_object(struct packed_git *p, |
| 1372 | const unsigned char *sha1) |
| 1373 | { |
| 1374 | unsigned i; |
| 1375 | for (i = 0; i < p->num_bad_objects; i++) |
| 1376 | if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i)) |
| 1377 | return; |
| 1378 | p->bad_object_sha1 = xrealloc(p->bad_object_sha1, 20 * (p->num_bad_objects + 1)); |
| 1379 | hashcpy(p->bad_object_sha1 + 20 * p->num_bad_objects, sha1); |
| 1380 | p->num_bad_objects++; |
| 1381 | } |
| 1382 | |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 1383 | static const struct packed_git *has_packed_and_bad(const unsigned char *sha1) |
Nicolas Pitre | ac93910 | 2008-07-14 21:46:48 -0400 | [diff] [blame] | 1384 | { |
| 1385 | struct packed_git *p; |
| 1386 | unsigned i; |
| 1387 | |
| 1388 | for (p = packed_git; p; p = p->next) |
| 1389 | for (i = 0; i < p->num_bad_objects; i++) |
| 1390 | if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i)) |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 1391 | return p; |
| 1392 | return NULL; |
Nicolas Pitre | ac93910 | 2008-07-14 21:46:48 -0400 | [diff] [blame] | 1393 | } |
| 1394 | |
Nguyễn Thái Ngọc Duy | 090ea12 | 2012-03-07 17:54:18 +0700 | [diff] [blame] | 1395 | /* |
| 1396 | * With an in-core object data in "map", rehash it to make sure the |
| 1397 | * object name actually matches "sha1" to detect object corruption. |
| 1398 | * With "map" == NULL, try reading the object named with "sha1" using |
| 1399 | * the streaming interface and rehash it to do the same. |
| 1400 | */ |
| 1401 | int check_sha1_signature(const unsigned char *sha1, void *map, |
| 1402 | unsigned long size, const char *type) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1403 | { |
| 1404 | unsigned char real_sha1[20]; |
Nguyễn Thái Ngọc Duy | 090ea12 | 2012-03-07 17:54:18 +0700 | [diff] [blame] | 1405 | enum object_type obj_type; |
| 1406 | struct git_istream *st; |
| 1407 | git_SHA_CTX c; |
| 1408 | char hdr[32]; |
| 1409 | int hdrlen; |
| 1410 | |
| 1411 | if (map) { |
| 1412 | hash_sha1_file(map, size, type, real_sha1); |
| 1413 | return hashcmp(sha1, real_sha1) ? -1 : 0; |
| 1414 | } |
| 1415 | |
| 1416 | st = open_istream(sha1, &obj_type, &size, NULL); |
| 1417 | if (!st) |
| 1418 | return -1; |
| 1419 | |
| 1420 | /* Generate the header */ |
| 1421 | hdrlen = sprintf(hdr, "%s %lu", typename(obj_type), size) + 1; |
| 1422 | |
| 1423 | /* Sha1.. */ |
| 1424 | git_SHA1_Init(&c); |
| 1425 | git_SHA1_Update(&c, hdr, hdrlen); |
| 1426 | for (;;) { |
| 1427 | char buf[1024 * 16]; |
| 1428 | ssize_t readlen = read_istream(st, buf, sizeof(buf)); |
| 1429 | |
Jeff King | f54fac5 | 2013-03-25 16:17:17 -0400 | [diff] [blame] | 1430 | if (readlen < 0) { |
| 1431 | close_istream(st); |
| 1432 | return -1; |
| 1433 | } |
Nguyễn Thái Ngọc Duy | 090ea12 | 2012-03-07 17:54:18 +0700 | [diff] [blame] | 1434 | if (!readlen) |
| 1435 | break; |
| 1436 | git_SHA1_Update(&c, buf, readlen); |
| 1437 | } |
| 1438 | git_SHA1_Final(real_sha1, &c); |
| 1439 | close_istream(st); |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 1440 | return hashcmp(sha1, real_sha1) ? -1 : 0; |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Vicent Marti | ec73f58 | 2013-10-24 14:01:47 -0400 | [diff] [blame] | 1443 | int git_open_noatime(const char *name) |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1444 | { |
| 1445 | static int sha1_file_open_flag = O_NOATIME; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1446 | |
Shawn O. Pearce | f2e872a | 2010-11-01 15:54:21 -0700 | [diff] [blame] | 1447 | for (;;) { |
| 1448 | int fd = open(name, O_RDONLY | sha1_file_open_flag); |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1449 | if (fd >= 0) |
Shawn O. Pearce | f2e872a | 2010-11-01 15:54:21 -0700 | [diff] [blame] | 1450 | return fd; |
| 1451 | |
Shawn O. Pearce | f2e872a | 2010-11-01 15:54:21 -0700 | [diff] [blame] | 1452 | /* Might the failure be due to O_NOATIME? */ |
| 1453 | if (errno != ENOENT && sha1_file_open_flag) { |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1454 | sha1_file_open_flag = 0; |
Shawn O. Pearce | f2e872a | 2010-11-01 15:54:21 -0700 | [diff] [blame] | 1455 | continue; |
| 1456 | } |
| 1457 | |
| 1458 | return -1; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1459 | } |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 1462 | static int stat_sha1_file(const unsigned char *sha1, struct stat *st) |
| 1463 | { |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 1464 | struct alternate_object_database *alt; |
| 1465 | |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 1466 | if (!lstat(sha1_file_name(sha1), st)) |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 1467 | return 0; |
| 1468 | |
| 1469 | prepare_alt_odb(); |
| 1470 | errno = ENOENT; |
| 1471 | for (alt = alt_odb_list; alt; alt = alt->next) { |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 1472 | fill_sha1_path(alt->name, sha1); |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 1473 | if (!lstat(alt->base, st)) |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | return -1; |
| 1478 | } |
| 1479 | |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1480 | static int open_sha1_file(const unsigned char *sha1) |
| 1481 | { |
| 1482 | int fd; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1483 | struct alternate_object_database *alt; |
Jeff King | d6c8a05 | 2014-05-15 04:54:06 -0400 | [diff] [blame] | 1484 | int most_interesting_errno; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1485 | |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 1486 | fd = git_open_noatime(sha1_file_name(sha1)); |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1487 | if (fd >= 0) |
| 1488 | return fd; |
Jeff King | d6c8a05 | 2014-05-15 04:54:06 -0400 | [diff] [blame] | 1489 | most_interesting_errno = errno; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1490 | |
| 1491 | prepare_alt_odb(); |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1492 | for (alt = alt_odb_list; alt; alt = alt->next) { |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 1493 | fill_sha1_path(alt->name, sha1); |
Junio C Hamano | f4e5168 | 2011-05-15 12:16:29 -0700 | [diff] [blame] | 1494 | fd = git_open_noatime(alt->base); |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1495 | if (fd >= 0) |
| 1496 | return fd; |
Jeff King | d6c8a05 | 2014-05-15 04:54:06 -0400 | [diff] [blame] | 1497 | if (most_interesting_errno == ENOENT) |
| 1498 | most_interesting_errno = errno; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1499 | } |
Jeff King | d6c8a05 | 2014-05-15 04:54:06 -0400 | [diff] [blame] | 1500 | errno = most_interesting_errno; |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1501 | return -1; |
| 1502 | } |
| 1503 | |
Junio C Hamano | f0270ef | 2011-05-14 19:42:10 -0700 | [diff] [blame] | 1504 | void *map_sha1_file(const unsigned char *sha1, unsigned long *size) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1505 | { |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1506 | void *map; |
Linus Torvalds | 144bde7 | 2005-04-23 11:09:32 -0700 | [diff] [blame] | 1507 | int fd; |
Junio C Hamano | ace1534 | 2005-05-07 00:38:04 -0700 | [diff] [blame] | 1508 | |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1509 | fd = open_sha1_file(sha1); |
| 1510 | map = NULL; |
| 1511 | if (fd >= 0) { |
| 1512 | struct stat st; |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1513 | |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1514 | if (!fstat(fd, &st)) { |
| 1515 | *size = xsize_t(st.st_size); |
Matthieu Moy | 33e42de | 2012-02-06 17:24:52 +0100 | [diff] [blame] | 1516 | if (!*size) { |
| 1517 | /* mmap() is forbidden on empty files */ |
| 1518 | error("object file %s is empty", sha1_file_name(sha1)); |
| 1519 | return NULL; |
| 1520 | } |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1521 | map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); |
Linus Torvalds | 144bde7 | 2005-04-23 11:09:32 -0700 | [diff] [blame] | 1522 | } |
Linus Torvalds | 44d1c19 | 2008-06-14 11:32:37 -0700 | [diff] [blame] | 1523 | close(fd); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1524 | } |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1525 | return map; |
| 1526 | } |
| 1527 | |
Nicolas Pitre | 09ded04 | 2008-10-29 19:02:46 -0400 | [diff] [blame] | 1528 | unsigned long unpack_object_header_buffer(const unsigned char *buf, |
| 1529 | unsigned long len, enum object_type *type, unsigned long *sizep) |
Junio C Hamano | ad1ed5e | 2006-09-01 15:17:01 -0700 | [diff] [blame] | 1530 | { |
| 1531 | unsigned shift; |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 1532 | unsigned long size, c; |
Junio C Hamano | ad1ed5e | 2006-09-01 15:17:01 -0700 | [diff] [blame] | 1533 | unsigned long used = 0; |
| 1534 | |
| 1535 | c = buf[used++]; |
| 1536 | *type = (c >> 4) & 7; |
| 1537 | size = c & 15; |
| 1538 | shift = 4; |
| 1539 | while (c & 0x80) { |
Pierre Habouzit | f630cfd | 2009-07-22 23:34:34 +0200 | [diff] [blame] | 1540 | if (len <= used || bitsizeof(long) <= shift) { |
Nicolas Pitre | 09ded04 | 2008-10-29 19:02:46 -0400 | [diff] [blame] | 1541 | error("bad object header"); |
Junio C Hamano | ea4f968 | 2011-10-27 11:42:57 -0700 | [diff] [blame] | 1542 | size = used = 0; |
| 1543 | break; |
Nicolas Pitre | 09ded04 | 2008-10-29 19:02:46 -0400 | [diff] [blame] | 1544 | } |
Junio C Hamano | ad1ed5e | 2006-09-01 15:17:01 -0700 | [diff] [blame] | 1545 | c = buf[used++]; |
| 1546 | size += (c & 0x7f) << shift; |
| 1547 | shift += 7; |
| 1548 | } |
| 1549 | *sizep = size; |
| 1550 | return used; |
| 1551 | } |
| 1552 | |
Junio C Hamano | eb4f407 | 2011-07-19 09:33:03 -0700 | [diff] [blame] | 1553 | int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz) |
Linus Torvalds | c448357 | 2005-06-01 17:54:59 -0700 | [diff] [blame] | 1554 | { |
| 1555 | /* Get the data stream */ |
| 1556 | memset(stream, 0, sizeof(*stream)); |
| 1557 | stream->next_in = map; |
| 1558 | stream->avail_in = mapsize; |
| 1559 | stream->next_out = buffer; |
Linus Torvalds | 93821bd | 2006-07-11 12:48:08 -0700 | [diff] [blame] | 1560 | stream->avail_out = bufsiz; |
Linus Torvalds | c448357 | 2005-06-01 17:54:59 -0700 | [diff] [blame] | 1561 | |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1562 | git_inflate_init(stream); |
Junio C Hamano | cc5c54e | 2011-06-08 11:29:01 -0700 | [diff] [blame] | 1563 | return git_inflate(stream, 0); |
Linus Torvalds | c448357 | 2005-06-01 17:54:59 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 1566 | static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1567 | { |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1568 | int bytes = strlen(buffer) + 1; |
Ilari Liusvaara | 3aee68a | 2010-01-26 20:24:14 +0200 | [diff] [blame] | 1569 | unsigned char *buf = xmallocz(size); |
Linus Torvalds | 93821bd | 2006-07-11 12:48:08 -0700 | [diff] [blame] | 1570 | unsigned long n; |
Junio C Hamano | 7efbff7 | 2007-03-05 00:21:37 -0800 | [diff] [blame] | 1571 | int status = Z_OK; |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1572 | |
Linus Torvalds | 93821bd | 2006-07-11 12:48:08 -0700 | [diff] [blame] | 1573 | n = stream->total_out - bytes; |
| 1574 | if (n > size) |
| 1575 | n = size; |
| 1576 | memcpy(buf, (char *) buffer + bytes, n); |
| 1577 | bytes = n; |
Linus Torvalds | 456cdf6 | 2007-03-19 22:49:53 -0700 | [diff] [blame] | 1578 | if (bytes <= size) { |
| 1579 | /* |
| 1580 | * The above condition must be (bytes <= size), not |
| 1581 | * (bytes < size). In other words, even though we |
Junio C Hamano | ccf5ace | 2011-05-15 12:16:03 -0700 | [diff] [blame] | 1582 | * expect no more output and set avail_out to zero, |
Linus Torvalds | 456cdf6 | 2007-03-19 22:49:53 -0700 | [diff] [blame] | 1583 | * the input zlib stream may have bytes that express |
| 1584 | * "this concludes the stream", and we *do* want to |
| 1585 | * eat that input. |
| 1586 | * |
| 1587 | * Otherwise we would not be able to test that we |
| 1588 | * consumed all the input to reach the expected size; |
| 1589 | * we also want to check that zlib tells us that all |
| 1590 | * went well with status == Z_STREAM_END at the end. |
| 1591 | */ |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1592 | stream->next_out = buf + bytes; |
| 1593 | stream->avail_out = size - bytes; |
Junio C Hamano | 7efbff7 | 2007-03-05 00:21:37 -0800 | [diff] [blame] | 1594 | while (status == Z_OK) |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1595 | status = git_inflate(stream, Z_FINISH); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1596 | } |
Linus Torvalds | 456cdf6 | 2007-03-19 22:49:53 -0700 | [diff] [blame] | 1597 | if (status == Z_STREAM_END && !stream->avail_in) { |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1598 | git_inflate_end(stream); |
Junio C Hamano | 7efbff7 | 2007-03-05 00:21:37 -0800 | [diff] [blame] | 1599 | return buf; |
| 1600 | } |
| 1601 | |
| 1602 | if (status < 0) |
| 1603 | error("corrupt loose object '%s'", sha1_to_hex(sha1)); |
| 1604 | else if (stream->avail_in) |
| 1605 | error("garbage at end of loose object '%s'", |
| 1606 | sha1_to_hex(sha1)); |
| 1607 | free(buf); |
| 1608 | return NULL; |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1611 | /* |
| 1612 | * We used to just use "sscanf()", but that's actually way |
| 1613 | * too permissive for what we want to check. So do an anal |
| 1614 | * object header parse by hand. |
| 1615 | */ |
Junio C Hamano | f0270ef | 2011-05-14 19:42:10 -0700 | [diff] [blame] | 1616 | int parse_sha1_header(const char *hdr, unsigned long *sizep) |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1617 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1618 | char type[10]; |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1619 | int i; |
| 1620 | unsigned long size; |
| 1621 | |
| 1622 | /* |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 1623 | * The type can be at most ten bytes (including the |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1624 | * terminating '\0' that we add), and is followed by |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1625 | * a space. |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1626 | */ |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1627 | i = 0; |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1628 | for (;;) { |
| 1629 | char c = *hdr++; |
| 1630 | if (c == ' ') |
| 1631 | break; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1632 | type[i++] = c; |
| 1633 | if (i >= sizeof(type)) |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1634 | return -1; |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1635 | } |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1636 | type[i] = 0; |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1637 | |
| 1638 | /* |
| 1639 | * The length must follow immediately, and be in canonical |
| 1640 | * decimal format (ie "010" is not valid). |
| 1641 | */ |
| 1642 | size = *hdr++ - '0'; |
| 1643 | if (size > 9) |
| 1644 | return -1; |
| 1645 | if (size) { |
| 1646 | for (;;) { |
| 1647 | unsigned long c = *hdr - '0'; |
| 1648 | if (c > 9) |
| 1649 | break; |
| 1650 | hdr++; |
| 1651 | size = size * 10 + c; |
| 1652 | } |
| 1653 | } |
| 1654 | *sizep = size; |
| 1655 | |
| 1656 | /* |
| 1657 | * The length must be followed by a zero byte |
| 1658 | */ |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1659 | return *hdr ? -1 : type_from_string(type); |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Junio C Hamano | 7efbff7 | 2007-03-05 00:21:37 -0800 | [diff] [blame] | 1662 | static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1) |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1663 | { |
| 1664 | int ret; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 1665 | git_zstream stream; |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1666 | char hdr[8192]; |
| 1667 | |
| 1668 | ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1669 | if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0) |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1670 | return NULL; |
| 1671 | |
Junio C Hamano | 7efbff7 | 2007-03-05 00:21:37 -0800 | [diff] [blame] | 1672 | return unpack_sha1_rest(&stream, hdr, *size, sha1); |
Linus Torvalds | 5180cac | 2005-06-02 07:57:25 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Nicolas Pitre | 54dab52 | 2007-04-16 12:31:56 -0400 | [diff] [blame] | 1675 | unsigned long get_size_from_delta(struct packed_git *p, |
| 1676 | struct pack_window **w_curs, |
| 1677 | off_t curpos) |
| 1678 | { |
| 1679 | const unsigned char *data; |
| 1680 | unsigned char delta_head[20], *in; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 1681 | git_zstream stream; |
Nicolas Pitre | 54dab52 | 2007-04-16 12:31:56 -0400 | [diff] [blame] | 1682 | int st; |
| 1683 | |
| 1684 | memset(&stream, 0, sizeof(stream)); |
| 1685 | stream.next_out = delta_head; |
| 1686 | stream.avail_out = sizeof(delta_head); |
| 1687 | |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1688 | git_inflate_init(&stream); |
Nicolas Pitre | 54dab52 | 2007-04-16 12:31:56 -0400 | [diff] [blame] | 1689 | do { |
| 1690 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
| 1691 | stream.next_in = in; |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1692 | st = git_inflate(&stream, Z_FINISH); |
Nicolas Pitre | 54dab52 | 2007-04-16 12:31:56 -0400 | [diff] [blame] | 1693 | curpos += stream.next_in - in; |
| 1694 | } while ((st == Z_OK || st == Z_BUF_ERROR) && |
| 1695 | stream.total_out < sizeof(delta_head)); |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1696 | git_inflate_end(&stream); |
Nicolas Pitre | 3d77d87 | 2008-10-29 19:02:47 -0400 | [diff] [blame] | 1697 | if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) { |
| 1698 | error("delta data unpack-initial failed"); |
| 1699 | return 0; |
| 1700 | } |
Nicolas Pitre | 54dab52 | 2007-04-16 12:31:56 -0400 | [diff] [blame] | 1701 | |
| 1702 | /* Examine the initial part of the delta to figure out |
| 1703 | * the result size. |
| 1704 | */ |
| 1705 | data = delta_head; |
| 1706 | |
| 1707 | /* ignore base size */ |
| 1708 | get_delta_hdr_size(&data, delta_head+sizeof(delta_head)); |
| 1709 | |
| 1710 | /* Read the result size */ |
| 1711 | return get_delta_hdr_size(&data, delta_head+sizeof(delta_head)); |
| 1712 | } |
| 1713 | |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1714 | static off_t get_delta_base(struct packed_git *p, |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1715 | struct pack_window **w_curs, |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1716 | off_t *curpos, |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1717 | enum object_type type, |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1718 | off_t delta_obj_offset) |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1719 | { |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1720 | unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL); |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1721 | off_t base_offset; |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1722 | |
Shawn O. Pearce | 8d8a4ea | 2006-12-23 02:34:18 -0500 | [diff] [blame] | 1723 | /* use_pack() assured us we have [base_info, base_info + 20) |
| 1724 | * as a range that we can look at without walking off the |
| 1725 | * end of the mapped window. Its actually the hash size |
| 1726 | * that is assured. An OFS_DELTA longer than the hash size |
| 1727 | * is stupid, as then a REF_DELTA would be smaller to store. |
| 1728 | */ |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1729 | if (type == OBJ_OFS_DELTA) { |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1730 | unsigned used = 0; |
| 1731 | unsigned char c = base_info[used++]; |
| 1732 | base_offset = c & 127; |
| 1733 | while (c & 128) { |
| 1734 | base_offset += 1; |
Nicolas Pitre | 8723f21 | 2007-04-09 01:06:29 -0400 | [diff] [blame] | 1735 | if (!base_offset || MSB(base_offset, 7)) |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 1736 | return 0; /* overflow */ |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1737 | c = base_info[used++]; |
| 1738 | base_offset = (base_offset << 7) + (c & 127); |
| 1739 | } |
| 1740 | base_offset = delta_obj_offset - base_offset; |
Nicolas Pitre | d8f3255 | 2008-10-29 19:02:45 -0400 | [diff] [blame] | 1741 | if (base_offset <= 0 || base_offset >= delta_obj_offset) |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 1742 | return 0; /* out of bound */ |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1743 | *curpos += used; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1744 | } else if (type == OBJ_REF_DELTA) { |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1745 | /* The base entry _must_ be in the same pack */ |
| 1746 | base_offset = find_pack_entry_one(base_info, p); |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1747 | *curpos += 20; |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1748 | } else |
| 1749 | die("I am totally screwed"); |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1750 | return base_offset; |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 1751 | } |
| 1752 | |
Jeff King | 5d642e7 | 2013-12-21 09:24:20 -0500 | [diff] [blame] | 1753 | /* |
| 1754 | * Like get_delta_base above, but we return the sha1 instead of the pack |
| 1755 | * offset. This means it is cheaper for REF deltas (we do not have to do |
| 1756 | * the final object lookup), but more expensive for OFS deltas (we |
| 1757 | * have to load the revidx to convert the offset back into a sha1). |
| 1758 | */ |
| 1759 | static const unsigned char *get_delta_base_sha1(struct packed_git *p, |
| 1760 | struct pack_window **w_curs, |
| 1761 | off_t curpos, |
| 1762 | enum object_type type, |
| 1763 | off_t delta_obj_offset) |
| 1764 | { |
| 1765 | if (type == OBJ_REF_DELTA) { |
| 1766 | unsigned char *base = use_pack(p, w_curs, curpos, NULL); |
| 1767 | return base; |
| 1768 | } else if (type == OBJ_OFS_DELTA) { |
| 1769 | struct revindex_entry *revidx; |
| 1770 | off_t base_offset = get_delta_base(p, w_curs, &curpos, |
| 1771 | type, delta_obj_offset); |
| 1772 | |
| 1773 | if (!base_offset) |
| 1774 | return NULL; |
| 1775 | |
| 1776 | revidx = find_pack_revindex(p, base_offset); |
| 1777 | if (!revidx) |
| 1778 | return NULL; |
| 1779 | |
| 1780 | return nth_packed_object_sha1(p, revidx->nr); |
| 1781 | } else |
| 1782 | return NULL; |
| 1783 | } |
| 1784 | |
Junio C Hamano | f8c8abc | 2011-05-13 15:33:33 -0700 | [diff] [blame] | 1785 | int unpack_object_header(struct packed_git *p, |
| 1786 | struct pack_window **w_curs, |
| 1787 | off_t *curpos, |
| 1788 | unsigned long *sizep) |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1789 | { |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1790 | unsigned char *base; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 1791 | unsigned long left; |
Junio C Hamano | ad1ed5e | 2006-09-01 15:17:01 -0700 | [diff] [blame] | 1792 | unsigned long used; |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1793 | enum object_type type; |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1794 | |
Shawn O. Pearce | 8d8a4ea | 2006-12-23 02:34:18 -0500 | [diff] [blame] | 1795 | /* use_pack() assures us we have [base, base + 20) available |
Jim Meyering | 0353a0c | 2011-04-13 17:39:40 +0200 | [diff] [blame] | 1796 | * as a range that we can look at. (Its actually the hash |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 1797 | * size that is assured.) With our object header encoding |
Shawn O. Pearce | 8d8a4ea | 2006-12-23 02:34:18 -0500 | [diff] [blame] | 1798 | * the maximum deflated object size is 2^137, which is just |
| 1799 | * insane, so we know won't exceed what we have been given. |
| 1800 | */ |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1801 | base = use_pack(p, w_curs, *curpos, &left); |
Nicolas Pitre | 09ded04 | 2008-10-29 19:02:46 -0400 | [diff] [blame] | 1802 | used = unpack_object_header_buffer(base, left, &type, sizep); |
| 1803 | if (!used) { |
| 1804 | type = OBJ_BAD; |
| 1805 | } else |
| 1806 | *curpos += used; |
Junio C Hamano | ad1ed5e | 2006-09-01 15:17:01 -0700 | [diff] [blame] | 1807 | |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1808 | return type; |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1811 | static int retry_bad_packed_offset(struct packed_git *p, off_t obj_offset) |
| 1812 | { |
| 1813 | int type; |
| 1814 | struct revindex_entry *revidx; |
| 1815 | const unsigned char *sha1; |
| 1816 | revidx = find_pack_revindex(p, obj_offset); |
| 1817 | if (!revidx) |
| 1818 | return OBJ_BAD; |
| 1819 | sha1 = nth_packed_object_sha1(p, revidx->nr); |
| 1820 | mark_bad_packed_object(p, sha1); |
| 1821 | type = sha1_object_info(sha1, NULL); |
| 1822 | if (type <= OBJ_NONE) |
| 1823 | return OBJ_BAD; |
| 1824 | return type; |
| 1825 | } |
| 1826 | |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1827 | #define POI_STACK_PREALLOC 64 |
| 1828 | |
Jeff King | 90191d3 | 2013-07-12 02:31:57 -0400 | [diff] [blame] | 1829 | static enum object_type packed_to_object_type(struct packed_git *p, |
| 1830 | off_t obj_offset, |
| 1831 | enum object_type type, |
| 1832 | struct pack_window **w_curs, |
| 1833 | off_t curpos) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1834 | { |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1835 | off_t small_poi_stack[POI_STACK_PREALLOC]; |
| 1836 | off_t *poi_stack = small_poi_stack; |
| 1837 | int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC; |
Junio C Hamano | 5db47c2 | 2005-06-27 23:58:08 -0700 | [diff] [blame] | 1838 | |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1839 | while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
| 1840 | off_t base_offset; |
Jeff King | 90191d3 | 2013-07-12 02:31:57 -0400 | [diff] [blame] | 1841 | unsigned long size; |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1842 | /* Push the object we're going to leave behind */ |
| 1843 | if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) { |
| 1844 | poi_stack_alloc = alloc_nr(poi_stack_nr); |
| 1845 | poi_stack = xmalloc(sizeof(off_t)*poi_stack_alloc); |
| 1846 | memcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr); |
| 1847 | } else { |
| 1848 | ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc); |
| 1849 | } |
| 1850 | poi_stack[poi_stack_nr++] = obj_offset; |
| 1851 | /* If parsing the base offset fails, just unwind */ |
Jeff King | 90191d3 | 2013-07-12 02:31:57 -0400 | [diff] [blame] | 1852 | base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset); |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1853 | if (!base_offset) |
| 1854 | goto unwind; |
| 1855 | curpos = obj_offset = base_offset; |
Jeff King | 90191d3 | 2013-07-12 02:31:57 -0400 | [diff] [blame] | 1856 | type = unpack_object_header(p, w_curs, &curpos, &size); |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1857 | if (type <= OBJ_NONE) { |
| 1858 | /* If getting the base itself fails, we first |
| 1859 | * retry the base, otherwise unwind */ |
| 1860 | type = retry_bad_packed_offset(p, base_offset); |
| 1861 | if (type > OBJ_NONE) |
| 1862 | goto out; |
| 1863 | goto unwind; |
| 1864 | } |
| 1865 | } |
| 1866 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1867 | switch (type) { |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1868 | case OBJ_BAD: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1869 | case OBJ_COMMIT: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1870 | case OBJ_TREE: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1871 | case OBJ_BLOB: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 1872 | case OBJ_TAG: |
Linus Torvalds | a69d094 | 2005-06-28 09:58:23 -0700 | [diff] [blame] | 1873 | break; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1874 | default: |
Nicolas Pitre | 3d77d87 | 2008-10-29 19:02:47 -0400 | [diff] [blame] | 1875 | error("unknown object type %i at offset %"PRIuMAX" in %s", |
| 1876 | type, (uintmax_t)obj_offset, p->pack_name); |
| 1877 | type = OBJ_BAD; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1878 | } |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1879 | |
| 1880 | out: |
| 1881 | if (poi_stack != small_poi_stack) |
| 1882 | free(poi_stack); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1883 | return type; |
Thomas Rast | 790d96c | 2013-03-25 19:07:39 +0100 | [diff] [blame] | 1884 | |
| 1885 | unwind: |
| 1886 | while (poi_stack_nr) { |
| 1887 | obj_offset = poi_stack[--poi_stack_nr]; |
| 1888 | type = retry_bad_packed_offset(p, obj_offset); |
| 1889 | if (type > OBJ_NONE) |
| 1890 | goto out; |
| 1891 | } |
| 1892 | type = OBJ_BAD; |
| 1893 | goto out; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1896 | static int packed_object_info(struct packed_git *p, off_t obj_offset, |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1897 | struct object_info *oi) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1898 | { |
| 1899 | struct pack_window *w_curs = NULL; |
| 1900 | unsigned long size; |
| 1901 | off_t curpos = obj_offset; |
| 1902 | enum object_type type; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1903 | |
Jeff King | 412916e | 2013-07-12 02:32:25 -0400 | [diff] [blame] | 1904 | /* |
| 1905 | * We always get the representation type, but only convert it to |
| 1906 | * a "real" type later if the caller is interested. |
| 1907 | */ |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1908 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
| 1909 | |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1910 | if (oi->sizep) { |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1911 | if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
| 1912 | off_t tmp_pos = curpos; |
| 1913 | off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos, |
| 1914 | type, obj_offset); |
| 1915 | if (!base_offset) { |
| 1916 | type = OBJ_BAD; |
| 1917 | goto out; |
| 1918 | } |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1919 | *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos); |
| 1920 | if (*oi->sizep == 0) { |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1921 | type = OBJ_BAD; |
| 1922 | goto out; |
| 1923 | } |
| 1924 | } else { |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1925 | *oi->sizep = size; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1929 | if (oi->disk_sizep) { |
Jeff King | 161f00e | 2013-07-07 06:04:00 -0400 | [diff] [blame] | 1930 | struct revindex_entry *revidx = find_pack_revindex(p, obj_offset); |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1931 | *oi->disk_sizep = revidx[1].offset - obj_offset; |
Jeff King | 161f00e | 2013-07-07 06:04:00 -0400 | [diff] [blame] | 1932 | } |
| 1933 | |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 1934 | if (oi->typep) { |
| 1935 | *oi->typep = packed_to_object_type(p, obj_offset, type, &w_curs, curpos); |
| 1936 | if (*oi->typep < 0) { |
Jeff King | 412916e | 2013-07-12 02:32:25 -0400 | [diff] [blame] | 1937 | type = OBJ_BAD; |
| 1938 | goto out; |
| 1939 | } |
| 1940 | } |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1941 | |
Jeff King | 5d642e7 | 2013-12-21 09:24:20 -0500 | [diff] [blame] | 1942 | if (oi->delta_base_sha1) { |
| 1943 | if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
| 1944 | const unsigned char *base; |
| 1945 | |
| 1946 | base = get_delta_base_sha1(p, &w_curs, curpos, |
| 1947 | type, obj_offset); |
| 1948 | if (!base) { |
| 1949 | type = OBJ_BAD; |
| 1950 | goto out; |
| 1951 | } |
| 1952 | |
| 1953 | hashcpy(oi->delta_base_sha1, base); |
| 1954 | } else |
| 1955 | hashclr(oi->delta_base_sha1); |
| 1956 | } |
| 1957 | |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1958 | out: |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1959 | unuse_pack(&w_curs); |
| 1960 | return type; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Shawn Pearce | eb950c1 | 2006-08-26 04:12:27 -0400 | [diff] [blame] | 1963 | static void *unpack_compressed_entry(struct packed_git *p, |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 1964 | struct pack_window **w_curs, |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 1965 | off_t curpos, |
Shawn Pearce | eb950c1 | 2006-08-26 04:12:27 -0400 | [diff] [blame] | 1966 | unsigned long size) |
Shawn Pearce | de530aa | 2006-08-26 04:10:43 -0400 | [diff] [blame] | 1967 | { |
| 1968 | int st; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 1969 | git_zstream stream; |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 1970 | unsigned char *buffer, *in; |
Shawn Pearce | de530aa | 2006-08-26 04:10:43 -0400 | [diff] [blame] | 1971 | |
Nguyễn Thái Ngọc Duy | 735efde | 2014-08-16 10:08:03 +0700 | [diff] [blame] | 1972 | buffer = xmallocz_gently(size); |
| 1973 | if (!buffer) |
| 1974 | return NULL; |
Shawn Pearce | de530aa | 2006-08-26 04:10:43 -0400 | [diff] [blame] | 1975 | memset(&stream, 0, sizeof(stream)); |
Shawn Pearce | de530aa | 2006-08-26 04:10:43 -0400 | [diff] [blame] | 1976 | stream.next_out = buffer; |
Junio C Hamano | 39eea7b | 2009-10-21 23:06:14 -0700 | [diff] [blame] | 1977 | stream.avail_out = size + 1; |
Shawn Pearce | de530aa | 2006-08-26 04:10:43 -0400 | [diff] [blame] | 1978 | |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1979 | git_inflate_init(&stream); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 1980 | do { |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1981 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 1982 | stream.next_in = in; |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1983 | st = git_inflate(&stream, Z_FINISH); |
Junio C Hamano | 39eea7b | 2009-10-21 23:06:14 -0700 | [diff] [blame] | 1984 | if (!stream.avail_out) |
| 1985 | break; /* the payload is larger than it should be */ |
Nicolas Pitre | 2b87c45 | 2007-02-26 14:55:56 -0500 | [diff] [blame] | 1986 | curpos += stream.next_in - in; |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 1987 | } while (st == Z_OK || st == Z_BUF_ERROR); |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 1988 | git_inflate_end(&stream); |
Shawn Pearce | de530aa | 2006-08-26 04:10:43 -0400 | [diff] [blame] | 1989 | if ((st != Z_STREAM_END) || stream.total_out != size) { |
| 1990 | free(buffer); |
| 1991 | return NULL; |
| 1992 | } |
| 1993 | |
| 1994 | return buffer; |
| 1995 | } |
| 1996 | |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 1997 | #define MAX_DELTA_CACHE (256) |
| 1998 | |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 1999 | static size_t delta_base_cached; |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2000 | |
| 2001 | static struct delta_base_cache_lru_list { |
| 2002 | struct delta_base_cache_lru_list *prev; |
| 2003 | struct delta_base_cache_lru_list *next; |
| 2004 | } delta_base_cache_lru = { &delta_base_cache_lru, &delta_base_cache_lru }; |
| 2005 | |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2006 | static struct delta_base_cache_entry { |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2007 | struct delta_base_cache_lru_list lru; |
| 2008 | void *data; |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2009 | struct packed_git *p; |
| 2010 | off_t base_offset; |
| 2011 | unsigned long size; |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2012 | enum object_type type; |
| 2013 | } delta_base_cache[MAX_DELTA_CACHE]; |
| 2014 | |
| 2015 | static unsigned long pack_entry_hash(struct packed_git *p, off_t base_offset) |
| 2016 | { |
| 2017 | unsigned long hash; |
| 2018 | |
| 2019 | hash = (unsigned long)p + (unsigned long)base_offset; |
| 2020 | hash += (hash >> 8) + (hash >> 16); |
Nicolas Pitre | 3358004 | 2007-03-19 16:28:51 -0400 | [diff] [blame] | 2021 | return hash % MAX_DELTA_CACHE; |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Thomas Rast | 84dd81c | 2013-03-27 21:03:41 +0100 | [diff] [blame] | 2024 | static struct delta_base_cache_entry * |
| 2025 | get_delta_base_cache_entry(struct packed_git *p, off_t base_offset) |
Junio C Hamano | 5266d36 | 2011-05-13 13:20:43 -0700 | [diff] [blame] | 2026 | { |
| 2027 | unsigned long hash = pack_entry_hash(p, base_offset); |
Thomas Rast | 84dd81c | 2013-03-27 21:03:41 +0100 | [diff] [blame] | 2028 | return delta_base_cache + hash; |
| 2029 | } |
| 2030 | |
| 2031 | static int eq_delta_base_cache_entry(struct delta_base_cache_entry *ent, |
| 2032 | struct packed_git *p, off_t base_offset) |
| 2033 | { |
Junio C Hamano | 5266d36 | 2011-05-13 13:20:43 -0700 | [diff] [blame] | 2034 | return (ent->data && ent->p == p && ent->base_offset == base_offset); |
| 2035 | } |
| 2036 | |
Thomas Rast | 84dd81c | 2013-03-27 21:03:41 +0100 | [diff] [blame] | 2037 | static int in_delta_base_cache(struct packed_git *p, off_t base_offset) |
| 2038 | { |
| 2039 | struct delta_base_cache_entry *ent; |
| 2040 | ent = get_delta_base_cache_entry(p, base_offset); |
| 2041 | return eq_delta_base_cache_entry(ent, p, base_offset); |
| 2042 | } |
| 2043 | |
| 2044 | static void clear_delta_base_cache_entry(struct delta_base_cache_entry *ent) |
| 2045 | { |
| 2046 | ent->data = NULL; |
| 2047 | ent->lru.next->prev = ent->lru.prev; |
| 2048 | ent->lru.prev->next = ent->lru.next; |
| 2049 | delta_base_cached -= ent->size; |
| 2050 | } |
| 2051 | |
Linus Torvalds | 62f255a | 2007-03-17 12:42:15 -0700 | [diff] [blame] | 2052 | static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset, |
Nicolas Pitre | a0cba10 | 2007-03-17 21:13:57 -0400 | [diff] [blame] | 2053 | unsigned long *base_size, enum object_type *type, int keep_cache) |
Linus Torvalds | 62f255a | 2007-03-17 12:42:15 -0700 | [diff] [blame] | 2054 | { |
Thomas Rast | 84dd81c | 2013-03-27 21:03:41 +0100 | [diff] [blame] | 2055 | struct delta_base_cache_entry *ent; |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2056 | void *ret; |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2057 | |
Thomas Rast | 84dd81c | 2013-03-27 21:03:41 +0100 | [diff] [blame] | 2058 | ent = get_delta_base_cache_entry(p, base_offset); |
| 2059 | |
| 2060 | if (!eq_delta_base_cache_entry(ent, p, base_offset)) |
Miklos Vajna | 749bc58 | 2008-10-09 02:11:24 +0200 | [diff] [blame] | 2061 | return unpack_entry(p, base_offset, type, base_size); |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2062 | |
Thomas Rast | 84dd81c | 2013-03-27 21:03:41 +0100 | [diff] [blame] | 2063 | ret = ent->data; |
| 2064 | |
| 2065 | if (!keep_cache) |
| 2066 | clear_delta_base_cache_entry(ent); |
| 2067 | else |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 2068 | ret = xmemdupz(ent->data, ent->size); |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2069 | *type = ent->type; |
| 2070 | *base_size = ent->size; |
| 2071 | return ret; |
Linus Torvalds | 62f255a | 2007-03-17 12:42:15 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 2074 | static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) |
| 2075 | { |
| 2076 | if (ent->data) { |
| 2077 | free(ent->data); |
| 2078 | ent->data = NULL; |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2079 | ent->lru.next->prev = ent->lru.prev; |
| 2080 | ent->lru.prev->next = ent->lru.next; |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 2081 | delta_base_cached -= ent->size; |
| 2082 | } |
| 2083 | } |
| 2084 | |
Shawn O. Pearce | 3d20c63 | 2009-02-10 13:36:12 -0800 | [diff] [blame] | 2085 | void clear_delta_base_cache(void) |
| 2086 | { |
| 2087 | unsigned long p; |
| 2088 | for (p = 0; p < MAX_DELTA_CACHE; p++) |
| 2089 | release_delta_base_cache(&delta_base_cache[p]); |
| 2090 | } |
| 2091 | |
Linus Torvalds | 62f255a | 2007-03-17 12:42:15 -0700 | [diff] [blame] | 2092 | static void add_delta_base_cache(struct packed_git *p, off_t base_offset, |
| 2093 | void *base, unsigned long base_size, enum object_type type) |
| 2094 | { |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2095 | unsigned long hash = pack_entry_hash(p, base_offset); |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2096 | struct delta_base_cache_entry *ent = delta_base_cache + hash; |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2097 | struct delta_base_cache_lru_list *lru; |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2098 | |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 2099 | release_delta_base_cache(ent); |
| 2100 | delta_base_cached += base_size; |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2101 | |
| 2102 | for (lru = delta_base_cache_lru.next; |
| 2103 | delta_base_cached > delta_base_cache_limit |
| 2104 | && lru != &delta_base_cache_lru; |
| 2105 | lru = lru->next) { |
| 2106 | struct delta_base_cache_entry *f = (void *)lru; |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 2107 | if (f->type == OBJ_BLOB) |
| 2108 | release_delta_base_cache(f); |
| 2109 | } |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2110 | for (lru = delta_base_cache_lru.next; |
| 2111 | delta_base_cached > delta_base_cache_limit |
| 2112 | && lru != &delta_base_cache_lru; |
| 2113 | lru = lru->next) { |
| 2114 | struct delta_base_cache_entry *f = (void *)lru; |
| 2115 | release_delta_base_cache(f); |
| 2116 | } |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 2117 | |
Linus Torvalds | e5e0161 | 2007-03-17 12:44:06 -0700 | [diff] [blame] | 2118 | ent->p = p; |
| 2119 | ent->base_offset = base_offset; |
| 2120 | ent->type = type; |
| 2121 | ent->data = base; |
| 2122 | ent->size = base_size; |
Nicolas Pitre | 5e08ecb | 2007-03-19 16:31:04 -0400 | [diff] [blame] | 2123 | ent->lru.next = &delta_base_cache_lru; |
| 2124 | ent->lru.prev = delta_base_cache_lru.prev; |
| 2125 | delta_base_cache_lru.prev->next = &ent->lru; |
| 2126 | delta_base_cache_lru.prev = &ent->lru; |
Linus Torvalds | 62f255a | 2007-03-17 12:42:15 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
Christian Couder | c2c5b27 | 2009-01-12 18:42:24 +0100 | [diff] [blame] | 2129 | static void *read_object(const unsigned char *sha1, enum object_type *type, |
| 2130 | unsigned long *size); |
| 2131 | |
Junio C Hamano | 5f44324 | 2011-07-06 19:08:55 -0700 | [diff] [blame] | 2132 | static void write_pack_access_log(struct packed_git *p, off_t obj_offset) |
| 2133 | { |
Karsten Blees | 67dc598 | 2014-07-12 02:01:38 +0200 | [diff] [blame] | 2134 | static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS); |
| 2135 | trace_printf_key(&pack_access, "%s %"PRIuMAX"\n", |
| 2136 | p->pack_name, (uintmax_t)obj_offset); |
Junio C Hamano | 5f44324 | 2011-07-06 19:08:55 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Nicolas Pitre | 0e8189e | 2008-10-31 11:31:08 -0400 | [diff] [blame] | 2139 | int do_check_packed_object_crc; |
| 2140 | |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2141 | #define UNPACK_ENTRY_STACK_PREALLOC 64 |
| 2142 | struct unpack_entry_stack_ent { |
| 2143 | off_t obj_offset; |
| 2144 | off_t curpos; |
| 2145 | unsigned long size; |
| 2146 | }; |
| 2147 | |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 2148 | void *unpack_entry(struct packed_git *p, off_t obj_offset, |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2149 | enum object_type *final_type, unsigned long *final_size) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 2150 | { |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 2151 | struct pack_window *w_curs = NULL; |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 2152 | off_t curpos = obj_offset; |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2153 | void *data = NULL; |
| 2154 | unsigned long size; |
| 2155 | enum object_type type; |
| 2156 | struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC]; |
| 2157 | struct unpack_entry_stack_ent *delta_stack = small_delta_stack; |
| 2158 | int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC; |
| 2159 | int base_from_cache = 0; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2160 | |
Karsten Blees | 67dc598 | 2014-07-12 02:01:38 +0200 | [diff] [blame] | 2161 | write_pack_access_log(p, obj_offset); |
Junio C Hamano | 5f44324 | 2011-07-06 19:08:55 -0700 | [diff] [blame] | 2162 | |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2163 | /* PHASE 1: drill down to the innermost base object */ |
| 2164 | for (;;) { |
| 2165 | off_t base_offset; |
| 2166 | int i; |
| 2167 | struct delta_base_cache_entry *ent; |
| 2168 | |
Nguyễn Thái Ngọc Duy | 77965f8 | 2013-09-13 18:03:00 +0700 | [diff] [blame] | 2169 | ent = get_delta_base_cache_entry(p, curpos); |
| 2170 | if (eq_delta_base_cache_entry(ent, p, curpos)) { |
| 2171 | type = ent->type; |
| 2172 | data = ent->data; |
| 2173 | size = ent->size; |
| 2174 | clear_delta_base_cache_entry(ent); |
| 2175 | base_from_cache = 1; |
| 2176 | break; |
| 2177 | } |
| 2178 | |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2179 | if (do_check_packed_object_crc && p->index_version > 1) { |
| 2180 | struct revindex_entry *revidx = find_pack_revindex(p, obj_offset); |
| 2181 | unsigned long len = revidx[1].offset - obj_offset; |
| 2182 | if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) { |
| 2183 | const unsigned char *sha1 = |
| 2184 | nth_packed_object_sha1(p, revidx->nr); |
| 2185 | error("bad packed object CRC for %s", |
| 2186 | sha1_to_hex(sha1)); |
| 2187 | mark_bad_packed_object(p, sha1); |
| 2188 | unuse_pack(&w_curs); |
| 2189 | return NULL; |
| 2190 | } |
Nicolas Pitre | 0e8189e | 2008-10-31 11:31:08 -0400 | [diff] [blame] | 2191 | } |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2192 | |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2193 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
| 2194 | if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA) |
| 2195 | break; |
| 2196 | |
| 2197 | base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset); |
| 2198 | if (!base_offset) { |
| 2199 | error("failed to validate delta base reference " |
| 2200 | "at offset %"PRIuMAX" from %s", |
| 2201 | (uintmax_t)curpos, p->pack_name); |
| 2202 | /* bail to phase 2, in hopes of recovery */ |
| 2203 | data = NULL; |
| 2204 | break; |
| 2205 | } |
| 2206 | |
| 2207 | /* push object, proceed to base */ |
| 2208 | if (delta_stack_nr >= delta_stack_alloc |
| 2209 | && delta_stack == small_delta_stack) { |
| 2210 | delta_stack_alloc = alloc_nr(delta_stack_nr); |
| 2211 | delta_stack = xmalloc(sizeof(*delta_stack)*delta_stack_alloc); |
| 2212 | memcpy(delta_stack, small_delta_stack, |
| 2213 | sizeof(*delta_stack)*delta_stack_nr); |
| 2214 | } else { |
| 2215 | ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc); |
| 2216 | } |
| 2217 | i = delta_stack_nr++; |
| 2218 | delta_stack[i].obj_offset = obj_offset; |
| 2219 | delta_stack[i].curpos = curpos; |
| 2220 | delta_stack[i].size = size; |
| 2221 | |
| 2222 | curpos = obj_offset = base_offset; |
Nicolas Pitre | 0e8189e | 2008-10-31 11:31:08 -0400 | [diff] [blame] | 2223 | } |
| 2224 | |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2225 | /* PHASE 2: handle the base */ |
| 2226 | switch (type) { |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 2227 | case OBJ_OFS_DELTA: |
| 2228 | case OBJ_REF_DELTA: |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2229 | if (data) |
| 2230 | die("BUG in unpack_entry: left loop at a valid delta"); |
Shawn O. Pearce | 4d703a1 | 2006-12-23 02:33:25 -0500 | [diff] [blame] | 2231 | break; |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 2232 | case OBJ_COMMIT: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 2233 | case OBJ_TREE: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 2234 | case OBJ_BLOB: |
Linus Torvalds | a733cb6 | 2005-06-28 14:21:02 -0700 | [diff] [blame] | 2235 | case OBJ_TAG: |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2236 | if (!base_from_cache) |
| 2237 | data = unpack_compressed_entry(p, &w_curs, curpos, size); |
Shawn O. Pearce | 4d703a1 | 2006-12-23 02:33:25 -0500 | [diff] [blame] | 2238 | break; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2239 | default: |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 2240 | data = NULL; |
| 2241 | error("unknown object type %i at offset %"PRIuMAX" in %s", |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2242 | type, (uintmax_t)obj_offset, p->pack_name); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2243 | } |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2244 | |
| 2245 | /* PHASE 3: apply deltas in order */ |
| 2246 | |
| 2247 | /* invariants: |
| 2248 | * 'data' holds the base data, or NULL if there was corruption |
| 2249 | */ |
| 2250 | while (delta_stack_nr) { |
| 2251 | void *delta_data; |
| 2252 | void *base = data; |
| 2253 | unsigned long delta_size, base_size = size; |
| 2254 | int i; |
| 2255 | |
| 2256 | data = NULL; |
| 2257 | |
| 2258 | if (base) |
| 2259 | add_delta_base_cache(p, obj_offset, base, base_size, type); |
| 2260 | |
| 2261 | if (!base) { |
| 2262 | /* |
| 2263 | * We're probably in deep shit, but let's try to fetch |
| 2264 | * the required base anyway from another pack or loose. |
| 2265 | * This is costly but should happen only in the presence |
| 2266 | * of a corrupted pack, and is better than failing outright. |
| 2267 | */ |
| 2268 | struct revindex_entry *revidx; |
| 2269 | const unsigned char *base_sha1; |
| 2270 | revidx = find_pack_revindex(p, obj_offset); |
| 2271 | if (revidx) { |
| 2272 | base_sha1 = nth_packed_object_sha1(p, revidx->nr); |
| 2273 | error("failed to read delta base object %s" |
| 2274 | " at offset %"PRIuMAX" from %s", |
| 2275 | sha1_to_hex(base_sha1), (uintmax_t)obj_offset, |
| 2276 | p->pack_name); |
| 2277 | mark_bad_packed_object(p, base_sha1); |
| 2278 | base = read_object(base_sha1, &type, &base_size); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | i = --delta_stack_nr; |
| 2283 | obj_offset = delta_stack[i].obj_offset; |
| 2284 | curpos = delta_stack[i].curpos; |
| 2285 | delta_size = delta_stack[i].size; |
| 2286 | |
| 2287 | if (!base) |
| 2288 | continue; |
| 2289 | |
| 2290 | delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size); |
| 2291 | |
| 2292 | if (!delta_data) { |
| 2293 | error("failed to unpack compressed delta " |
| 2294 | "at offset %"PRIuMAX" from %s", |
| 2295 | (uintmax_t)curpos, p->pack_name); |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2296 | data = NULL; |
| 2297 | continue; |
| 2298 | } |
| 2299 | |
| 2300 | data = patch_delta(base, base_size, |
| 2301 | delta_data, delta_size, |
| 2302 | &size); |
Jeff King | 1ee886c | 2013-06-14 17:53:34 -0400 | [diff] [blame] | 2303 | |
| 2304 | /* |
| 2305 | * We could not apply the delta; warn the user, but keep going. |
| 2306 | * Our failure will be noticed either in the next iteration of |
| 2307 | * the loop, or if this is the final delta, in the caller when |
| 2308 | * we return NULL. Those code paths will take care of making |
| 2309 | * a more explicit warning and retrying with another copy of |
| 2310 | * the object. |
| 2311 | */ |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2312 | if (!data) |
Jeff King | 1ee886c | 2013-06-14 17:53:34 -0400 | [diff] [blame] | 2313 | error("failed to apply delta"); |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2314 | |
Felipe Contreras | 4b8f772 | 2013-05-30 08:56:21 -0500 | [diff] [blame] | 2315 | free(delta_data); |
Thomas Rast | abe601b | 2013-03-27 21:03:42 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | *final_type = type; |
| 2319 | *final_size = size; |
| 2320 | |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 2321 | unuse_pack(&w_curs); |
Nguyễn Thái Ngọc Duy | 019d1e6 | 2014-02-21 06:47:47 +0700 | [diff] [blame] | 2322 | |
| 2323 | if (delta_stack != small_delta_stack) |
| 2324 | free(delta_stack); |
| 2325 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2326 | return data; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 2329 | const unsigned char *nth_packed_object_sha1(struct packed_git *p, |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 2330 | uint32_t n) |
Junio C Hamano | 9a217f2 | 2005-06-28 14:56:57 -0700 | [diff] [blame] | 2331 | { |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 2332 | const unsigned char *index = p->index_data; |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 2333 | if (!index) { |
| 2334 | if (open_pack_index(p)) |
| 2335 | return NULL; |
| 2336 | index = p->index_data; |
| 2337 | } |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 2338 | if (n >= p->num_objects) |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 2339 | return NULL; |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 2340 | index += 4 * 256; |
| 2341 | if (p->index_version == 1) { |
| 2342 | return index + 24 * n + 4; |
| 2343 | } else { |
| 2344 | index += 8; |
| 2345 | return index + 20 * n; |
| 2346 | } |
| 2347 | } |
| 2348 | |
Nicolas Pitre | 9909323 | 2008-06-24 23:17:12 -0400 | [diff] [blame] | 2349 | off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n) |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 2350 | { |
| 2351 | const unsigned char *index = p->index_data; |
| 2352 | index += 4 * 256; |
| 2353 | if (p->index_version == 1) { |
| 2354 | return ntohl(*((uint32_t *)(index + 24 * n))); |
| 2355 | } else { |
| 2356 | uint32_t off; |
| 2357 | index += 8 + p->num_objects * (20 + 4); |
| 2358 | off = ntohl(*((uint32_t *)(index + 4 * n))); |
| 2359 | if (!(off & 0x80000000)) |
| 2360 | return off; |
| 2361 | index += p->num_objects * 4 + (off & 0x7fffffff) * 8; |
| 2362 | return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) | |
| 2363 | ntohl(*((uint32_t *)(index + 4))); |
| 2364 | } |
Junio C Hamano | 9a217f2 | 2005-06-28 14:56:57 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 2367 | off_t find_pack_entry_one(const unsigned char *sha1, |
Nicolas Pitre | 4305730 | 2006-09-21 00:05:37 -0400 | [diff] [blame] | 2368 | struct packed_git *p) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2369 | { |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 2370 | const uint32_t *level1_ofs = p->index_data; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 2371 | const unsigned char *index = p->index_data; |
Junio C Hamano | 628522e | 2007-12-29 02:05:47 -0800 | [diff] [blame] | 2372 | unsigned hi, lo, stride; |
| 2373 | static int use_lookup = -1; |
| 2374 | static int debug_lookup = -1; |
| 2375 | |
| 2376 | if (debug_lookup < 0) |
| 2377 | debug_lookup = !!getenv("GIT_DEBUG_LOOKUP"); |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 2378 | |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 2379 | if (!index) { |
| 2380 | if (open_pack_index(p)) |
| 2381 | return 0; |
| 2382 | level1_ofs = p->index_data; |
| 2383 | index = p->index_data; |
| 2384 | } |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 2385 | if (p->index_version > 1) { |
| 2386 | level1_ofs += 2; |
| 2387 | index += 8; |
| 2388 | } |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 2389 | index += 4 * 256; |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 2390 | hi = ntohl(level1_ofs[*sha1]); |
| 2391 | lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1])); |
Junio C Hamano | 628522e | 2007-12-29 02:05:47 -0800 | [diff] [blame] | 2392 | if (p->index_version > 1) { |
| 2393 | stride = 20; |
| 2394 | } else { |
| 2395 | stride = 24; |
| 2396 | index += 4; |
| 2397 | } |
| 2398 | |
| 2399 | if (debug_lookup) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 2400 | printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n", |
Junio C Hamano | 628522e | 2007-12-29 02:05:47 -0800 | [diff] [blame] | 2401 | sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects); |
| 2402 | |
| 2403 | if (use_lookup < 0) |
| 2404 | use_lookup = !!getenv("GIT_USE_LOOKUP"); |
| 2405 | if (use_lookup) { |
| 2406 | int pos = sha1_entry_pos(index, stride, 0, |
| 2407 | lo, hi, p->num_objects, sha1); |
| 2408 | if (pos < 0) |
| 2409 | return 0; |
| 2410 | return nth_packed_object_offset(p, pos); |
| 2411 | } |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2412 | |
| 2413 | do { |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 2414 | unsigned mi = (lo + hi) / 2; |
Junio C Hamano | 628522e | 2007-12-29 02:05:47 -0800 | [diff] [blame] | 2415 | int cmp = hashcmp(index + mi * stride, sha1); |
| 2416 | |
| 2417 | if (debug_lookup) |
| 2418 | printf("lo %u hi %u rg %u mi %u\n", |
| 2419 | lo, hi, hi - lo, mi); |
Nicolas Pitre | 4305730 | 2006-09-21 00:05:37 -0400 | [diff] [blame] | 2420 | if (!cmp) |
Nicolas Pitre | 74e34e1 | 2007-04-09 01:06:35 -0400 | [diff] [blame] | 2421 | return nth_packed_object_offset(p, mi); |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2422 | if (cmp > 0) |
| 2423 | hi = mi; |
| 2424 | else |
| 2425 | lo = mi+1; |
| 2426 | } while (lo < hi); |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
Jeff King | 4c08018 | 2011-10-14 14:03:48 -0400 | [diff] [blame] | 2430 | int is_pack_valid(struct packed_git *p) |
Shawn O. Pearce | d131b7a | 2011-03-02 10:01:54 -0800 | [diff] [blame] | 2431 | { |
| 2432 | /* An already open pack is known to be valid. */ |
| 2433 | if (p->pack_fd != -1) |
| 2434 | return 1; |
| 2435 | |
| 2436 | /* If the pack has one window completely covering the |
| 2437 | * file size, the pack is known to be valid even if |
| 2438 | * the descriptor is not currently open. |
| 2439 | */ |
| 2440 | if (p->windows) { |
| 2441 | struct pack_window *w = p->windows; |
| 2442 | |
| 2443 | if (!w->offset && w->len == p->pack_size) |
| 2444 | return 1; |
| 2445 | } |
| 2446 | |
| 2447 | /* Force the pack to open to prove its valid. */ |
| 2448 | return !open_packed_git(p); |
| 2449 | } |
| 2450 | |
Nguyễn Thái Ngọc Duy | 9509973 | 2012-02-01 20:48:54 +0700 | [diff] [blame] | 2451 | static int fill_pack_entry(const unsigned char *sha1, |
| 2452 | struct pack_entry *e, |
| 2453 | struct packed_git *p) |
| 2454 | { |
| 2455 | off_t offset; |
| 2456 | |
| 2457 | if (p->num_bad_objects) { |
| 2458 | unsigned i; |
| 2459 | for (i = 0; i < p->num_bad_objects; i++) |
| 2460 | if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i)) |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
| 2464 | offset = find_pack_entry_one(sha1, p); |
| 2465 | if (!offset) |
| 2466 | return 0; |
| 2467 | |
| 2468 | /* |
| 2469 | * We are about to tell the caller where they can locate the |
| 2470 | * requested object. We better make sure the packfile is |
| 2471 | * still here and can be accessed before supplying that |
| 2472 | * answer, as it may have been deleted since the index was |
| 2473 | * loaded! |
| 2474 | */ |
| 2475 | if (!is_pack_valid(p)) { |
| 2476 | warning("packfile %s cannot be accessed", p->pack_name); |
| 2477 | return 0; |
| 2478 | } |
| 2479 | e->offset = offset; |
| 2480 | e->p = p; |
| 2481 | hashcpy(e->sha1, sha1); |
| 2482 | return 1; |
| 2483 | } |
| 2484 | |
Michael Haggerty | d40d535 | 2014-02-21 17:32:06 +0100 | [diff] [blame] | 2485 | /* |
| 2486 | * Iff a pack file contains the object named by sha1, return true and |
| 2487 | * store its location to e. |
| 2488 | */ |
Brandon Casey | 4d6acb7 | 2009-03-19 22:47:54 -0500 | [diff] [blame] | 2489 | static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2490 | { |
| 2491 | struct packed_git *p; |
Nicolas Pitre | 4305730 | 2006-09-21 00:05:37 -0400 | [diff] [blame] | 2492 | |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2493 | prepare_packed_git(); |
Nicolas Pitre | f7c22cc | 2007-05-30 22:48:13 -0400 | [diff] [blame] | 2494 | if (!packed_git) |
| 2495 | return 0; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2496 | |
Nguyễn Thái Ngọc Duy | c01f51c | 2012-02-01 20:48:55 +0700 | [diff] [blame] | 2497 | if (last_found_pack && fill_pack_entry(sha1, e, last_found_pack)) |
| 2498 | return 1; |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 2499 | |
Nguyễn Thái Ngọc Duy | c01f51c | 2012-02-01 20:48:55 +0700 | [diff] [blame] | 2500 | for (p = packed_git; p; p = p->next) { |
Michael Haggerty | 1b1005d | 2014-02-21 17:32:04 +0100 | [diff] [blame] | 2501 | if (p == last_found_pack) |
| 2502 | continue; /* we already checked this one */ |
Nicolas Pitre | f7c22cc | 2007-05-30 22:48:13 -0400 | [diff] [blame] | 2503 | |
Michael Haggerty | 1b1005d | 2014-02-21 17:32:04 +0100 | [diff] [blame] | 2504 | if (fill_pack_entry(sha1, e, p)) { |
| 2505 | last_found_pack = p; |
| 2506 | return 1; |
| 2507 | } |
Nguyễn Thái Ngọc Duy | c01f51c | 2012-02-01 20:48:55 +0700 | [diff] [blame] | 2508 | } |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2509 | return 0; |
| 2510 | } |
| 2511 | |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 2512 | struct packed_git *find_sha1_pack(const unsigned char *sha1, |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 2513 | struct packed_git *packs) |
| 2514 | { |
| 2515 | struct packed_git *p; |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 2516 | |
| 2517 | for (p = packs; p; p = p->next) { |
Nicolas Pitre | 4305730 | 2006-09-21 00:05:37 -0400 | [diff] [blame] | 2518 | if (find_pack_entry_one(sha1, p)) |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 2519 | return p; |
| 2520 | } |
| 2521 | return NULL; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2522 | |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 2523 | } |
| 2524 | |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 2525 | static int sha1_loose_object_info(const unsigned char *sha1, |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2526 | struct object_info *oi) |
Junio C Hamano | 65c2e0c | 2005-06-02 15:20:54 -0700 | [diff] [blame] | 2527 | { |
Junio C Hamano | 36e4d74 | 2005-06-27 03:34:06 -0700 | [diff] [blame] | 2528 | int status; |
Junio C Hamano | 65c2e0c | 2005-06-02 15:20:54 -0700 | [diff] [blame] | 2529 | unsigned long mapsize, size; |
| 2530 | void *map; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 2531 | git_zstream stream; |
Nicolas Pitre | d65a16f | 2007-02-26 14:55:55 -0500 | [diff] [blame] | 2532 | char hdr[32]; |
Junio C Hamano | 65c2e0c | 2005-06-02 15:20:54 -0700 | [diff] [blame] | 2533 | |
Jeff King | 5d642e7 | 2013-12-21 09:24:20 -0500 | [diff] [blame] | 2534 | if (oi->delta_base_sha1) |
| 2535 | hashclr(oi->delta_base_sha1); |
| 2536 | |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 2537 | /* |
| 2538 | * If we don't care about type or size, then we don't |
Junio C Hamano | 4ef8d1d | 2013-11-06 10:00:57 -0800 | [diff] [blame] | 2539 | * need to look inside the object at all. Note that we |
| 2540 | * do not optimize out the stat call, even if the |
| 2541 | * caller doesn't care about the disk-size, since our |
| 2542 | * return value implicitly indicates whether the |
| 2543 | * object even exists. |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 2544 | */ |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2545 | if (!oi->typep && !oi->sizep) { |
Junio C Hamano | 4ef8d1d | 2013-11-06 10:00:57 -0800 | [diff] [blame] | 2546 | struct stat st; |
| 2547 | if (stat_sha1_file(sha1, &st) < 0) |
| 2548 | return -1; |
| 2549 | if (oi->disk_sizep) |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2550 | *oi->disk_sizep = st.st_size; |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 2551 | return 0; |
| 2552 | } |
| 2553 | |
Junio C Hamano | bb6b8e4 | 2006-07-17 15:04:47 -0700 | [diff] [blame] | 2554 | map = map_sha1_file(sha1, &mapsize); |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2555 | if (!map) |
Thomas Rast | dbea72a | 2013-05-30 22:00:22 +0200 | [diff] [blame] | 2556 | return -1; |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2557 | if (oi->disk_sizep) |
| 2558 | *oi->disk_sizep = mapsize; |
Junio C Hamano | 36e4d74 | 2005-06-27 03:34:06 -0700 | [diff] [blame] | 2559 | if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) |
| 2560 | status = error("unable to unpack %s header", |
| 2561 | sha1_to_hex(sha1)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2562 | else if ((status = parse_sha1_header(hdr, &size)) < 0) |
Junio C Hamano | 36e4d74 | 2005-06-27 03:34:06 -0700 | [diff] [blame] | 2563 | status = error("unable to parse %s header", sha1_to_hex(sha1)); |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2564 | else if (oi->sizep) |
| 2565 | *oi->sizep = size; |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 2566 | git_inflate_end(&stream); |
Junio C Hamano | 65c2e0c | 2005-06-02 15:20:54 -0700 | [diff] [blame] | 2567 | munmap(map, mapsize); |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2568 | if (oi->typep) |
| 2569 | *oi->typep = status; |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 2570 | return 0; |
Junio C Hamano | 65c2e0c | 2005-06-02 15:20:54 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Christian Couder | de7b5d6 | 2013-12-11 08:46:07 +0100 | [diff] [blame] | 2573 | int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags) |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2574 | { |
Nguyễn Thái Ngọc Duy | c4d9986 | 2011-02-05 21:03:02 +0700 | [diff] [blame] | 2575 | struct cached_object *co; |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2576 | struct pack_entry e; |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2577 | int rtype; |
Christian Couder | 1f7117e | 2013-12-11 08:46:09 +0100 | [diff] [blame] | 2578 | const unsigned char *real = lookup_replace_object_extended(sha1, flags); |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2579 | |
Christian Couder | 1f7117e | 2013-12-11 08:46:09 +0100 | [diff] [blame] | 2580 | co = find_cached_object(real); |
Nguyễn Thái Ngọc Duy | c4d9986 | 2011-02-05 21:03:02 +0700 | [diff] [blame] | 2581 | if (co) { |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2582 | if (oi->typep) |
| 2583 | *(oi->typep) = co->type; |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2584 | if (oi->sizep) |
| 2585 | *(oi->sizep) = co->size; |
Jeff King | 161f00e | 2013-07-07 06:04:00 -0400 | [diff] [blame] | 2586 | if (oi->disk_sizep) |
| 2587 | *(oi->disk_sizep) = 0; |
Jeff King | 5d642e7 | 2013-12-21 09:24:20 -0500 | [diff] [blame] | 2588 | if (oi->delta_base_sha1) |
| 2589 | hashclr(oi->delta_base_sha1); |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2590 | oi->whence = OI_CACHED; |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2591 | return 0; |
Nguyễn Thái Ngọc Duy | c4d9986 | 2011-02-05 21:03:02 +0700 | [diff] [blame] | 2592 | } |
| 2593 | |
Christian Couder | 1f7117e | 2013-12-11 08:46:09 +0100 | [diff] [blame] | 2594 | if (!find_pack_entry(real, &e)) { |
Steven Grimm | ddd63e6 | 2008-08-05 13:08:41 -0700 | [diff] [blame] | 2595 | /* Most likely it's a loose object. */ |
Christian Couder | 1f7117e | 2013-12-11 08:46:09 +0100 | [diff] [blame] | 2596 | if (!sha1_loose_object_info(real, oi)) { |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2597 | oi->whence = OI_LOOSE; |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2598 | return 0; |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2599 | } |
Steven Grimm | ddd63e6 | 2008-08-05 13:08:41 -0700 | [diff] [blame] | 2600 | |
| 2601 | /* Not a loose object; someone else may have just packed it. */ |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2602 | reprepare_packed_git(); |
Christian Couder | 1f7117e | 2013-12-11 08:46:09 +0100 | [diff] [blame] | 2603 | if (!find_pack_entry(real, &e)) |
Jeff King | 052fe5e | 2013-07-12 02:30:48 -0400 | [diff] [blame] | 2604 | return -1; |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2605 | } |
Nicolas Pitre | 3d77d87 | 2008-10-29 19:02:47 -0400 | [diff] [blame] | 2606 | |
Jeff King | 23c339c | 2013-07-12 02:37:53 -0400 | [diff] [blame] | 2607 | rtype = packed_object_info(e.p, e.offset, oi); |
Jeff King | 412916e | 2013-07-12 02:32:25 -0400 | [diff] [blame] | 2608 | if (rtype < 0) { |
Christian Couder | 1f7117e | 2013-12-11 08:46:09 +0100 | [diff] [blame] | 2609 | mark_bad_packed_object(e.p, real); |
| 2610 | return sha1_object_info_extended(real, oi, 0); |
Junio C Hamano | 5266d36 | 2011-05-13 13:20:43 -0700 | [diff] [blame] | 2611 | } else if (in_delta_base_cache(e.p, e.offset)) { |
| 2612 | oi->whence = OI_DBCACHED; |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2613 | } else { |
| 2614 | oi->whence = OI_PACKED; |
| 2615 | oi->u.packed.offset = e.offset; |
| 2616 | oi->u.packed.pack = e.p; |
| 2617 | oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA || |
| 2618 | rtype == OBJ_OFS_DELTA); |
Nicolas Pitre | 3d77d87 | 2008-10-29 19:02:47 -0400 | [diff] [blame] | 2619 | } |
| 2620 | |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2621 | return 0; |
Johannes Schindelin | f0df4ed | 2006-11-28 00:18:55 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
Christian Couder | 3fc0dca | 2013-10-27 00:34:30 +0200 | [diff] [blame] | 2624 | /* returns enum object_type or negative */ |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2625 | int sha1_object_info(const unsigned char *sha1, unsigned long *sizep) |
| 2626 | { |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2627 | enum object_type type; |
Ramsay Jones | d099b71 | 2013-07-18 21:25:50 +0100 | [diff] [blame] | 2628 | struct object_info oi = {NULL}; |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2629 | |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2630 | oi.typep = &type; |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2631 | oi.sizep = sizep; |
Christian Couder | de7b5d6 | 2013-12-11 08:46:07 +0100 | [diff] [blame] | 2632 | if (sha1_object_info_extended(sha1, &oi, LOOKUP_REPLACE_OBJECT) < 0) |
Jeff King | 5b08640 | 2013-07-12 02:34:57 -0400 | [diff] [blame] | 2633 | return -1; |
| 2634 | return type; |
Junio C Hamano | 9a49059 | 2011-05-12 15:51:38 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2637 | static void *read_packed_sha1(const unsigned char *sha1, |
| 2638 | enum object_type *type, unsigned long *size) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2639 | { |
| 2640 | struct pack_entry e; |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 2641 | void *data; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2642 | |
Junio C Hamano | cd673c1 | 2009-02-27 23:15:53 -0800 | [diff] [blame] | 2643 | if (!find_pack_entry(sha1, &e)) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2644 | return NULL; |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 2645 | data = cache_or_unpack_entry(e.p, e.offset, size, type, 1); |
| 2646 | if (!data) { |
| 2647 | /* |
| 2648 | * We're probably in deep shit, but let's try to fetch |
| 2649 | * the required object anyway from another pack or loose. |
| 2650 | * This should happen only in the presence of a corrupted |
| 2651 | * pack, and is better than failing outright. |
| 2652 | */ |
| 2653 | error("failed to read object %s at offset %"PRIuMAX" from %s", |
| 2654 | sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name); |
| 2655 | mark_bad_packed_object(e.p, sha1); |
Nicolas Pitre | ac93910 | 2008-07-14 21:46:48 -0400 | [diff] [blame] | 2656 | data = read_object(sha1, type, size); |
Nicolas Pitre | 8eca0b4 | 2008-06-23 21:23:39 -0400 | [diff] [blame] | 2657 | } |
| 2658 | return data; |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2661 | int pretend_sha1_file(void *buf, unsigned long len, enum object_type type, |
| 2662 | unsigned char *sha1) |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2663 | { |
| 2664 | struct cached_object *co; |
| 2665 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2666 | hash_sha1_file(buf, len, typename(type), sha1); |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2667 | if (has_sha1_file(sha1) || find_cached_object(sha1)) |
| 2668 | return 0; |
Dmitry S. Dolzhenko | c735396 | 2014-03-04 02:32:02 +0400 | [diff] [blame] | 2669 | ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc); |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2670 | co = &cached_objects[cached_object_nr++]; |
| 2671 | co->size = len; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2672 | co->type = type; |
Junio C Hamano | efa13f7 | 2007-02-15 17:02:06 -0800 | [diff] [blame] | 2673 | co->buf = xmalloc(len); |
| 2674 | memcpy(co->buf, buf, len); |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2675 | hashcpy(co->sha1, sha1); |
| 2676 | return 0; |
| 2677 | } |
| 2678 | |
Christian Couder | c2c5b27 | 2009-01-12 18:42:24 +0100 | [diff] [blame] | 2679 | static void *read_object(const unsigned char *sha1, enum object_type *type, |
| 2680 | unsigned long *size) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2681 | { |
| 2682 | unsigned long mapsize; |
| 2683 | void *map, *buf; |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2684 | struct cached_object *co; |
| 2685 | |
| 2686 | co = find_cached_object(sha1); |
| 2687 | if (co) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2688 | *type = co->type; |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2689 | *size = co->size; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 2690 | return xmemdupz(co->buf, co->size); |
Junio C Hamano | d66b37b | 2007-02-04 21:42:38 -0800 | [diff] [blame] | 2691 | } |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2692 | |
Peter Eriksen | 8276c00 | 2007-01-22 21:29:45 +0100 | [diff] [blame] | 2693 | buf = read_packed_sha1(sha1, type, size); |
| 2694 | if (buf) |
| 2695 | return buf; |
Junio C Hamano | bb6b8e4 | 2006-07-17 15:04:47 -0700 | [diff] [blame] | 2696 | map = map_sha1_file(sha1, &mapsize); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2697 | if (map) { |
Junio C Hamano | 7efbff7 | 2007-03-05 00:21:37 -0800 | [diff] [blame] | 2698 | buf = unpack_sha1_file(map, mapsize, type, size, sha1); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2699 | munmap(map, mapsize); |
| 2700 | return buf; |
| 2701 | } |
Jeff King | 637cdd9 | 2006-06-02 11:32:23 -0400 | [diff] [blame] | 2702 | reprepare_packed_git(); |
Peter Eriksen | 8276c00 | 2007-01-22 21:29:45 +0100 | [diff] [blame] | 2703 | return read_packed_sha1(sha1, type, size); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2706 | /* |
| 2707 | * This function dies on corrupt objects; the callers who want to |
| 2708 | * deal with them should arrange to call read_object() and give error |
| 2709 | * messages themselves. |
| 2710 | */ |
Junio C Hamano | 5bf29b9 | 2011-05-15 12:54:54 -0700 | [diff] [blame] | 2711 | void *read_sha1_file_extended(const unsigned char *sha1, |
| 2712 | enum object_type *type, |
| 2713 | unsigned long *size, |
| 2714 | unsigned flag) |
Nicolas Pitre | ac93910 | 2008-07-14 21:46:48 -0400 | [diff] [blame] | 2715 | { |
Junio C Hamano | 3ba7a06 | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2716 | void *data; |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2717 | const struct packed_git *p; |
Christian Couder | bf93eea | 2013-12-11 08:46:06 +0100 | [diff] [blame] | 2718 | const unsigned char *repl = lookup_replace_object_extended(sha1, flag); |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2719 | |
Junio C Hamano | 3ba7a06 | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2720 | errno = 0; |
| 2721 | data = read_object(repl, type, size); |
Junio C Hamano | 4bbf5a2 | 2011-05-15 12:54:52 -0700 | [diff] [blame] | 2722 | if (data) |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2723 | return data; |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 2724 | |
Björn Steinbrink | 25f3af3 | 2011-01-20 21:12:20 +0100 | [diff] [blame] | 2725 | if (errno && errno != ENOENT) |
Junio C Hamano | 3ba7a06 | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2726 | die_errno("failed to read object %s", sha1_to_hex(sha1)); |
| 2727 | |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 2728 | /* die if we replaced an object with one that does not exist */ |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2729 | if (repl != sha1) |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 2730 | die("replacement %s not found for %s", |
| 2731 | sha1_to_hex(repl), sha1_to_hex(sha1)); |
| 2732 | |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2733 | if (has_loose_object(repl)) { |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 2734 | const char *path = sha1_file_name(sha1); |
| 2735 | |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2736 | die("loose object %s (stored in %s) is corrupt", |
| 2737 | sha1_to_hex(repl), path); |
Ævar Arnfjörð Bjarmason | e8b15e6 | 2010-06-10 12:47:01 +0000 | [diff] [blame] | 2738 | } |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 2739 | |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2740 | if ((p = has_packed_and_bad(repl)) != NULL) |
| 2741 | die("packed object %s (stored in %s) is corrupt", |
| 2742 | sha1_to_hex(repl), p->pack_name); |
Christian Couder | f5552ae | 2009-01-23 10:07:01 +0100 | [diff] [blame] | 2743 | |
Junio C Hamano | b6c4cec | 2010-10-28 11:13:06 -0700 | [diff] [blame] | 2744 | return NULL; |
Nicolas Pitre | ac93910 | 2008-07-14 21:46:48 -0400 | [diff] [blame] | 2745 | } |
| 2746 | |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2747 | void *read_object_with_reference(const unsigned char *sha1, |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2748 | const char *required_type_name, |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2749 | unsigned long *size, |
| 2750 | unsigned char *actual_sha1_return) |
Junio C Hamano | f4913f9 | 2005-04-20 18:06:49 -0700 | [diff] [blame] | 2751 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2752 | enum object_type type, required_type; |
Junio C Hamano | f4913f9 | 2005-04-20 18:06:49 -0700 | [diff] [blame] | 2753 | void *buffer; |
| 2754 | unsigned long isize; |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2755 | unsigned char actual_sha1[20]; |
Junio C Hamano | f4913f9 | 2005-04-20 18:06:49 -0700 | [diff] [blame] | 2756 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2757 | required_type = type_from_string(required_type_name); |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 2758 | hashcpy(actual_sha1, sha1); |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2759 | while (1) { |
| 2760 | int ref_length = -1; |
| 2761 | const char *ref_type = NULL; |
Junio C Hamano | f4913f9 | 2005-04-20 18:06:49 -0700 | [diff] [blame] | 2762 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2763 | buffer = read_sha1_file(actual_sha1, &type, &isize); |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2764 | if (!buffer) |
| 2765 | return NULL; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2766 | if (type == required_type) { |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2767 | *size = isize; |
| 2768 | if (actual_sha1_return) |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 2769 | hashcpy(actual_sha1_return, actual_sha1); |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2770 | return buffer; |
| 2771 | } |
| 2772 | /* Handle references */ |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2773 | else if (type == OBJ_COMMIT) |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2774 | ref_type = "tree "; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 2775 | else if (type == OBJ_TAG) |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2776 | ref_type = "object "; |
| 2777 | else { |
| 2778 | free(buffer); |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | ref_length = strlen(ref_type); |
| 2782 | |
Martin Koegler | 50974ec | 2008-02-18 21:47:52 +0100 | [diff] [blame] | 2783 | if (ref_length + 40 > isize || |
| 2784 | memcmp(buffer, ref_type, ref_length) || |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 2785 | get_sha1_hex((char *) buffer + ref_length, actual_sha1)) { |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2786 | free(buffer); |
| 2787 | return NULL; |
| 2788 | } |
Sergey Vlasov | 1cf58e7 | 2005-08-08 22:44:43 +0400 | [diff] [blame] | 2789 | free(buffer); |
Junio C Hamano | 40469ee | 2005-04-28 16:42:27 -0700 | [diff] [blame] | 2790 | /* Now we have the ID of the referred-to object in |
| 2791 | * actual_sha1. Check again. */ |
Junio C Hamano | f4913f9 | 2005-04-20 18:06:49 -0700 | [diff] [blame] | 2792 | } |
Junio C Hamano | f4913f9 | 2005-04-20 18:06:49 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
Nicolas Pitre | ce9fbf1 | 2007-03-20 16:02:09 -0400 | [diff] [blame] | 2795 | static void write_sha1_file_prepare(const void *buf, unsigned long len, |
Rene Scharfe | 972a915 | 2006-10-15 14:02:03 +0200 | [diff] [blame] | 2796 | const char *type, unsigned char *sha1, |
Nicolas Pitre | d65a16f | 2007-02-26 14:55:55 -0500 | [diff] [blame] | 2797 | char *hdr, int *hdrlen) |
Junio C Hamano | d410c0f | 2005-06-27 19:03:13 -0700 | [diff] [blame] | 2798 | { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 2799 | git_SHA_CTX c; |
Junio C Hamano | d410c0f | 2005-06-27 19:03:13 -0700 | [diff] [blame] | 2800 | |
| 2801 | /* Generate the header */ |
Nicolas Pitre | d65a16f | 2007-02-26 14:55:55 -0500 | [diff] [blame] | 2802 | *hdrlen = sprintf(hdr, "%s %lu", type, len)+1; |
Junio C Hamano | d410c0f | 2005-06-27 19:03:13 -0700 | [diff] [blame] | 2803 | |
| 2804 | /* Sha1.. */ |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 2805 | git_SHA1_Init(&c); |
| 2806 | git_SHA1_Update(&c, hdr, *hdrlen); |
| 2807 | git_SHA1_Update(&c, buf, len); |
| 2808 | git_SHA1_Final(sha1, &c); |
Junio C Hamano | d410c0f | 2005-06-27 19:03:13 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2811 | /* |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 2812 | * Move the just written object into its final resting place. |
| 2813 | * NEEDSWORK: this should be renamed to finalize_temp_file() as |
| 2814 | * "moving" is only a part of what it does, when no patch between |
| 2815 | * master to pu changes the call sites of this function. |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2816 | */ |
Junio C Hamano | 839837b | 2006-09-01 00:17:47 -0700 | [diff] [blame] | 2817 | int move_temp_to_file(const char *tmpfile, const char *filename) |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2818 | { |
Thomas Rast | e32c0a9 | 2008-09-19 00:24:46 +0200 | [diff] [blame] | 2819 | int ret = 0; |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 2820 | |
Johannes Schindelin | 348df16 | 2009-04-28 00:32:25 +0200 | [diff] [blame] | 2821 | if (object_creation_mode == OBJECT_CREATION_USES_RENAMES) |
Johannes Schindelin | be66a6c | 2009-04-25 11:57:14 +0200 | [diff] [blame] | 2822 | goto try_rename; |
| 2823 | else if (link(tmpfile, filename)) |
Thomas Rast | e32c0a9 | 2008-09-19 00:24:46 +0200 | [diff] [blame] | 2824 | ret = errno; |
Linus Torvalds | 7ebb6fc | 2005-10-26 10:27:36 -0700 | [diff] [blame] | 2825 | |
| 2826 | /* |
| 2827 | * Coda hack - coda doesn't like cross-directory links, |
| 2828 | * so we fall back to a rename, which will mean that it |
| 2829 | * won't be able to check collisions, but that's not a |
| 2830 | * big deal. |
| 2831 | * |
| 2832 | * The same holds for FAT formatted media. |
| 2833 | * |
Junio C Hamano | 3be1f18 | 2009-03-27 23:14:39 -0700 | [diff] [blame] | 2834 | * When this succeeds, we just return. We have nothing |
Linus Torvalds | 7ebb6fc | 2005-10-26 10:27:36 -0700 | [diff] [blame] | 2835 | * left to unlink. |
| 2836 | */ |
| 2837 | if (ret && ret != EEXIST) { |
Johannes Schindelin | be66a6c | 2009-04-25 11:57:14 +0200 | [diff] [blame] | 2838 | try_rename: |
Linus Torvalds | 7ebb6fc | 2005-10-26 10:27:36 -0700 | [diff] [blame] | 2839 | if (!rename(tmpfile, filename)) |
Junio C Hamano | 3be1f18 | 2009-03-27 23:14:39 -0700 | [diff] [blame] | 2840 | goto out; |
Johannes Schindelin | 9e48b38 | 2005-10-26 01:41:20 +0200 | [diff] [blame] | 2841 | ret = errno; |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2842 | } |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 2843 | unlink_or_warn(tmpfile); |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2844 | if (ret) { |
| 2845 | if (ret != EEXIST) { |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 2846 | return error("unable to write sha1 filename %s: %s", filename, strerror(ret)); |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2847 | } |
| 2848 | /* FIXME!!! Collision check here ? */ |
| 2849 | } |
| 2850 | |
Junio C Hamano | 3be1f18 | 2009-03-27 23:14:39 -0700 | [diff] [blame] | 2851 | out: |
Matthieu Moy | 5256b00 | 2010-02-22 23:32:16 +0100 | [diff] [blame] | 2852 | if (adjust_shared_perm(filename)) |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 2853 | return error("unable to set permission to '%s'", filename); |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 2854 | return 0; |
| 2855 | } |
| 2856 | |
Linus Torvalds | 4d54815 | 2006-05-24 08:30:54 -0700 | [diff] [blame] | 2857 | static int write_buffer(int fd, const void *buf, size_t len) |
| 2858 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 2859 | if (write_in_full(fd, buf, len) < 0) |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 2860 | return error("file write error (%s)", strerror(errno)); |
Linus Torvalds | 4d54815 | 2006-05-24 08:30:54 -0700 | [diff] [blame] | 2861 | return 0; |
| 2862 | } |
| 2863 | |
Nicolas Pitre | ce9fbf1 | 2007-03-20 16:02:09 -0400 | [diff] [blame] | 2864 | int hash_sha1_file(const void *buf, unsigned long len, const char *type, |
Rene Scharfe | abdc3fc | 2006-10-14 12:45:36 +0200 | [diff] [blame] | 2865 | unsigned char *sha1) |
| 2866 | { |
Nicolas Pitre | d65a16f | 2007-02-26 14:55:55 -0500 | [diff] [blame] | 2867 | char hdr[32]; |
Rene Scharfe | abdc3fc | 2006-10-14 12:45:36 +0200 | [diff] [blame] | 2868 | int hdrlen; |
| 2869 | write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); |
| 2870 | return 0; |
| 2871 | } |
| 2872 | |
Linus Torvalds | e9039dd | 2008-06-10 18:47:18 -0700 | [diff] [blame] | 2873 | /* Finalize a file on disk, and close it. */ |
| 2874 | static void close_sha1_file(int fd) |
| 2875 | { |
Linus Torvalds | aafe9fb | 2008-06-18 15:18:44 -0700 | [diff] [blame] | 2876 | if (fsync_object_files) |
| 2877 | fsync_or_die(fd, "sha1 file"); |
Linus Torvalds | e9039dd | 2008-06-10 18:47:18 -0700 | [diff] [blame] | 2878 | if (close(fd) != 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 2879 | die_errno("error when closing sha1 file"); |
Linus Torvalds | e9039dd | 2008-06-10 18:47:18 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
Linus Torvalds | 5723fe7 | 2008-06-14 10:50:12 -0700 | [diff] [blame] | 2882 | /* Size of directory component, including the ending '/' */ |
| 2883 | static inline int directory_size(const char *filename) |
| 2884 | { |
| 2885 | const char *s = strrchr(filename, '/'); |
| 2886 | if (!s) |
| 2887 | return 0; |
| 2888 | return s - filename + 1; |
| 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | * This creates a temporary file in the same directory as the final |
| 2893 | * 'filename' |
| 2894 | * |
| 2895 | * We want to avoid cross-directory filename renames, because those |
| 2896 | * can have problems on various filesystems (FAT, NFS, Coda). |
| 2897 | */ |
| 2898 | static int create_tmpfile(char *buffer, size_t bufsiz, const char *filename) |
| 2899 | { |
| 2900 | int fd, dirlen = directory_size(filename); |
| 2901 | |
| 2902 | if (dirlen + 20 > bufsiz) { |
| 2903 | errno = ENAMETOOLONG; |
| 2904 | return -1; |
| 2905 | } |
| 2906 | memcpy(buffer, filename, dirlen); |
| 2907 | strcpy(buffer + dirlen, "tmp_obj_XXXXXX"); |
Matthieu Moy | 5256b00 | 2010-02-22 23:32:16 +0100 | [diff] [blame] | 2908 | fd = git_mkstemp_mode(buffer, 0444); |
Joey Hess | cbacbf4 | 2008-11-20 13:56:28 -0500 | [diff] [blame] | 2909 | if (fd < 0 && dirlen && errno == ENOENT) { |
Linus Torvalds | 5723fe7 | 2008-06-14 10:50:12 -0700 | [diff] [blame] | 2910 | /* Make sure the directory exists */ |
Patrick Higgins | 6ff6af6 | 2008-06-23 15:33:41 -0600 | [diff] [blame] | 2911 | memcpy(buffer, filename, dirlen); |
Linus Torvalds | 5723fe7 | 2008-06-14 10:50:12 -0700 | [diff] [blame] | 2912 | buffer[dirlen-1] = 0; |
Johan Herland | b2476a6 | 2013-10-27 12:35:43 +0100 | [diff] [blame] | 2913 | if (mkdir(buffer, 0777) && errno != EEXIST) |
| 2914 | return -1; |
| 2915 | if (adjust_shared_perm(buffer)) |
Linus Torvalds | 5723fe7 | 2008-06-14 10:50:12 -0700 | [diff] [blame] | 2916 | return -1; |
| 2917 | |
| 2918 | /* Try again */ |
| 2919 | strcpy(buffer + dirlen - 1, "/tmp_obj_XXXXXX"); |
Matthieu Moy | 5256b00 | 2010-02-22 23:32:16 +0100 | [diff] [blame] | 2920 | fd = git_mkstemp_mode(buffer, 0444); |
Linus Torvalds | 5723fe7 | 2008-06-14 10:50:12 -0700 | [diff] [blame] | 2921 | } |
| 2922 | return fd; |
| 2923 | } |
| 2924 | |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 2925 | static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen, |
Jeff King | c00e657 | 2010-04-01 20:03:18 -0400 | [diff] [blame] | 2926 | const void *buf, unsigned long len, time_t mtime) |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2927 | { |
Jeff King | 915308b | 2009-01-29 00:56:34 -0500 | [diff] [blame] | 2928 | int fd, ret; |
Nicolas Pitre | 9892beb | 2010-02-20 23:27:31 -0500 | [diff] [blame] | 2929 | unsigned char compressed[4096]; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 2930 | git_zstream stream; |
Nicolas Pitre | 748af44 | 2010-02-21 15:48:06 -0500 | [diff] [blame] | 2931 | git_SHA_CTX c; |
| 2932 | unsigned char parano_sha1[20]; |
Ævar Arnfjörð Bjarmason | ab1900a | 2011-12-21 01:18:21 +0000 | [diff] [blame] | 2933 | static char tmp_file[PATH_MAX]; |
Michael Haggerty | 30d6c6e | 2014-02-21 17:32:05 +0100 | [diff] [blame] | 2934 | const char *filename = sha1_file_name(sha1); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 2935 | |
Ævar Arnfjörð Bjarmason | ab1900a | 2011-12-21 01:18:21 +0000 | [diff] [blame] | 2936 | fd = create_tmpfile(tmp_file, sizeof(tmp_file), filename); |
Linus Torvalds | aac1794 | 2005-05-03 11:46:16 -0700 | [diff] [blame] | 2937 | if (fd < 0) { |
Sam Vilain | 3524357 | 2008-11-14 20:19:34 +1300 | [diff] [blame] | 2938 | if (errno == EACCES) |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 2939 | return error("insufficient permission for adding an object to repository database %s", get_object_directory()); |
Petr Baudis | 916d081 | 2006-11-09 13:52:05 +0100 | [diff] [blame] | 2940 | else |
Pete Wyckoff | 5eaeda7 | 2012-04-29 20:28:46 -0400 | [diff] [blame] | 2941 | return error("unable to create temporary file: %s", strerror(errno)); |
Linus Torvalds | aac1794 | 2005-05-03 11:46:16 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2944 | /* Set it up */ |
| 2945 | memset(&stream, 0, sizeof(stream)); |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 2946 | git_deflate_init(&stream, zlib_compression_level); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2947 | stream.next_out = compressed; |
Nicolas Pitre | 9892beb | 2010-02-20 23:27:31 -0500 | [diff] [blame] | 2948 | stream.avail_out = sizeof(compressed); |
Nicolas Pitre | 748af44 | 2010-02-21 15:48:06 -0500 | [diff] [blame] | 2949 | git_SHA1_Init(&c); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 2950 | |
| 2951 | /* First header.. */ |
Nicolas Pitre | d65a16f | 2007-02-26 14:55:55 -0500 | [diff] [blame] | 2952 | stream.next_in = (unsigned char *)hdr; |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 2953 | stream.avail_in = hdrlen; |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 2954 | while (git_deflate(&stream, 0) == Z_OK) |
| 2955 | ; /* nothing */ |
Nicolas Pitre | 748af44 | 2010-02-21 15:48:06 -0500 | [diff] [blame] | 2956 | git_SHA1_Update(&c, hdr, hdrlen); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 2957 | |
| 2958 | /* Then the data itself.. */ |
Jeff King | c00e657 | 2010-04-01 20:03:18 -0400 | [diff] [blame] | 2959 | stream.next_in = (void *)buf; |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 2960 | stream.avail_in = len; |
Nicolas Pitre | 9892beb | 2010-02-20 23:27:31 -0500 | [diff] [blame] | 2961 | do { |
Nicolas Pitre | 748af44 | 2010-02-21 15:48:06 -0500 | [diff] [blame] | 2962 | unsigned char *in0 = stream.next_in; |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 2963 | ret = git_deflate(&stream, Z_FINISH); |
Nicolas Pitre | 748af44 | 2010-02-21 15:48:06 -0500 | [diff] [blame] | 2964 | git_SHA1_Update(&c, in0, stream.next_in - in0); |
Nicolas Pitre | 9892beb | 2010-02-20 23:27:31 -0500 | [diff] [blame] | 2965 | if (write_buffer(fd, compressed, stream.next_out - compressed) < 0) |
| 2966 | die("unable to write sha1 file"); |
| 2967 | stream.next_out = compressed; |
| 2968 | stream.avail_out = sizeof(compressed); |
| 2969 | } while (ret == Z_OK); |
| 2970 | |
Linus Torvalds | ac54c27 | 2007-03-20 11:38:34 -0700 | [diff] [blame] | 2971 | if (ret != Z_STREAM_END) |
| 2972 | die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret); |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 2973 | ret = git_deflate_end_gently(&stream); |
Linus Torvalds | ac54c27 | 2007-03-20 11:38:34 -0700 | [diff] [blame] | 2974 | if (ret != Z_OK) |
| 2975 | die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret); |
Nicolas Pitre | 748af44 | 2010-02-21 15:48:06 -0500 | [diff] [blame] | 2976 | git_SHA1_Final(parano_sha1, &c); |
| 2977 | if (hashcmp(sha1, parano_sha1) != 0) |
| 2978 | die("confused by unstable object source data for %s", sha1_to_hex(sha1)); |
Linus Torvalds | ac54c27 | 2007-03-20 11:38:34 -0700 | [diff] [blame] | 2979 | |
Linus Torvalds | e9039dd | 2008-06-10 18:47:18 -0700 | [diff] [blame] | 2980 | close_sha1_file(fd); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2981 | |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 2982 | if (mtime) { |
| 2983 | struct utimbuf utb; |
| 2984 | utb.actime = mtime; |
| 2985 | utb.modtime = mtime; |
Ævar Arnfjörð Bjarmason | ab1900a | 2011-12-21 01:18:21 +0000 | [diff] [blame] | 2986 | if (utime(tmp_file, &utb) < 0) |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 2987 | warning("failed utime() on %s: %s", |
Ævar Arnfjörð Bjarmason | ab1900a | 2011-12-21 01:18:21 +0000 | [diff] [blame] | 2988 | tmp_file, strerror(errno)); |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 2989 | } |
| 2990 | |
Ævar Arnfjörð Bjarmason | ab1900a | 2011-12-21 01:18:21 +0000 | [diff] [blame] | 2991 | return move_temp_to_file(tmp_file, filename); |
Linus Torvalds | 0fcfd16 | 2005-04-18 13:04:43 -0700 | [diff] [blame] | 2992 | } |
Daniel Barkalow | 8237b18 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 2993 | |
Jeff King | 33d4221 | 2014-10-15 18:42:22 -0400 | [diff] [blame] | 2994 | static int freshen_loose_object(const unsigned char *sha1) |
| 2995 | { |
| 2996 | return check_and_freshen(sha1, 1); |
| 2997 | } |
| 2998 | |
| 2999 | static int freshen_packed_object(const unsigned char *sha1) |
| 3000 | { |
| 3001 | struct pack_entry e; |
| 3002 | return find_pack_entry(sha1, &e) && freshen_file(e.p->pack_name); |
| 3003 | } |
| 3004 | |
Jeff King | c00e657 | 2010-04-01 20:03:18 -0400 | [diff] [blame] | 3005 | int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *returnsha1) |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 3006 | { |
| 3007 | unsigned char sha1[20]; |
| 3008 | char hdr[32]; |
| 3009 | int hdrlen; |
| 3010 | |
| 3011 | /* Normally if we have it in the pack then we do not bother writing |
| 3012 | * it out into .git/objects/??/?{38} file. |
| 3013 | */ |
| 3014 | write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); |
| 3015 | if (returnsha1) |
| 3016 | hashcpy(returnsha1, sha1); |
Jeff King | b5f52f3 | 2015-04-20 15:54:03 -0400 | [diff] [blame^] | 3017 | if (freshen_packed_object(sha1) || freshen_loose_object(sha1)) |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 3018 | return 0; |
| 3019 | return write_loose_object(sha1, hdr, hdrlen, buf, len, 0); |
| 3020 | } |
| 3021 | |
| 3022 | int force_object_loose(const unsigned char *sha1, time_t mtime) |
| 3023 | { |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 3024 | void *buf; |
| 3025 | unsigned long len; |
| 3026 | enum object_type type; |
| 3027 | char hdr[32]; |
| 3028 | int hdrlen; |
Björn Steinbrink | 1fb23e6 | 2008-10-18 02:37:31 +0200 | [diff] [blame] | 3029 | int ret; |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 3030 | |
Linus Torvalds | c529d75 | 2008-06-14 11:43:01 -0700 | [diff] [blame] | 3031 | if (has_loose_object(sha1)) |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 3032 | return 0; |
| 3033 | buf = read_packed_sha1(sha1, &type, &len); |
| 3034 | if (!buf) |
| 3035 | return error("cannot read sha1_file for %s", sha1_to_hex(sha1)); |
| 3036 | hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1; |
Björn Steinbrink | 1fb23e6 | 2008-10-18 02:37:31 +0200 | [diff] [blame] | 3037 | ret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime); |
| 3038 | free(buf); |
| 3039 | |
| 3040 | return ret; |
Nicolas Pitre | bbac731 | 2008-05-14 01:32:48 -0400 | [diff] [blame] | 3041 | } |
| 3042 | |
barkalow@iabervon.org | bf592c5 | 2005-07-31 20:53:44 -0400 | [diff] [blame] | 3043 | int has_pack_index(const unsigned char *sha1) |
| 3044 | { |
| 3045 | struct stat st; |
| 3046 | if (stat(sha1_pack_index_name(sha1), &st)) |
| 3047 | return 0; |
| 3048 | return 1; |
| 3049 | } |
| 3050 | |
Junio C Hamano | cd673c1 | 2009-02-27 23:15:53 -0800 | [diff] [blame] | 3051 | int has_sha1_pack(const unsigned char *sha1) |
Linus Torvalds | dade09c | 2005-07-03 13:06:36 -0700 | [diff] [blame] | 3052 | { |
| 3053 | struct pack_entry e; |
Junio C Hamano | cd673c1 | 2009-02-27 23:15:53 -0800 | [diff] [blame] | 3054 | return find_pack_entry(sha1, &e); |
| 3055 | } |
| 3056 | |
Daniel Barkalow | 8237b18 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 3057 | int has_sha1_file(const unsigned char *sha1) |
| 3058 | { |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 3059 | struct pack_entry e; |
| 3060 | |
Junio C Hamano | cd673c1 | 2009-02-27 23:15:53 -0800 | [diff] [blame] | 3061 | if (find_pack_entry(sha1, &e)) |
Junio C Hamano | 1f68855 | 2005-06-27 03:35:33 -0700 | [diff] [blame] | 3062 | return 1; |
Jeff King | 45e8a74 | 2013-08-30 15:14:13 -0400 | [diff] [blame] | 3063 | if (has_loose_object(sha1)) |
| 3064 | return 1; |
| 3065 | reprepare_packed_git(); |
| 3066 | return find_pack_entry(sha1, &e); |
Daniel Barkalow | 8237b18 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 3067 | } |
Junio C Hamano | 74400e7 | 2005-05-01 23:45:49 -0700 | [diff] [blame] | 3068 | |
Nguyễn Thái Ngọc Duy | c879daa | 2011-02-05 17:52:21 +0700 | [diff] [blame] | 3069 | static void check_tree(const void *buf, size_t size) |
| 3070 | { |
| 3071 | struct tree_desc desc; |
| 3072 | struct name_entry entry; |
| 3073 | |
| 3074 | init_tree_desc(&desc, buf, size); |
| 3075 | while (tree_entry(&desc, &entry)) |
| 3076 | /* do nothing |
| 3077 | * tree_entry() will die() on malformed entries */ |
| 3078 | ; |
| 3079 | } |
| 3080 | |
| 3081 | static void check_commit(const void *buf, size_t size) |
| 3082 | { |
| 3083 | struct commit c; |
| 3084 | memset(&c, 0, sizeof(c)); |
| 3085 | if (parse_commit_buffer(&c, buf, size)) |
| 3086 | die("corrupt commit"); |
| 3087 | } |
| 3088 | |
| 3089 | static void check_tag(const void *buf, size_t size) |
| 3090 | { |
| 3091 | struct tag t; |
| 3092 | memset(&t, 0, sizeof(t)); |
| 3093 | if (parse_tag_buffer(&t, buf, size)) |
| 3094 | die("corrupt tag"); |
| 3095 | } |
| 3096 | |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3097 | static int index_mem(unsigned char *sha1, void *buf, size_t size, |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3098 | enum object_type type, |
| 3099 | const char *path, unsigned flags) |
Björn Engelmann | e7332f9 | 2006-05-23 20:19:04 +0200 | [diff] [blame] | 3100 | { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 3101 | int ret, re_allocated = 0; |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3102 | int write_object = flags & HASH_WRITE_OBJECT; |
Junio C Hamano | 74400e7 | 2005-05-01 23:45:49 -0700 | [diff] [blame] | 3103 | |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 3104 | if (!type) |
Junio C Hamano | edaec3f | 2007-02-28 11:45:56 -0800 | [diff] [blame] | 3105 | type = OBJ_BLOB; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 3106 | |
| 3107 | /* |
| 3108 | * Convert blobs to git internal format |
| 3109 | */ |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3110 | if ((type == OBJ_BLOB) && path) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 3111 | struct strbuf nbuf = STRBUF_INIT; |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 3112 | if (convert_to_git(path, buf, size, &nbuf, |
Ramkumar Ramachandra | 5e12e78 | 2011-11-15 22:29:39 +0530 | [diff] [blame] | 3113 | write_object ? safe_crlf : SAFE_CRLF_FALSE)) { |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 3114 | buf = strbuf_detach(&nbuf, &size); |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 3115 | re_allocated = 1; |
| 3116 | } |
| 3117 | } |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3118 | if (flags & HASH_FORMAT_CHECK) { |
Nguyễn Thái Ngọc Duy | c879daa | 2011-02-05 17:52:21 +0700 | [diff] [blame] | 3119 | if (type == OBJ_TREE) |
| 3120 | check_tree(buf, size); |
| 3121 | if (type == OBJ_COMMIT) |
| 3122 | check_commit(buf, size); |
| 3123 | if (type == OBJ_TAG) |
| 3124 | check_tag(buf, size); |
| 3125 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 3126 | |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 3127 | if (write_object) |
Junio C Hamano | edaec3f | 2007-02-28 11:45:56 -0800 | [diff] [blame] | 3128 | ret = write_sha1_file(buf, size, typename(type), sha1); |
Rene Scharfe | abdc3fc | 2006-10-14 12:45:36 +0200 | [diff] [blame] | 3129 | else |
Junio C Hamano | edaec3f | 2007-02-28 11:45:56 -0800 | [diff] [blame] | 3130 | ret = hash_sha1_file(buf, size, typename(type), sha1); |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3131 | if (re_allocated) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 3132 | free(buf); |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3133 | return ret; |
| 3134 | } |
| 3135 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 3136 | static int index_stream_convert_blob(unsigned char *sha1, int fd, |
| 3137 | const char *path, unsigned flags) |
| 3138 | { |
| 3139 | int ret; |
| 3140 | const int write_object = flags & HASH_WRITE_OBJECT; |
| 3141 | struct strbuf sbuf = STRBUF_INIT; |
| 3142 | |
| 3143 | assert(path); |
| 3144 | assert(would_convert_to_git_filter_fd(path)); |
| 3145 | |
| 3146 | convert_to_git_filter_fd(path, fd, &sbuf, |
| 3147 | write_object ? safe_crlf : SAFE_CRLF_FALSE); |
| 3148 | |
| 3149 | if (write_object) |
| 3150 | ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB), |
| 3151 | sha1); |
| 3152 | else |
| 3153 | ret = hash_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB), |
| 3154 | sha1); |
| 3155 | strbuf_release(&sbuf); |
| 3156 | return ret; |
| 3157 | } |
| 3158 | |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3159 | static int index_pipe(unsigned char *sha1, int fd, enum object_type type, |
| 3160 | const char *path, unsigned flags) |
| 3161 | { |
| 3162 | struct strbuf sbuf = STRBUF_INIT; |
| 3163 | int ret; |
| 3164 | |
| 3165 | if (strbuf_read(&sbuf, fd, 4096) >= 0) |
| 3166 | ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags); |
| 3167 | else |
| 3168 | ret = -1; |
| 3169 | strbuf_release(&sbuf); |
| 3170 | return ret; |
| 3171 | } |
| 3172 | |
Dmitry Potapov | ea68b0c | 2010-02-21 09:32:19 +0300 | [diff] [blame] | 3173 | #define SMALL_FILE_SIZE (32*1024) |
| 3174 | |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3175 | static int index_core(unsigned char *sha1, int fd, size_t size, |
| 3176 | enum object_type type, const char *path, |
| 3177 | unsigned flags) |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3178 | { |
| 3179 | int ret; |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3180 | |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3181 | if (!size) { |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3182 | ret = index_mem(sha1, NULL, size, type, path, flags); |
Dmitry Potapov | ea68b0c | 2010-02-21 09:32:19 +0300 | [diff] [blame] | 3183 | } else if (size <= SMALL_FILE_SIZE) { |
| 3184 | char *buf = xmalloc(size); |
| 3185 | if (size == read_in_full(fd, buf, size)) |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3186 | ret = index_mem(sha1, buf, size, type, path, flags); |
Dmitry Potapov | ea68b0c | 2010-02-21 09:32:19 +0300 | [diff] [blame] | 3187 | else |
| 3188 | ret = error("short read %s", strerror(errno)); |
| 3189 | free(buf); |
Dmitry Potapov | 08bda20 | 2010-05-11 01:38:17 +0400 | [diff] [blame] | 3190 | } else { |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3191 | void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3192 | ret = index_mem(sha1, buf, size, type, path, flags); |
Linus Torvalds | aac1794 | 2005-05-03 11:46:16 -0700 | [diff] [blame] | 3193 | munmap(buf, size); |
Dmitry Potapov | 08bda20 | 2010-05-11 01:38:17 +0400 | [diff] [blame] | 3194 | } |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3195 | return ret; |
| 3196 | } |
| 3197 | |
Junio C Hamano | 4dd1fbc | 2011-05-08 01:47:35 -0700 | [diff] [blame] | 3198 | /* |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 3199 | * This creates one packfile per large blob unless bulk-checkin |
| 3200 | * machinery is "plugged". |
Junio C Hamano | 4dd1fbc | 2011-05-08 01:47:35 -0700 | [diff] [blame] | 3201 | * |
| 3202 | * This also bypasses the usual "convert-to-git" dance, and that is on |
| 3203 | * purpose. We could write a streaming version of the converting |
| 3204 | * functions and insert that before feeding the data to fast-import |
Jeff King | 4f22b10 | 2012-02-24 17:10:17 -0500 | [diff] [blame] | 3205 | * (or equivalent in-core API described above). However, that is |
| 3206 | * somewhat complicated, as we do not know the size of the filter |
| 3207 | * result, which we need to know beforehand when writing a git object. |
| 3208 | * Since the primary motivation for trying to stream from the working |
| 3209 | * tree file and to avoid mmaping it in core is to deal with large |
| 3210 | * binary blobs, they generally do not want to get any conversion, and |
| 3211 | * callers should avoid this code path when filters are requested. |
Junio C Hamano | 4dd1fbc | 2011-05-08 01:47:35 -0700 | [diff] [blame] | 3212 | */ |
| 3213 | static int index_stream(unsigned char *sha1, int fd, size_t size, |
| 3214 | enum object_type type, const char *path, |
| 3215 | unsigned flags) |
| 3216 | { |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 3217 | return index_bulk_checkin(sha1, fd, size, type, path, flags); |
Junio C Hamano | 4dd1fbc | 2011-05-08 01:47:35 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3220 | int index_fd(unsigned char *sha1, int fd, struct stat *st, |
| 3221 | enum object_type type, const char *path, unsigned flags) |
| 3222 | { |
| 3223 | int ret; |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3224 | |
Steffen Prohaska | 9079ab7 | 2014-09-21 12:03:26 +0200 | [diff] [blame] | 3225 | /* |
| 3226 | * Call xsize_t() only when needed to avoid potentially unnecessary |
| 3227 | * die() for large files. |
| 3228 | */ |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 3229 | if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path)) |
| 3230 | ret = index_stream_convert_blob(sha1, fd, path, flags); |
| 3231 | else if (!S_ISREG(st->st_mode)) |
Junio C Hamano | 7b41e1e | 2011-05-08 01:47:34 -0700 | [diff] [blame] | 3232 | ret = index_pipe(sha1, fd, type, path, flags); |
Steffen Prohaska | 9079ab7 | 2014-09-21 12:03:26 +0200 | [diff] [blame] | 3233 | else if (st->st_size <= big_file_threshold || type != OBJ_BLOB || |
Steffen Prohaska | 7ce7c76 | 2014-08-21 18:05:08 +0200 | [diff] [blame] | 3234 | (path && would_convert_to_git(path))) |
Steffen Prohaska | 9079ab7 | 2014-09-21 12:03:26 +0200 | [diff] [blame] | 3235 | ret = index_core(sha1, fd, xsize_t(st->st_size), type, path, |
| 3236 | flags); |
Junio C Hamano | 4dd1fbc | 2011-05-08 01:47:35 -0700 | [diff] [blame] | 3237 | else |
Steffen Prohaska | 9079ab7 | 2014-09-21 12:03:26 +0200 | [diff] [blame] | 3238 | ret = index_stream(sha1, fd, xsize_t(st->st_size), type, path, |
| 3239 | flags); |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 3240 | close(fd); |
Linus Torvalds | aac1794 | 2005-05-03 11:46:16 -0700 | [diff] [blame] | 3241 | return ret; |
Junio C Hamano | 74400e7 | 2005-05-01 23:45:49 -0700 | [diff] [blame] | 3242 | } |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3243 | |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3244 | int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags) |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3245 | { |
| 3246 | int fd; |
Linus Torvalds | b760d3a | 2008-12-17 09:51:53 -0800 | [diff] [blame] | 3247 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3248 | |
| 3249 | switch (st->st_mode & S_IFMT) { |
| 3250 | case S_IFREG: |
| 3251 | fd = open(path, O_RDONLY); |
| 3252 | if (fd < 0) |
| 3253 | return error("open(\"%s\"): %s", path, |
| 3254 | strerror(errno)); |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3255 | if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0) |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3256 | return error("%s: failed to insert into database", |
| 3257 | path); |
| 3258 | break; |
| 3259 | case S_IFLNK: |
Linus Torvalds | b760d3a | 2008-12-17 09:51:53 -0800 | [diff] [blame] | 3260 | if (strbuf_readlink(&sb, path, st->st_size)) { |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3261 | char *errstr = strerror(errno); |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3262 | return error("readlink(\"%s\"): %s", path, |
| 3263 | errstr); |
| 3264 | } |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 3265 | if (!(flags & HASH_WRITE_OBJECT)) |
Linus Torvalds | b760d3a | 2008-12-17 09:51:53 -0800 | [diff] [blame] | 3266 | hash_sha1_file(sb.buf, sb.len, blob_type, sha1); |
| 3267 | else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1)) |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3268 | return error("%s: failed to insert into database", |
| 3269 | path); |
Linus Torvalds | b760d3a | 2008-12-17 09:51:53 -0800 | [diff] [blame] | 3270 | strbuf_release(&sb); |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3271 | break; |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 3272 | case S_IFDIR: |
| 3273 | return resolve_gitlink_ref(path, "HEAD", sha1); |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 3274 | default: |
| 3275 | return error("%s: unsupported file type", path); |
| 3276 | } |
| 3277 | return 0; |
| 3278 | } |
Junio C Hamano | a69e542 | 2007-01-22 21:55:18 -0800 | [diff] [blame] | 3279 | |
| 3280 | int read_pack_header(int fd, struct pack_header *header) |
| 3281 | { |
Heikki Orsila | c697ad1 | 2008-05-03 16:27:26 +0300 | [diff] [blame] | 3282 | if (read_in_full(fd, header, sizeof(*header)) < sizeof(*header)) |
| 3283 | /* "eof before pack header was fully read" */ |
| 3284 | return PH_ERROR_EOF; |
| 3285 | |
Junio C Hamano | a69e542 | 2007-01-22 21:55:18 -0800 | [diff] [blame] | 3286 | if (header->hdr_signature != htonl(PACK_SIGNATURE)) |
| 3287 | /* "protocol error (pack signature mismatch detected)" */ |
| 3288 | return PH_ERROR_PACK_SIGNATURE; |
| 3289 | if (!pack_version_ok(header->hdr_version)) |
| 3290 | /* "protocol error (pack version unsupported)" */ |
| 3291 | return PH_ERROR_PROTOCOL; |
| 3292 | return 0; |
| 3293 | } |
Jeff King | 40d52ff | 2010-04-01 20:05:23 -0400 | [diff] [blame] | 3294 | |
| 3295 | void assert_sha1_type(const unsigned char *sha1, enum object_type expect) |
| 3296 | { |
| 3297 | enum object_type type = sha1_object_info(sha1, NULL); |
| 3298 | if (type < 0) |
| 3299 | die("%s is not a valid object", sha1_to_hex(sha1)); |
| 3300 | if (type != expect) |
| 3301 | die("%s is not a valid '%s' object", sha1_to_hex(sha1), |
| 3302 | typename(expect)); |
| 3303 | } |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3304 | |
| 3305 | static int for_each_file_in_obj_subdir(int subdir_nr, |
| 3306 | struct strbuf *path, |
| 3307 | each_loose_object_fn obj_cb, |
| 3308 | each_loose_cruft_fn cruft_cb, |
| 3309 | each_loose_subdir_fn subdir_cb, |
| 3310 | void *data) |
| 3311 | { |
| 3312 | size_t baselen = path->len; |
| 3313 | DIR *dir = opendir(path->buf); |
| 3314 | struct dirent *de; |
| 3315 | int r = 0; |
| 3316 | |
| 3317 | if (!dir) { |
| 3318 | if (errno == ENOENT) |
| 3319 | return 0; |
| 3320 | return error("unable to open %s: %s", path->buf, strerror(errno)); |
| 3321 | } |
| 3322 | |
| 3323 | while ((de = readdir(dir))) { |
| 3324 | if (is_dot_or_dotdot(de->d_name)) |
| 3325 | continue; |
| 3326 | |
| 3327 | strbuf_setlen(path, baselen); |
| 3328 | strbuf_addf(path, "/%s", de->d_name); |
| 3329 | |
| 3330 | if (strlen(de->d_name) == 38) { |
| 3331 | char hex[41]; |
| 3332 | unsigned char sha1[20]; |
| 3333 | |
| 3334 | snprintf(hex, sizeof(hex), "%02x%s", |
| 3335 | subdir_nr, de->d_name); |
| 3336 | if (!get_sha1_hex(hex, sha1)) { |
| 3337 | if (obj_cb) { |
| 3338 | r = obj_cb(sha1, path->buf, data); |
| 3339 | if (r) |
| 3340 | break; |
| 3341 | } |
| 3342 | continue; |
| 3343 | } |
| 3344 | } |
| 3345 | |
| 3346 | if (cruft_cb) { |
| 3347 | r = cruft_cb(de->d_name, path->buf, data); |
| 3348 | if (r) |
| 3349 | break; |
| 3350 | } |
| 3351 | } |
| 3352 | strbuf_setlen(path, baselen); |
| 3353 | |
| 3354 | if (!r && subdir_cb) |
| 3355 | r = subdir_cb(subdir_nr, path->buf, data); |
| 3356 | |
| 3357 | closedir(dir); |
| 3358 | return r; |
| 3359 | } |
| 3360 | |
Jeff King | e6f875e | 2015-02-08 20:13:22 -0500 | [diff] [blame] | 3361 | int for_each_loose_file_in_objdir_buf(struct strbuf *path, |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3362 | each_loose_object_fn obj_cb, |
| 3363 | each_loose_cruft_fn cruft_cb, |
| 3364 | each_loose_subdir_fn subdir_cb, |
| 3365 | void *data) |
| 3366 | { |
Jeff King | e6f875e | 2015-02-08 20:13:22 -0500 | [diff] [blame] | 3367 | size_t baselen = path->len; |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3368 | int r = 0; |
| 3369 | int i; |
| 3370 | |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3371 | for (i = 0; i < 256; i++) { |
Jeff King | e6f875e | 2015-02-08 20:13:22 -0500 | [diff] [blame] | 3372 | strbuf_addf(path, "/%02x", i); |
| 3373 | r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb, |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3374 | subdir_cb, data); |
Jeff King | e6f875e | 2015-02-08 20:13:22 -0500 | [diff] [blame] | 3375 | strbuf_setlen(path, baselen); |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3376 | if (r) |
| 3377 | break; |
| 3378 | } |
| 3379 | |
Jeff King | e6f875e | 2015-02-08 20:13:22 -0500 | [diff] [blame] | 3380 | return r; |
| 3381 | } |
| 3382 | |
| 3383 | int for_each_loose_file_in_objdir(const char *path, |
| 3384 | each_loose_object_fn obj_cb, |
| 3385 | each_loose_cruft_fn cruft_cb, |
| 3386 | each_loose_subdir_fn subdir_cb, |
| 3387 | void *data) |
| 3388 | { |
| 3389 | struct strbuf buf = STRBUF_INIT; |
| 3390 | int r; |
| 3391 | |
| 3392 | strbuf_addstr(&buf, path); |
| 3393 | r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb, |
| 3394 | subdir_cb, data); |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3395 | strbuf_release(&buf); |
Jeff King | e6f875e | 2015-02-08 20:13:22 -0500 | [diff] [blame] | 3396 | |
Jeff King | 27e1e22 | 2014-10-15 18:38:55 -0400 | [diff] [blame] | 3397 | return r; |
| 3398 | } |
Jeff King | 660c889 | 2014-10-15 18:41:21 -0400 | [diff] [blame] | 3399 | |
| 3400 | struct loose_alt_odb_data { |
| 3401 | each_loose_object_fn *cb; |
| 3402 | void *data; |
| 3403 | }; |
| 3404 | |
| 3405 | static int loose_from_alt_odb(struct alternate_object_database *alt, |
| 3406 | void *vdata) |
| 3407 | { |
| 3408 | struct loose_alt_odb_data *data = vdata; |
Jonathon Mah | b0a4264 | 2015-02-08 20:15:39 -0500 | [diff] [blame] | 3409 | struct strbuf buf = STRBUF_INIT; |
| 3410 | int r; |
| 3411 | |
| 3412 | /* copy base not including trailing '/' */ |
| 3413 | strbuf_add(&buf, alt->base, alt->name - alt->base - 1); |
| 3414 | r = for_each_loose_file_in_objdir_buf(&buf, |
| 3415 | data->cb, NULL, NULL, |
| 3416 | data->data); |
| 3417 | strbuf_release(&buf); |
| 3418 | return r; |
Jeff King | 660c889 | 2014-10-15 18:41:21 -0400 | [diff] [blame] | 3419 | } |
| 3420 | |
Jeff King | 1385bb7 | 2015-03-27 07:32:41 -0400 | [diff] [blame] | 3421 | int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags) |
Jeff King | 660c889 | 2014-10-15 18:41:21 -0400 | [diff] [blame] | 3422 | { |
| 3423 | struct loose_alt_odb_data alt; |
| 3424 | int r; |
| 3425 | |
| 3426 | r = for_each_loose_file_in_objdir(get_object_directory(), |
| 3427 | cb, NULL, NULL, data); |
| 3428 | if (r) |
| 3429 | return r; |
| 3430 | |
Jeff King | 1385bb7 | 2015-03-27 07:32:41 -0400 | [diff] [blame] | 3431 | if (flags & FOR_EACH_OBJECT_LOCAL_ONLY) |
| 3432 | return 0; |
| 3433 | |
Jeff King | 660c889 | 2014-10-15 18:41:21 -0400 | [diff] [blame] | 3434 | alt.cb = cb; |
| 3435 | alt.data = data; |
| 3436 | return foreach_alt_odb(loose_from_alt_odb, &alt); |
| 3437 | } |
| 3438 | |
| 3439 | static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data) |
| 3440 | { |
| 3441 | uint32_t i; |
| 3442 | int r = 0; |
| 3443 | |
| 3444 | for (i = 0; i < p->num_objects; i++) { |
| 3445 | const unsigned char *sha1 = nth_packed_object_sha1(p, i); |
| 3446 | |
| 3447 | if (!sha1) |
| 3448 | return error("unable to get sha1 of object %u in %s", |
| 3449 | i, p->pack_name); |
| 3450 | |
| 3451 | r = cb(sha1, p, i, data); |
| 3452 | if (r) |
| 3453 | break; |
| 3454 | } |
| 3455 | return r; |
| 3456 | } |
| 3457 | |
Jeff King | 1385bb7 | 2015-03-27 07:32:41 -0400 | [diff] [blame] | 3458 | int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags) |
Jeff King | 660c889 | 2014-10-15 18:41:21 -0400 | [diff] [blame] | 3459 | { |
| 3460 | struct packed_git *p; |
| 3461 | int r = 0; |
| 3462 | |
| 3463 | prepare_packed_git(); |
| 3464 | for (p = packed_git; p; p = p->next) { |
Jeff King | 1385bb7 | 2015-03-27 07:32:41 -0400 | [diff] [blame] | 3465 | if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local) |
| 3466 | continue; |
Jeff King | 660c889 | 2014-10-15 18:41:21 -0400 | [diff] [blame] | 3467 | r = for_each_object_in_pack(p, cb, data); |
| 3468 | if (r) |
| 3469 | break; |
| 3470 | } |
| 3471 | return r; |
| 3472 | } |