2011-04-07 22:23:40 +04:00
|
|
|
#include "builtin.h"
|
2005-10-12 23:01:31 +04:00
|
|
|
#include "delta.h"
|
|
|
|
#include "pack.h"
|
|
|
|
#include "csum-file.h"
|
2006-04-02 16:44:09 +04:00
|
|
|
#include "blob.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "tag.h"
|
|
|
|
#include "tree.h"
|
2007-04-18 22:27:45 +04:00
|
|
|
#include "progress.h"
|
2008-02-26 00:46:12 +03:00
|
|
|
#include "fsck.h"
|
2009-01-18 15:00:12 +03:00
|
|
|
#include "exec_cmd.h"
|
2012-05-24 17:55:44 +04:00
|
|
|
#include "streaming.h"
|
2012-05-06 16:31:55 +04:00
|
|
|
#include "thread-utils.h"
|
2005-10-12 23:01:31 +04:00
|
|
|
|
|
|
|
static const char index_pack_usage[] =
|
2011-02-03 04:29:01 +03:00
|
|
|
"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2011-03-16 10:08:34 +03:00
|
|
|
struct object_entry {
|
2007-06-01 23:18:05 +04:00
|
|
|
struct pack_idx_entry idx;
|
2006-10-20 22:45:21 +04:00
|
|
|
unsigned long size;
|
|
|
|
unsigned int hdr_size;
|
2005-10-12 23:01:31 +04:00
|
|
|
enum object_type type;
|
|
|
|
enum object_type real_type;
|
2011-06-04 02:32:15 +04:00
|
|
|
unsigned delta_depth;
|
|
|
|
int base_object_no;
|
2005-10-12 23:01:31 +04:00
|
|
|
};
|
|
|
|
|
2006-09-21 08:08:33 +04:00
|
|
|
union delta_base {
|
|
|
|
unsigned char sha1[20];
|
2007-04-09 09:06:30 +04:00
|
|
|
off_t offset;
|
2006-09-21 08:08:33 +04:00
|
|
|
};
|
|
|
|
|
2008-07-14 06:07:44 +04:00
|
|
|
struct base_data {
|
2008-07-14 06:07:45 +04:00
|
|
|
struct base_data *base;
|
|
|
|
struct base_data *child;
|
2008-07-14 06:07:46 +04:00
|
|
|
struct object_entry *obj;
|
2008-07-14 06:07:44 +04:00
|
|
|
void *data;
|
|
|
|
unsigned long size;
|
2012-01-14 16:19:54 +04:00
|
|
|
int ref_first, ref_last;
|
|
|
|
int ofs_first, ofs_last;
|
2008-07-14 06:07:44 +04:00
|
|
|
};
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
struct thread_local {
|
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
pthread_t thread;
|
|
|
|
#endif
|
|
|
|
struct base_data *base_cache;
|
|
|
|
size_t base_cache_used;
|
2014-03-25 17:41:41 +04:00
|
|
|
int pack_fd;
|
2012-05-06 16:31:55 +04:00
|
|
|
};
|
|
|
|
|
2006-10-18 00:23:26 +04:00
|
|
|
/*
|
|
|
|
* Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
|
|
|
|
* to memcmp() only the first 20 bytes.
|
|
|
|
*/
|
|
|
|
#define UNION_BASE_SZ 20
|
|
|
|
|
2008-02-26 00:46:12 +03:00
|
|
|
#define FLAG_LINK (1u<<20)
|
|
|
|
#define FLAG_CHECKED (1u<<21)
|
|
|
|
|
2011-03-16 10:08:34 +03:00
|
|
|
struct delta_entry {
|
2006-09-21 08:08:33 +04:00
|
|
|
union delta_base base;
|
2006-10-26 07:28:17 +04:00
|
|
|
int obj_no;
|
2005-10-12 23:01:31 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct object_entry *objects;
|
|
|
|
static struct delta_entry *deltas;
|
2012-05-06 16:31:55 +04:00
|
|
|
static struct thread_local nothread_data;
|
2005-10-12 23:01:31 +04:00
|
|
|
static int nr_objects;
|
|
|
|
static int nr_deltas;
|
2006-10-26 07:28:17 +04:00
|
|
|
static int nr_resolved_deltas;
|
2012-05-06 16:31:55 +04:00
|
|
|
static int nr_threads;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2006-10-23 22:50:18 +04:00
|
|
|
static int from_stdin;
|
2008-02-26 00:46:12 +03:00
|
|
|
static int strict;
|
2013-05-26 05:16:17 +04:00
|
|
|
static int do_fsck_object;
|
2006-10-26 07:32:59 +04:00
|
|
|
static int verbose;
|
2013-03-19 17:01:15 +04:00
|
|
|
static int show_stat;
|
2013-05-26 05:16:17 +04:00
|
|
|
static int check_self_contained_and_connected;
|
2006-10-26 07:32:59 +04:00
|
|
|
|
2007-10-30 21:57:32 +03:00
|
|
|
static struct progress *progress;
|
2006-10-23 22:50:18 +04:00
|
|
|
|
2006-10-20 22:45:21 +04:00
|
|
|
/* We always read in 4kB chunks. */
|
|
|
|
static unsigned char input_buffer[4096];
|
2007-04-09 09:06:30 +04:00
|
|
|
static unsigned int input_offset, input_len;
|
|
|
|
static off_t consumed_bytes;
|
2011-06-04 02:32:16 +04:00
|
|
|
static unsigned deepest_delta;
|
2008-10-01 22:05:20 +04:00
|
|
|
static git_SHA_CTX input_ctx;
|
2007-04-09 09:06:32 +04:00
|
|
|
static uint32_t input_crc32;
|
2014-03-25 17:41:41 +04:00
|
|
|
static int input_fd, output_fd;
|
|
|
|
static const char *curr_pack;
|
2006-10-20 22:45:21 +04:00
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
|
|
|
|
static struct thread_local *thread_data;
|
|
|
|
static int nr_dispatched;
|
|
|
|
static int threads_active;
|
|
|
|
|
|
|
|
static pthread_mutex_t read_mutex;
|
|
|
|
#define read_lock() lock_mutex(&read_mutex)
|
|
|
|
#define read_unlock() unlock_mutex(&read_mutex)
|
|
|
|
|
|
|
|
static pthread_mutex_t counter_mutex;
|
|
|
|
#define counter_lock() lock_mutex(&counter_mutex)
|
|
|
|
#define counter_unlock() unlock_mutex(&counter_mutex)
|
|
|
|
|
|
|
|
static pthread_mutex_t work_mutex;
|
|
|
|
#define work_lock() lock_mutex(&work_mutex)
|
|
|
|
#define work_unlock() unlock_mutex(&work_mutex)
|
|
|
|
|
2013-03-19 17:01:15 +04:00
|
|
|
static pthread_mutex_t deepest_delta_mutex;
|
|
|
|
#define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
|
|
|
|
#define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
|
|
|
|
|
index-pack: fix race condition with duplicate bases
When we are resolving deltas in an indexed pack, we do it by
first selecting a potential base (either one stored in full
in the pack, or one created by resolving another delta), and
then resolving any deltas that use that base. When we
resolve a particular delta, we flip its "real_type" field
from OBJ_{REF,OFS}_DELTA to whatever the real type is.
We assume that traversing the objects this way will visit
each delta only once. This is correct for most packs; we
visit the delta only when we process its base, and each
object (and thus each base) appears only once. However, if a
base object appears multiple times in the pack, we will try
to resolve any deltas based on it once for each instance.
We can detect this case by noting that a delta we are about
to resolve has already had its real_type field flipped, and
we already do so with an assert(). However, if multiple
threads are in use, we may race with another thread on
comparing and flipping the field. We need to synchronize the
access.
The right mechanism for doing this is a compare-and-swap (we
atomically "claim" the delta for our own and find out
whether our claim was successful). We can implement this
in C by using a pthread mutex to protect the operation. This
is not the fastest way of doing a compare-and-swap; many
processors provide instructions for this, and gcc and other
compilers provide builtins to access them. However, some
experiments showed that lock contention does not cause a
significant slowdown here. Adding c-a-s support for many
compilers would increase the maintenance burden (and we
would still end up including the pthread version as a
fallback).
Note that we only need to touch the OBJ_REF_DELTA codepath
here. An OBJ_OFS_DELTA object points to its base using an
offset, and therefore has only one base, even if another
copy of that base object appears in the pack (we do still
touch it briefly because the setting of real_type is
factored out of resolve_data).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-08-30 00:57:47 +04:00
|
|
|
static pthread_mutex_t type_cas_mutex;
|
|
|
|
#define type_cas_lock() lock_mutex(&type_cas_mutex)
|
|
|
|
#define type_cas_unlock() unlock_mutex(&type_cas_mutex)
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
static pthread_key_t key;
|
|
|
|
|
|
|
|
static inline void lock_mutex(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (threads_active)
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void unlock_mutex(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (threads_active)
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mutex and conditional variable can't be statically-initialized on Windows.
|
|
|
|
*/
|
|
|
|
static void init_thread(void)
|
|
|
|
{
|
2014-03-25 17:41:41 +04:00
|
|
|
int i;
|
2012-05-06 16:31:55 +04:00
|
|
|
init_recursive_mutex(&read_mutex);
|
|
|
|
pthread_mutex_init(&counter_mutex, NULL);
|
|
|
|
pthread_mutex_init(&work_mutex, NULL);
|
index-pack: fix race condition with duplicate bases
When we are resolving deltas in an indexed pack, we do it by
first selecting a potential base (either one stored in full
in the pack, or one created by resolving another delta), and
then resolving any deltas that use that base. When we
resolve a particular delta, we flip its "real_type" field
from OBJ_{REF,OFS}_DELTA to whatever the real type is.
We assume that traversing the objects this way will visit
each delta only once. This is correct for most packs; we
visit the delta only when we process its base, and each
object (and thus each base) appears only once. However, if a
base object appears multiple times in the pack, we will try
to resolve any deltas based on it once for each instance.
We can detect this case by noting that a delta we are about
to resolve has already had its real_type field flipped, and
we already do so with an assert(). However, if multiple
threads are in use, we may race with another thread on
comparing and flipping the field. We need to synchronize the
access.
The right mechanism for doing this is a compare-and-swap (we
atomically "claim" the delta for our own and find out
whether our claim was successful). We can implement this
in C by using a pthread mutex to protect the operation. This
is not the fastest way of doing a compare-and-swap; many
processors provide instructions for this, and gcc and other
compilers provide builtins to access them. However, some
experiments showed that lock contention does not cause a
significant slowdown here. Adding c-a-s support for many
compilers would increase the maintenance burden (and we
would still end up including the pthread version as a
fallback).
Note that we only need to touch the OBJ_REF_DELTA codepath
here. An OBJ_OFS_DELTA object points to its base using an
offset, and therefore has only one base, even if another
copy of that base object appears in the pack (we do still
touch it briefly because the setting of real_type is
factored out of resolve_data).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-08-30 00:57:47 +04:00
|
|
|
pthread_mutex_init(&type_cas_mutex, NULL);
|
2013-03-19 17:01:15 +04:00
|
|
|
if (show_stat)
|
|
|
|
pthread_mutex_init(&deepest_delta_mutex, NULL);
|
2012-05-06 16:31:55 +04:00
|
|
|
pthread_key_create(&key, NULL);
|
|
|
|
thread_data = xcalloc(nr_threads, sizeof(*thread_data));
|
2014-03-25 17:41:41 +04:00
|
|
|
for (i = 0; i < nr_threads; i++) {
|
|
|
|
thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
|
|
|
|
if (thread_data[i].pack_fd == -1)
|
|
|
|
die_errno(_("unable to open %s"), curr_pack);
|
|
|
|
}
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
threads_active = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_thread(void)
|
|
|
|
{
|
2014-03-25 17:41:41 +04:00
|
|
|
int i;
|
2012-05-06 16:31:55 +04:00
|
|
|
if (!threads_active)
|
|
|
|
return;
|
|
|
|
threads_active = 0;
|
|
|
|
pthread_mutex_destroy(&read_mutex);
|
|
|
|
pthread_mutex_destroy(&counter_mutex);
|
|
|
|
pthread_mutex_destroy(&work_mutex);
|
index-pack: fix race condition with duplicate bases
When we are resolving deltas in an indexed pack, we do it by
first selecting a potential base (either one stored in full
in the pack, or one created by resolving another delta), and
then resolving any deltas that use that base. When we
resolve a particular delta, we flip its "real_type" field
from OBJ_{REF,OFS}_DELTA to whatever the real type is.
We assume that traversing the objects this way will visit
each delta only once. This is correct for most packs; we
visit the delta only when we process its base, and each
object (and thus each base) appears only once. However, if a
base object appears multiple times in the pack, we will try
to resolve any deltas based on it once for each instance.
We can detect this case by noting that a delta we are about
to resolve has already had its real_type field flipped, and
we already do so with an assert(). However, if multiple
threads are in use, we may race with another thread on
comparing and flipping the field. We need to synchronize the
access.
The right mechanism for doing this is a compare-and-swap (we
atomically "claim" the delta for our own and find out
whether our claim was successful). We can implement this
in C by using a pthread mutex to protect the operation. This
is not the fastest way of doing a compare-and-swap; many
processors provide instructions for this, and gcc and other
compilers provide builtins to access them. However, some
experiments showed that lock contention does not cause a
significant slowdown here. Adding c-a-s support for many
compilers would increase the maintenance burden (and we
would still end up including the pthread version as a
fallback).
Note that we only need to touch the OBJ_REF_DELTA codepath
here. An OBJ_OFS_DELTA object points to its base using an
offset, and therefore has only one base, even if another
copy of that base object appears in the pack (we do still
touch it briefly because the setting of real_type is
factored out of resolve_data).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-08-30 00:57:47 +04:00
|
|
|
pthread_mutex_destroy(&type_cas_mutex);
|
2013-03-19 17:01:15 +04:00
|
|
|
if (show_stat)
|
|
|
|
pthread_mutex_destroy(&deepest_delta_mutex);
|
2014-03-25 17:41:41 +04:00
|
|
|
for (i = 0; i < nr_threads; i++)
|
|
|
|
close(thread_data[i].pack_fd);
|
2012-05-06 16:31:55 +04:00
|
|
|
pthread_key_delete(key);
|
|
|
|
free(thread_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define read_lock()
|
|
|
|
#define read_unlock()
|
|
|
|
|
|
|
|
#define counter_lock()
|
|
|
|
#define counter_unlock()
|
|
|
|
|
|
|
|
#define work_lock()
|
|
|
|
#define work_unlock()
|
|
|
|
|
2013-03-19 17:01:15 +04:00
|
|
|
#define deepest_delta_lock()
|
|
|
|
#define deepest_delta_unlock()
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-02-26 00:46:12 +03:00
|
|
|
static int mark_link(struct object *obj, int type, void *data)
|
|
|
|
{
|
|
|
|
if (!obj)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (type != OBJ_ANY && obj->type != type)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("object type mismatch at %s"), sha1_to_hex(obj->sha1));
|
2008-02-26 00:46:12 +03:00
|
|
|
|
|
|
|
obj->flags |= FLAG_LINK;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The content of each linked object must have been checked
|
|
|
|
or it must be already present in the object database */
|
2013-05-26 05:16:17 +04:00
|
|
|
static unsigned check_object(struct object *obj)
|
2008-02-26 00:46:12 +03:00
|
|
|
{
|
|
|
|
if (!obj)
|
2013-05-26 05:16:17 +04:00
|
|
|
return 0;
|
2008-02-26 00:46:12 +03:00
|
|
|
|
|
|
|
if (!(obj->flags & FLAG_LINK))
|
2013-05-26 05:16:17 +04:00
|
|
|
return 0;
|
2008-02-26 00:46:12 +03:00
|
|
|
|
|
|
|
if (!(obj->flags & FLAG_CHECKED)) {
|
|
|
|
unsigned long size;
|
|
|
|
int type = sha1_object_info(obj->sha1, &size);
|
2014-05-12 08:38:39 +04:00
|
|
|
if (type <= 0)
|
|
|
|
die(_("did not receive expected object %s"),
|
|
|
|
sha1_to_hex(obj->sha1));
|
|
|
|
if (type != obj->type)
|
|
|
|
die(_("object %s: expected type %s, found %s"),
|
|
|
|
sha1_to_hex(obj->sha1),
|
|
|
|
typename(obj->type), typename(type));
|
2008-02-26 00:46:12 +03:00
|
|
|
obj->flags |= FLAG_CHECKED;
|
2013-05-26 05:16:17 +04:00
|
|
|
return 1;
|
2008-02-26 00:46:12 +03:00
|
|
|
}
|
2013-05-26 05:16:17 +04:00
|
|
|
|
|
|
|
return 0;
|
2008-02-26 00:46:12 +03:00
|
|
|
}
|
|
|
|
|
2013-05-26 05:16:17 +04:00
|
|
|
static unsigned check_objects(void)
|
2008-02-26 00:46:12 +03:00
|
|
|
{
|
2013-05-26 05:16:17 +04:00
|
|
|
unsigned i, max, foreign_nr = 0;
|
2008-02-26 00:46:12 +03:00
|
|
|
|
|
|
|
max = get_max_object_index();
|
|
|
|
for (i = 0; i < max; i++)
|
2013-05-26 05:16:17 +04:00
|
|
|
foreign_nr += check_object(get_indexed_object(i));
|
|
|
|
return foreign_nr;
|
2008-02-26 00:46:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-26 07:28:17 +04:00
|
|
|
/* Discard current buffer used content. */
|
2006-11-18 15:07:06 +03:00
|
|
|
static void flush(void)
|
2006-10-26 07:28:17 +04:00
|
|
|
{
|
|
|
|
if (input_offset) {
|
|
|
|
if (output_fd >= 0)
|
|
|
|
write_or_die(output_fd, input_buffer, input_offset);
|
2008-10-01 22:05:20 +04:00
|
|
|
git_SHA1_Update(&input_ctx, input_buffer, input_offset);
|
2006-12-11 21:06:34 +03:00
|
|
|
memmove(input_buffer, input_buffer + input_offset, input_len);
|
2006-10-26 07:28:17 +04:00
|
|
|
input_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-20 22:45:21 +04:00
|
|
|
/*
|
|
|
|
* Make sure at least "min" bytes are available in the buffer, and
|
|
|
|
* return the pointer to the buffer.
|
|
|
|
*/
|
2006-10-28 00:14:23 +04:00
|
|
|
static void *fill(int min)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2006-10-20 22:45:21 +04:00
|
|
|
if (min <= input_len)
|
|
|
|
return input_buffer + input_offset;
|
|
|
|
if (min > sizeof(input_buffer))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(Q_("cannot fill %d byte",
|
|
|
|
"cannot fill %d bytes",
|
|
|
|
min),
|
|
|
|
min);
|
2006-10-26 07:28:17 +04:00
|
|
|
flush();
|
2006-10-20 22:45:21 +04:00
|
|
|
do {
|
2007-05-15 16:49:22 +04:00
|
|
|
ssize_t ret = xread(input_fd, input_buffer + input_len,
|
2006-10-20 22:45:21 +04:00
|
|
|
sizeof(input_buffer) - input_len);
|
|
|
|
if (ret <= 0) {
|
|
|
|
if (!ret)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("early EOF"));
|
|
|
|
die_errno(_("read error on input"));
|
2006-10-20 22:45:21 +04:00
|
|
|
}
|
|
|
|
input_len += ret;
|
2007-11-05 06:15:41 +03:00
|
|
|
if (from_stdin)
|
|
|
|
display_throughput(progress, consumed_bytes + input_len);
|
2006-10-20 22:45:21 +04:00
|
|
|
} while (input_len < min);
|
|
|
|
return input_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void use(int bytes)
|
|
|
|
{
|
|
|
|
if (bytes > input_len)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("used more bytes than were available"));
|
2007-04-09 09:06:32 +04:00
|
|
|
input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
|
2006-10-20 22:45:21 +04:00
|
|
|
input_len -= bytes;
|
|
|
|
input_offset += bytes;
|
2007-04-09 09:06:30 +04:00
|
|
|
|
|
|
|
/* make sure off_t is sufficiently large not to wrap */
|
2010-10-05 11:24:10 +04:00
|
|
|
if (signed_add_overflows(consumed_bytes, bytes))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("pack too large for current definition of off_t"));
|
2006-10-20 22:45:21 +04:00
|
|
|
consumed_bytes += bytes;
|
|
|
|
}
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2010-01-22 18:55:19 +03:00
|
|
|
static const char *open_pack_file(const char *pack_name)
|
2006-10-20 22:45:21 +04:00
|
|
|
{
|
2006-10-23 22:50:18 +04:00
|
|
|
if (from_stdin) {
|
|
|
|
input_fd = 0;
|
|
|
|
if (!pack_name) {
|
2011-12-21 05:18:21 +04:00
|
|
|
static char tmp_file[PATH_MAX];
|
|
|
|
output_fd = odb_mkstemp(tmp_file, sizeof(tmp_file),
|
2009-02-25 10:11:29 +03:00
|
|
|
"pack/tmp_pack_XXXXXX");
|
2011-12-21 05:18:21 +04:00
|
|
|
pack_name = xstrdup(tmp_file);
|
2006-10-23 22:50:18 +04:00
|
|
|
} else
|
|
|
|
output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
|
|
|
|
if (output_fd < 0)
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("unable to create '%s'"), pack_name);
|
2014-03-25 17:41:41 +04:00
|
|
|
nothread_data.pack_fd = output_fd;
|
2006-10-23 22:50:18 +04:00
|
|
|
} else {
|
|
|
|
input_fd = open(pack_name, O_RDONLY);
|
|
|
|
if (input_fd < 0)
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("cannot open packfile '%s'"), pack_name);
|
2006-10-23 22:50:18 +04:00
|
|
|
output_fd = -1;
|
2014-03-25 17:41:41 +04:00
|
|
|
nothread_data.pack_fd = input_fd;
|
2006-10-23 22:50:18 +04:00
|
|
|
}
|
2008-10-01 22:05:20 +04:00
|
|
|
git_SHA1_Init(&input_ctx);
|
2006-10-23 22:50:18 +04:00
|
|
|
return pack_name;
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_pack_header(void)
|
|
|
|
{
|
2006-10-20 22:45:21 +04:00
|
|
|
struct pack_header *hdr = fill(sizeof(struct pack_header));
|
2005-10-12 23:01:31 +04:00
|
|
|
|
|
|
|
/* Header consistency check */
|
|
|
|
if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("pack signature mismatch"));
|
2006-02-10 01:50:04 +03:00
|
|
|
if (!pack_version_ok(hdr->hdr_version))
|
2012-08-31 16:13:04 +04:00
|
|
|
die(_("pack version %"PRIu32" unsupported"),
|
2008-07-03 19:52:09 +04:00
|
|
|
ntohl(hdr->hdr_version));
|
2005-10-12 23:01:31 +04:00
|
|
|
|
|
|
|
nr_objects = ntohl(hdr->hdr_entries);
|
2006-10-20 22:45:21 +04:00
|
|
|
use(sizeof(struct pack_header));
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
2009-09-30 22:05:49 +04:00
|
|
|
static NORETURN void bad_object(unsigned long offset, const char *format,
|
|
|
|
...) __attribute__((format (printf, 2, 3)));
|
2005-10-12 23:01:31 +04:00
|
|
|
|
Fix sparse warnings
Fix warnings from 'make check'.
- These files don't include 'builtin.h' causing sparse to complain that
cmd_* isn't declared:
builtin/clone.c:364, builtin/fetch-pack.c:797,
builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78,
builtin/merge-index.c:69, builtin/merge-recursive.c:22
builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426
builtin/notes.c:822, builtin/pack-redundant.c:596,
builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149,
builtin/remote.c:1512, builtin/remote-ext.c:240,
builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384,
builtin/unpack-file.c:25, builtin/var.c:75
- These files have symbols which should be marked static since they're
only file scope:
submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13,
submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79,
unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123,
url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48
- These files redeclare symbols to be different types:
builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571,
usage.c:49, usage.c:58, usage.c:63, usage.c:72
- These files use a literal integer 0 when they really should use a NULL
pointer:
daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362
While we're in the area, clean up some unused #includes in builtin files
(mostly exec_cmd.h).
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-22 10:51:05 +03:00
|
|
|
static NORETURN void bad_object(unsigned long offset, const char *format, ...)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
va_start(params, format);
|
|
|
|
vsnprintf(buf, sizeof(buf), format, params);
|
|
|
|
va_end(params);
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("pack has bad object at offset %lu: %s"), offset, buf);
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
static inline struct thread_local *get_thread_data(void)
|
|
|
|
{
|
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
if (threads_active)
|
|
|
|
return pthread_getspecific(key);
|
|
|
|
assert(!threads_active &&
|
|
|
|
"This should only be reached when all threads are gone");
|
|
|
|
#endif
|
|
|
|
return ¬hread_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
static void set_thread_data(struct thread_local *data)
|
|
|
|
{
|
|
|
|
if (threads_active)
|
|
|
|
pthread_setspecific(key, data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
static struct base_data *alloc_base_data(void)
|
|
|
|
{
|
2014-07-19 17:56:26 +04:00
|
|
|
struct base_data *base = xcalloc(1, sizeof(struct base_data));
|
2012-01-14 16:19:54 +04:00
|
|
|
base->ref_last = -1;
|
|
|
|
base->ofs_last = -1;
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2008-10-17 23:57:58 +04:00
|
|
|
static void free_base_data(struct base_data *c)
|
|
|
|
{
|
|
|
|
if (c->data) {
|
|
|
|
free(c->data);
|
|
|
|
c->data = NULL;
|
2012-05-06 16:31:55 +04:00
|
|
|
get_thread_data()->base_cache_used -= c->size;
|
2008-10-17 23:57:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-15 08:45:34 +04:00
|
|
|
static void prune_base_data(struct base_data *retain)
|
|
|
|
{
|
2009-03-16 00:01:20 +03:00
|
|
|
struct base_data *b;
|
2012-05-06 16:31:55 +04:00
|
|
|
struct thread_local *data = get_thread_data();
|
|
|
|
for (b = data->base_cache;
|
|
|
|
data->base_cache_used > delta_base_cache_limit && b;
|
2008-07-15 08:45:34 +04:00
|
|
|
b = b->child) {
|
2008-10-17 23:57:58 +04:00
|
|
|
if (b->data && b != retain)
|
|
|
|
free_base_data(b);
|
2008-07-15 08:45:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-14 06:07:45 +04:00
|
|
|
static void link_base_data(struct base_data *base, struct base_data *c)
|
|
|
|
{
|
|
|
|
if (base)
|
|
|
|
base->child = c;
|
|
|
|
else
|
2012-05-06 16:31:55 +04:00
|
|
|
get_thread_data()->base_cache = c;
|
2008-07-14 06:07:45 +04:00
|
|
|
|
|
|
|
c->base = base;
|
|
|
|
c->child = NULL;
|
2008-10-17 23:57:57 +04:00
|
|
|
if (c->data)
|
2012-05-06 16:31:55 +04:00
|
|
|
get_thread_data()->base_cache_used += c->size;
|
2008-07-15 08:45:34 +04:00
|
|
|
prune_base_data(c);
|
2008-07-14 06:07:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unlink_base_data(struct base_data *c)
|
|
|
|
{
|
|
|
|
struct base_data *base = c->base;
|
|
|
|
if (base)
|
|
|
|
base->child = NULL;
|
|
|
|
else
|
2012-05-06 16:31:55 +04:00
|
|
|
get_thread_data()->base_cache = NULL;
|
2008-10-17 23:57:58 +04:00
|
|
|
free_base_data(c);
|
2008-07-14 06:07:45 +04:00
|
|
|
}
|
|
|
|
|
2012-05-23 18:09:46 +04:00
|
|
|
static int is_delta_type(enum object_type type)
|
|
|
|
{
|
|
|
|
return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *unpack_entry_data(unsigned long offset, unsigned long size,
|
|
|
|
enum object_type type, unsigned char *sha1)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2012-05-23 18:09:47 +04:00
|
|
|
static char fixed_buf[8192];
|
2010-04-12 20:12:06 +04:00
|
|
|
int status;
|
2011-06-10 22:52:15 +04:00
|
|
|
git_zstream stream;
|
2012-05-23 18:09:47 +04:00
|
|
|
void *buf;
|
2012-05-23 18:09:46 +04:00
|
|
|
git_SHA_CTX c;
|
|
|
|
char hdr[32];
|
|
|
|
int hdrlen;
|
|
|
|
|
|
|
|
if (!is_delta_type(type)) {
|
|
|
|
hdrlen = sprintf(hdr, "%s %lu", typename(type), size) + 1;
|
|
|
|
git_SHA1_Init(&c);
|
|
|
|
git_SHA1_Update(&c, hdr, hdrlen);
|
|
|
|
} else
|
|
|
|
sha1 = NULL;
|
2012-05-23 18:09:47 +04:00
|
|
|
if (type == OBJ_BLOB && size > big_file_threshold)
|
|
|
|
buf = fixed_buf;
|
|
|
|
else
|
|
|
|
buf = xmalloc(size);
|
2005-10-12 23:01:31 +04:00
|
|
|
|
|
|
|
memset(&stream, 0, sizeof(stream));
|
2010-04-12 20:12:06 +04:00
|
|
|
git_inflate_init(&stream);
|
2005-10-12 23:01:31 +04:00
|
|
|
stream.next_out = buf;
|
2012-05-23 18:09:47 +04:00
|
|
|
stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2010-04-12 20:12:06 +04:00
|
|
|
do {
|
2012-05-23 18:09:46 +04:00
|
|
|
unsigned char *last_out = stream.next_out;
|
2006-10-20 22:45:21 +04:00
|
|
|
stream.next_in = fill(1);
|
|
|
|
stream.avail_in = input_len;
|
2010-04-12 20:12:06 +04:00
|
|
|
status = git_inflate(&stream, 0);
|
|
|
|
use(input_len - stream.avail_in);
|
2012-05-23 18:09:46 +04:00
|
|
|
if (sha1)
|
|
|
|
git_SHA1_Update(&c, last_out, stream.next_out - last_out);
|
2012-05-23 18:09:47 +04:00
|
|
|
if (buf == fixed_buf) {
|
|
|
|
stream.next_out = buf;
|
|
|
|
stream.avail_out = sizeof(fixed_buf);
|
|
|
|
}
|
2010-04-12 20:12:06 +04:00
|
|
|
} while (status == Z_OK);
|
|
|
|
if (stream.total_out != size || status != Z_STREAM_END)
|
2012-04-23 16:30:29 +04:00
|
|
|
bad_object(offset, _("inflate returned %d"), status);
|
2009-01-08 06:54:47 +03:00
|
|
|
git_inflate_end(&stream);
|
2012-05-23 18:09:46 +04:00
|
|
|
if (sha1)
|
|
|
|
git_SHA1_Final(sha1, &c);
|
2012-05-23 18:09:47 +04:00
|
|
|
return buf == fixed_buf ? NULL : buf;
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
2012-05-23 18:09:46 +04:00
|
|
|
static void *unpack_raw_entry(struct object_entry *obj,
|
|
|
|
union delta_base *delta_base,
|
|
|
|
unsigned char *sha1)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
Fix big left-shifts of unsigned char
Shifting 'unsigned char' or 'unsigned short' left can result in sign
extension errors, since the C integer promotion rules means that the
unsigned char/short will get implicitly promoted to a signed 'int' due to
the shift (or due to other operations).
This normally doesn't matter, but if you shift things up sufficiently, it
will now set the sign bit in 'int', and a subsequent cast to a bigger type
(eg 'long' or 'unsigned long') will now sign-extend the value despite the
original expression being unsigned.
One example of this would be something like
unsigned long size;
unsigned char c;
size += c << 24;
where despite all the variables being unsigned, 'c << 24' ends up being a
signed entity, and will get sign-extended when then doing the addition in
an 'unsigned long' type.
Since git uses 'unsigned char' pointers extensively, we actually have this
bug in a couple of places.
I may have missed some, but this is the result of looking at
git grep '[^0-9 ][ ]*<<[ ][a-z]' -- '*.c' '*.h'
git grep '<<[ ]*24'
which catches at least the common byte cases (shifting variables by a
variable amount, and shifting by 24 bits).
I also grepped for just 'unsigned char' variables in general, and
converted the ones that most obviously ended up getting implicitly cast
immediately anyway (eg hash_name(), encode_85()).
In addition to just avoiding 'unsigned char', this patch also tries to use
a common idiom for the delta header size thing. We had three different
variations on it: "& 0x7fUL" in one place (getting the sign extension
right), and "& ~0x80" and "& 0x7f" in two other places (not getting it
right). Apart from making them all just avoid using "unsigned char" at
all, I also unified them to then use a simple "& 0x7f".
I considered making a sparse extension which warns about doing implicit
casts from unsigned types to signed types, but it gets rather complex very
quickly, so this is just a hack.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-06-18 04:22:27 +04:00
|
|
|
unsigned char *p;
|
|
|
|
unsigned long size, c;
|
2007-04-09 09:06:30 +04:00
|
|
|
off_t base_offset;
|
2005-10-12 23:01:31 +04:00
|
|
|
unsigned shift;
|
2007-04-09 09:06:32 +04:00
|
|
|
void *data;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2007-06-01 23:18:05 +04:00
|
|
|
obj->idx.offset = consumed_bytes;
|
2011-04-03 11:06:54 +04:00
|
|
|
input_crc32 = crc32(0, NULL, 0);
|
2006-10-20 22:45:21 +04:00
|
|
|
|
|
|
|
p = fill(1);
|
|
|
|
c = *p;
|
|
|
|
use(1);
|
|
|
|
obj->type = (c >> 4) & 7;
|
2005-10-12 23:01:31 +04:00
|
|
|
size = (c & 15);
|
|
|
|
shift = 4;
|
|
|
|
while (c & 0x80) {
|
2006-10-20 22:45:21 +04:00
|
|
|
p = fill(1);
|
|
|
|
c = *p;
|
|
|
|
use(1);
|
Fix big left-shifts of unsigned char
Shifting 'unsigned char' or 'unsigned short' left can result in sign
extension errors, since the C integer promotion rules means that the
unsigned char/short will get implicitly promoted to a signed 'int' due to
the shift (or due to other operations).
This normally doesn't matter, but if you shift things up sufficiently, it
will now set the sign bit in 'int', and a subsequent cast to a bigger type
(eg 'long' or 'unsigned long') will now sign-extend the value despite the
original expression being unsigned.
One example of this would be something like
unsigned long size;
unsigned char c;
size += c << 24;
where despite all the variables being unsigned, 'c << 24' ends up being a
signed entity, and will get sign-extended when then doing the addition in
an 'unsigned long' type.
Since git uses 'unsigned char' pointers extensively, we actually have this
bug in a couple of places.
I may have missed some, but this is the result of looking at
git grep '[^0-9 ][ ]*<<[ ][a-z]' -- '*.c' '*.h'
git grep '<<[ ]*24'
which catches at least the common byte cases (shifting variables by a
variable amount, and shifting by 24 bits).
I also grepped for just 'unsigned char' variables in general, and
converted the ones that most obviously ended up getting implicitly cast
immediately anyway (eg hash_name(), encode_85()).
In addition to just avoiding 'unsigned char', this patch also tries to use
a common idiom for the delta header size thing. We had three different
variations on it: "& 0x7fUL" in one place (getting the sign extension
right), and "& ~0x80" and "& 0x7f" in two other places (not getting it
right). Apart from making them all just avoid using "unsigned char" at
all, I also unified them to then use a simple "& 0x7f".
I considered making a sparse extension which warns about doing implicit
casts from unsigned types to signed types, but it gets rather complex very
quickly, so this is just a hack.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-06-18 04:22:27 +04:00
|
|
|
size += (c & 0x7f) << shift;
|
2005-10-12 23:01:31 +04:00
|
|
|
shift += 7;
|
|
|
|
}
|
2006-10-20 22:45:21 +04:00
|
|
|
obj->size = size;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2006-10-20 22:45:21 +04:00
|
|
|
switch (obj->type) {
|
2006-09-21 08:06:49 +04:00
|
|
|
case OBJ_REF_DELTA:
|
2006-10-20 22:45:21 +04:00
|
|
|
hashcpy(delta_base->sha1, fill(20));
|
|
|
|
use(20);
|
2006-09-21 08:08:33 +04:00
|
|
|
break;
|
|
|
|
case OBJ_OFS_DELTA:
|
|
|
|
memset(delta_base, 0, sizeof(*delta_base));
|
2006-10-20 22:45:21 +04:00
|
|
|
p = fill(1);
|
|
|
|
c = *p;
|
|
|
|
use(1);
|
2006-09-21 08:08:33 +04:00
|
|
|
base_offset = c & 127;
|
|
|
|
while (c & 128) {
|
|
|
|
base_offset += 1;
|
2007-04-09 09:06:29 +04:00
|
|
|
if (!base_offset || MSB(base_offset, 7))
|
2012-04-23 16:30:29 +04:00
|
|
|
bad_object(obj->idx.offset, _("offset value overflow for delta base object"));
|
2006-10-20 22:45:21 +04:00
|
|
|
p = fill(1);
|
|
|
|
c = *p;
|
|
|
|
use(1);
|
2006-09-21 08:08:33 +04:00
|
|
|
base_offset = (base_offset << 7) + (c & 127);
|
|
|
|
}
|
2007-06-01 23:18:05 +04:00
|
|
|
delta_base->offset = obj->idx.offset - base_offset;
|
2008-10-30 02:02:45 +03:00
|
|
|
if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset)
|
2012-04-23 16:30:29 +04:00
|
|
|
bad_object(obj->idx.offset, _("delta base offset is out of bound"));
|
2006-09-21 08:08:33 +04:00
|
|
|
break;
|
2005-10-12 23:01:31 +04:00
|
|
|
case OBJ_COMMIT:
|
|
|
|
case OBJ_TREE:
|
|
|
|
case OBJ_BLOB:
|
|
|
|
case OBJ_TAG:
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-23 16:30:29 +04:00
|
|
|
bad_object(obj->idx.offset, _("unknown object type %d"), obj->type);
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
2007-06-01 23:18:05 +04:00
|
|
|
obj->hdr_size = consumed_bytes - obj->idx.offset;
|
2006-10-20 22:45:21 +04:00
|
|
|
|
2012-05-23 18:09:46 +04:00
|
|
|
data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, sha1);
|
2007-06-01 23:18:05 +04:00
|
|
|
obj->idx.crc32 = input_crc32;
|
2007-04-09 09:06:32 +04:00
|
|
|
return data;
|
2006-10-20 22:45:21 +04:00
|
|
|
}
|
|
|
|
|
2012-05-23 18:09:48 +04:00
|
|
|
static void *unpack_data(struct object_entry *obj,
|
|
|
|
int (*consume)(const unsigned char *, unsigned long, void *),
|
|
|
|
void *cb_data)
|
2006-10-20 22:45:21 +04:00
|
|
|
{
|
2007-11-11 07:29:10 +03:00
|
|
|
off_t from = obj[0].idx.offset + obj[0].hdr_size;
|
2007-06-01 23:18:05 +04:00
|
|
|
unsigned long len = obj[1].idx.offset - from;
|
2010-04-12 20:11:07 +04:00
|
|
|
unsigned char *data, *inbuf;
|
2011-06-10 22:52:15 +04:00
|
|
|
git_zstream stream;
|
2010-04-12 20:11:07 +04:00
|
|
|
int status;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2012-05-23 18:09:48 +04:00
|
|
|
data = xmalloc(consume ? 64*1024 : obj->size);
|
2010-04-12 20:11:07 +04:00
|
|
|
inbuf = xmalloc((len < 64*1024) ? len : 64*1024);
|
|
|
|
|
2006-10-20 22:45:21 +04:00
|
|
|
memset(&stream, 0, sizeof(stream));
|
2010-04-12 20:11:07 +04:00
|
|
|
git_inflate_init(&stream);
|
2006-10-20 22:45:21 +04:00
|
|
|
stream.next_out = data;
|
2012-05-23 18:09:48 +04:00
|
|
|
stream.avail_out = consume ? 64*1024 : obj->size;
|
2010-04-12 20:11:07 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
ssize_t n = (len < 64*1024) ? len : 64*1024;
|
2014-06-03 23:06:42 +04:00
|
|
|
n = xpread(get_thread_data()->pack_fd, inbuf, n, from);
|
2010-04-12 20:11:07 +04:00
|
|
|
if (n < 0)
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("cannot pread pack file"));
|
2010-04-12 20:11:07 +04:00
|
|
|
if (!n)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(Q_("premature end of pack file, %lu byte missing",
|
|
|
|
"premature end of pack file, %lu bytes missing",
|
|
|
|
len),
|
|
|
|
len);
|
2010-04-12 20:11:07 +04:00
|
|
|
from += n;
|
|
|
|
len -= n;
|
|
|
|
stream.next_in = inbuf;
|
|
|
|
stream.avail_in = n;
|
index-pack: loop while inflating objects in unpack_data
When the unpack_data function is given a consume() callback,
it unpacks only 64K of the input at a time, feeding it to
git_inflate along with a 64K output buffer. However,
because we are inflating, there is a good chance that the
output buffer will fill before consuming all of the input.
In this case, we need to loop on git_inflate until we have
fed the whole input buffer, feeding each chunk of output to
the consume buffer.
The current code does not do this, and as a result, will
fail the loop condition and trigger a fatal "serious inflate
inconsistency" error in this case.
While we're rearranging the loop, let's get rid of the
extra last_out pointer. It is meant to point to the
beginning of the buffer that we feed to git_inflate, but in
practice this is always the beginning of our same 64K
buffer, because:
1. At the beginning of the loop, we are feeding the
buffer.
2. At the end of the loop, if we are using a consume()
function, we reset git_inflate's pointer to the
beginning of the buffer. If we are not using a
consume() function, then we do not care about the value
of last_out at all.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-07-04 11:12:14 +04:00
|
|
|
if (!consume)
|
|
|
|
status = git_inflate(&stream, 0);
|
|
|
|
else {
|
|
|
|
do {
|
|
|
|
status = git_inflate(&stream, 0);
|
|
|
|
if (consume(data, stream.next_out - data, cb_data)) {
|
|
|
|
free(inbuf);
|
|
|
|
free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
stream.next_out = data;
|
|
|
|
stream.avail_out = 64*1024;
|
|
|
|
} while (status == Z_OK && stream.avail_in);
|
2012-05-23 18:09:48 +04:00
|
|
|
}
|
2010-04-12 20:11:07 +04:00
|
|
|
} while (len && status == Z_OK && !stream.avail_in);
|
|
|
|
|
|
|
|
/* This has been inflated OK when first encountered, so... */
|
|
|
|
if (status != Z_STREAM_END || stream.total_out != obj->size)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("serious inflate inconsistency"));
|
2010-04-12 20:11:07 +04:00
|
|
|
|
|
|
|
git_inflate_end(&stream);
|
|
|
|
free(inbuf);
|
2012-05-23 18:09:48 +04:00
|
|
|
if (consume) {
|
|
|
|
free(data);
|
|
|
|
data = NULL;
|
|
|
|
}
|
2005-10-12 23:01:31 +04:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:09:48 +04:00
|
|
|
static void *get_data_from_pack(struct object_entry *obj)
|
|
|
|
{
|
|
|
|
return unpack_data(obj, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2011-02-02 21:06:51 +03:00
|
|
|
static int compare_delta_bases(const union delta_base *base1,
|
|
|
|
const union delta_base *base2,
|
|
|
|
enum object_type type1,
|
|
|
|
enum object_type type2)
|
|
|
|
{
|
|
|
|
int cmp = type1 - type2;
|
|
|
|
if (cmp)
|
|
|
|
return cmp;
|
|
|
|
return memcmp(base1, base2, UNION_BASE_SZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_delta(const union delta_base *base, enum object_type type)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
|
|
|
int first = 0, last = nr_deltas;
|
|
|
|
|
|
|
|
while (first < last) {
|
|
|
|
int next = (first + last) / 2;
|
|
|
|
struct delta_entry *delta = &deltas[next];
|
|
|
|
int cmp;
|
|
|
|
|
2011-02-02 21:06:51 +03:00
|
|
|
cmp = compare_delta_bases(base, &delta->base,
|
|
|
|
type, objects[delta->obj_no].type);
|
2005-10-12 23:01:31 +04:00
|
|
|
if (!cmp)
|
|
|
|
return next;
|
|
|
|
if (cmp < 0) {
|
|
|
|
last = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
first = next+1;
|
|
|
|
}
|
|
|
|
return -first-1;
|
|
|
|
}
|
|
|
|
|
2008-10-17 23:57:58 +04:00
|
|
|
static void find_delta_children(const union delta_base *base,
|
2011-02-02 21:06:51 +03:00
|
|
|
int *first_index, int *last_index,
|
|
|
|
enum object_type type)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2011-02-02 21:06:51 +03:00
|
|
|
int first = find_delta(base, type);
|
2005-10-12 23:01:31 +04:00
|
|
|
int last = first;
|
|
|
|
int end = nr_deltas - 1;
|
|
|
|
|
2008-10-17 23:57:58 +04:00
|
|
|
if (first < 0) {
|
|
|
|
*first_index = 0;
|
|
|
|
*last_index = -1;
|
|
|
|
return;
|
|
|
|
}
|
2006-10-18 00:23:26 +04:00
|
|
|
while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
|
2005-10-12 23:01:31 +04:00
|
|
|
--first;
|
2006-10-18 00:23:26 +04:00
|
|
|
while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
|
2005-10-12 23:01:31 +04:00
|
|
|
++last;
|
|
|
|
*first_index = first;
|
|
|
|
*last_index = last;
|
|
|
|
}
|
|
|
|
|
2012-05-24 17:55:44 +04:00
|
|
|
struct compare_data {
|
|
|
|
struct object_entry *entry;
|
|
|
|
struct git_istream *st;
|
|
|
|
unsigned char *buf;
|
|
|
|
unsigned long buf_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int compare_objects(const unsigned char *buf, unsigned long size,
|
|
|
|
void *cb_data)
|
|
|
|
{
|
|
|
|
struct compare_data *data = cb_data;
|
|
|
|
|
|
|
|
if (data->buf_size < size) {
|
|
|
|
free(data->buf);
|
|
|
|
data->buf = xmalloc(size);
|
|
|
|
data->buf_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (size) {
|
|
|
|
ssize_t len = read_istream(data->st, data->buf, size);
|
|
|
|
if (len == 0)
|
|
|
|
die(_("SHA1 COLLISION FOUND WITH %s !"),
|
|
|
|
sha1_to_hex(data->entry->idx.sha1));
|
|
|
|
if (len < 0)
|
|
|
|
die(_("unable to read %s"),
|
|
|
|
sha1_to_hex(data->entry->idx.sha1));
|
|
|
|
if (memcmp(buf, data->buf, len))
|
|
|
|
die(_("SHA1 COLLISION FOUND WITH %s !"),
|
|
|
|
sha1_to_hex(data->entry->idx.sha1));
|
|
|
|
size -= len;
|
|
|
|
buf += len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_collison(struct object_entry *entry)
|
|
|
|
{
|
|
|
|
struct compare_data data;
|
|
|
|
enum object_type type;
|
|
|
|
unsigned long size;
|
|
|
|
|
|
|
|
if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
data.entry = entry;
|
|
|
|
data.st = open_istream(entry->idx.sha1, &type, &size, NULL);
|
|
|
|
if (!data.st)
|
|
|
|
return -1;
|
|
|
|
if (size != entry->size || type != entry->type)
|
|
|
|
die(_("SHA1 COLLISION FOUND WITH %s !"),
|
|
|
|
sha1_to_hex(entry->idx.sha1));
|
|
|
|
unpack_data(entry, compare_objects, &data);
|
|
|
|
close_istream(data.st);
|
|
|
|
free(data.buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:09:47 +04:00
|
|
|
static void sha1_object(const void *data, struct object_entry *obj_entry,
|
|
|
|
unsigned long size, enum object_type type,
|
|
|
|
const unsigned char *sha1)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2012-05-23 18:09:47 +04:00
|
|
|
void *new_data = NULL;
|
2012-05-24 17:55:44 +04:00
|
|
|
int collision_test_needed;
|
2012-05-23 18:09:47 +04:00
|
|
|
|
|
|
|
assert(data || obj_entry);
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
read_lock();
|
2012-05-24 17:55:44 +04:00
|
|
|
collision_test_needed = has_sha1_file(sha1);
|
|
|
|
read_unlock();
|
|
|
|
|
|
|
|
if (collision_test_needed && !data) {
|
|
|
|
read_lock();
|
|
|
|
if (!check_collison(obj_entry))
|
|
|
|
collision_test_needed = 0;
|
|
|
|
read_unlock();
|
|
|
|
}
|
|
|
|
if (collision_test_needed) {
|
2007-03-20 22:32:35 +03:00
|
|
|
void *has_data;
|
|
|
|
enum object_type has_type;
|
|
|
|
unsigned long has_size;
|
2012-05-24 17:55:44 +04:00
|
|
|
read_lock();
|
|
|
|
has_type = sha1_object_info(sha1, &has_size);
|
|
|
|
if (has_type != type || has_size != size)
|
|
|
|
die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1));
|
2007-03-20 22:32:35 +03:00
|
|
|
has_data = read_sha1_file(sha1, &has_type, &has_size);
|
2012-05-06 16:31:55 +04:00
|
|
|
read_unlock();
|
2012-05-24 17:55:44 +04:00
|
|
|
if (!data)
|
|
|
|
data = new_data = get_data_from_pack(obj_entry);
|
2007-03-20 22:32:35 +03:00
|
|
|
if (!has_data)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("cannot read existing object %s"), sha1_to_hex(sha1));
|
2007-03-20 22:32:35 +03:00
|
|
|
if (size != has_size || type != has_type ||
|
|
|
|
memcmp(data, has_data, size) != 0)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1));
|
2007-04-03 20:33:46 +04:00
|
|
|
free(has_data);
|
2012-05-24 17:55:44 +04:00
|
|
|
}
|
2012-05-06 16:31:55 +04:00
|
|
|
|
2008-02-26 00:46:12 +03:00
|
|
|
if (strict) {
|
2012-05-06 16:31:55 +04:00
|
|
|
read_lock();
|
2008-02-26 00:46:12 +03:00
|
|
|
if (type == OBJ_BLOB) {
|
|
|
|
struct blob *blob = lookup_blob(sha1);
|
|
|
|
if (blob)
|
|
|
|
blob->object.flags |= FLAG_CHECKED;
|
|
|
|
else
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("invalid blob object %s"), sha1_to_hex(sha1));
|
2008-02-26 00:46:12 +03:00
|
|
|
} else {
|
|
|
|
struct object *obj;
|
|
|
|
int eaten;
|
|
|
|
void *buf = (void *) data;
|
|
|
|
|
2013-05-26 05:16:16 +04:00
|
|
|
assert(data && "data can only be NULL for large _blobs_");
|
2012-05-23 18:09:47 +04:00
|
|
|
|
2008-02-26 00:46:12 +03:00
|
|
|
/*
|
|
|
|
* we do not need to free the memory here, as the
|
|
|
|
* buf is deleted by the caller.
|
|
|
|
*/
|
|
|
|
obj = parse_object_buffer(sha1, type, size, buf, &eaten);
|
|
|
|
if (!obj)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("invalid %s"), typename(type));
|
2013-05-26 05:16:17 +04:00
|
|
|
if (do_fsck_object &&
|
2014-09-10 17:52:51 +04:00
|
|
|
fsck_object(obj, buf, size, 1,
|
|
|
|
fsck_error_function))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("Error in object"));
|
2009-06-18 21:28:43 +04:00
|
|
|
if (fsck_walk(obj, mark_link, NULL))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("Not all child objects of %s are reachable"), sha1_to_hex(obj->sha1));
|
2008-02-26 00:46:12 +03:00
|
|
|
|
|
|
|
if (obj->type == OBJ_TREE) {
|
|
|
|
struct tree *item = (struct tree *) obj;
|
|
|
|
item->buffer = NULL;
|
2013-06-06 02:37:39 +04:00
|
|
|
obj->parsed = 0;
|
2008-02-26 00:46:12 +03:00
|
|
|
}
|
|
|
|
if (obj->type == OBJ_COMMIT) {
|
|
|
|
struct commit *commit = (struct commit *) obj;
|
2014-06-11 01:44:13 +04:00
|
|
|
if (detach_commit_buffer(commit, NULL) != data)
|
provide a helper to free commit buffer
This converts two lines into one at each caller. But more
importantly, it abstracts the concept of freeing the buffer,
which will make it easier to change later.
Note that we also need to provide a "detach" mechanism for a
tricky case in index-pack. We are passed a buffer for the
object generated by processing the incoming pack. If we are
not using --strict, we just calculate the sha1 on that
buffer and return, leaving the caller to free it. But if we
are using --strict, we actually attach that buffer to an
object, pass the object to the fsck functions, and then
detach the buffer from the object again (so that the caller
can free it as usual). In this case, we don't want to free
the buffer ourselves, but just make sure it is no longer
associated with the commit.
Note that we are making the assumption here that the
attach/detach process does not impact the buffer at all
(e.g., it is never reallocated or modified). That holds true
now, and we have no plans to change that. However, as we
abstract the commit_buffer code, this dependency becomes
less obvious. So when we detach, let's also make sure that
we get back the same buffer that we gave to the
commit_buffer code.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-06-13 02:05:37 +04:00
|
|
|
die("BUG: parse_object_buffer transmogrified our buffer");
|
2008-02-26 00:46:12 +03:00
|
|
|
}
|
|
|
|
obj->flags |= FLAG_CHECKED;
|
|
|
|
}
|
2012-05-06 16:31:55 +04:00
|
|
|
read_unlock();
|
2008-02-26 00:46:12 +03:00
|
|
|
}
|
2012-05-23 18:09:47 +04:00
|
|
|
|
|
|
|
free(new_data);
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
2012-01-14 16:19:55 +04:00
|
|
|
/*
|
|
|
|
* This function is part of find_unresolved_deltas(). There are two
|
|
|
|
* walkers going in the opposite ways.
|
|
|
|
*
|
|
|
|
* The first one in find_unresolved_deltas() traverses down from
|
|
|
|
* parent node to children, deflating nodes along the way. However,
|
|
|
|
* memory for deflated nodes is limited by delta_base_cache_limit, so
|
|
|
|
* at some point parent node's deflated content may be freed.
|
|
|
|
*
|
|
|
|
* The second walker is this function, which goes from current node up
|
|
|
|
* to top parent if necessary to deflate the node. In normal
|
|
|
|
* situation, its parent node would be already deflated, so it just
|
|
|
|
* needs to apply delta.
|
|
|
|
*
|
|
|
|
* In the worst case scenario, parent node is no longer deflated because
|
|
|
|
* we're running out of delta_base_cache_limit; we need to re-deflate
|
|
|
|
* parents, possibly up to the top base.
|
|
|
|
*
|
|
|
|
* All deflated objects here are subject to be freed if we exceed
|
|
|
|
* delta_base_cache_limit, just like in find_unresolved_deltas(), we
|
|
|
|
* just need to make sure the last node is not freed.
|
|
|
|
*/
|
2008-07-15 08:45:34 +04:00
|
|
|
static void *get_base_data(struct base_data *c)
|
|
|
|
{
|
|
|
|
if (!c->data) {
|
|
|
|
struct object_entry *obj = c->obj;
|
2012-01-14 16:19:55 +04:00
|
|
|
struct base_data **delta = NULL;
|
|
|
|
int delta_nr = 0, delta_alloc = 0;
|
2008-07-15 08:45:34 +04:00
|
|
|
|
2012-01-14 16:19:55 +04:00
|
|
|
while (is_delta_type(c->obj->type) && !c->data) {
|
|
|
|
ALLOC_GROW(delta, delta_nr + 1, delta_alloc);
|
|
|
|
delta[delta_nr++] = c;
|
|
|
|
c = c->base;
|
|
|
|
}
|
|
|
|
if (!delta_nr) {
|
|
|
|
c->data = get_data_from_pack(obj);
|
|
|
|
c->size = obj->size;
|
2012-05-06 16:31:55 +04:00
|
|
|
get_thread_data()->base_cache_used += c->size;
|
2012-01-14 16:19:55 +04:00
|
|
|
prune_base_data(c);
|
|
|
|
}
|
|
|
|
for (; delta_nr > 0; delta_nr--) {
|
|
|
|
void *base, *raw;
|
|
|
|
c = delta[delta_nr - 1];
|
|
|
|
obj = c->obj;
|
|
|
|
base = get_base_data(c->base);
|
|
|
|
raw = get_data_from_pack(obj);
|
2008-07-15 08:45:34 +04:00
|
|
|
c->data = patch_delta(
|
|
|
|
base, c->base->size,
|
|
|
|
raw, obj->size,
|
|
|
|
&c->size);
|
|
|
|
free(raw);
|
|
|
|
if (!c->data)
|
2012-04-23 16:30:29 +04:00
|
|
|
bad_object(obj->idx.offset, _("failed to apply delta"));
|
2012-05-06 16:31:55 +04:00
|
|
|
get_thread_data()->base_cache_used += c->size;
|
2012-01-14 16:19:55 +04:00
|
|
|
prune_base_data(c);
|
2008-10-17 23:57:57 +04:00
|
|
|
}
|
2012-01-14 16:19:55 +04:00
|
|
|
free(delta);
|
2008-07-15 08:45:34 +04:00
|
|
|
}
|
|
|
|
return c->data;
|
|
|
|
}
|
|
|
|
|
2008-07-14 06:07:44 +04:00
|
|
|
static void resolve_delta(struct object_entry *delta_obj,
|
2008-10-17 23:57:57 +04:00
|
|
|
struct base_data *base, struct base_data *result)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2008-10-21 00:46:19 +04:00
|
|
|
void *base_data, *delta_data;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2013-03-19 17:01:15 +04:00
|
|
|
if (show_stat) {
|
|
|
|
delta_obj->delta_depth = base->obj->delta_depth + 1;
|
|
|
|
deepest_delta_lock();
|
|
|
|
if (deepest_delta < delta_obj->delta_depth)
|
|
|
|
deepest_delta = delta_obj->delta_depth;
|
|
|
|
deepest_delta_unlock();
|
|
|
|
}
|
2011-06-04 02:32:15 +04:00
|
|
|
delta_obj->base_object_no = base->obj - objects;
|
2006-10-26 07:28:17 +04:00
|
|
|
delta_data = get_data_from_pack(delta_obj);
|
2008-10-21 00:46:19 +04:00
|
|
|
base_data = get_base_data(base);
|
2008-10-17 23:57:57 +04:00
|
|
|
result->obj = delta_obj;
|
2008-10-21 00:46:19 +04:00
|
|
|
result->data = patch_delta(base_data, base->size,
|
|
|
|
delta_data, delta_obj->size, &result->size);
|
2005-10-12 23:01:31 +04:00
|
|
|
free(delta_data);
|
2008-10-17 23:57:57 +04:00
|
|
|
if (!result->data)
|
2012-04-23 16:30:29 +04:00
|
|
|
bad_object(delta_obj->idx.offset, _("failed to apply delta"));
|
2012-05-23 18:09:46 +04:00
|
|
|
hash_sha1_file(result->data, result->size,
|
|
|
|
typename(delta_obj->real_type), delta_obj->idx.sha1);
|
2012-05-23 18:09:47 +04:00
|
|
|
sha1_object(result->data, NULL, result->size, delta_obj->real_type,
|
2008-10-17 23:57:57 +04:00
|
|
|
delta_obj->idx.sha1);
|
2012-05-06 16:31:55 +04:00
|
|
|
counter_lock();
|
2006-10-26 07:28:17 +04:00
|
|
|
nr_resolved_deltas++;
|
2012-05-06 16:31:55 +04:00
|
|
|
counter_unlock();
|
2008-10-17 23:57:57 +04:00
|
|
|
}
|
|
|
|
|
index-pack: fix race condition with duplicate bases
When we are resolving deltas in an indexed pack, we do it by
first selecting a potential base (either one stored in full
in the pack, or one created by resolving another delta), and
then resolving any deltas that use that base. When we
resolve a particular delta, we flip its "real_type" field
from OBJ_{REF,OFS}_DELTA to whatever the real type is.
We assume that traversing the objects this way will visit
each delta only once. This is correct for most packs; we
visit the delta only when we process its base, and each
object (and thus each base) appears only once. However, if a
base object appears multiple times in the pack, we will try
to resolve any deltas based on it once for each instance.
We can detect this case by noting that a delta we are about
to resolve has already had its real_type field flipped, and
we already do so with an assert(). However, if multiple
threads are in use, we may race with another thread on
comparing and flipping the field. We need to synchronize the
access.
The right mechanism for doing this is a compare-and-swap (we
atomically "claim" the delta for our own and find out
whether our claim was successful). We can implement this
in C by using a pthread mutex to protect the operation. This
is not the fastest way of doing a compare-and-swap; many
processors provide instructions for this, and gcc and other
compilers provide builtins to access them. However, some
experiments showed that lock contention does not cause a
significant slowdown here. Adding c-a-s support for many
compilers would increase the maintenance burden (and we
would still end up including the pthread version as a
fallback).
Note that we only need to touch the OBJ_REF_DELTA codepath
here. An OBJ_OFS_DELTA object points to its base using an
offset, and therefore has only one base, even if another
copy of that base object appears in the pack (we do still
touch it briefly because the setting of real_type is
factored out of resolve_data).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-08-30 00:57:47 +04:00
|
|
|
/*
|
|
|
|
* Standard boolean compare-and-swap: atomically check whether "*type" is
|
|
|
|
* "want"; if so, swap in "set" and return true. Otherwise, leave it untouched
|
|
|
|
* and return false.
|
|
|
|
*/
|
|
|
|
static int compare_and_swap_type(enum object_type *type,
|
|
|
|
enum object_type want,
|
|
|
|
enum object_type set)
|
|
|
|
{
|
|
|
|
enum object_type old;
|
|
|
|
|
|
|
|
type_cas_lock();
|
|
|
|
old = *type;
|
|
|
|
if (old == want)
|
|
|
|
*type = set;
|
|
|
|
type_cas_unlock();
|
|
|
|
|
|
|
|
return old == want;
|
|
|
|
}
|
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
static struct base_data *find_unresolved_deltas_1(struct base_data *base,
|
|
|
|
struct base_data *prev_base)
|
2008-10-17 23:57:57 +04:00
|
|
|
{
|
2012-01-14 16:19:54 +04:00
|
|
|
if (base->ref_last == -1 && base->ofs_last == -1) {
|
2008-10-17 23:57:57 +04:00
|
|
|
union delta_base base_spec;
|
|
|
|
|
|
|
|
hashcpy(base_spec.sha1, base->obj->idx.sha1);
|
2011-02-02 21:06:51 +03:00
|
|
|
find_delta_children(&base_spec,
|
2012-01-14 16:19:54 +04:00
|
|
|
&base->ref_first, &base->ref_last, OBJ_REF_DELTA);
|
2006-09-21 08:08:33 +04:00
|
|
|
|
2008-10-17 23:57:57 +04:00
|
|
|
memset(&base_spec, 0, sizeof(base_spec));
|
|
|
|
base_spec.offset = base->obj->idx.offset;
|
2011-02-02 21:06:51 +03:00
|
|
|
find_delta_children(&base_spec,
|
2012-01-14 16:19:54 +04:00
|
|
|
&base->ofs_first, &base->ofs_last, OBJ_OFS_DELTA);
|
2008-07-14 06:07:45 +04:00
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
if (base->ref_last == -1 && base->ofs_last == -1) {
|
|
|
|
free(base->data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-09-21 08:08:33 +04:00
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
link_base_data(prev_base, base);
|
|
|
|
}
|
2008-07-14 06:07:45 +04:00
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
if (base->ref_first <= base->ref_last) {
|
|
|
|
struct object_entry *child = objects + deltas[base->ref_first].obj_no;
|
|
|
|
struct base_data *result = alloc_base_data();
|
2011-02-02 21:06:51 +03:00
|
|
|
|
index-pack: fix race condition with duplicate bases
When we are resolving deltas in an indexed pack, we do it by
first selecting a potential base (either one stored in full
in the pack, or one created by resolving another delta), and
then resolving any deltas that use that base. When we
resolve a particular delta, we flip its "real_type" field
from OBJ_{REF,OFS}_DELTA to whatever the real type is.
We assume that traversing the objects this way will visit
each delta only once. This is correct for most packs; we
visit the delta only when we process its base, and each
object (and thus each base) appears only once. However, if a
base object appears multiple times in the pack, we will try
to resolve any deltas based on it once for each instance.
We can detect this case by noting that a delta we are about
to resolve has already had its real_type field flipped, and
we already do so with an assert(). However, if multiple
threads are in use, we may race with another thread on
comparing and flipping the field. We need to synchronize the
access.
The right mechanism for doing this is a compare-and-swap (we
atomically "claim" the delta for our own and find out
whether our claim was successful). We can implement this
in C by using a pthread mutex to protect the operation. This
is not the fastest way of doing a compare-and-swap; many
processors provide instructions for this, and gcc and other
compilers provide builtins to access them. However, some
experiments showed that lock contention does not cause a
significant slowdown here. Adding c-a-s support for many
compilers would increase the maintenance burden (and we
would still end up including the pthread version as a
fallback).
Note that we only need to touch the OBJ_REF_DELTA codepath
here. An OBJ_OFS_DELTA object points to its base using an
offset, and therefore has only one base, even if another
copy of that base object appears in the pack (we do still
touch it briefly because the setting of real_type is
factored out of resolve_data).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-08-30 00:57:47 +04:00
|
|
|
if (!compare_and_swap_type(&child->real_type, OBJ_REF_DELTA,
|
|
|
|
base->obj->real_type))
|
|
|
|
die("BUG: child->real_type != OBJ_REF_DELTA");
|
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
resolve_delta(child, base, result);
|
|
|
|
if (base->ref_first == base->ref_last && base->ofs_last == -1)
|
2011-02-02 21:06:51 +03:00
|
|
|
free_base_data(base);
|
2012-01-14 16:19:54 +04:00
|
|
|
|
|
|
|
base->ref_first++;
|
|
|
|
return result;
|
2006-09-21 08:08:33 +04:00
|
|
|
}
|
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
if (base->ofs_first <= base->ofs_last) {
|
|
|
|
struct object_entry *child = objects + deltas[base->ofs_first].obj_no;
|
|
|
|
struct base_data *result = alloc_base_data();
|
2011-02-02 21:06:51 +03:00
|
|
|
|
|
|
|
assert(child->real_type == OBJ_OFS_DELTA);
|
index-pack: fix race condition with duplicate bases
When we are resolving deltas in an indexed pack, we do it by
first selecting a potential base (either one stored in full
in the pack, or one created by resolving another delta), and
then resolving any deltas that use that base. When we
resolve a particular delta, we flip its "real_type" field
from OBJ_{REF,OFS}_DELTA to whatever the real type is.
We assume that traversing the objects this way will visit
each delta only once. This is correct for most packs; we
visit the delta only when we process its base, and each
object (and thus each base) appears only once. However, if a
base object appears multiple times in the pack, we will try
to resolve any deltas based on it once for each instance.
We can detect this case by noting that a delta we are about
to resolve has already had its real_type field flipped, and
we already do so with an assert(). However, if multiple
threads are in use, we may race with another thread on
comparing and flipping the field. We need to synchronize the
access.
The right mechanism for doing this is a compare-and-swap (we
atomically "claim" the delta for our own and find out
whether our claim was successful). We can implement this
in C by using a pthread mutex to protect the operation. This
is not the fastest way of doing a compare-and-swap; many
processors provide instructions for this, and gcc and other
compilers provide builtins to access them. However, some
experiments showed that lock contention does not cause a
significant slowdown here. Adding c-a-s support for many
compilers would increase the maintenance burden (and we
would still end up including the pthread version as a
fallback).
Note that we only need to touch the OBJ_REF_DELTA codepath
here. An OBJ_OFS_DELTA object points to its base using an
offset, and therefore has only one base, even if another
copy of that base object appears in the pack (we do still
touch it briefly because the setting of real_type is
factored out of resolve_data).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-08-30 00:57:47 +04:00
|
|
|
child->real_type = base->obj->real_type;
|
2012-01-14 16:19:54 +04:00
|
|
|
resolve_delta(child, base, result);
|
|
|
|
if (base->ofs_first == base->ofs_last)
|
2011-02-02 21:06:51 +03:00
|
|
|
free_base_data(base);
|
2012-01-14 16:19:54 +04:00
|
|
|
|
|
|
|
base->ofs_first++;
|
|
|
|
return result;
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
2006-09-21 08:08:33 +04:00
|
|
|
|
2008-10-17 23:57:57 +04:00
|
|
|
unlink_base_data(base);
|
2012-01-14 16:19:54 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_unresolved_deltas(struct base_data *base)
|
|
|
|
{
|
|
|
|
struct base_data *new_base, *prev_base = NULL;
|
|
|
|
for (;;) {
|
|
|
|
new_base = find_unresolved_deltas_1(base, prev_base);
|
|
|
|
|
|
|
|
if (new_base) {
|
|
|
|
prev_base = base;
|
|
|
|
base = new_base;
|
|
|
|
} else {
|
|
|
|
free(base);
|
|
|
|
base = prev_base;
|
|
|
|
if (!base)
|
|
|
|
return;
|
|
|
|
prev_base = base->base;
|
|
|
|
}
|
|
|
|
}
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_delta_entry(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct delta_entry *delta_a = a;
|
|
|
|
const struct delta_entry *delta_b = b;
|
2011-02-02 21:06:51 +03:00
|
|
|
|
|
|
|
/* group by type (ref vs ofs) and then by value (sha-1 or offset) */
|
|
|
|
return compare_delta_bases(&delta_a->base, &delta_b->base,
|
|
|
|
objects[delta_a->obj_no].type,
|
|
|
|
objects[delta_b->obj_no].type);
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
2012-05-06 16:31:54 +04:00
|
|
|
static void resolve_base(struct object_entry *obj)
|
|
|
|
{
|
|
|
|
struct base_data *base_obj = alloc_base_data();
|
|
|
|
base_obj->obj = obj;
|
|
|
|
base_obj->data = NULL;
|
|
|
|
find_unresolved_deltas(base_obj);
|
|
|
|
}
|
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
static void *threaded_second_pass(void *data)
|
|
|
|
{
|
|
|
|
set_thread_data(data);
|
|
|
|
for (;;) {
|
|
|
|
int i;
|
2013-03-19 18:16:41 +04:00
|
|
|
counter_lock();
|
2012-05-06 16:31:55 +04:00
|
|
|
display_progress(progress, nr_resolved_deltas);
|
2013-03-19 18:16:41 +04:00
|
|
|
counter_unlock();
|
|
|
|
work_lock();
|
2012-05-06 16:31:55 +04:00
|
|
|
while (nr_dispatched < nr_objects &&
|
|
|
|
is_delta_type(objects[nr_dispatched].type))
|
|
|
|
nr_dispatched++;
|
|
|
|
if (nr_dispatched >= nr_objects) {
|
|
|
|
work_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = nr_dispatched++;
|
|
|
|
work_unlock();
|
|
|
|
|
|
|
|
resolve_base(&objects[i]);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-05-06 16:31:54 +04:00
|
|
|
/*
|
|
|
|
* First pass:
|
|
|
|
* - find locations of all objects;
|
|
|
|
* - calculate SHA1 of all non-delta objects;
|
|
|
|
* - remember base (SHA1 or offset) for all deltas.
|
|
|
|
*/
|
2006-10-20 22:45:21 +04:00
|
|
|
static void parse_pack_objects(unsigned char *sha1)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2012-05-23 18:09:47 +04:00
|
|
|
int i, nr_delays = 0;
|
2006-09-21 08:08:33 +04:00
|
|
|
struct delta_entry *delta = deltas;
|
2006-10-20 22:45:21 +04:00
|
|
|
struct stat st;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2007-04-20 22:10:07 +04:00
|
|
|
if (verbose)
|
2007-10-30 21:57:35 +03:00
|
|
|
progress = start_progress(
|
2012-04-23 16:30:29 +04:00
|
|
|
from_stdin ? _("Receiving objects") : _("Indexing objects"),
|
2007-10-30 21:57:35 +03:00
|
|
|
nr_objects);
|
2005-10-12 23:01:31 +04:00
|
|
|
for (i = 0; i < nr_objects; i++) {
|
|
|
|
struct object_entry *obj = &objects[i];
|
2012-05-23 18:09:46 +04:00
|
|
|
void *data = unpack_raw_entry(obj, &delta->base, obj->idx.sha1);
|
2005-10-12 23:01:31 +04:00
|
|
|
obj->real_type = obj->type;
|
2011-06-04 02:32:14 +04:00
|
|
|
if (is_delta_type(obj->type)) {
|
2006-09-21 08:08:33 +04:00
|
|
|
nr_deltas++;
|
2006-10-26 07:28:17 +04:00
|
|
|
delta->obj_no = i;
|
2006-09-21 08:08:33 +04:00
|
|
|
delta++;
|
2012-05-23 18:09:47 +04:00
|
|
|
} else if (!data) {
|
|
|
|
/* large blobs, check later */
|
|
|
|
obj->real_type = OBJ_BAD;
|
|
|
|
nr_delays++;
|
2005-10-12 23:01:31 +04:00
|
|
|
} else
|
2012-05-23 18:09:47 +04:00
|
|
|
sha1_object(data, NULL, obj->size, obj->type, obj->idx.sha1);
|
2005-10-12 23:01:31 +04:00
|
|
|
free(data);
|
2007-10-30 21:57:33 +03:00
|
|
|
display_progress(progress, i+1);
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
2007-06-01 23:18:05 +04:00
|
|
|
objects[i].idx.offset = consumed_bytes;
|
2007-10-30 21:57:33 +03:00
|
|
|
stop_progress(&progress);
|
2006-10-20 22:45:21 +04:00
|
|
|
|
|
|
|
/* Check pack integrity */
|
2006-10-26 07:28:17 +04:00
|
|
|
flush();
|
2008-10-01 22:05:20 +04:00
|
|
|
git_SHA1_Final(sha1, &input_ctx);
|
2006-10-20 22:45:21 +04:00
|
|
|
if (hashcmp(fill(20), sha1))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("pack is corrupted (SHA1 mismatch)"));
|
2006-10-26 07:31:53 +04:00
|
|
|
use(20);
|
2006-10-20 22:45:21 +04:00
|
|
|
|
|
|
|
/* If input_fd is a file, we should have reached its end now. */
|
|
|
|
if (fstat(input_fd, &st))
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("cannot fstat packfile"));
|
git-bundle: assorted fixes
This patch fixes issues mentioned by Junio, Nico and Simon:
- I forgot to convert the usage string when removing the "--" from
the subcommands,
- a style fix in the bundle_header,
- use xread() instead of read(),
- use write_or_die() instead of write(),
- make the bundle header extensible,
- fail if the whitespace after a sha1 of a reference is missing,
- close() the fds passed to a subprocess,
- in verify_bundle(), do not use "rev-list --stdin", but rather
pass the revs directly (avoiding a fork()),
- fix a corrupted comment in show_object(), and
- fix the size check in index_pack.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-22 21:14:14 +03:00
|
|
|
if (S_ISREG(st.st_mode) &&
|
|
|
|
lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("pack has junk at the end"));
|
2012-05-23 18:09:47 +04:00
|
|
|
|
|
|
|
for (i = 0; i < nr_objects; i++) {
|
|
|
|
struct object_entry *obj = &objects[i];
|
|
|
|
if (obj->real_type != OBJ_BAD)
|
|
|
|
continue;
|
|
|
|
obj->real_type = obj->type;
|
|
|
|
sha1_object(NULL, obj, obj->size, obj->type, obj->idx.sha1);
|
|
|
|
nr_delays--;
|
|
|
|
}
|
|
|
|
if (nr_delays)
|
|
|
|
die(_("confusion beyond insanity in parse_pack_objects()"));
|
2012-05-06 16:31:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second pass:
|
|
|
|
* - for all non-delta objects, look if it is used as a base for
|
|
|
|
* deltas;
|
|
|
|
* - if used as a base, uncompress the object and apply all deltas,
|
|
|
|
* recursively checking if the resulting object is used as a base
|
|
|
|
* for some more deltas.
|
|
|
|
*/
|
|
|
|
static void resolve_deltas(void)
|
|
|
|
{
|
|
|
|
int i;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2006-10-26 07:32:59 +04:00
|
|
|
if (!nr_deltas)
|
|
|
|
return;
|
|
|
|
|
2006-09-21 08:08:33 +04:00
|
|
|
/* Sort deltas by base SHA1/offset for fast searching */
|
2005-10-12 23:01:31 +04:00
|
|
|
qsort(deltas, nr_deltas, sizeof(struct delta_entry),
|
|
|
|
compare_delta_entry);
|
|
|
|
|
2007-04-20 22:10:07 +04:00
|
|
|
if (verbose)
|
2012-04-23 16:30:29 +04:00
|
|
|
progress = start_progress(_("Resolving deltas"), nr_deltas);
|
2012-05-06 16:31:55 +04:00
|
|
|
|
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
nr_dispatched = 0;
|
|
|
|
if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
|
|
|
|
init_thread();
|
|
|
|
for (i = 0; i < nr_threads; i++) {
|
|
|
|
int ret = pthread_create(&thread_data[i].thread, NULL,
|
|
|
|
threaded_second_pass, thread_data + i);
|
|
|
|
if (ret)
|
2012-08-31 16:13:04 +04:00
|
|
|
die(_("unable to create thread: %s"),
|
|
|
|
strerror(ret));
|
2012-05-06 16:31:55 +04:00
|
|
|
}
|
|
|
|
for (i = 0; i < nr_threads; i++)
|
|
|
|
pthread_join(thread_data[i].thread, NULL);
|
|
|
|
cleanup_thread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-10-12 23:01:31 +04:00
|
|
|
for (i = 0; i < nr_objects; i++) {
|
|
|
|
struct object_entry *obj = &objects[i];
|
|
|
|
|
2011-06-04 02:32:14 +04:00
|
|
|
if (is_delta_type(obj->type))
|
2005-10-12 23:01:31 +04:00
|
|
|
continue;
|
2012-05-06 16:31:54 +04:00
|
|
|
resolve_base(obj);
|
2007-10-30 21:57:33 +03:00
|
|
|
display_progress(progress, nr_resolved_deltas);
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
2006-10-26 07:28:17 +04:00
|
|
|
}
|
|
|
|
|
2012-05-06 16:31:54 +04:00
|
|
|
/*
|
|
|
|
* Third pass:
|
|
|
|
* - append objects to convert thin pack to full pack if required
|
|
|
|
* - write the final 20-byte SHA-1
|
|
|
|
*/
|
|
|
|
static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved);
|
|
|
|
static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_sha1)
|
|
|
|
{
|
|
|
|
if (nr_deltas == nr_resolved_deltas) {
|
|
|
|
stop_progress(&progress);
|
|
|
|
/* Flush remaining pack final 20-byte SHA1. */
|
|
|
|
flush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fix_thin_pack) {
|
|
|
|
struct sha1file *f;
|
|
|
|
unsigned char read_sha1[20], tail_sha1[20];
|
2013-03-16 05:25:18 +04:00
|
|
|
struct strbuf msg = STRBUF_INIT;
|
2012-05-06 16:31:54 +04:00
|
|
|
int nr_unresolved = nr_deltas - nr_resolved_deltas;
|
|
|
|
int nr_objects_initial = nr_objects;
|
|
|
|
if (nr_unresolved <= 0)
|
2012-05-14 22:50:40 +04:00
|
|
|
die(_("confusion beyond insanity"));
|
2012-05-06 16:31:54 +04:00
|
|
|
objects = xrealloc(objects,
|
|
|
|
(nr_objects + nr_unresolved + 1)
|
|
|
|
* sizeof(*objects));
|
2013-03-19 20:17:22 +04:00
|
|
|
memset(objects + nr_objects + 1, 0,
|
|
|
|
nr_unresolved * sizeof(*objects));
|
2012-05-06 16:31:54 +04:00
|
|
|
f = sha1fd(output_fd, curr_pack);
|
|
|
|
fix_unresolved_deltas(f, nr_unresolved);
|
2013-03-16 05:25:18 +04:00
|
|
|
strbuf_addf(&msg, _("completed with %d local objects"),
|
|
|
|
nr_objects - nr_objects_initial);
|
|
|
|
stop_progress_msg(&progress, msg.buf);
|
|
|
|
strbuf_release(&msg);
|
2012-05-06 16:31:54 +04:00
|
|
|
sha1close(f, tail_sha1, 0);
|
|
|
|
hashcpy(read_sha1, pack_sha1);
|
|
|
|
fixup_pack_header_footer(output_fd, pack_sha1,
|
|
|
|
curr_pack, nr_objects,
|
|
|
|
read_sha1, consumed_bytes-20);
|
|
|
|
if (hashcmp(read_sha1, tail_sha1) != 0)
|
2012-08-31 16:13:04 +04:00
|
|
|
die(_("Unexpected tail checksum for %s "
|
|
|
|
"(disk corruption?)"), curr_pack);
|
2012-05-06 16:31:54 +04:00
|
|
|
}
|
|
|
|
if (nr_deltas != nr_resolved_deltas)
|
2012-05-14 22:50:40 +04:00
|
|
|
die(Q_("pack has %d unresolved delta",
|
|
|
|
"pack has %d unresolved deltas",
|
|
|
|
nr_deltas - nr_resolved_deltas),
|
2012-05-06 16:31:54 +04:00
|
|
|
nr_deltas - nr_resolved_deltas);
|
|
|
|
}
|
|
|
|
|
2008-08-30 00:08:01 +04:00
|
|
|
static int write_compressed(struct sha1file *f, void *in, unsigned int size)
|
2006-10-26 07:28:17 +04:00
|
|
|
{
|
2011-06-10 22:52:15 +04:00
|
|
|
git_zstream stream;
|
2010-04-13 00:50:35 +04:00
|
|
|
int status;
|
|
|
|
unsigned char outbuf[4096];
|
2006-10-26 07:28:17 +04:00
|
|
|
|
|
|
|
memset(&stream, 0, sizeof(stream));
|
2011-06-10 21:55:10 +04:00
|
|
|
git_deflate_init(&stream, zlib_compression_level);
|
2006-10-26 07:28:17 +04:00
|
|
|
stream.next_in = in;
|
|
|
|
stream.avail_in = size;
|
|
|
|
|
2010-04-13 00:50:35 +04:00
|
|
|
do {
|
|
|
|
stream.next_out = outbuf;
|
|
|
|
stream.avail_out = sizeof(outbuf);
|
2011-06-10 21:55:10 +04:00
|
|
|
status = git_deflate(&stream, Z_FINISH);
|
2010-04-13 00:50:35 +04:00
|
|
|
sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
|
|
|
|
} while (status == Z_OK);
|
|
|
|
|
|
|
|
if (status != Z_STREAM_END)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("unable to deflate appended object (%d)"), status);
|
2006-10-26 07:28:17 +04:00
|
|
|
size = stream.total_out;
|
2011-06-10 21:55:10 +04:00
|
|
|
git_deflate_end(&stream);
|
2006-10-26 07:28:17 +04:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-08-30 00:08:01 +04:00
|
|
|
static struct object_entry *append_obj_to_pack(struct sha1file *f,
|
2008-07-14 06:07:46 +04:00
|
|
|
const unsigned char *sha1, void *buf,
|
2006-10-26 07:28:17 +04:00
|
|
|
unsigned long size, enum object_type type)
|
|
|
|
{
|
|
|
|
struct object_entry *obj = &objects[nr_objects++];
|
|
|
|
unsigned char header[10];
|
|
|
|
unsigned long s = size;
|
|
|
|
int n = 0;
|
|
|
|
unsigned char c = (type << 4) | (s & 15);
|
|
|
|
s >>= 4;
|
|
|
|
while (s) {
|
|
|
|
header[n++] = c | 0x80;
|
|
|
|
c = s & 0x7f;
|
|
|
|
s >>= 7;
|
|
|
|
}
|
|
|
|
header[n++] = c;
|
2008-08-30 00:08:01 +04:00
|
|
|
crc32_begin(f);
|
|
|
|
sha1write(f, header, n);
|
2008-07-24 21:32:00 +04:00
|
|
|
obj[0].size = size;
|
|
|
|
obj[0].hdr_size = n;
|
|
|
|
obj[0].type = type;
|
|
|
|
obj[0].real_type = type;
|
2007-06-01 23:18:05 +04:00
|
|
|
obj[1].idx.offset = obj[0].idx.offset + n;
|
2008-08-30 00:08:01 +04:00
|
|
|
obj[1].idx.offset += write_compressed(f, buf, size);
|
|
|
|
obj[0].idx.crc32 = crc32_end(f);
|
2008-10-10 06:08:51 +04:00
|
|
|
sha1flush(f);
|
2007-06-01 23:18:05 +04:00
|
|
|
hashcpy(obj->idx.sha1, sha1);
|
2008-07-14 06:07:46 +04:00
|
|
|
return obj;
|
2006-10-26 07:28:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int delta_pos_compare(const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
struct delta_entry *a = *(struct delta_entry **)_a;
|
|
|
|
struct delta_entry *b = *(struct delta_entry **)_b;
|
|
|
|
return a->obj_no - b->obj_no;
|
|
|
|
}
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2008-08-30 00:08:01 +04:00
|
|
|
static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
|
2006-10-26 07:28:17 +04:00
|
|
|
{
|
|
|
|
struct delta_entry **sorted_by_pos;
|
2007-04-18 22:27:45 +04:00
|
|
|
int i, n = 0;
|
2006-10-26 07:28:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since many unresolved deltas may well be themselves base objects
|
|
|
|
* for more unresolved deltas, we really want to include the
|
|
|
|
* smallest number of base objects that would cover as much delta
|
|
|
|
* as possible by picking the
|
|
|
|
* trunc deltas first, allowing for other deltas to resolve without
|
|
|
|
* additional base objects. Since most base objects are to be found
|
|
|
|
* before deltas depending on them, a good heuristic is to start
|
|
|
|
* resolving deltas in the same order as their position in the pack.
|
|
|
|
*/
|
|
|
|
sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
|
2005-10-12 23:01:31 +04:00
|
|
|
for (i = 0; i < nr_deltas; i++) {
|
2006-10-26 07:28:17 +04:00
|
|
|
if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
|
|
|
|
continue;
|
|
|
|
sorted_by_pos[n++] = &deltas[i];
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
2006-10-26 07:28:17 +04:00
|
|
|
qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct delta_entry *d = sorted_by_pos[i];
|
2007-02-26 22:55:59 +03:00
|
|
|
enum object_type type;
|
2012-01-14 16:19:54 +04:00
|
|
|
struct base_data *base_obj = alloc_base_data();
|
2006-10-26 07:28:17 +04:00
|
|
|
|
|
|
|
if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
|
|
|
|
continue;
|
2012-01-14 16:19:54 +04:00
|
|
|
base_obj->data = read_sha1_file(d->base.sha1, &type, &base_obj->size);
|
|
|
|
if (!base_obj->data)
|
2006-10-26 07:28:17 +04:00
|
|
|
continue;
|
|
|
|
|
2012-01-14 16:19:54 +04:00
|
|
|
if (check_sha1_signature(d->base.sha1, base_obj->data,
|
|
|
|
base_obj->size, typename(type)))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("local object %s is corrupt"), sha1_to_hex(d->base.sha1));
|
2012-01-14 16:19:54 +04:00
|
|
|
base_obj->obj = append_obj_to_pack(f, d->base.sha1,
|
|
|
|
base_obj->data, base_obj->size, type);
|
|
|
|
find_unresolved_deltas(base_obj);
|
2007-10-30 21:57:33 +03:00
|
|
|
display_progress(progress, nr_resolved_deltas);
|
2006-10-26 07:28:17 +04:00
|
|
|
}
|
|
|
|
free(sorted_by_pos);
|
|
|
|
}
|
|
|
|
|
2006-10-23 22:50:18 +04:00
|
|
|
static void final(const char *final_pack_name, const char *curr_pack_name,
|
|
|
|
const char *final_index_name, const char *curr_index_name,
|
2006-10-29 12:41:59 +03:00
|
|
|
const char *keep_name, const char *keep_msg,
|
2006-10-23 22:50:18 +04:00
|
|
|
unsigned char *sha1)
|
|
|
|
{
|
2007-03-07 04:44:17 +03:00
|
|
|
const char *report = "pack";
|
2006-10-23 22:50:18 +04:00
|
|
|
char name[PATH_MAX];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!from_stdin) {
|
|
|
|
close(input_fd);
|
|
|
|
} else {
|
2008-05-30 19:42:16 +04:00
|
|
|
fsync_or_die(output_fd, curr_pack_name);
|
2006-10-23 22:50:18 +04:00
|
|
|
err = close(output_fd);
|
|
|
|
if (err)
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("error while closing pack file"));
|
2006-10-23 22:50:18 +04:00
|
|
|
}
|
|
|
|
|
2006-10-29 12:41:59 +03:00
|
|
|
if (keep_msg) {
|
|
|
|
int keep_fd, keep_msg_len = strlen(keep_msg);
|
2009-02-25 10:11:29 +03:00
|
|
|
|
|
|
|
if (!keep_name)
|
|
|
|
keep_fd = odb_pack_keep(name, sizeof(name), sha1);
|
|
|
|
else
|
|
|
|
keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
|
|
|
|
|
2006-11-02 01:06:24 +03:00
|
|
|
if (keep_fd < 0) {
|
|
|
|
if (errno != EEXIST)
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("cannot write keep file '%s'"),
|
2014-03-18 02:08:36 +04:00
|
|
|
keep_name ? keep_name : name);
|
2006-11-02 01:06:24 +03:00
|
|
|
} else {
|
|
|
|
if (keep_msg_len > 0) {
|
|
|
|
write_or_die(keep_fd, keep_msg, keep_msg_len);
|
|
|
|
write_or_die(keep_fd, "\n", 1);
|
|
|
|
}
|
2007-06-24 23:20:41 +04:00
|
|
|
if (close(keep_fd) != 0)
|
2012-04-23 16:30:29 +04:00
|
|
|
die_errno(_("cannot close written keep file '%s'"),
|
2014-03-18 02:08:36 +04:00
|
|
|
keep_name ? keep_name : name);
|
2006-11-02 01:06:25 +03:00
|
|
|
report = "keep";
|
2006-10-29 12:41:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-23 22:50:18 +04:00
|
|
|
if (final_pack_name != curr_pack_name) {
|
|
|
|
if (!final_pack_name) {
|
|
|
|
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
|
|
|
|
get_object_directory(), sha1_to_hex(sha1));
|
|
|
|
final_pack_name = name;
|
|
|
|
}
|
|
|
|
if (move_temp_to_file(curr_pack_name, final_pack_name))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("cannot store pack file"));
|
2009-03-26 18:16:47 +03:00
|
|
|
} else if (from_stdin)
|
2008-10-03 14:20:43 +04:00
|
|
|
chmod(final_pack_name, 0444);
|
2006-10-23 22:50:18 +04:00
|
|
|
|
|
|
|
if (final_index_name != curr_index_name) {
|
|
|
|
if (!final_index_name) {
|
|
|
|
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
|
|
|
|
get_object_directory(), sha1_to_hex(sha1));
|
|
|
|
final_index_name = name;
|
|
|
|
}
|
|
|
|
if (move_temp_to_file(curr_index_name, final_index_name))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("cannot store index file"));
|
2009-03-26 18:16:47 +03:00
|
|
|
} else
|
|
|
|
chmod(final_index_name, 0444);
|
2006-11-02 01:06:25 +03:00
|
|
|
|
|
|
|
if (!from_stdin) {
|
|
|
|
printf("%s\n", sha1_to_hex(sha1));
|
|
|
|
} else {
|
|
|
|
char buf[48];
|
|
|
|
int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
|
|
|
|
report, sha1_to_hex(sha1));
|
2007-01-12 00:15:51 +03:00
|
|
|
write_or_die(1, buf, len);
|
2006-11-02 01:06:25 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Let's just mimic git-unpack-objects here and write
|
|
|
|
* the last part of the input buffer to stdout.
|
|
|
|
*/
|
|
|
|
while (input_len) {
|
|
|
|
err = xwrite(1, input_buffer + input_offset, input_len);
|
|
|
|
if (err <= 0)
|
|
|
|
break;
|
|
|
|
input_len -= err;
|
|
|
|
input_offset += err;
|
|
|
|
}
|
|
|
|
}
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 21:46:53 +04:00
|
|
|
static int git_index_pack_config(const char *k, const char *v, void *cb)
|
2007-11-02 06:26:04 +03:00
|
|
|
{
|
2011-02-26 02:43:25 +03:00
|
|
|
struct pack_idx_option *opts = cb;
|
|
|
|
|
2007-11-02 06:26:04 +03:00
|
|
|
if (!strcmp(k, "pack.indexversion")) {
|
2011-02-26 02:43:25 +03:00
|
|
|
opts->version = git_config_int(k, v);
|
|
|
|
if (opts->version > 2)
|
2012-08-31 16:13:04 +04:00
|
|
|
die(_("bad pack.indexversion=%"PRIu32), opts->version);
|
2007-11-02 06:26:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2012-05-06 16:31:55 +04:00
|
|
|
if (!strcmp(k, "pack.threads")) {
|
|
|
|
nr_threads = git_config_int(k, v);
|
|
|
|
if (nr_threads < 0)
|
2012-08-31 16:13:04 +04:00
|
|
|
die(_("invalid number of threads specified (%d)"),
|
2012-05-06 16:31:55 +04:00
|
|
|
nr_threads);
|
|
|
|
#ifdef NO_PTHREADS
|
|
|
|
if (nr_threads != 1)
|
2012-08-31 16:13:04 +04:00
|
|
|
warning(_("no threads support, ignoring %s"), k);
|
2012-05-06 16:31:55 +04:00
|
|
|
nr_threads = 1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-14 21:46:53 +04:00
|
|
|
return git_default_config(k, v, cb);
|
2007-11-02 06:26:04 +03:00
|
|
|
}
|
|
|
|
|
2011-02-26 03:55:26 +03:00
|
|
|
static int cmp_uint32(const void *a_, const void *b_)
|
|
|
|
{
|
|
|
|
uint32_t a = *((uint32_t *)a_);
|
|
|
|
uint32_t b = *((uint32_t *)b_);
|
|
|
|
|
|
|
|
return (a < b) ? -1 : (a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_v2_anomalous_offsets(struct packed_git *p,
|
|
|
|
struct pack_idx_option *opts)
|
|
|
|
{
|
|
|
|
const uint32_t *idx1, *idx2;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
/* The address of the 4-byte offset table */
|
|
|
|
idx1 = (((const uint32_t *)p->index_data)
|
|
|
|
+ 2 /* 8-byte header */
|
|
|
|
+ 256 /* fan out */
|
|
|
|
+ 5 * p->num_objects /* 20-byte SHA-1 table */
|
|
|
|
+ p->num_objects /* CRC32 table */
|
|
|
|
);
|
|
|
|
|
|
|
|
/* The address of the 8-byte offset table */
|
|
|
|
idx2 = idx1 + p->num_objects;
|
|
|
|
|
|
|
|
for (i = 0; i < p->num_objects; i++) {
|
|
|
|
uint32_t off = ntohl(idx1[i]);
|
|
|
|
if (!(off & 0x80000000))
|
|
|
|
continue;
|
|
|
|
off = off & 0x7fffffff;
|
|
|
|
if (idx2[off * 2])
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* The real offset is ntohl(idx2[off * 2]) in high 4
|
|
|
|
* octets, and ntohl(idx2[off * 2 + 1]) in low 4
|
|
|
|
* octets. But idx2[off * 2] is Zero!!!
|
|
|
|
*/
|
|
|
|
ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
|
|
|
|
opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1 < opts->anomaly_nr)
|
|
|
|
qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32);
|
|
|
|
}
|
|
|
|
|
2011-02-03 04:29:01 +03:00
|
|
|
static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
|
|
|
|
{
|
|
|
|
struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
|
|
|
|
|
|
|
|
if (!p)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("Cannot open existing pack file '%s'"), pack_name);
|
2011-02-03 04:29:01 +03:00
|
|
|
if (open_pack_index(p))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("Cannot open existing pack idx file for '%s'"), pack_name);
|
2011-02-03 04:29:01 +03:00
|
|
|
|
|
|
|
/* Read the attributes from the existing idx file */
|
|
|
|
opts->version = p->index_version;
|
|
|
|
|
2011-02-26 03:55:26 +03:00
|
|
|
if (opts->version == 2)
|
|
|
|
read_v2_anomalous_offsets(p, opts);
|
|
|
|
|
2011-02-03 04:29:01 +03:00
|
|
|
/*
|
|
|
|
* Get rid of the idx file as we do not need it anymore.
|
|
|
|
* NEEDSWORK: extract this bit from free_pack_by_name() in
|
|
|
|
* sha1_file.c, perhaps? It shouldn't matter very much as we
|
|
|
|
* know we haven't installed this pack (hence we never have
|
|
|
|
* read anything from it).
|
|
|
|
*/
|
|
|
|
close_pack_index(p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2011-06-04 02:32:15 +04:00
|
|
|
static void show_pack_info(int stat_only)
|
|
|
|
{
|
2011-06-04 02:32:16 +04:00
|
|
|
int i, baseobjects = nr_objects - nr_deltas;
|
|
|
|
unsigned long *chain_histogram = NULL;
|
|
|
|
|
|
|
|
if (deepest_delta)
|
|
|
|
chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long));
|
|
|
|
|
2011-06-04 02:32:15 +04:00
|
|
|
for (i = 0; i < nr_objects; i++) {
|
|
|
|
struct object_entry *obj = &objects[i];
|
|
|
|
|
2011-06-04 02:32:16 +04:00
|
|
|
if (is_delta_type(obj->type))
|
|
|
|
chain_histogram[obj->delta_depth - 1]++;
|
2011-06-04 02:32:15 +04:00
|
|
|
if (stat_only)
|
|
|
|
continue;
|
|
|
|
printf("%s %-6s %lu %lu %"PRIuMAX,
|
|
|
|
sha1_to_hex(obj->idx.sha1),
|
|
|
|
typename(obj->real_type), obj->size,
|
|
|
|
(unsigned long)(obj[1].idx.offset - obj->idx.offset),
|
|
|
|
(uintmax_t)obj->idx.offset);
|
|
|
|
if (is_delta_type(obj->type)) {
|
|
|
|
struct object_entry *bobj = &objects[obj->base_object_no];
|
|
|
|
printf(" %u %s", obj->delta_depth, sha1_to_hex(bobj->idx.sha1));
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2011-06-04 02:32:16 +04:00
|
|
|
|
|
|
|
if (baseobjects)
|
2012-04-23 16:30:29 +04:00
|
|
|
printf_ln(Q_("non delta: %d object",
|
|
|
|
"non delta: %d objects",
|
|
|
|
baseobjects),
|
|
|
|
baseobjects);
|
2011-06-04 02:32:16 +04:00
|
|
|
for (i = 0; i < deepest_delta; i++) {
|
|
|
|
if (!chain_histogram[i])
|
|
|
|
continue;
|
2012-04-23 16:30:29 +04:00
|
|
|
printf_ln(Q_("chain length = %d: %lu object",
|
|
|
|
"chain length = %d: %lu objects",
|
|
|
|
chain_histogram[i]),
|
|
|
|
i + 1,
|
|
|
|
chain_histogram[i]);
|
2011-06-04 02:32:16 +04:00
|
|
|
}
|
2011-06-04 02:32:15 +04:00
|
|
|
}
|
|
|
|
|
2010-01-22 18:55:19 +03:00
|
|
|
int cmd_index_pack(int argc, const char **argv, const char *prefix)
|
2005-10-12 23:01:31 +04:00
|
|
|
{
|
2013-03-19 17:01:15 +04:00
|
|
|
int i, fix_thin_pack = 0, verify = 0, stat_only = 0;
|
2014-03-25 17:41:41 +04:00
|
|
|
const char *curr_index;
|
2010-01-22 18:55:19 +03:00
|
|
|
const char *index_name = NULL, *pack_name = NULL;
|
2006-10-29 12:41:59 +03:00
|
|
|
const char *keep_name = NULL, *keep_msg = NULL;
|
2014-06-30 20:59:10 +04:00
|
|
|
struct strbuf index_name_buf = STRBUF_INIT,
|
|
|
|
keep_name_buf = STRBUF_INIT;
|
2007-06-01 23:18:05 +04:00
|
|
|
struct pack_idx_entry **idx_objects;
|
2011-02-26 02:43:25 +03:00
|
|
|
struct pack_idx_option opts;
|
2008-08-30 00:08:01 +04:00
|
|
|
unsigned char pack_sha1[20];
|
2013-05-26 05:16:17 +04:00
|
|
|
unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2009-11-09 18:05:01 +03:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "-h"))
|
|
|
|
usage(index_pack_usage);
|
|
|
|
|
2014-02-18 15:24:55 +04:00
|
|
|
check_replace_refs = 0;
|
2010-08-12 18:18:12 +04:00
|
|
|
|
2011-02-26 02:43:25 +03:00
|
|
|
reset_pack_idx_option(&opts);
|
|
|
|
git_config(git_index_pack_config, &opts);
|
2010-07-24 15:30:49 +04:00
|
|
|
if (prefix && chdir(prefix))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("Cannot come back to cwd"));
|
2007-11-02 06:26:04 +03:00
|
|
|
|
2005-10-12 23:01:31 +04:00
|
|
|
for (i = 1; i < argc; i++) {
|
2010-01-22 18:55:19 +03:00
|
|
|
const char *arg = argv[i];
|
2005-10-12 23:01:31 +04:00
|
|
|
|
|
|
|
if (*arg == '-') {
|
2006-10-23 22:50:18 +04:00
|
|
|
if (!strcmp(arg, "--stdin")) {
|
|
|
|
from_stdin = 1;
|
2006-10-26 07:28:17 +04:00
|
|
|
} else if (!strcmp(arg, "--fix-thin")) {
|
|
|
|
fix_thin_pack = 1;
|
2008-02-26 00:46:12 +03:00
|
|
|
} else if (!strcmp(arg, "--strict")) {
|
|
|
|
strict = 1;
|
2013-05-26 05:16:17 +04:00
|
|
|
do_fsck_object = 1;
|
|
|
|
} else if (!strcmp(arg, "--check-self-contained-and-connected")) {
|
|
|
|
strict = 1;
|
|
|
|
check_self_contained_and_connected = 1;
|
2011-02-03 04:29:01 +03:00
|
|
|
} else if (!strcmp(arg, "--verify")) {
|
|
|
|
verify = 1;
|
2011-06-04 02:32:15 +04:00
|
|
|
} else if (!strcmp(arg, "--verify-stat")) {
|
|
|
|
verify = 1;
|
2013-03-19 17:01:15 +04:00
|
|
|
show_stat = 1;
|
2011-06-04 02:32:15 +04:00
|
|
|
} else if (!strcmp(arg, "--verify-stat-only")) {
|
|
|
|
verify = 1;
|
2013-03-19 17:01:15 +04:00
|
|
|
show_stat = 1;
|
2011-06-04 02:32:15 +04:00
|
|
|
stat_only = 1;
|
2006-10-29 12:41:59 +03:00
|
|
|
} else if (!strcmp(arg, "--keep")) {
|
|
|
|
keep_msg = "";
|
2013-12-01 00:55:40 +04:00
|
|
|
} else if (starts_with(arg, "--keep=")) {
|
2006-10-29 12:41:59 +03:00
|
|
|
keep_msg = arg + 7;
|
2013-12-01 00:55:40 +04:00
|
|
|
} else if (starts_with(arg, "--threads=")) {
|
2012-05-06 16:31:55 +04:00
|
|
|
char *end;
|
|
|
|
nr_threads = strtoul(arg+10, &end, 0);
|
|
|
|
if (!arg[10] || *end || nr_threads < 0)
|
|
|
|
usage(index_pack_usage);
|
|
|
|
#ifdef NO_PTHREADS
|
|
|
|
if (nr_threads != 1)
|
2012-08-31 16:13:04 +04:00
|
|
|
warning(_("no threads support, "
|
|
|
|
"ignoring %s"), arg);
|
2012-05-06 16:31:55 +04:00
|
|
|
nr_threads = 1;
|
|
|
|
#endif
|
2013-12-01 00:55:40 +04:00
|
|
|
} else if (starts_with(arg, "--pack_header=")) {
|
2006-11-02 01:06:20 +03:00
|
|
|
struct pack_header *hdr;
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
hdr = (struct pack_header *)input_buffer;
|
|
|
|
hdr->hdr_signature = htonl(PACK_SIGNATURE);
|
|
|
|
hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
|
|
|
|
if (*c != ',')
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("bad %s"), arg);
|
2006-11-02 01:06:20 +03:00
|
|
|
hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
|
|
|
|
if (*c)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("bad %s"), arg);
|
2006-11-02 01:06:20 +03:00
|
|
|
input_len = sizeof(*hdr);
|
2006-10-26 07:32:59 +04:00
|
|
|
} else if (!strcmp(arg, "-v")) {
|
|
|
|
verbose = 1;
|
2006-10-23 22:50:18 +04:00
|
|
|
} else if (!strcmp(arg, "-o")) {
|
2005-10-12 23:01:31 +04:00
|
|
|
if (index_name || (i+1) >= argc)
|
|
|
|
usage(index_pack_usage);
|
|
|
|
index_name = argv[++i];
|
2013-12-01 00:55:40 +04:00
|
|
|
} else if (starts_with(arg, "--index-version=")) {
|
2007-04-10 01:32:03 +04:00
|
|
|
char *c;
|
2011-02-26 02:43:25 +03:00
|
|
|
opts.version = strtoul(arg + 16, &c, 10);
|
|
|
|
if (opts.version > 2)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("bad %s"), arg);
|
2007-04-10 01:32:03 +04:00
|
|
|
if (*c == ',')
|
2011-02-26 02:43:25 +03:00
|
|
|
opts.off32_limit = strtoul(c+1, &c, 0);
|
|
|
|
if (*c || opts.off32_limit & 0x80000000)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("bad %s"), arg);
|
2005-10-12 23:01:31 +04:00
|
|
|
} else
|
|
|
|
usage(index_pack_usage);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pack_name)
|
|
|
|
usage(index_pack_usage);
|
|
|
|
pack_name = arg;
|
|
|
|
}
|
|
|
|
|
2006-10-23 22:50:18 +04:00
|
|
|
if (!pack_name && !from_stdin)
|
2005-10-12 23:01:31 +04:00
|
|
|
usage(index_pack_usage);
|
2006-10-26 07:28:17 +04:00
|
|
|
if (fix_thin_pack && !from_stdin)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("--fix-thin cannot be used without --stdin"));
|
2006-10-23 22:50:18 +04:00
|
|
|
if (!index_name && pack_name) {
|
2014-06-30 20:59:10 +04:00
|
|
|
size_t len;
|
|
|
|
if (!strip_suffix(pack_name, ".pack", &len))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("packfile name '%s' does not end with '.pack'"),
|
2005-10-12 23:01:31 +04:00
|
|
|
pack_name);
|
2014-06-30 20:59:10 +04:00
|
|
|
strbuf_add(&index_name_buf, pack_name, len);
|
|
|
|
strbuf_addstr(&index_name_buf, ".idx");
|
|
|
|
index_name = index_name_buf.buf;
|
2005-10-12 23:01:31 +04:00
|
|
|
}
|
2006-10-29 12:41:59 +03:00
|
|
|
if (keep_msg && !keep_name && pack_name) {
|
2014-06-30 20:59:10 +04:00
|
|
|
size_t len;
|
|
|
|
if (!strip_suffix(pack_name, ".pack", &len))
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("packfile name '%s' does not end with '.pack'"),
|
2006-10-29 12:41:59 +03:00
|
|
|
pack_name);
|
2014-06-30 20:59:10 +04:00
|
|
|
strbuf_add(&keep_name_buf, pack_name, len);
|
|
|
|
strbuf_addstr(&keep_name_buf, ".idx");
|
|
|
|
keep_name = keep_name_buf.buf;
|
2006-10-29 12:41:59 +03:00
|
|
|
}
|
2011-02-03 04:29:01 +03:00
|
|
|
if (verify) {
|
|
|
|
if (!index_name)
|
2012-04-23 16:30:29 +04:00
|
|
|
die(_("--verify with no packfile name given"));
|
2011-02-03 04:29:01 +03:00
|
|
|
read_idx_option(&opts, index_name);
|
2011-11-17 10:04:13 +04:00
|
|
|
opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
|
2011-02-03 04:29:01 +03:00
|
|
|
}
|
2011-11-17 10:04:13 +04:00
|
|
|
if (strict)
|
|
|
|
opts.flags |= WRITE_IDX_STRICT;
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2012-05-06 16:31:55 +04:00
|
|
|
#ifndef NO_PTHREADS
|
|
|
|
if (!nr_threads) {
|
|
|
|
nr_threads = online_cpus();
|
|
|
|
/* An experiment showed that more threads does not mean faster */
|
|
|
|
if (nr_threads > 3)
|
|
|
|
nr_threads = 3;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-23 22:50:18 +04:00
|
|
|
curr_pack = open_pack_file(pack_name);
|
2005-10-12 23:01:31 +04:00
|
|
|
parse_pack_header();
|
2011-06-04 02:32:15 +04:00
|
|
|
objects = xcalloc(nr_objects + 1, sizeof(struct object_entry));
|
|
|
|
deltas = xcalloc(nr_objects, sizeof(struct delta_entry));
|
2008-08-30 00:08:01 +04:00
|
|
|
parse_pack_objects(pack_sha1);
|
2012-05-06 16:31:54 +04:00
|
|
|
resolve_deltas();
|
|
|
|
conclude_pack(fix_thin_pack, curr_pack, pack_sha1);
|
2005-10-12 23:01:31 +04:00
|
|
|
free(deltas);
|
2008-02-26 00:46:12 +03:00
|
|
|
if (strict)
|
2013-05-26 05:16:17 +04:00
|
|
|
foreign_nr = check_objects();
|
2007-06-01 23:18:05 +04:00
|
|
|
|
2013-03-19 17:01:15 +04:00
|
|
|
if (show_stat)
|
2011-06-04 02:32:15 +04:00
|
|
|
show_pack_info(stat_only);
|
|
|
|
|
2007-06-01 23:18:05 +04:00
|
|
|
idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
|
|
|
|
for (i = 0; i < nr_objects; i++)
|
|
|
|
idx_objects[i] = &objects[i].idx;
|
2011-02-26 02:43:25 +03:00
|
|
|
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
|
2007-06-01 23:18:05 +04:00
|
|
|
free(idx_objects);
|
|
|
|
|
2011-02-03 04:29:01 +03:00
|
|
|
if (!verify)
|
|
|
|
final(pack_name, curr_pack,
|
|
|
|
index_name, curr_index,
|
|
|
|
keep_name, keep_msg,
|
|
|
|
pack_sha1);
|
|
|
|
else
|
|
|
|
close(input_fd);
|
2005-10-12 23:01:31 +04:00
|
|
|
free(objects);
|
2014-06-30 20:59:10 +04:00
|
|
|
strbuf_release(&index_name_buf);
|
|
|
|
strbuf_release(&keep_name_buf);
|
2007-10-17 05:55:50 +04:00
|
|
|
if (pack_name == NULL)
|
2010-01-22 18:55:19 +03:00
|
|
|
free((void *) curr_pack);
|
2007-10-17 05:55:50 +04:00
|
|
|
if (index_name == NULL)
|
2010-01-22 18:55:19 +03:00
|
|
|
free((void *) curr_index);
|
2005-10-12 23:01:31 +04:00
|
|
|
|
2013-05-26 05:16:17 +04:00
|
|
|
/*
|
|
|
|
* Let the caller know this pack is not self contained
|
|
|
|
*/
|
|
|
|
if (check_self_contained_and_connected && foreign_nr)
|
|
|
|
return 1;
|
|
|
|
|
2005-10-12 23:01:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|