Blame src/indexer.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "git2/indexer.h"
Packit Service 20376f
#include "git2/object.h"
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "pack.h"
Packit Service 20376f
#include "mwindow.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "pack.h"
Packit Service 20376f
#include "filebuf.h"
Packit Service 20376f
#include "oid.h"
Packit Service 20376f
#include "oidmap.h"
Packit Service 20376f
#include "zstream.h"
Packit Service 20376f
#include "object.h"
Packit Service 20376f
Packit Service 20376f
extern git_mutex git__mwindow_mutex;
Packit Service 20376f
Packit Service 20376f
#define UINT31_MAX (0x7FFFFFFF)
Packit Service 20376f
Packit Service 20376f
struct entry {
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	uint32_t crc;
Packit Service 20376f
	uint32_t offset;
Packit Service 20376f
	uint64_t offset_long;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct git_indexer {
Packit Service 20376f
	unsigned int parsed_header :1,
Packit Service 20376f
		pack_committed :1,
Packit Service 20376f
		have_stream :1,
Packit Service 20376f
		have_delta :1,
Packit Service 20376f
		do_fsync :1;
Packit Service 20376f
	struct git_pack_header hdr;
Packit Service 20376f
	struct git_pack_file *pack;
Packit Service 20376f
	unsigned int mode;
Packit Service 20376f
	git_off_t off;
Packit Service 20376f
	git_off_t entry_start;
Packit Service 20376f
	git_packfile_stream stream;
Packit Service 20376f
	size_t nr_objects;
Packit Service 20376f
	git_vector objects;
Packit Service 20376f
	git_vector deltas;
Packit Service 20376f
	unsigned int fanout[256];
Packit Service 20376f
	git_hash_ctx hash_ctx;
Packit Service 20376f
	git_oid hash;
Packit Service 20376f
	git_transfer_progress_cb progress_cb;
Packit Service 20376f
	void *progress_payload;
Packit Service 20376f
	char objbuf[8*1024];
Packit Service 20376f
Packit Service 20376f
	/* Needed to look up objects which we want to inject to fix a thin pack */
Packit Service 20376f
	git_odb *odb;
Packit Service 20376f
Packit Service 20376f
	/* Fields for calculating the packfile trailer (hash of everything before it) */
Packit Service 20376f
	char inbuf[GIT_OID_RAWSZ];
Packit Service 20376f
	size_t inbuf_len;
Packit Service 20376f
	git_hash_ctx trailer;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct delta_info {
Packit Service 20376f
	git_off_t delta_off;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
const git_oid *git_indexer_hash(const git_indexer *idx)
Packit Service 20376f
{
Packit Service 20376f
	return &idx->hash;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_map map;
Packit Service 20376f
Packit Service 20376f
	if ((error = p_mmap(&map, sizeof(*hdr), GIT_PROT_READ, GIT_MAP_SHARED, pack->mwf.fd, 0)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	memcpy(hdr, map.data, sizeof(*hdr));
Packit Service 20376f
	p_munmap(&map);
Packit Service 20376f
Packit Service 20376f
	/* Verify we recognize this pack file format. */
Packit Service 20376f
	if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "wrong pack signature");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!pack_version_ok(hdr->hdr_version)) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "wrong pack version");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int objects_cmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const struct entry *entrya = a;
Packit Service 20376f
	const struct entry *entryb = b;
Packit Service 20376f
Packit Service 20376f
	return git_oid__cmp(&entrya->oid, &entryb->oid);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_indexer_new(
Packit Service 20376f
		git_indexer **out,
Packit Service 20376f
		const char *prefix,
Packit Service 20376f
		unsigned int mode,
Packit Service 20376f
		git_odb *odb,
Packit Service 20376f
		git_transfer_progress_cb progress_cb,
Packit Service 20376f
		void *progress_payload)
Packit Service 20376f
{
Packit Service 20376f
	git_indexer *idx;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, tmp_path = GIT_BUF_INIT;
Packit Service 20376f
	static const char suff[] = "/pack";
Packit Service 20376f
	int error, fd = -1;
Packit Service 20376f
Packit Service 20376f
	idx = git__calloc(1, sizeof(git_indexer));
Packit Service 20376f
	GITERR_CHECK_ALLOC(idx);
Packit Service 20376f
	idx->odb = odb;
Packit Service 20376f
	idx->progress_cb = progress_cb;
Packit Service 20376f
	idx->progress_payload = progress_payload;
Packit Service 20376f
	idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
Packit Service 20376f
	git_hash_ctx_init(&idx->hash_ctx);
Packit Service 20376f
	git_hash_ctx_init(&idx->trailer);
Packit Service 20376f
Packit Service 20376f
	if (git_repository__fsync_gitdir)
Packit Service 20376f
		idx->do_fsync = 1;
Packit Service 20376f
Packit Service 20376f
	error = git_buf_joinpath(&path, prefix, suff);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	fd = git_futils_mktmp(&tmp_path, git_buf_cstr(&path), idx->mode);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	if (fd < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = git_packfile_alloc(&idx->pack, git_buf_cstr(&tmp_path));
Packit Service 20376f
	git_buf_free(&tmp_path);
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	idx->pack->mwf.fd = fd;
Packit Service 20376f
	if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	*out = idx;
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (fd != -1)
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_len(&tmp_path) > 0)
Packit Service 20376f
		p_unlink(git_buf_cstr(&tmp_path));
Packit Service 20376f
Packit Service 20376f
	if (idx->pack != NULL)
Packit Service 20376f
		p_unlink(idx->pack->pack_name);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&tmp_path);
Packit Service 20376f
	git__free(idx);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_indexer__set_fsync(git_indexer *idx, int do_fsync)
Packit Service 20376f
{
Packit Service 20376f
	idx->do_fsync = !!do_fsync;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Try to store the delta so we can try to resolve it later */
Packit Service 20376f
static int store_delta(git_indexer *idx)
Packit Service 20376f
{
Packit Service 20376f
	struct delta_info *delta;
Packit Service 20376f
Packit Service 20376f
	delta = git__calloc(1, sizeof(struct delta_info));
Packit Service 20376f
	GITERR_CHECK_ALLOC(delta);
Packit Service 20376f
	delta->delta_off = idx->entry_start;
Packit Service 20376f
Packit Service 20376f
	if (git_vector_insert(&idx->deltas, delta) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void hash_header(git_hash_ctx *ctx, git_off_t len, git_otype type)
Packit Service 20376f
{
Packit Service 20376f
	char buffer[64];
Packit Service 20376f
	size_t hdrlen;
Packit Service 20376f
Packit Service 20376f
	hdrlen = git_odb__format_object_header(buffer, sizeof(buffer), (size_t)len, type);
Packit Service 20376f
	git_hash_update(ctx, buffer, hdrlen);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int hash_object_stream(git_indexer*idx, git_packfile_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t read;
Packit Service 20376f
Packit Service 20376f
	assert(idx && stream);
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		if ((read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf))) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		git_hash_update(&idx->hash_ctx, idx->objbuf, read);
Packit Service 20376f
	} while (read > 0);
Packit Service 20376f
Packit Service 20376f
	if (read < 0)
Packit Service 20376f
		return (int)read;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* In order to create the packfile stream, we need to skip over the delta base description */
Packit Service 20376f
static int advance_delta_offset(git_indexer *idx, git_otype type)
Packit Service 20376f
{
Packit Service 20376f
	git_mwindow *w = NULL;
Packit Service 20376f
Packit Service 20376f
	assert(type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA);
Packit Service 20376f
Packit Service 20376f
	if (type == GIT_OBJ_REF_DELTA) {
Packit Service 20376f
		idx->off += GIT_OID_RAWSZ;
Packit Service 20376f
	} else {
Packit Service 20376f
		git_off_t base_off = get_delta_base(idx->pack, &w, &idx->off, type, idx->entry_start);
Packit Service 20376f
		git_mwindow_close(&w);
Packit Service 20376f
		if (base_off < 0)
Packit Service 20376f
			return (int)base_off;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Read from the stream and discard any output */
Packit Service 20376f
static int read_object_stream(git_indexer *idx, git_packfile_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t read;
Packit Service 20376f
Packit Service 20376f
	assert(stream);
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf));
Packit Service 20376f
	} while (read > 0);
Packit Service 20376f
Packit Service 20376f
	if (read < 0)
Packit Service 20376f
		return (int)read;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, git_off_t start, git_off_t size)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr;
Packit Service 20376f
	uint32_t crc;
Packit Service 20376f
	unsigned int left, len;
Packit Service 20376f
	git_mwindow *w = NULL;
Packit Service 20376f
Packit Service 20376f
	crc = crc32(0L, Z_NULL, 0);
Packit Service 20376f
	while (size) {
Packit Service 20376f
		ptr = git_mwindow_open(mwf, &w, start, (size_t)size, &left);
Packit Service 20376f
		if (ptr == NULL)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		len = min(left, (unsigned int)size);
Packit Service 20376f
		crc = crc32(crc, ptr, len);
Packit Service 20376f
		size -= len;
Packit Service 20376f
		start += len;
Packit Service 20376f
		git_mwindow_close(&w);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*crc_out = htonl(crc);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int store_object(git_indexer *idx)
Packit Service 20376f
{
Packit Service 20376f
	int i, error;
Packit Service 20376f
	khiter_t k;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	struct entry *entry;
Packit Service 20376f
	git_off_t entry_size;
Packit Service 20376f
	struct git_pack_entry *pentry;
Packit Service 20376f
	git_off_t entry_start = idx->entry_start;
Packit Service 20376f
Packit Service 20376f
	entry = git__calloc(1, sizeof(*entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(entry);
Packit Service 20376f
Packit Service 20376f
	pentry = git__calloc(1, sizeof(struct git_pack_entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pentry);
Packit Service 20376f
Packit Service 20376f
	git_hash_final(&oid, &idx->hash_ctx);
Packit Service 20376f
	entry_size = idx->off - entry_start;
Packit Service 20376f
	if (entry_start > UINT31_MAX) {
Packit Service 20376f
		entry->offset = UINT32_MAX;
Packit Service 20376f
		entry->offset_long = entry_start;
Packit Service 20376f
	} else {
Packit Service 20376f
		entry->offset = (uint32_t)entry_start;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&pentry->sha1, &oid;;
Packit Service 20376f
	pentry->offset = entry_start;
Packit Service 20376f
Packit Service 20376f
	k = git_oidmap_put(idx->pack->idx_cache, &pentry->sha1, &error);
Packit Service 20376f
	if (error == -1) {
Packit Service 20376f
		git__free(pentry);
Packit Service 20376f
		giterr_set_oom();
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error == 0) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "duplicate object %s found in pack", git_oid_tostr_s(&pentry->sha1));
Packit Service 20376f
		git__free(pentry);
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	git_oidmap_set_value_at(idx->pack->idx_cache, k, pentry);
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&entry->oid, &oid;;
Packit Service 20376f
Packit Service 20376f
	if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* Add the object to the list */
Packit Service 20376f
	if (git_vector_insert(&idx->objects, entry) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	for (i = oid.id[0]; i < 256; ++i) {
Packit Service 20376f
		idx->fanout[i]++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git__free(entry);
Packit Service 20376f
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id)
Packit Service 20376f
{
Packit Service 20376f
	return git_oidmap_exists(idx->pack->idx_cache, id);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, git_off_t entry_start)
Packit Service 20376f
{
Packit Service 20376f
	int i, error;
Packit Service 20376f
	khiter_t k;
Packit Service 20376f
Packit Service 20376f
	if (entry_start > UINT31_MAX) {
Packit Service 20376f
		entry->offset = UINT32_MAX;
Packit Service 20376f
		entry->offset_long = entry_start;
Packit Service 20376f
	} else {
Packit Service 20376f
		entry->offset = (uint32_t)entry_start;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	pentry->offset = entry_start;
Packit Service 20376f
	k = git_oidmap_put(idx->pack->idx_cache, &pentry->sha1, &error);
Packit Service 20376f
Packit Service 20376f
	if (error <= 0) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "cannot insert object into pack");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_oidmap_set_value_at(idx->pack->idx_cache, k, pentry);
Packit Service 20376f
Packit Service 20376f
	/* Add the object to the list */
Packit Service 20376f
	if (git_vector_insert(&idx->objects, entry) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	for (i = entry->oid.id[0]; i < 256; ++i) {
Packit Service 20376f
		idx->fanout[i]++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_start)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	size_t entry_size;
Packit Service 20376f
	struct entry *entry;
Packit Service 20376f
	struct git_pack_entry *pentry = NULL;
Packit Service 20376f
Packit Service 20376f
	entry = git__calloc(1, sizeof(*entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(entry);
Packit Service 20376f
Packit Service 20376f
	if (git_odb__hashobj(&oid, obj) < 0) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "failed to hash object");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	pentry = git__calloc(1, sizeof(struct git_pack_entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pentry);
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&pentry->sha1, &oid;;
Packit Service 20376f
	git_oid_cpy(&entry->oid, &oid;;
Packit Service 20376f
	entry->crc = crc32(0L, Z_NULL, 0);
Packit Service 20376f
Packit Service 20376f
	entry_size = (size_t)(idx->off - entry_start);
Packit Service 20376f
	if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	return save_entry(idx, entry, pentry, entry_start);
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git__free(pentry);
Packit Service 20376f
	git__free(entry);
Packit Service 20376f
	git__free(obj->data);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int do_progress_callback(git_indexer *idx, git_transfer_progress *stats)
Packit Service 20376f
{
Packit Service 20376f
	if (idx->progress_cb)
Packit Service 20376f
		return giterr_set_after_callback_function(
Packit Service 20376f
			idx->progress_cb(stats, idx->progress_payload),
Packit Service 20376f
			"indexer progress");
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Hash everything but the last 20B of input */
Packit Service 20376f
static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	size_t to_expell, to_keep;
Packit Service 20376f
Packit Service 20376f
	if (size == 0)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	/* Easy case, dump the buffer and the data minus the last 20 bytes */
Packit Service 20376f
	if (size >= GIT_OID_RAWSZ) {
Packit Service 20376f
		git_hash_update(&idx->trailer, idx->inbuf, idx->inbuf_len);
Packit Service 20376f
		git_hash_update(&idx->trailer, data, size - GIT_OID_RAWSZ);
Packit Service 20376f
Packit Service 20376f
		data += size - GIT_OID_RAWSZ;
Packit Service 20376f
		memcpy(idx->inbuf, data, GIT_OID_RAWSZ);
Packit Service 20376f
		idx->inbuf_len = GIT_OID_RAWSZ;
Packit Service 20376f
		return;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* We can just append */
Packit Service 20376f
	if (idx->inbuf_len + size <= GIT_OID_RAWSZ) {
Packit Service 20376f
		memcpy(idx->inbuf + idx->inbuf_len, data, size);
Packit Service 20376f
		idx->inbuf_len += size;
Packit Service 20376f
		return;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* We need to partially drain the buffer and then append */
Packit Service 20376f
	to_keep   = GIT_OID_RAWSZ - size;
Packit Service 20376f
	to_expell = idx->inbuf_len - to_keep;
Packit Service 20376f
Packit Service 20376f
	git_hash_update(&idx->trailer, idx->inbuf, to_expell);
Packit Service 20376f
Packit Service 20376f
	memmove(idx->inbuf, idx->inbuf + to_expell, to_keep);
Packit Service 20376f
	memcpy(idx->inbuf + to_keep, data, size);
Packit Service 20376f
	idx->inbuf_len += size - to_expell;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	git_file fd = idx->pack->mwf.fd;
Packit Service 20376f
	size_t mmap_alignment;
Packit Service 20376f
	size_t page_offset;
Packit Service 20376f
	git_off_t page_start;
Packit Service 20376f
	unsigned char *map_data;
Packit Service 20376f
	git_map map;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(data && size);
Packit Service 20376f
Packit Service 20376f
	if ((error = git__mmap_alignment(&mmap_alignment)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* the offset needs to be at the mmap boundary for the platform */
Packit Service 20376f
	page_offset = offset % mmap_alignment;
Packit Service 20376f
	page_start = offset - page_offset;
Packit Service 20376f
Packit Service 20376f
	if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	map_data = (unsigned char *)map.data;
Packit Service 20376f
	memcpy(map_data + page_offset, data, size);
Packit Service 20376f
	p_munmap(&map);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int append_to_pack(git_indexer *idx, const void *data, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	git_off_t new_size;
Packit Service 20376f
	size_t mmap_alignment;
Packit Service 20376f
	size_t page_offset;
Packit Service 20376f
	git_off_t page_start;
Packit Service 20376f
	git_off_t current_size = idx->pack->mwf.size;
Packit Service 20376f
	int fd = idx->pack->mwf.fd;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!size)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git__mmap_alignment(&mmap_alignment)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* Write a single byte to force the file system to allocate space now or
Packit Service 20376f
	 * report an error, since we can't report errors when writing using mmap.
Packit Service 20376f
	 * Round the size up to the nearest page so that we only need to perform file
Packit Service 20376f
	 * I/O when we add a page, instead of whenever we write even a single byte. */
Packit Service 20376f
	new_size = current_size + size;
Packit Service 20376f
	page_offset = new_size % mmap_alignment;
Packit Service 20376f
	page_start = new_size - page_offset;
Packit Service 20376f
Packit Service 20376f
	if (p_lseek(fd, page_start + mmap_alignment - 1, SEEK_SET) < 0 ||
Packit Service 20376f
	    p_write(idx->pack->mwf.fd, data, 1) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "cannot extend packfile '%s'", idx->pack->pack_name);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return write_at(idx, data, idx->pack->mwf.size, size);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats)
Packit Service 20376f
{
Packit Service 20376f
	int error = -1;
Packit Service 20376f
	size_t processed;
Packit Service 20376f
	struct git_pack_header *hdr = &idx->hdr;
Packit Service 20376f
	git_mwindow_file *mwf = &idx->pack->mwf;
Packit Service 20376f
Packit Service 20376f
	assert(idx && data && stats);
Packit Service 20376f
Packit Service 20376f
	processed = stats->indexed_objects;
Packit Service 20376f
Packit Service 20376f
	if ((error = append_to_pack(idx, data, size)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	hash_partially(idx, data, (int)size);
Packit Service 20376f
Packit Service 20376f
	/* Make sure we set the new size of the pack */
Packit Service 20376f
	idx->pack->mwf.size += size;
Packit Service 20376f
Packit Service 20376f
	if (!idx->parsed_header) {
Packit Service 20376f
		unsigned int total_objects;
Packit Service 20376f
Packit Service 20376f
		if ((unsigned)idx->pack->mwf.size < sizeof(struct git_pack_header))
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		if ((error = parse_header(&idx->hdr, idx->pack)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		idx->parsed_header = 1;
Packit Service 20376f
		idx->nr_objects = ntohl(hdr->hdr_entries);
Packit Service 20376f
		idx->off = sizeof(struct git_pack_header);
Packit Service 20376f
Packit Service 20376f
		/* for now, limit to 2^32 objects */
Packit Service 20376f
		assert(idx->nr_objects == (size_t)((unsigned int)idx->nr_objects));
Packit Service 20376f
		if (idx->nr_objects == (size_t)((unsigned int)idx->nr_objects))
Packit Service 20376f
			total_objects = (unsigned int)idx->nr_objects;
Packit Service 20376f
		else
Packit Service 20376f
			total_objects = UINT_MAX;
Packit Service 20376f
Packit Service 20376f
		idx->pack->idx_cache = git_oidmap_alloc();
Packit Service 20376f
		GITERR_CHECK_ALLOC(idx->pack->idx_cache);
Packit Service 20376f
Packit Service 20376f
		idx->pack->has_cache = 1;
Packit Service 20376f
		if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		if (git_vector_init(&idx->deltas, total_objects / 2, NULL) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		stats->received_objects = 0;
Packit Service 20376f
		stats->local_objects = 0;
Packit Service 20376f
		stats->total_deltas = 0;
Packit Service 20376f
		stats->indexed_deltas = 0;
Packit Service 20376f
		processed = stats->indexed_objects = 0;
Packit Service 20376f
		stats->total_objects = total_objects;
Packit Service 20376f
Packit Service 20376f
		if ((error = do_progress_callback(idx, stats)) != 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Now that we have data in the pack, let's try to parse it */
Packit Service 20376f
Packit Service 20376f
	/* As the file grows any windows we try to use will be out of date */
Packit Service 20376f
	git_mwindow_free_all(mwf);
Packit Service 20376f
Packit Service 20376f
	while (processed < idx->nr_objects) {
Packit Service 20376f
		git_packfile_stream *stream = &idx->stream;
Packit Service 20376f
		git_off_t entry_start = idx->off;
Packit Service 20376f
		size_t entry_size;
Packit Service 20376f
		git_otype type;
Packit Service 20376f
		git_mwindow *w = NULL;
Packit Service 20376f
Packit Service 20376f
		if (idx->pack->mwf.size <= idx->off + 20)
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		if (!idx->have_stream) {
Packit Service 20376f
			error = git_packfile_unpack_header(&entry_size, &type, mwf, &w, &idx->off);
Packit Service 20376f
			if (error == GIT_EBUFS) {
Packit Service 20376f
				idx->off = entry_start;
Packit Service 20376f
				return 0;
Packit Service 20376f
			}
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
Packit Service 20376f
			git_mwindow_close(&w);
Packit Service 20376f
			idx->entry_start = entry_start;
Packit Service 20376f
			git_hash_init(&idx->hash_ctx);
Packit Service 20376f
Packit Service 20376f
			if (type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA) {
Packit Service 20376f
				error = advance_delta_offset(idx, type);
Packit Service 20376f
				if (error == GIT_EBUFS) {
Packit Service 20376f
					idx->off = entry_start;
Packit Service 20376f
					return 0;
Packit Service 20376f
				}
Packit Service 20376f
				if (error < 0)
Packit Service 20376f
					goto on_error;
Packit Service 20376f
Packit Service 20376f
				idx->have_delta = 1;
Packit Service 20376f
			} else {
Packit Service 20376f
				idx->have_delta = 0;
Packit Service 20376f
				hash_header(&idx->hash_ctx, entry_size, type);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			idx->have_stream = 1;
Packit Service 20376f
Packit Service 20376f
			error = git_packfile_stream_open(stream, idx->pack, idx->off);
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (idx->have_delta) {
Packit Service 20376f
			error = read_object_stream(idx, stream);
Packit Service 20376f
		} else {
Packit Service 20376f
			error = hash_object_stream(idx, stream);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		idx->off = stream->curpos;
Packit Service 20376f
		if (error == GIT_EBUFS)
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		/* We want to free the stream reasorces no matter what here */
Packit Service 20376f
		idx->have_stream = 0;
Packit Service 20376f
		git_packfile_stream_free(stream);
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		if (idx->have_delta) {
Packit Service 20376f
			error = store_delta(idx);
Packit Service 20376f
		} else {
Packit Service 20376f
			error = store_object(idx);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		if (!idx->have_delta) {
Packit Service 20376f
			stats->indexed_objects = (unsigned int)++processed;
Packit Service 20376f
		}
Packit Service 20376f
		stats->received_objects++;
Packit Service 20376f
Packit Service 20376f
		if ((error = do_progress_callback(idx, stats)) != 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git_mwindow_free_all(mwf);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_path(git_buf *path, git_indexer *idx, const char *suffix)
Packit Service 20376f
{
Packit Service 20376f
	const char prefix[] = "pack-";
Packit Service 20376f
	size_t slash = (size_t)path->size;
Packit Service 20376f
Packit Service 20376f
	/* search backwards for '/' */
Packit Service 20376f
	while (slash > 0 && path->ptr[slash - 1] != '/')
Packit Service 20376f
		slash--;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_grow(path, slash + 1 + strlen(prefix) +
Packit Service 20376f
					 GIT_OID_HEXSZ + strlen(suffix) + 1) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_buf_truncate(path, slash);
Packit Service 20376f
	git_buf_puts(path, prefix);
Packit Service 20376f
	git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash);
Packit Service 20376f
	path->size += GIT_OID_HEXSZ;
Packit Service 20376f
	git_buf_puts(path, suffix);
Packit Service 20376f
Packit Service 20376f
	return git_buf_oom(path) ? -1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Rewind the packfile by the trailer, as we might need to fix the
Packit Service 20376f
 * packfile by injecting objects at the tail and must overwrite it.
Packit Service 20376f
 */
Packit Service 20376f
static void seek_back_trailer(git_indexer *idx)
Packit Service 20376f
{
Packit Service 20376f
	idx->pack->mwf.size -= GIT_OID_RAWSZ;
Packit Service 20376f
	git_mwindow_free_all(&idx->pack->mwf);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int inject_object(git_indexer *idx, git_oid *id)
Packit Service 20376f
{
Packit Service 20376f
	git_odb_object *obj;
Packit Service 20376f
	struct entry *entry;
Packit Service 20376f
	struct git_pack_entry *pentry = NULL;
Packit Service 20376f
	git_oid foo = {{0}};
Packit Service 20376f
	unsigned char hdr[64];
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	git_off_t entry_start;
Packit Service 20376f
	const void *data;
Packit Service 20376f
	size_t len, hdr_len;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	seek_back_trailer(idx);
Packit Service 20376f
	entry_start = idx->pack->mwf.size;
Packit Service 20376f
Packit Service 20376f
	if (git_odb_read(&obj, idx->odb, id) < 0) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "missing delta bases");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	data = git_odb_object_data(obj);
Packit Service 20376f
	len = git_odb_object_size(obj);
Packit Service 20376f
Packit Service 20376f
	entry = git__calloc(1, sizeof(*entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(entry);
Packit Service 20376f
Packit Service 20376f
	entry->crc = crc32(0L, Z_NULL, 0);
Packit Service 20376f
Packit Service 20376f
	/* Write out the object header */
Packit Service 20376f
	hdr_len = git_packfile__object_header(hdr, len, git_odb_object_type(obj));
Packit Service 20376f
	if ((error = append_to_pack(idx, hdr, hdr_len)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	idx->pack->mwf.size += hdr_len;
Packit Service 20376f
	entry->crc = crc32(entry->crc, hdr, (uInt)hdr_len);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_zstream_deflatebuf(&buf, data, len)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* And then the compressed object */
Packit Service 20376f
	if ((error = append_to_pack(idx, buf.ptr, buf.size)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	idx->pack->mwf.size += buf.size;
Packit Service 20376f
	entry->crc = htonl(crc32(entry->crc, (unsigned char *)buf.ptr, (uInt)buf.size));
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	/* Write a fake trailer so the pack functions play ball */
Packit Service 20376f
Packit Service 20376f
	if ((error = append_to_pack(idx, &foo, GIT_OID_RAWSZ)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	idx->pack->mwf.size += GIT_OID_RAWSZ;
Packit Service 20376f
Packit Service 20376f
	pentry = git__calloc(1, sizeof(struct git_pack_entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pentry);
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&pentry->sha1, id);
Packit Service 20376f
	git_oid_cpy(&entry->oid, id);
Packit Service 20376f
	idx->off = entry_start + hdr_len + len;
Packit Service 20376f
Packit Service 20376f
	error = save_entry(idx, entry, pentry, entry_start);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (error) {
Packit Service 20376f
		git__free(entry);
Packit Service 20376f
		git__free(pentry);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_odb_object_free(obj);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int fix_thin_pack(git_indexer *idx, git_transfer_progress *stats)
Packit Service 20376f
{
Packit Service 20376f
	int error, found_ref_delta = 0;
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	struct delta_info *delta;
Packit Service 20376f
	size_t size;
Packit Service 20376f
	git_otype type;
Packit Service 20376f
	git_mwindow *w = NULL;
Packit Service 20376f
	git_off_t curpos = 0;
Packit Service 20376f
	unsigned char *base_info;
Packit Service 20376f
	unsigned int left = 0;
Packit Service 20376f
	git_oid base;
Packit Service 20376f
Packit Service 20376f
	assert(git_vector_length(&idx->deltas) > 0);
Packit Service 20376f
Packit Service 20376f
	if (idx->odb == NULL) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "cannot fix a thin pack without an ODB");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Loop until we find the first REF delta */
Packit Service 20376f
	git_vector_foreach(&idx->deltas, i, delta) {
Packit Service 20376f
		if (!delta)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		curpos = delta->delta_off;
Packit Service 20376f
		error = git_packfile_unpack_header(&size, &type, &idx->pack->mwf, &w, &curpos);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (type == GIT_OBJ_REF_DELTA) {
Packit Service 20376f
			found_ref_delta = 1;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!found_ref_delta) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "no REF_DELTA found, cannot inject object");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* curpos now points to the base information, which is an OID */
Packit Service 20376f
	base_info = git_mwindow_open(&idx->pack->mwf, &w, curpos, GIT_OID_RAWSZ, &left);
Packit Service 20376f
	if (base_info == NULL) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "failed to map delta information");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_oid_fromraw(&base, base_info);
Packit Service 20376f
	git_mwindow_close(&w);
Packit Service 20376f
Packit Service 20376f
	if (has_entry(idx, &base))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (inject_object(idx, &base) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	stats->local_objects++;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int resolve_deltas(git_indexer *idx, git_transfer_progress *stats)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	int error;
Packit Service 20376f
	struct delta_info *delta;
Packit Service 20376f
	int progressed = 0, non_null = 0, progress_cb_result;
Packit Service 20376f
Packit Service 20376f
	while (idx->deltas.length > 0) {
Packit Service 20376f
		progressed = 0;
Packit Service 20376f
		non_null = 0;
Packit Service 20376f
		git_vector_foreach(&idx->deltas, i, delta) {
Packit Service 20376f
			git_rawobj obj = {NULL};
Packit Service 20376f
Packit Service 20376f
			if (!delta)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			non_null = 1;
Packit Service 20376f
			idx->off = delta->delta_off;
Packit Service 20376f
			if ((error = git_packfile_unpack(&obj, idx->pack, &idx->off)) < 0) {
Packit Service 20376f
				if (error == GIT_PASSTHROUGH) {
Packit Service 20376f
					/* We have not seen the base object, we'll try again later. */
Packit Service 20376f
					continue;
Packit Service 20376f
				}
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			if (hash_and_save(idx, &obj, delta->delta_off) < 0)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			git__free(obj.data);
Packit Service 20376f
			stats->indexed_objects++;
Packit Service 20376f
			stats->indexed_deltas++;
Packit Service 20376f
			progressed = 1;
Packit Service 20376f
			if ((progress_cb_result = do_progress_callback(idx, stats)) < 0)
Packit Service 20376f
				return progress_cb_result;
Packit Service 20376f
Packit Service 20376f
			/* remove from the list */
Packit Service 20376f
			git_vector_set(NULL, &idx->deltas, i, NULL);
Packit Service 20376f
			git__free(delta);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* if none were actually set, we're done */
Packit Service 20376f
		if (!non_null)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (!progressed && (fix_thin_pack(idx, stats) < 0)) {
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int update_header_and_rehash(git_indexer *idx, git_transfer_progress *stats)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr;
Packit Service 20376f
	size_t chunk = 1024*1024;
Packit Service 20376f
	git_off_t hashed = 0;
Packit Service 20376f
	git_mwindow *w = NULL;
Packit Service 20376f
	git_mwindow_file *mwf;
Packit Service 20376f
	unsigned int left;
Packit Service 20376f
Packit Service 20376f
	mwf = &idx->pack->mwf;
Packit Service 20376f
Packit Service 20376f
	git_hash_init(&idx->trailer);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	/* Update the header to include the numer of local objects we injected */
Packit Service 20376f
	idx->hdr.hdr_entries = htonl(stats->total_objects + stats->local_objects);
Packit Service 20376f
	if (write_at(idx, &idx->hdr, 0, sizeof(struct git_pack_header)) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * We now use the same technique as before to determine the
Packit Service 20376f
	 * hash. We keep reading up to the end and let
Packit Service 20376f
	 * hash_partially() keep the existing trailer out of the
Packit Service 20376f
	 * calculation.
Packit Service 20376f
	 */
Packit Service 20376f
	git_mwindow_free_all(mwf);
Packit Service 20376f
	idx->inbuf_len = 0;
Packit Service 20376f
	while (hashed < mwf->size) {
Packit Service 20376f
		ptr = git_mwindow_open(mwf, &w, hashed, chunk, &left);
Packit Service 20376f
		if (ptr == NULL)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		hash_partially(idx, ptr, left);
Packit Service 20376f
		hashed += left;
Packit Service 20376f
Packit Service 20376f
		git_mwindow_close(&w);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
Packit Service 20376f
{
Packit Service 20376f
	git_mwindow *w = NULL;
Packit Service 20376f
	unsigned int i, long_offsets = 0, left;
Packit Service 20376f
	int error;
Packit Service 20376f
	struct git_pack_idx_header hdr;
Packit Service 20376f
	git_buf filename = GIT_BUF_INIT;
Packit Service 20376f
	struct entry *entry;
Packit Service 20376f
	git_oid trailer_hash, file_hash;
Packit Service 20376f
	git_filebuf index_file = {0};
Packit Service 20376f
	void *packfile_trailer;
Packit Service 20376f
Packit Service 20376f
	if (!idx->parsed_header) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "incomplete pack header");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Test for this before resolve_deltas(), as it plays with idx->off */
Packit Service 20376f
	if (idx->off + 20 < idx->pack->mwf.size) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "unexpected data at the end of the pack");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
	if (idx->off + 20 > idx->pack->mwf.size) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "missing trailer at the end of the pack");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	packfile_trailer = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left);
Packit Service 20376f
	if (packfile_trailer == NULL) {
Packit Service 20376f
		git_mwindow_close(&w);
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Compare the packfile trailer as it was sent to us and what we calculated */
Packit Service 20376f
	git_oid_fromraw(&file_hash, packfile_trailer);
Packit Service 20376f
	git_mwindow_close(&w);
Packit Service 20376f
Packit Service 20376f
	git_hash_final(&trailer_hash, &idx->trailer);
Packit Service 20376f
	if (git_oid_cmp(&file_hash, &trailer_hash)) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "packfile trailer mismatch");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Freeze the number of deltas */
Packit Service 20376f
	stats->total_deltas = stats->total_objects - stats->indexed_objects;
Packit Service 20376f
Packit Service 20376f
	if ((error = resolve_deltas(idx, stats)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (stats->indexed_objects != stats->total_objects) {
Packit Service 20376f
		giterr_set(GITERR_INDEXER, "early EOF");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (stats->local_objects > 0) {
Packit Service 20376f
		if (update_header_and_rehash(idx, stats) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		git_hash_final(&trailer_hash, &idx->trailer);
Packit Service 20376f
		write_at(idx, &trailer_hash, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_sort(&idx->objects);
Packit Service 20376f
Packit Service 20376f
	/* Use the trailer hash as the pack file name to ensure
Packit Service 20376f
	 * files with different contents have different names */
Packit Service 20376f
	git_oid_cpy(&idx->hash, &trailer_hash);
Packit Service 20376f
Packit Service 20376f
	git_buf_sets(&filename, idx->pack->pack_name);
Packit Service 20376f
	git_buf_shorten(&filename, strlen("pack"));
Packit Service 20376f
	git_buf_puts(&filename, "idx");
Packit Service 20376f
	if (git_buf_oom(&filename))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_filebuf_open(&index_file, filename.ptr,
Packit Service 20376f
		GIT_FILEBUF_HASH_CONTENTS |
Packit Service 20376f
		(idx->do_fsync ? GIT_FILEBUF_FSYNC : 0),
Packit Service 20376f
		idx->mode) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* Write out the header */
Packit Service 20376f
	hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
Packit Service 20376f
	hdr.idx_version = htonl(2);
Packit Service 20376f
	git_filebuf_write(&index_file, &hdr, sizeof(hdr));
Packit Service 20376f
Packit Service 20376f
	/* Write out the fanout table */
Packit Service 20376f
	for (i = 0; i < 256; ++i) {
Packit Service 20376f
		uint32_t n = htonl(idx->fanout[i]);
Packit Service 20376f
		git_filebuf_write(&index_file, &n, sizeof(n));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Write out the object names (SHA-1 hashes) */
Packit Service 20376f
	git_vector_foreach(&idx->objects, i, entry) {
Packit Service 20376f
		git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Write out the CRC32 values */
Packit Service 20376f
	git_vector_foreach(&idx->objects, i, entry) {
Packit Service 20376f
		git_filebuf_write(&index_file, &entry->crc, sizeof(uint32_t));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Write out the offsets */
Packit Service 20376f
	git_vector_foreach(&idx->objects, i, entry) {
Packit Service 20376f
		uint32_t n;
Packit Service 20376f
Packit Service 20376f
		if (entry->offset == UINT32_MAX)
Packit Service 20376f
			n = htonl(0x80000000 | long_offsets++);
Packit Service 20376f
		else
Packit Service 20376f
			n = htonl(entry->offset);
Packit Service 20376f
Packit Service 20376f
		git_filebuf_write(&index_file, &n, sizeof(uint32_t));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Write out the long offsets */
Packit Service 20376f
	git_vector_foreach(&idx->objects, i, entry) {
Packit Service 20376f
		uint32_t split[2];
Packit Service 20376f
Packit Service 20376f
		if (entry->offset != UINT32_MAX)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		split[0] = htonl(entry->offset_long >> 32);
Packit Service 20376f
		split[1] = htonl(entry->offset_long & 0xffffffff);
Packit Service 20376f
Packit Service 20376f
		git_filebuf_write(&index_file, &split, sizeof(uint32_t) * 2);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Write out the packfile trailer to the index */
Packit Service 20376f
	if (git_filebuf_write(&index_file, &trailer_hash, GIT_OID_RAWSZ) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* Write out the hash of the idx */
Packit Service 20376f
	if (git_filebuf_hash(&trailer_hash, &index_file) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	git_filebuf_write(&index_file, &trailer_hash, sizeof(git_oid));
Packit Service 20376f
Packit Service 20376f
	/* Figure out what the final name should be */
Packit Service 20376f
	if (index_path(&filename, idx, ".idx") < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* Commit file */
Packit Service 20376f
	if (git_filebuf_commit_at(&index_file, filename.ptr) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	git_mwindow_free_all(&idx->pack->mwf);
Packit Service 20376f
Packit Service 20376f
	/* Truncate file to undo rounding up to next page_size in append_to_pack */
Packit Service 20376f
	if (p_ftruncate(idx->pack->mwf.fd, idx->pack->mwf.size) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to truncate pack file '%s'", idx->pack->pack_name);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (idx->do_fsync && p_fsync(idx->pack->mwf.fd) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to fsync packfile");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* We need to close the descriptor here so Windows doesn't choke on commit_at */
Packit Service 20376f
	if (p_close(idx->pack->mwf.fd) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to close packfile");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	idx->pack->mwf.fd = -1;
Packit Service 20376f
Packit Service 20376f
	if (index_path(&filename, idx, ".pack") < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* And don't forget to rename the packfile to its new place. */
Packit Service 20376f
	if (p_rename(idx->pack->pack_name, git_buf_cstr(&filename)) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* And fsync the parent directory if we're asked to. */
Packit Service 20376f
	if (idx->do_fsync &&
Packit Service 20376f
		git_futils_fsync_parent(git_buf_cstr(&filename)) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	idx->pack_committed = 1;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&filename);
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git_mwindow_free_all(&idx->pack->mwf);
Packit Service 20376f
	git_filebuf_cleanup(&index_file);
Packit Service 20376f
	git_buf_free(&filename);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_indexer_free(git_indexer *idx)
Packit Service 20376f
{
Packit Service 20376f
	if (idx == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	if (idx->have_stream)
Packit Service 20376f
		git_packfile_stream_free(&idx->stream);
Packit Service 20376f
Packit Service 20376f
	git_vector_free_deep(&idx->objects);
Packit Service 20376f
Packit Service 20376f
	if (idx->pack->idx_cache) {
Packit Service 20376f
		struct git_pack_entry *pentry;
Packit Service 20376f
		git_oidmap_foreach_value(idx->pack->idx_cache, pentry, {
Packit Service 20376f
			git__free(pentry);
Packit Service 20376f
		});
Packit Service 20376f
Packit Service 20376f
		git_oidmap_free(idx->pack->idx_cache);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_free_deep(&idx->deltas);
Packit Service 20376f
Packit Service 20376f
	if (!git_mutex_lock(&git__mwindow_mutex)) {
Packit Service 20376f
		if (!idx->pack_committed)
Packit Service 20376f
			git_packfile_close(idx->pack, true);
Packit Service 20376f
Packit Service 20376f
		git_packfile_free(idx->pack);
Packit Service 20376f
		git_mutex_unlock(&git__mwindow_mutex);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_hash_ctx_cleanup(&idx->trailer);
Packit Service 20376f
	git_hash_ctx_cleanup(&idx->hash_ctx);
Packit Service 20376f
	git__free(idx);
Packit Service 20376f
}