Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "delta.h" |
| 3 | #include "pack.h" |
| 4 | #include "csum-file.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 5 | #include "blob.h" |
| 6 | #include "commit.h" |
| 7 | #include "tag.h" |
| 8 | #include "tree.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 9 | #include "progress.h" |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 10 | #include "fsck.h" |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 11 | #include "exec_cmd.h" |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 12 | |
| 13 | static const char index_pack_usage[] = |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 14 | "git index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--strict] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }"; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 15 | |
| 16 | struct object_entry |
| 17 | { |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 18 | struct pack_idx_entry idx; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 19 | unsigned long size; |
| 20 | unsigned int hdr_size; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 21 | enum object_type type; |
| 22 | enum object_type real_type; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 25 | union delta_base { |
| 26 | unsigned char sha1[20]; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 27 | off_t offset; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 28 | }; |
| 29 | |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 30 | struct base_data { |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 31 | struct base_data *base; |
| 32 | struct base_data *child; |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 33 | struct object_entry *obj; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 34 | void *data; |
| 35 | unsigned long size; |
| 36 | }; |
| 37 | |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 38 | /* |
| 39 | * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want |
| 40 | * to memcmp() only the first 20 bytes. |
| 41 | */ |
| 42 | #define UNION_BASE_SZ 20 |
| 43 | |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 44 | #define FLAG_LINK (1u<<20) |
| 45 | #define FLAG_CHECKED (1u<<21) |
| 46 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 47 | struct delta_entry |
| 48 | { |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 49 | union delta_base base; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 50 | int obj_no; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 53 | static struct object_entry *objects; |
| 54 | static struct delta_entry *deltas; |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 55 | static struct base_data *base_cache; |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 56 | static size_t base_cache_used; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 57 | static int nr_objects; |
| 58 | static int nr_deltas; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 59 | static int nr_resolved_deltas; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 60 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 61 | static int from_stdin; |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 62 | static int strict; |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 63 | static int verbose; |
| 64 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 65 | static struct progress *progress; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 66 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 67 | /* We always read in 4kB chunks. */ |
| 68 | static unsigned char input_buffer[4096]; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 69 | static unsigned int input_offset, input_len; |
| 70 | static off_t consumed_bytes; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 71 | static git_SHA_CTX input_ctx; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 72 | static uint32_t input_crc32; |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 73 | static int input_fd, output_fd, pack_fd; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 74 | |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 75 | static int mark_link(struct object *obj, int type, void *data) |
| 76 | { |
| 77 | if (!obj) |
| 78 | return -1; |
| 79 | |
| 80 | if (type != OBJ_ANY && obj->type != type) |
| 81 | die("object type mismatch at %s", sha1_to_hex(obj->sha1)); |
| 82 | |
| 83 | obj->flags |= FLAG_LINK; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* The content of each linked object must have been checked |
| 88 | or it must be already present in the object database */ |
| 89 | static void check_object(struct object *obj) |
| 90 | { |
| 91 | if (!obj) |
| 92 | return; |
| 93 | |
| 94 | if (!(obj->flags & FLAG_LINK)) |
| 95 | return; |
| 96 | |
| 97 | if (!(obj->flags & FLAG_CHECKED)) { |
| 98 | unsigned long size; |
| 99 | int type = sha1_object_info(obj->sha1, &size); |
| 100 | if (type != obj->type || type <= 0) |
| 101 | die("object of unexpected type"); |
| 102 | obj->flags |= FLAG_CHECKED; |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void check_objects(void) |
| 108 | { |
| 109 | unsigned i, max; |
| 110 | |
| 111 | max = get_max_object_index(); |
| 112 | for (i = 0; i < max; i++) |
| 113 | check_object(get_indexed_object(i)); |
| 114 | } |
| 115 | |
| 116 | |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 117 | /* Discard current buffer used content. */ |
Rene Scharfe | a6e8a76 | 2006-11-18 13:07:06 +0100 | [diff] [blame] | 118 | static void flush(void) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 119 | { |
| 120 | if (input_offset) { |
| 121 | if (output_fd >= 0) |
| 122 | write_or_die(output_fd, input_buffer, input_offset); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 123 | git_SHA1_Update(&input_ctx, input_buffer, input_offset); |
Jim Meyering | 554a263 | 2006-12-11 19:06:34 +0100 | [diff] [blame] | 124 | memmove(input_buffer, input_buffer + input_offset, input_len); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 125 | input_offset = 0; |
| 126 | } |
| 127 | } |
| 128 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 129 | /* |
| 130 | * Make sure at least "min" bytes are available in the buffer, and |
| 131 | * return the pointer to the buffer. |
| 132 | */ |
Nicolas Pitre | b89c4e9 | 2006-10-27 16:14:23 -0400 | [diff] [blame] | 133 | static void *fill(int min) |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 134 | { |
| 135 | if (min <= input_len) |
| 136 | return input_buffer + input_offset; |
| 137 | if (min > sizeof(input_buffer)) |
| 138 | die("cannot fill %d bytes", min); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 139 | flush(); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 140 | do { |
Johan Herland | 8a912bc | 2007-05-15 14:49:22 +0200 | [diff] [blame] | 141 | ssize_t ret = xread(input_fd, input_buffer + input_len, |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 142 | sizeof(input_buffer) - input_len); |
| 143 | if (ret <= 0) { |
| 144 | if (!ret) |
| 145 | die("early EOF"); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 146 | die_errno("read error on input"); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 147 | } |
| 148 | input_len += ret; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 149 | if (from_stdin) |
| 150 | display_throughput(progress, consumed_bytes + input_len); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 151 | } while (input_len < min); |
| 152 | return input_buffer; |
| 153 | } |
| 154 | |
| 155 | static void use(int bytes) |
| 156 | { |
| 157 | if (bytes > input_len) |
| 158 | die("used more bytes than were available"); |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 159 | input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 160 | input_len -= bytes; |
| 161 | input_offset += bytes; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 162 | |
| 163 | /* make sure off_t is sufficiently large not to wrap */ |
| 164 | if (consumed_bytes > consumed_bytes + bytes) |
| 165 | die("pack too large for current definition of off_t"); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 166 | consumed_bytes += bytes; |
| 167 | } |
| 168 | |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 169 | static const char *open_pack_file(const char *pack_name) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 170 | { |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 171 | if (from_stdin) { |
| 172 | input_fd = 0; |
| 173 | if (!pack_name) { |
| 174 | static char tmpfile[PATH_MAX]; |
Junio C Hamano | 6e180cd | 2009-02-24 23:11:29 -0800 | [diff] [blame] | 175 | output_fd = odb_mkstemp(tmpfile, sizeof(tmpfile), |
| 176 | "pack/tmp_pack_XXXXXX"); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 177 | pack_name = xstrdup(tmpfile); |
| 178 | } else |
| 179 | output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); |
| 180 | if (output_fd < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 181 | die_errno("unable to create '%s'", pack_name); |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 182 | pack_fd = output_fd; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 183 | } else { |
| 184 | input_fd = open(pack_name, O_RDONLY); |
| 185 | if (input_fd < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 186 | die_errno("cannot open packfile '%s'", pack_name); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 187 | output_fd = -1; |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 188 | pack_fd = input_fd; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 189 | } |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 190 | git_SHA1_Init(&input_ctx); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 191 | return pack_name; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void parse_pack_header(void) |
| 195 | { |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 196 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 197 | |
| 198 | /* Header consistency check */ |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 199 | if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 200 | die("pack signature mismatch"); |
Nicolas Pitre | d60fc1c | 2006-02-09 17:50:04 -0500 | [diff] [blame] | 201 | if (!pack_version_ok(hdr->hdr_version)) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 202 | die("pack version %"PRIu32" unsupported", |
| 203 | ntohl(hdr->hdr_version)); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 204 | |
| 205 | nr_objects = ntohl(hdr->hdr_entries); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 206 | use(sizeof(struct pack_header)); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Erik Faye-Lund | a4f3131 | 2009-09-30 18:05:49 +0000 | [diff] [blame] | 209 | static NORETURN void bad_object(unsigned long offset, const char *format, |
| 210 | ...) __attribute__((format (printf, 2, 3))); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 211 | |
| 212 | static void bad_object(unsigned long offset, const char *format, ...) |
| 213 | { |
| 214 | va_list params; |
| 215 | char buf[1024]; |
| 216 | |
| 217 | va_start(params, format); |
| 218 | vsnprintf(buf, sizeof(buf), format, params); |
| 219 | va_end(params); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 220 | die("pack has bad object at offset %lu: %s", offset, buf); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 223 | static void free_base_data(struct base_data *c) |
| 224 | { |
| 225 | if (c->data) { |
| 226 | free(c->data); |
| 227 | c->data = NULL; |
| 228 | base_cache_used -= c->size; |
| 229 | } |
| 230 | } |
| 231 | |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 232 | static void prune_base_data(struct base_data *retain) |
| 233 | { |
Benjamin Kramer | 8e24cba | 2009-03-15 22:01:20 +0100 | [diff] [blame] | 234 | struct base_data *b; |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 235 | for (b = base_cache; |
| 236 | base_cache_used > delta_base_cache_limit && b; |
| 237 | b = b->child) { |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 238 | if (b->data && b != retain) |
| 239 | free_base_data(b); |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 243 | static void link_base_data(struct base_data *base, struct base_data *c) |
| 244 | { |
| 245 | if (base) |
| 246 | base->child = c; |
| 247 | else |
| 248 | base_cache = c; |
| 249 | |
| 250 | c->base = base; |
| 251 | c->child = NULL; |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 252 | if (c->data) |
| 253 | base_cache_used += c->size; |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 254 | prune_base_data(c); |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void unlink_base_data(struct base_data *c) |
| 258 | { |
| 259 | struct base_data *base = c->base; |
| 260 | if (base) |
| 261 | base->child = NULL; |
| 262 | else |
| 263 | base_cache = NULL; |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 264 | free_base_data(c); |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 267 | static void *unpack_entry_data(unsigned long offset, unsigned long size) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 268 | { |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 269 | z_stream stream; |
| 270 | void *buf = xmalloc(size); |
| 271 | |
| 272 | memset(&stream, 0, sizeof(stream)); |
| 273 | stream.next_out = buf; |
| 274 | stream.avail_out = size; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 275 | stream.next_in = fill(1); |
| 276 | stream.avail_in = input_len; |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 277 | git_inflate_init(&stream); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 278 | |
| 279 | for (;;) { |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 280 | int ret = git_inflate(&stream, 0); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 281 | use(input_len - stream.avail_in); |
| 282 | if (stream.total_out == size && ret == Z_STREAM_END) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 283 | break; |
| 284 | if (ret != Z_OK) |
| 285 | bad_object(offset, "inflate returned %d", ret); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 286 | stream.next_in = fill(1); |
| 287 | stream.avail_in = input_len; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 288 | } |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 289 | git_inflate_end(&stream); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 290 | return buf; |
| 291 | } |
| 292 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 293 | static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 294 | { |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 295 | unsigned char *p; |
| 296 | unsigned long size, c; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 297 | off_t base_offset; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 298 | unsigned shift; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 299 | void *data; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 300 | |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 301 | obj->idx.offset = consumed_bytes; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 302 | input_crc32 = crc32(0, Z_NULL, 0); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 303 | |
| 304 | p = fill(1); |
| 305 | c = *p; |
| 306 | use(1); |
| 307 | obj->type = (c >> 4) & 7; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 308 | size = (c & 15); |
| 309 | shift = 4; |
| 310 | while (c & 0x80) { |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 311 | p = fill(1); |
| 312 | c = *p; |
| 313 | use(1); |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 314 | size += (c & 0x7f) << shift; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 315 | shift += 7; |
| 316 | } |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 317 | obj->size = size; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 318 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 319 | switch (obj->type) { |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 320 | case OBJ_REF_DELTA: |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 321 | hashcpy(delta_base->sha1, fill(20)); |
| 322 | use(20); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 323 | break; |
| 324 | case OBJ_OFS_DELTA: |
| 325 | memset(delta_base, 0, sizeof(*delta_base)); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 326 | p = fill(1); |
| 327 | c = *p; |
| 328 | use(1); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 329 | base_offset = c & 127; |
| 330 | while (c & 128) { |
| 331 | base_offset += 1; |
Nicolas Pitre | 8723f21 | 2007-04-09 01:06:29 -0400 | [diff] [blame] | 332 | if (!base_offset || MSB(base_offset, 7)) |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 333 | bad_object(obj->idx.offset, "offset value overflow for delta base object"); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 334 | p = fill(1); |
| 335 | c = *p; |
| 336 | use(1); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 337 | base_offset = (base_offset << 7) + (c & 127); |
| 338 | } |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 339 | delta_base->offset = obj->idx.offset - base_offset; |
Nicolas Pitre | d8f3255 | 2008-10-29 19:02:45 -0400 | [diff] [blame] | 340 | if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset) |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 341 | bad_object(obj->idx.offset, "delta base offset is out of bound"); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 342 | break; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 343 | case OBJ_COMMIT: |
| 344 | case OBJ_TREE: |
| 345 | case OBJ_BLOB: |
| 346 | case OBJ_TAG: |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 347 | break; |
| 348 | default: |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 349 | bad_object(obj->idx.offset, "unknown object type %d", obj->type); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 350 | } |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 351 | obj->hdr_size = consumed_bytes - obj->idx.offset; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 352 | |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 353 | data = unpack_entry_data(obj->idx.offset, obj->size); |
| 354 | obj->idx.crc32 = input_crc32; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 355 | return data; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Nicolas Pitre | b89c4e9 | 2006-10-27 16:14:23 -0400 | [diff] [blame] | 358 | static void *get_data_from_pack(struct object_entry *obj) |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 359 | { |
Nicolas Pitre | a91ef6e | 2007-11-10 23:29:10 -0500 | [diff] [blame] | 360 | off_t from = obj[0].idx.offset + obj[0].hdr_size; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 361 | unsigned long len = obj[1].idx.offset - from; |
Shawn O. Pearce | a91d49c | 2007-02-27 23:47:19 -0500 | [diff] [blame] | 362 | unsigned long rdy = 0; |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 363 | unsigned char *src, *data; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 364 | z_stream stream; |
| 365 | int st; |
| 366 | |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 367 | src = xmalloc(len); |
Shawn O. Pearce | a91d49c | 2007-02-27 23:47:19 -0500 | [diff] [blame] | 368 | data = src; |
| 369 | do { |
| 370 | ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy); |
Samuel Tardieu | fb74243 | 2008-10-06 19:28:41 +0200 | [diff] [blame] | 371 | if (n < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 372 | die_errno("cannot pread pack file"); |
Samuel Tardieu | fb74243 | 2008-10-06 19:28:41 +0200 | [diff] [blame] | 373 | if (!n) |
| 374 | die("premature end of pack file, %lu bytes missing", |
| 375 | len - rdy); |
Shawn O. Pearce | a91d49c | 2007-02-27 23:47:19 -0500 | [diff] [blame] | 376 | rdy += n; |
| 377 | } while (rdy < len); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 378 | data = xmalloc(obj->size); |
| 379 | memset(&stream, 0, sizeof(stream)); |
| 380 | stream.next_out = data; |
| 381 | stream.avail_out = obj->size; |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 382 | stream.next_in = src; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 383 | stream.avail_in = len; |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 384 | git_inflate_init(&stream); |
| 385 | while ((st = git_inflate(&stream, Z_FINISH)) == Z_OK); |
| 386 | git_inflate_end(&stream); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 387 | if (st != Z_STREAM_END || stream.total_out != obj->size) |
| 388 | die("serious inflate inconsistency"); |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 389 | free(src); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 390 | return data; |
| 391 | } |
| 392 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 393 | static int find_delta(const union delta_base *base) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 394 | { |
| 395 | int first = 0, last = nr_deltas; |
| 396 | |
| 397 | while (first < last) { |
| 398 | int next = (first + last) / 2; |
| 399 | struct delta_entry *delta = &deltas[next]; |
| 400 | int cmp; |
| 401 | |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 402 | cmp = memcmp(base, &delta->base, UNION_BASE_SZ); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 403 | if (!cmp) |
| 404 | return next; |
| 405 | if (cmp < 0) { |
| 406 | last = next; |
| 407 | continue; |
| 408 | } |
| 409 | first = next+1; |
| 410 | } |
| 411 | return -first-1; |
| 412 | } |
| 413 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 414 | static void find_delta_children(const union delta_base *base, |
| 415 | int *first_index, int *last_index) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 416 | { |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 417 | int first = find_delta(base); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 418 | int last = first; |
| 419 | int end = nr_deltas - 1; |
| 420 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 421 | if (first < 0) { |
| 422 | *first_index = 0; |
| 423 | *last_index = -1; |
| 424 | return; |
| 425 | } |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 426 | while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ)) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 427 | --first; |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 428 | while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ)) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 429 | ++last; |
| 430 | *first_index = first; |
| 431 | *last_index = last; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void sha1_object(const void *data, unsigned long size, |
Nicolas Pitre | 9096c66 | 2007-03-20 17:07:48 -0400 | [diff] [blame] | 435 | enum object_type type, unsigned char *sha1) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 436 | { |
Nicolas Pitre | ce9fbf1 | 2007-03-20 16:02:09 -0400 | [diff] [blame] | 437 | hash_sha1_file(data, size, typename(type), sha1); |
Nicolas Pitre | 9096c66 | 2007-03-20 17:07:48 -0400 | [diff] [blame] | 438 | if (has_sha1_file(sha1)) { |
Nicolas Pitre | 8685da4 | 2007-03-20 15:32:35 -0400 | [diff] [blame] | 439 | void *has_data; |
| 440 | enum object_type has_type; |
| 441 | unsigned long has_size; |
| 442 | has_data = read_sha1_file(sha1, &has_type, &has_size); |
| 443 | if (!has_data) |
| 444 | die("cannot read existing object %s", sha1_to_hex(sha1)); |
| 445 | if (size != has_size || type != has_type || |
| 446 | memcmp(data, has_data, size) != 0) |
| 447 | die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1)); |
Nicolas Pitre | bbf4b41 | 2007-04-03 12:33:46 -0400 | [diff] [blame] | 448 | free(has_data); |
Nicolas Pitre | 8685da4 | 2007-03-20 15:32:35 -0400 | [diff] [blame] | 449 | } |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 450 | if (strict) { |
| 451 | if (type == OBJ_BLOB) { |
| 452 | struct blob *blob = lookup_blob(sha1); |
| 453 | if (blob) |
| 454 | blob->object.flags |= FLAG_CHECKED; |
| 455 | else |
| 456 | die("invalid blob object %s", sha1_to_hex(sha1)); |
| 457 | } else { |
| 458 | struct object *obj; |
| 459 | int eaten; |
| 460 | void *buf = (void *) data; |
| 461 | |
| 462 | /* |
| 463 | * we do not need to free the memory here, as the |
| 464 | * buf is deleted by the caller. |
| 465 | */ |
| 466 | obj = parse_object_buffer(sha1, type, size, buf, &eaten); |
| 467 | if (!obj) |
| 468 | die("invalid %s", typename(type)); |
| 469 | if (fsck_object(obj, 1, fsck_error_function)) |
| 470 | die("Error in object"); |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 471 | if (fsck_walk(obj, mark_link, NULL)) |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 472 | die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1)); |
| 473 | |
| 474 | if (obj->type == OBJ_TREE) { |
| 475 | struct tree *item = (struct tree *) obj; |
| 476 | item->buffer = NULL; |
| 477 | } |
| 478 | if (obj->type == OBJ_COMMIT) { |
| 479 | struct commit *commit = (struct commit *) obj; |
| 480 | commit->buffer = NULL; |
| 481 | } |
| 482 | obj->flags |= FLAG_CHECKED; |
| 483 | } |
| 484 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 487 | static void *get_base_data(struct base_data *c) |
| 488 | { |
| 489 | if (!c->data) { |
| 490 | struct object_entry *obj = c->obj; |
| 491 | |
| 492 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) { |
| 493 | void *base = get_base_data(c->base); |
| 494 | void *raw = get_data_from_pack(obj); |
| 495 | c->data = patch_delta( |
| 496 | base, c->base->size, |
| 497 | raw, obj->size, |
| 498 | &c->size); |
| 499 | free(raw); |
| 500 | if (!c->data) |
| 501 | bad_object(obj->idx.offset, "failed to apply delta"); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 502 | } else { |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 503 | c->data = get_data_from_pack(obj); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 504 | c->size = obj->size; |
| 505 | } |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 506 | |
| 507 | base_cache_used += c->size; |
| 508 | prune_base_data(c); |
| 509 | } |
| 510 | return c->data; |
| 511 | } |
| 512 | |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 513 | static void resolve_delta(struct object_entry *delta_obj, |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 514 | struct base_data *base, struct base_data *result) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 515 | { |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 516 | void *base_data, *delta_data; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 517 | |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 518 | delta_obj->real_type = base->obj->real_type; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 519 | delta_data = get_data_from_pack(delta_obj); |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 520 | base_data = get_base_data(base); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 521 | result->obj = delta_obj; |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 522 | result->data = patch_delta(base_data, base->size, |
| 523 | delta_data, delta_obj->size, &result->size); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 524 | free(delta_data); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 525 | if (!result->data) |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 526 | bad_object(delta_obj->idx.offset, "failed to apply delta"); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 527 | sha1_object(result->data, result->size, delta_obj->real_type, |
| 528 | delta_obj->idx.sha1); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 529 | nr_resolved_deltas++; |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 530 | } |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 531 | |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 532 | static void find_unresolved_deltas(struct base_data *base, |
| 533 | struct base_data *prev_base) |
| 534 | { |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 535 | int i, ref_first, ref_last, ofs_first, ofs_last; |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 536 | |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 537 | /* |
| 538 | * This is a recursive function. Those brackets should help reducing |
| 539 | * stack usage by limiting the scope of the delta_base union. |
| 540 | */ |
| 541 | { |
| 542 | union delta_base base_spec; |
| 543 | |
| 544 | hashcpy(base_spec.sha1, base->obj->idx.sha1); |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 545 | find_delta_children(&base_spec, &ref_first, &ref_last); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 546 | |
| 547 | memset(&base_spec, 0, sizeof(base_spec)); |
| 548 | base_spec.offset = base->obj->idx.offset; |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 549 | find_delta_children(&base_spec, &ofs_first, &ofs_last); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 550 | } |
| 551 | |
Nicolas Pitre | 9ed1961 | 2008-10-23 15:05:59 -0400 | [diff] [blame] | 552 | if (ref_last == -1 && ofs_last == -1) { |
| 553 | free(base->data); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 554 | return; |
Nicolas Pitre | 9ed1961 | 2008-10-23 15:05:59 -0400 | [diff] [blame] | 555 | } |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 556 | |
| 557 | link_base_data(prev_base, base); |
| 558 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 559 | for (i = ref_first; i <= ref_last; i++) { |
| 560 | struct object_entry *child = objects + deltas[i].obj_no; |
| 561 | if (child->real_type == OBJ_REF_DELTA) { |
| 562 | struct base_data result; |
| 563 | resolve_delta(child, base, &result); |
| 564 | if (i == ref_last && ofs_last == -1) |
| 565 | free_base_data(base); |
| 566 | find_unresolved_deltas(&result, base); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 567 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 568 | } |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 569 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 570 | for (i = ofs_first; i <= ofs_last; i++) { |
| 571 | struct object_entry *child = objects + deltas[i].obj_no; |
| 572 | if (child->real_type == OBJ_OFS_DELTA) { |
| 573 | struct base_data result; |
| 574 | resolve_delta(child, base, &result); |
| 575 | if (i == ofs_last) |
| 576 | free_base_data(base); |
| 577 | find_unresolved_deltas(&result, base); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 578 | } |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 581 | unlink_base_data(base); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static int compare_delta_entry(const void *a, const void *b) |
| 585 | { |
| 586 | const struct delta_entry *delta_a = a; |
| 587 | const struct delta_entry *delta_b = b; |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 588 | return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 591 | /* Parse all objects and return the pack content SHA1 hash */ |
| 592 | static void parse_pack_objects(unsigned char *sha1) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 593 | { |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 594 | int i; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 595 | struct delta_entry *delta = deltas; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 596 | struct stat st; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 597 | |
| 598 | /* |
| 599 | * First pass: |
| 600 | * - find locations of all objects; |
| 601 | * - calculate SHA1 of all non-delta objects; |
Nicolas Pitre | b89c4e9 | 2006-10-27 16:14:23 -0400 | [diff] [blame] | 602 | * - remember base (SHA1 or offset) for all deltas. |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 603 | */ |
Nicolas Pitre | 13aaf14 | 2007-04-20 14:10:07 -0400 | [diff] [blame] | 604 | if (verbose) |
Nicolas Pitre | 29e63ed | 2007-10-30 14:57:35 -0400 | [diff] [blame] | 605 | progress = start_progress( |
| 606 | from_stdin ? "Receiving objects" : "Indexing objects", |
| 607 | nr_objects); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 608 | for (i = 0; i < nr_objects; i++) { |
| 609 | struct object_entry *obj = &objects[i]; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 610 | void *data = unpack_raw_entry(obj, &delta->base); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 611 | obj->real_type = obj->type; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 612 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) { |
| 613 | nr_deltas++; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 614 | delta->obj_no = i; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 615 | delta++; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 616 | } else |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 617 | sha1_object(data, obj->size, obj->type, obj->idx.sha1); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 618 | free(data); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 619 | display_progress(progress, i+1); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 620 | } |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 621 | objects[i].idx.offset = consumed_bytes; |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 622 | stop_progress(&progress); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 623 | |
| 624 | /* Check pack integrity */ |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 625 | flush(); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 626 | git_SHA1_Final(sha1, &input_ctx); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 627 | if (hashcmp(fill(20), sha1)) |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 628 | die("pack is corrupted (SHA1 mismatch)"); |
Nicolas Pitre | 9bee247 | 2006-10-25 23:31:53 -0400 | [diff] [blame] | 629 | use(20); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 630 | |
| 631 | /* If input_fd is a file, we should have reached its end now. */ |
| 632 | if (fstat(input_fd, &st)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 633 | die_errno("cannot fstat packfile"); |
Johannes Schindelin | fa257b0 | 2007-02-22 19:14:14 +0100 | [diff] [blame] | 634 | if (S_ISREG(st.st_mode) && |
| 635 | lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 636 | die("pack has junk at the end"); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 637 | |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 638 | if (!nr_deltas) |
| 639 | return; |
| 640 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 641 | /* Sort deltas by base SHA1/offset for fast searching */ |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 642 | qsort(deltas, nr_deltas, sizeof(struct delta_entry), |
| 643 | compare_delta_entry); |
| 644 | |
| 645 | /* |
| 646 | * Second pass: |
| 647 | * - for all non-delta objects, look if it is used as a base for |
| 648 | * deltas; |
| 649 | * - if used as a base, uncompress the object and apply all deltas, |
| 650 | * recursively checking if the resulting object is used as a base |
| 651 | * for some more deltas. |
| 652 | */ |
Nicolas Pitre | 13aaf14 | 2007-04-20 14:10:07 -0400 | [diff] [blame] | 653 | if (verbose) |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 654 | progress = start_progress("Resolving deltas", nr_deltas); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 655 | for (i = 0; i < nr_objects; i++) { |
| 656 | struct object_entry *obj = &objects[i]; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 657 | struct base_data base_obj; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 658 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 659 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 660 | continue; |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 661 | base_obj.obj = obj; |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 662 | base_obj.data = NULL; |
| 663 | find_unresolved_deltas(&base_obj, NULL); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 664 | display_progress(progress, nr_resolved_deltas); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 665 | } |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 666 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 667 | |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 668 | static int write_compressed(struct sha1file *f, void *in, unsigned int size) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 669 | { |
| 670 | z_stream stream; |
| 671 | unsigned long maxsize; |
| 672 | void *out; |
| 673 | |
| 674 | memset(&stream, 0, sizeof(stream)); |
| 675 | deflateInit(&stream, zlib_compression_level); |
| 676 | maxsize = deflateBound(&stream, size); |
| 677 | out = xmalloc(maxsize); |
| 678 | |
| 679 | /* Compress it */ |
| 680 | stream.next_in = in; |
| 681 | stream.avail_in = size; |
| 682 | stream.next_out = out; |
| 683 | stream.avail_out = maxsize; |
| 684 | while (deflate(&stream, Z_FINISH) == Z_OK); |
| 685 | deflateEnd(&stream); |
| 686 | |
| 687 | size = stream.total_out; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 688 | sha1write(f, out, size); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 689 | free(out); |
| 690 | return size; |
| 691 | } |
| 692 | |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 693 | static struct object_entry *append_obj_to_pack(struct sha1file *f, |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 694 | const unsigned char *sha1, void *buf, |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 695 | unsigned long size, enum object_type type) |
| 696 | { |
| 697 | struct object_entry *obj = &objects[nr_objects++]; |
| 698 | unsigned char header[10]; |
| 699 | unsigned long s = size; |
| 700 | int n = 0; |
| 701 | unsigned char c = (type << 4) | (s & 15); |
| 702 | s >>= 4; |
| 703 | while (s) { |
| 704 | header[n++] = c | 0x80; |
| 705 | c = s & 0x7f; |
| 706 | s >>= 7; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 707 | } |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 708 | header[n++] = c; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 709 | crc32_begin(f); |
| 710 | sha1write(f, header, n); |
Björn Steinbrink | 72de288 | 2008-07-24 18:32:00 +0100 | [diff] [blame] | 711 | obj[0].size = size; |
| 712 | obj[0].hdr_size = n; |
| 713 | obj[0].type = type; |
| 714 | obj[0].real_type = type; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 715 | obj[1].idx.offset = obj[0].idx.offset + n; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 716 | obj[1].idx.offset += write_compressed(f, buf, size); |
| 717 | obj[0].idx.crc32 = crc32_end(f); |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 718 | sha1flush(f); |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 719 | hashcpy(obj->idx.sha1, sha1); |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 720 | return obj; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static int delta_pos_compare(const void *_a, const void *_b) |
| 724 | { |
| 725 | struct delta_entry *a = *(struct delta_entry **)_a; |
| 726 | struct delta_entry *b = *(struct delta_entry **)_b; |
| 727 | return a->obj_no - b->obj_no; |
| 728 | } |
| 729 | |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 730 | static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 731 | { |
| 732 | struct delta_entry **sorted_by_pos; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 733 | int i, n = 0; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Since many unresolved deltas may well be themselves base objects |
| 737 | * for more unresolved deltas, we really want to include the |
| 738 | * smallest number of base objects that would cover as much delta |
| 739 | * as possible by picking the |
| 740 | * trunc deltas first, allowing for other deltas to resolve without |
| 741 | * additional base objects. Since most base objects are to be found |
| 742 | * before deltas depending on them, a good heuristic is to start |
| 743 | * resolving deltas in the same order as their position in the pack. |
| 744 | */ |
| 745 | sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos)); |
| 746 | for (i = 0; i < nr_deltas; i++) { |
| 747 | if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA) |
| 748 | continue; |
| 749 | sorted_by_pos[n++] = &deltas[i]; |
| 750 | } |
| 751 | qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare); |
| 752 | |
| 753 | for (i = 0; i < n; i++) { |
| 754 | struct delta_entry *d = sorted_by_pos[i]; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 755 | enum object_type type; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 756 | struct base_data base_obj; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 757 | |
| 758 | if (objects[d->obj_no].real_type != OBJ_REF_DELTA) |
| 759 | continue; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 760 | base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size); |
| 761 | if (!base_obj.data) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 762 | continue; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 763 | |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 764 | if (check_sha1_signature(d->base.sha1, base_obj.data, |
| 765 | base_obj.size, typename(type))) |
| 766 | die("local object %s is corrupt", sha1_to_hex(d->base.sha1)); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 767 | base_obj.obj = append_obj_to_pack(f, d->base.sha1, |
| 768 | base_obj.data, base_obj.size, type); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 769 | find_unresolved_deltas(&base_obj, NULL); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 770 | display_progress(progress, nr_resolved_deltas); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 771 | } |
| 772 | free(sorted_by_pos); |
| 773 | } |
| 774 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 775 | static void final(const char *final_pack_name, const char *curr_pack_name, |
| 776 | const char *final_index_name, const char *curr_index_name, |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 777 | const char *keep_name, const char *keep_msg, |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 778 | unsigned char *sha1) |
| 779 | { |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 780 | const char *report = "pack"; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 781 | char name[PATH_MAX]; |
| 782 | int err; |
| 783 | |
| 784 | if (!from_stdin) { |
| 785 | close(input_fd); |
| 786 | } else { |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 787 | fsync_or_die(output_fd, curr_pack_name); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 788 | err = close(output_fd); |
| 789 | if (err) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 790 | die_errno("error while closing pack file"); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 793 | if (keep_msg) { |
| 794 | int keep_fd, keep_msg_len = strlen(keep_msg); |
Junio C Hamano | 6e180cd | 2009-02-24 23:11:29 -0800 | [diff] [blame] | 795 | |
| 796 | if (!keep_name) |
| 797 | keep_fd = odb_pack_keep(name, sizeof(name), sha1); |
| 798 | else |
| 799 | keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 800 | |
Nicolas Pitre | 9ca4a20 | 2006-11-01 17:06:24 -0500 | [diff] [blame] | 801 | if (keep_fd < 0) { |
| 802 | if (errno != EEXIST) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 803 | die_errno("cannot write keep file '%s'", |
| 804 | keep_name); |
Nicolas Pitre | 9ca4a20 | 2006-11-01 17:06:24 -0500 | [diff] [blame] | 805 | } else { |
| 806 | if (keep_msg_len > 0) { |
| 807 | write_or_die(keep_fd, keep_msg, keep_msg_len); |
| 808 | write_or_die(keep_fd, "\n", 1); |
| 809 | } |
Jim Meyering | 91c8d59 | 2007-06-24 21:20:41 +0200 | [diff] [blame] | 810 | if (close(keep_fd) != 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 811 | die_errno("cannot close written keep file '%s'", |
| 812 | keep_name); |
Nicolas Pitre | 576162a | 2006-11-01 17:06:25 -0500 | [diff] [blame] | 813 | report = "keep"; |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 814 | } |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 817 | if (final_pack_name != curr_pack_name) { |
| 818 | if (!final_pack_name) { |
| 819 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", |
| 820 | get_object_directory(), sha1_to_hex(sha1)); |
| 821 | final_pack_name = name; |
| 822 | } |
| 823 | if (move_temp_to_file(curr_pack_name, final_pack_name)) |
| 824 | die("cannot store pack file"); |
Johan Herland | fb8b193 | 2009-03-26 16:16:47 +0100 | [diff] [blame] | 825 | } else if (from_stdin) |
Petr Baudis | 33b6503 | 2008-10-03 12:20:43 +0200 | [diff] [blame] | 826 | chmod(final_pack_name, 0444); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 827 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 828 | if (final_index_name != curr_index_name) { |
| 829 | if (!final_index_name) { |
| 830 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", |
| 831 | get_object_directory(), sha1_to_hex(sha1)); |
| 832 | final_index_name = name; |
| 833 | } |
| 834 | if (move_temp_to_file(curr_index_name, final_index_name)) |
| 835 | die("cannot store index file"); |
Johan Herland | fb8b193 | 2009-03-26 16:16:47 +0100 | [diff] [blame] | 836 | } else |
| 837 | chmod(final_index_name, 0444); |
Nicolas Pitre | 576162a | 2006-11-01 17:06:25 -0500 | [diff] [blame] | 838 | |
| 839 | if (!from_stdin) { |
| 840 | printf("%s\n", sha1_to_hex(sha1)); |
| 841 | } else { |
| 842 | char buf[48]; |
| 843 | int len = snprintf(buf, sizeof(buf), "%s\t%s\n", |
| 844 | report, sha1_to_hex(sha1)); |
Junio C Hamano | d1b2ddc | 2007-01-11 13:15:51 -0800 | [diff] [blame] | 845 | write_or_die(1, buf, len); |
Nicolas Pitre | 576162a | 2006-11-01 17:06:25 -0500 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * Let's just mimic git-unpack-objects here and write |
| 849 | * the last part of the input buffer to stdout. |
| 850 | */ |
| 851 | while (input_len) { |
| 852 | err = xwrite(1, input_buffer + input_offset, input_len); |
| 853 | if (err <= 0) |
| 854 | break; |
| 855 | input_len -= err; |
| 856 | input_offset += err; |
| 857 | } |
| 858 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 861 | static int git_index_pack_config(const char *k, const char *v, void *cb) |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 862 | { |
| 863 | if (!strcmp(k, "pack.indexversion")) { |
| 864 | pack_idx_default_version = git_config_int(k, v); |
| 865 | if (pack_idx_default_version > 2) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 866 | die("bad pack.indexversion=%"PRIu32, |
| 867 | pack_idx_default_version); |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 868 | return 0; |
| 869 | } |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 870 | return git_default_config(k, v, cb); |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 871 | } |
| 872 | |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 873 | int cmd_index_pack(int argc, const char **argv, const char *prefix) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 874 | { |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 875 | int i, fix_thin_pack = 0; |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 876 | const char *curr_pack, *curr_index; |
| 877 | const char *index_name = NULL, *pack_name = NULL; |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 878 | const char *keep_name = NULL, *keep_msg = NULL; |
| 879 | char *index_name_buf = NULL, *keep_name_buf = NULL; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 880 | struct pack_idx_entry **idx_objects; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 881 | unsigned char pack_sha1[20]; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 882 | |
Jonathan Nieder | 99caeed | 2009-11-09 09:05:01 -0600 | [diff] [blame] | 883 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 884 | usage(index_pack_usage); |
| 885 | |
Nicolas Pitre | a672ea6 | 2008-10-20 21:17:07 -0400 | [diff] [blame] | 886 | /* |
| 887 | * We wish to read the repository's config file if any, and |
| 888 | * for that it is necessary to call setup_git_directory_gently(). |
| 889 | * However if the cwd was inside .git/objects/pack/ then we need |
| 890 | * to go back there or all the pack name arguments will be wrong. |
| 891 | * And in that case we cannot rely on any prefix returned by |
| 892 | * setup_git_directory_gently() either. |
| 893 | */ |
| 894 | { |
| 895 | char cwd[PATH_MAX+1]; |
| 896 | int nongit; |
| 897 | |
| 898 | if (!getcwd(cwd, sizeof(cwd)-1)) |
| 899 | die("Unable to get current working directory"); |
| 900 | setup_git_directory_gently(&nongit); |
| 901 | git_config(git_index_pack_config, NULL); |
| 902 | if (chdir(cwd)) |
| 903 | die("Cannot come back to cwd"); |
| 904 | } |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 905 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 906 | for (i = 1; i < argc; i++) { |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 907 | const char *arg = argv[i]; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 908 | |
| 909 | if (*arg == '-') { |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 910 | if (!strcmp(arg, "--stdin")) { |
| 911 | from_stdin = 1; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 912 | } else if (!strcmp(arg, "--fix-thin")) { |
| 913 | fix_thin_pack = 1; |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 914 | } else if (!strcmp(arg, "--strict")) { |
| 915 | strict = 1; |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 916 | } else if (!strcmp(arg, "--keep")) { |
| 917 | keep_msg = ""; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 918 | } else if (!prefixcmp(arg, "--keep=")) { |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 919 | keep_msg = arg + 7; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 920 | } else if (!prefixcmp(arg, "--pack_header=")) { |
Nicolas Pitre | bed006f | 2006-11-01 17:06:20 -0500 | [diff] [blame] | 921 | struct pack_header *hdr; |
| 922 | char *c; |
| 923 | |
| 924 | hdr = (struct pack_header *)input_buffer; |
| 925 | hdr->hdr_signature = htonl(PACK_SIGNATURE); |
| 926 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); |
| 927 | if (*c != ',') |
| 928 | die("bad %s", arg); |
| 929 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); |
| 930 | if (*c) |
| 931 | die("bad %s", arg); |
| 932 | input_len = sizeof(*hdr); |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 933 | } else if (!strcmp(arg, "-v")) { |
| 934 | verbose = 1; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 935 | } else if (!strcmp(arg, "-o")) { |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 936 | if (index_name || (i+1) >= argc) |
| 937 | usage(index_pack_usage); |
| 938 | index_name = argv[++i]; |
Nicolas Pitre | 4ba7d71 | 2007-04-09 17:32:03 -0400 | [diff] [blame] | 939 | } else if (!prefixcmp(arg, "--index-version=")) { |
| 940 | char *c; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 941 | pack_idx_default_version = strtoul(arg + 16, &c, 10); |
| 942 | if (pack_idx_default_version > 2) |
Nicolas Pitre | 4ba7d71 | 2007-04-09 17:32:03 -0400 | [diff] [blame] | 943 | die("bad %s", arg); |
| 944 | if (*c == ',') |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 945 | pack_idx_off32_limit = strtoul(c+1, &c, 0); |
| 946 | if (*c || pack_idx_off32_limit & 0x80000000) |
Nicolas Pitre | 4ba7d71 | 2007-04-09 17:32:03 -0400 | [diff] [blame] | 947 | die("bad %s", arg); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 948 | } else |
| 949 | usage(index_pack_usage); |
| 950 | continue; |
| 951 | } |
| 952 | |
| 953 | if (pack_name) |
| 954 | usage(index_pack_usage); |
| 955 | pack_name = arg; |
| 956 | } |
| 957 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 958 | if (!pack_name && !from_stdin) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 959 | usage(index_pack_usage); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 960 | if (fix_thin_pack && !from_stdin) |
| 961 | die("--fix-thin cannot be used without --stdin"); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 962 | if (!index_name && pack_name) { |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 963 | int len = strlen(pack_name); |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 964 | if (!has_extension(pack_name, ".pack")) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 965 | die("packfile name '%s' does not end with '.pack'", |
| 966 | pack_name); |
Pavel Roskin | 6689f08 | 2005-12-21 15:35:48 -0500 | [diff] [blame] | 967 | index_name_buf = xmalloc(len); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 968 | memcpy(index_name_buf, pack_name, len - 5); |
| 969 | strcpy(index_name_buf + len - 5, ".idx"); |
| 970 | index_name = index_name_buf; |
| 971 | } |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 972 | if (keep_msg && !keep_name && pack_name) { |
| 973 | int len = strlen(pack_name); |
| 974 | if (!has_extension(pack_name, ".pack")) |
| 975 | die("packfile name '%s' does not end with '.pack'", |
| 976 | pack_name); |
| 977 | keep_name_buf = xmalloc(len); |
| 978 | memcpy(keep_name_buf, pack_name, len - 5); |
| 979 | strcpy(keep_name_buf + len - 5, ".keep"); |
| 980 | keep_name = keep_name_buf; |
| 981 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 982 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 983 | curr_pack = open_pack_file(pack_name); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 984 | parse_pack_header(); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 985 | objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry)); |
| 986 | deltas = xmalloc(nr_objects * sizeof(struct delta_entry)); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 987 | parse_pack_objects(pack_sha1); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 988 | if (nr_deltas == nr_resolved_deltas) { |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 989 | stop_progress(&progress); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 990 | /* Flush remaining pack final 20-byte SHA1. */ |
| 991 | flush(); |
| 992 | } else { |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 993 | if (fix_thin_pack) { |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 994 | struct sha1file *f; |
| 995 | unsigned char read_sha1[20], tail_sha1[20]; |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 996 | char msg[48]; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 997 | int nr_unresolved = nr_deltas - nr_resolved_deltas; |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 998 | int nr_objects_initial = nr_objects; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 999 | if (nr_unresolved <= 0) |
| 1000 | die("confusion beyond insanity"); |
| 1001 | objects = xrealloc(objects, |
| 1002 | (nr_objects + nr_unresolved + 1) |
| 1003 | * sizeof(*objects)); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1004 | f = sha1fd(output_fd, curr_pack); |
| 1005 | fix_unresolved_deltas(f, nr_unresolved); |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 1006 | sprintf(msg, "completed with %d local objects", |
| 1007 | nr_objects - nr_objects_initial); |
| 1008 | stop_progress_msg(&progress, msg); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1009 | sha1close(f, tail_sha1, 0); |
| 1010 | hashcpy(read_sha1, pack_sha1); |
| 1011 | fixup_pack_header_footer(output_fd, pack_sha1, |
Nicolas Pitre | abeb40e | 2008-08-29 16:07:59 -0400 | [diff] [blame] | 1012 | curr_pack, nr_objects, |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1013 | read_sha1, consumed_bytes-20); |
| 1014 | if (hashcmp(read_sha1, tail_sha1) != 0) |
| 1015 | die("Unexpected tail checksum for %s " |
| 1016 | "(disk corruption?)", curr_pack); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 1017 | } |
| 1018 | if (nr_deltas != nr_resolved_deltas) |
| 1019 | die("pack has %d unresolved deltas", |
| 1020 | nr_deltas - nr_resolved_deltas); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 1021 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1022 | free(deltas); |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 1023 | if (strict) |
| 1024 | check_objects(); |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 1025 | |
| 1026 | idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *)); |
| 1027 | for (i = 0; i < nr_objects; i++) |
| 1028 | idx_objects[i] = &objects[i].idx; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1029 | curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1); |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 1030 | free(idx_objects); |
| 1031 | |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 1032 | final(pack_name, curr_pack, |
| 1033 | index_name, curr_index, |
| 1034 | keep_name, keep_msg, |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1035 | pack_sha1); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1036 | free(objects); |
| 1037 | free(index_name_buf); |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 1038 | free(keep_name_buf); |
Nicolas Pitre | c85228e | 2007-10-16 21:55:50 -0400 | [diff] [blame] | 1039 | if (pack_name == NULL) |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 1040 | free((void *) curr_pack); |
Nicolas Pitre | c85228e | 2007-10-16 21:55:50 -0400 | [diff] [blame] | 1041 | if (index_name == NULL) |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 1042 | free((void *) curr_index); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1043 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |