Commit b5b474dd by Carlos Martín Nieto

Modify the given offset in git_packfile_unpack

The callers immediately throw away the offset, so we don't need any
logical changes in any of them. This will be useful for the indexer,
as it does need to know where the compressed data ends.

Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
parent a070f152
...@@ -32,55 +32,37 @@ ...@@ -32,55 +32,37 @@
#include "mwindow.h" #include "mwindow.h"
#include "posix.h" #include "posix.h"
struct entry {
unsigned char sha[GIT_OID_RAWSZ];
uint32_t crc;
uint32_t offset;
uint64_t offset_long;
};
typedef struct git_indexer { typedef struct git_indexer {
struct git_pack_file *pack; struct git_pack_file *pack;
git_vector objects;
git_vector deltas;
struct stat st; struct stat st;
git_indexer_stats stats; git_indexer_stats stats;
struct git_pack_header hdr;
struct entry *objects;
} git_indexer; } git_indexer;
static int parse_header(git_indexer *idx) static int parse_header(git_indexer *idx)
{ {
struct git_pack_header hdr;
int error; int error;
/* Verify we recognize this pack file format. */ /* Verify we recognize this pack file format. */
if ((error = p_read(idx->pack->mwf.fd, &hdr, sizeof(hdr))) < GIT_SUCCESS) if ((error = p_read(idx->pack->mwf.fd, &idx->hdr, sizeof(idx->hdr))) < GIT_SUCCESS)
goto cleanup; return git__rethrow(error, "Failed to read in pack header");
if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) { if (idx->hdr.hdr_signature != htonl(PACK_SIGNATURE))
error = git__throw(GIT_EOBJCORRUPTED, "Wrong pack signature"); return git__throw(GIT_EOBJCORRUPTED, "Wrong pack signature");
goto cleanup;
}
if (!pack_version_ok(hdr.hdr_version)) { if (!pack_version_ok(idx->hdr.hdr_version))
error = git__throw(GIT_EOBJCORRUPTED, "Wrong pack version"); return git__throw(GIT_EOBJCORRUPTED, "Wrong pack version");
goto cleanup;
}
/*
* FIXME: At this point we have no idea how many of the are
* deltas, so assume all objects are both until we get a better
* idea
*/
error = git_vector_init(&idx->objects, hdr.hdr_entries, NULL /* FIXME: probably need something */);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_vector_init(&idx->deltas, hdr.hdr_entries, NULL /* FIXME: probably need something */);
if (error < GIT_SUCCESS)
goto cleanup;
idx->stats.total = hdr.hdr_entries;
return GIT_SUCCESS; return GIT_SUCCESS;
cleanup:
git_vector_free(&idx->objects);
git_vector_free(&idx->deltas);
return error;
} }
int git_indexer_new(git_indexer **out, const char *packname) int git_indexer_new(git_indexer **out, const char *packname)
...@@ -127,6 +109,14 @@ int git_indexer_new(git_indexer **out, const char *packname) ...@@ -127,6 +109,14 @@ int git_indexer_new(git_indexer **out, const char *packname)
goto cleanup; goto cleanup;
} }
idx->objects = git__calloc(sizeof(struct entry), idx->hdr.hdr_entries);
if (idx->objects == NULL) {
error = GIT_ENOMEM;
goto cleanup;
}
idx->stats.total = idx->hdr.hdr_entries;
*out = idx; *out = idx;
return GIT_SUCCESS; return GIT_SUCCESS;
...@@ -139,41 +129,6 @@ cleanup: ...@@ -139,41 +129,6 @@ cleanup:
} }
/* /*
* Parse the variable-width length and return it. Assumes that the
* whole number exists inside the buffer. As this is the git format,
* the first byte only contains length information in the lower nibble
* because the higher one is used for type and continuation. The
* output parameter is necessary because we don't know how long the
* entry is actually going to be.
*/
static unsigned long entry_len(const char **bufout, const char *buf)
{
unsigned long size, c;
const char *p = buf;
unsigned shift;
c = *p;
size = c & 0xf;
shift = 4;
/* As long as the MSB is set, we need to continue */
while (c & 0x80) {
p++;
c = *p;
size += (c & 0x7f) << shift;
shift += 7;
}
*bufout = p;
return size;
}
static git_otype entry_type(const char *buf)
{
return (*buf >> 4) & 7;
}
/*
* Create the index. Every time something interesting happens * Create the index. Every time something interesting happens
* (something has been parse or resolved), the callback gets called * (something has been parse or resolved), the callback gets called
* with some stats so it can tell the user how hard we're working * with some stats so it can tell the user how hard we're working
...@@ -181,12 +136,12 @@ static git_otype entry_type(const char *buf) ...@@ -181,12 +136,12 @@ static git_otype entry_type(const char *buf)
int git_indexer_run(git_indexer *idx, int (*cb)(const git_indexer_stats *, void *), void *cb_data) int git_indexer_run(git_indexer *idx, int (*cb)(const git_indexer_stats *, void *), void *cb_data)
{ {
git_mwindow_file *mwf = &idx->pack->mwf; git_mwindow_file *mwf = &idx->pack->mwf;
git_mwindow *w = NULL;
off_t off = 0; off_t off = 0;
int error; int error;
const char *ptr;
unsigned int fanout[256] = {0}; unsigned int fanout[256] = {0};
/* FIXME: Write the keep file */
error = git_mwindow_file_register(mwf); error = git_mwindow_file_register(mwf);
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to register mwindow file"); return git__rethrow(error, "Failed to register mwindow file");
...@@ -196,29 +151,45 @@ int git_indexer_run(git_indexer *idx, int (*cb)(const git_indexer_stats *, void ...@@ -196,29 +151,45 @@ int git_indexer_run(git_indexer *idx, int (*cb)(const git_indexer_stats *, void
cb(&idx->stats, cb_data); cb(&idx->stats, cb_data);
while (idx->stats.processed < idx->stats.total) { while (idx->stats.processed < idx->stats.total) {
size_t size; git_rawobj obj;
git_otype type; git_oid oid;
struct entry entry;
error = git_packfile_unpack_header(&size, &type, mwf, &w, &off); char hdr[512] = {0}; /* FIXME: How long should this be? */
int i, hdr_len;
switch (type) {
case GIT_OBJ_COMMIT: memset(&entry, 0x0, sizeof(entry)); /* Necessary? */
case GIT_OBJ_TREE:
case GIT_OBJ_BLOB: if (off > UINT31_MAX) {
case GIT_OBJ_TAG: entry.offset = ~0ULL;
break; entry.offset_long = off;
default: } else {
error = git__throw(GIT_EOBJCORRUPTED, "Invalid object type"); entry.offset = off;
}
error = git_packfile_unpack(&obj, idx->pack, &off);
if (error < GIT_SUCCESS) {
error = git__rethrow(error, "Failed to unpack object");
goto cleanup; goto cleanup;
} }
/* error = git_odb__hash_obj(&oid, hdr, sizeof(hdr), &hdr_len, &obj);
* Do we need to uncompress everything if we're not running in if (error < GIT_SUCCESS) {
* strict mode? Or at least can't we free the data? error = git__rethrow(error, "Failed to hash object");
*/ goto cleanup;
}
memcpy(&entry.sha, oid.id, GIT_OID_RAWSZ);
/* entry.crc = crc32(obj.data) */
/* Get a window for the compressed data */ /* Add the object to the list */
//ptr = git_mwindow_open(mwf, &w, idx->pack->pack_fd, size, data - ptr, 0, NULL); //memcpy(&idx->objects[idx->stats.processed], &entry, sizeof(entry));
idx->objects[idx->stats.processed] = entry;
for (i = oid.id[0]; i < 256; ++i) {
fanout[i]++;
}
free(obj.data);
idx->stats.processed++; idx->stats.processed++;
...@@ -227,6 +198,10 @@ int git_indexer_run(git_indexer *idx, int (*cb)(const git_indexer_stats *, void ...@@ -227,6 +198,10 @@ int git_indexer_run(git_indexer *idx, int (*cb)(const git_indexer_stats *, void
} }
/*
* All's gone well, so let's write the index file.
*/
cleanup: cleanup:
git_mwindow_free_all(mwf); git_mwindow_free_all(mwf);
...@@ -237,8 +212,7 @@ cleanup: ...@@ -237,8 +212,7 @@ cleanup:
void git_indexer_free(git_indexer *idx) void git_indexer_free(git_indexer *idx)
{ {
p_close(idx->pack->mwf.fd); p_close(idx->pack->mwf.fd);
git_vector_free(&idx->objects); free(idx->objects);
git_vector_free(&idx->deltas);
free(idx->pack); free(idx->pack);
free(idx); free(idx);
} }
......
...@@ -389,7 +389,7 @@ int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_od ...@@ -389,7 +389,7 @@ int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_od
if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < GIT_SUCCESS) if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to read pack backend"); return git__rethrow(error, "Failed to read pack backend");
if ((error = git_packfile_unpack(&raw, e.p, e.offset)) < GIT_SUCCESS) if ((error = git_packfile_unpack(&raw, e.p, &e.offset)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to read pack backend"); return git__rethrow(error, "Failed to read pack backend");
*buffer_p = raw.data; *buffer_p = raw.data;
...@@ -426,7 +426,7 @@ int pack_backend__read_prefix( ...@@ -426,7 +426,7 @@ int pack_backend__read_prefix(
if ((error = pack_entry_find_prefix(&e, (struct pack_backend *)backend, short_oid, len)) < GIT_SUCCESS) if ((error = pack_entry_find_prefix(&e, (struct pack_backend *)backend, short_oid, len)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to read pack backend"); return git__rethrow(error, "Failed to read pack backend");
if ((error = git_packfile_unpack(&raw, e.p, e.offset)) < GIT_SUCCESS) if ((error = git_packfile_unpack(&raw, e.p, &e.offset)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to read pack backend"); return git__rethrow(error, "Failed to read pack backend");
*buffer_p = raw.data; *buffer_p = raw.data;
......
...@@ -38,7 +38,7 @@ int packfile_unpack_compressed( ...@@ -38,7 +38,7 @@ int packfile_unpack_compressed(
git_rawobj *obj, git_rawobj *obj,
struct git_pack_file *p, struct git_pack_file *p,
git_mwindow **w_curs, git_mwindow **w_curs,
off_t curpos, off_t *curpos,
size_t size, size_t size,
git_otype type); git_otype type);
...@@ -286,7 +286,7 @@ static int packfile_unpack_delta( ...@@ -286,7 +286,7 @@ static int packfile_unpack_delta(
git_rawobj *obj, git_rawobj *obj,
struct git_pack_file *p, struct git_pack_file *p,
git_mwindow **w_curs, git_mwindow **w_curs,
off_t curpos, off_t *curpos,
size_t delta_size, size_t delta_size,
git_otype delta_type, git_otype delta_type,
off_t obj_offset) off_t obj_offset)
...@@ -295,12 +295,12 @@ static int packfile_unpack_delta( ...@@ -295,12 +295,12 @@ static int packfile_unpack_delta(
git_rawobj base, delta; git_rawobj base, delta;
int error; int error;
base_offset = get_delta_base(p, w_curs, &curpos, delta_type, obj_offset); base_offset = get_delta_base(p, w_curs, curpos, delta_type, obj_offset);
if (base_offset == 0) if (base_offset == 0)
return git__throw(GIT_EOBJCORRUPTED, "Delta offset is zero"); return git__throw(GIT_EOBJCORRUPTED, "Delta offset is zero");
git_mwindow_close(w_curs); git_mwindow_close(w_curs);
error = git_packfile_unpack(&base, p, base_offset); error = git_packfile_unpack(&base, p, &base_offset);
/* /*
* TODO: git.git tries to load the base from other packfiles * TODO: git.git tries to load the base from other packfiles
...@@ -333,10 +333,10 @@ static int packfile_unpack_delta( ...@@ -333,10 +333,10 @@ static int packfile_unpack_delta(
int git_packfile_unpack( int git_packfile_unpack(
git_rawobj *obj, git_rawobj *obj,
struct git_pack_file *p, struct git_pack_file *p,
off_t obj_offset) off_t *obj_offset)
{ {
git_mwindow *w_curs = NULL; git_mwindow *w_curs = NULL;
off_t curpos = obj_offset; off_t curpos = *obj_offset;
int error; int error;
size_t size = 0; size_t size = 0;
...@@ -358,8 +358,8 @@ int git_packfile_unpack( ...@@ -358,8 +358,8 @@ int git_packfile_unpack(
case GIT_OBJ_OFS_DELTA: case GIT_OBJ_OFS_DELTA:
case GIT_OBJ_REF_DELTA: case GIT_OBJ_REF_DELTA:
error = packfile_unpack_delta( error = packfile_unpack_delta(
obj, p, &w_curs, curpos, obj, p, &w_curs, &curpos,
size, type, obj_offset); size, type, *obj_offset);
break; break;
case GIT_OBJ_COMMIT: case GIT_OBJ_COMMIT:
...@@ -367,7 +367,7 @@ int git_packfile_unpack( ...@@ -367,7 +367,7 @@ int git_packfile_unpack(
case GIT_OBJ_BLOB: case GIT_OBJ_BLOB:
case GIT_OBJ_TAG: case GIT_OBJ_TAG:
error = packfile_unpack_compressed( error = packfile_unpack_compressed(
obj, p, &w_curs, curpos, obj, p, &w_curs, &curpos,
size, type); size, type);
break; break;
...@@ -377,14 +377,19 @@ int git_packfile_unpack( ...@@ -377,14 +377,19 @@ int git_packfile_unpack(
} }
git_mwindow_close(&w_curs); git_mwindow_close(&w_curs);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to unpack packfile");
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to unpack object");
*obj_offset = curpos;
return GIT_SUCCESS;
} }
int packfile_unpack_compressed( int packfile_unpack_compressed(
git_rawobj *obj, git_rawobj *obj,
struct git_pack_file *p, struct git_pack_file *p,
git_mwindow **w_curs, git_mwindow **w_curs,
off_t curpos, off_t *curpos,
size_t size, size_t size,
git_otype type) git_otype type)
{ {
...@@ -406,14 +411,14 @@ int packfile_unpack_compressed( ...@@ -406,14 +411,14 @@ int packfile_unpack_compressed(
} }
do { do {
in = pack_window_open(p, w_curs, curpos, &stream.avail_in); in = pack_window_open(p, w_curs, *curpos, &stream.avail_in);
stream.next_in = in; stream.next_in = in;
st = inflate(&stream, Z_FINISH); st = inflate(&stream, Z_FINISH);
if (!stream.avail_out) if (!stream.avail_out)
break; /* the payload is larger than it should be */ break; /* the payload is larger than it should be */
curpos += stream.next_in - in; *curpos += stream.next_in - in;
} while (st == Z_OK || st == Z_BUF_ERROR); } while (st == Z_OK || st == Z_BUF_ERROR);
inflateEnd(&stream); inflateEnd(&stream);
...@@ -429,6 +434,10 @@ int packfile_unpack_compressed( ...@@ -429,6 +434,10 @@ int packfile_unpack_compressed(
return GIT_SUCCESS; return GIT_SUCCESS;
} }
/*
* curpos is where the data starts, delta_obj_offset is the where the
* header starts
*/
off_t get_delta_base( off_t get_delta_base(
struct git_pack_file *p, struct git_pack_file *p,
git_mwindow **w_curs, git_mwindow **w_curs,
......
...@@ -97,7 +97,7 @@ int git_packfile_unpack_header( ...@@ -97,7 +97,7 @@ int git_packfile_unpack_header(
git_mwindow **w_curs, git_mwindow **w_curs,
off_t *curpos); off_t *curpos);
int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, off_t obj_offset); int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, off_t *obj_offset);
off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs, off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
off_t *curpos, git_otype type, off_t *curpos, git_otype type,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment