Blame csum-file.c

Packit 4511e4
/*
Packit 4511e4
 * csum-file.c
Packit 4511e4
 *
Packit 4511e4
 * Copyright (C) 2005 Linus Torvalds
Packit 4511e4
 *
Packit 4511e4
 * Simple file write infrastructure for writing SHA1-summed
Packit 4511e4
 * files. Useful when you write a file that you want to be
Packit 4511e4
 * able to verify hasn't been messed with afterwards.
Packit 4511e4
 */
Packit 4511e4
#include "cache.h"
Packit 4511e4
#include "progress.h"
Packit 4511e4
#include "csum-file.h"
Packit 4511e4
Packit 4511e4
static void flush(struct hashfile *f, const void *buf, unsigned int count)
Packit 4511e4
{
Packit 4511e4
	if (0 <= f->check_fd && count)  {
Packit 4511e4
		unsigned char check_buffer[8192];
Packit 4511e4
		ssize_t ret = read_in_full(f->check_fd, check_buffer, count);
Packit 4511e4
Packit 4511e4
		if (ret < 0)
Packit 4511e4
			die_errno("%s: sha1 file read error", f->name);
Packit 4511e4
		if (ret != count)
Packit 4511e4
			die("%s: sha1 file truncated", f->name);
Packit 4511e4
		if (memcmp(buf, check_buffer, count))
Packit 4511e4
			die("sha1 file '%s' validation error", f->name);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	for (;;) {
Packit 4511e4
		int ret = xwrite(f->fd, buf, count);
Packit 4511e4
		if (ret > 0) {
Packit 4511e4
			f->total += ret;
Packit 4511e4
			display_throughput(f->tp, f->total);
Packit 4511e4
			buf = (char *) buf + ret;
Packit 4511e4
			count -= ret;
Packit 4511e4
			if (count)
Packit 4511e4
				continue;
Packit 4511e4
			return;
Packit 4511e4
		}
Packit 4511e4
		if (!ret)
Packit 4511e4
			die("sha1 file '%s' write error. Out of diskspace", f->name);
Packit 4511e4
		die_errno("sha1 file '%s' write error", f->name);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void hashflush(struct hashfile *f)
Packit 4511e4
{
Packit 4511e4
	unsigned offset = f->offset;
Packit 4511e4
Packit 4511e4
	if (offset) {
Packit 4511e4
		the_hash_algo->update_fn(&f->ctx, f->buffer, offset);
Packit 4511e4
		flush(f, f->buffer, offset);
Packit 4511e4
		f->offset = 0;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags)
Packit 4511e4
{
Packit 4511e4
	int fd;
Packit 4511e4
Packit 4511e4
	hashflush(f);
Packit 4511e4
	the_hash_algo->final_fn(f->buffer, &f->ctx);
Packit 4511e4
	if (result)
Packit 4511e4
		hashcpy(result, f->buffer);
Packit 4511e4
	if (flags & CSUM_HASH_IN_STREAM)
Packit 4511e4
		flush(f, f->buffer, the_hash_algo->rawsz);
Packit 4511e4
	if (flags & CSUM_FSYNC)
Packit 4511e4
		fsync_or_die(f->fd, f->name);
Packit 4511e4
	if (flags & CSUM_CLOSE) {
Packit 4511e4
		if (close(f->fd))
Packit 4511e4
			die_errno("%s: sha1 file error on close", f->name);
Packit 4511e4
		fd = 0;
Packit 4511e4
	} else
Packit 4511e4
		fd = f->fd;
Packit 4511e4
	if (0 <= f->check_fd) {
Packit 4511e4
		char discard;
Packit 4511e4
		int cnt = read_in_full(f->check_fd, &discard, 1);
Packit 4511e4
		if (cnt < 0)
Packit 4511e4
			die_errno("%s: error when reading the tail of sha1 file",
Packit 4511e4
				  f->name);
Packit 4511e4
		if (cnt)
Packit 4511e4
			die("%s: sha1 file has trailing garbage", f->name);
Packit 4511e4
		if (close(f->check_fd))
Packit 4511e4
			die_errno("%s: sha1 file error on close", f->name);
Packit 4511e4
	}
Packit 4511e4
	free(f);
Packit 4511e4
	return fd;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
Packit 4511e4
{
Packit 4511e4
	while (count) {
Packit 4511e4
		unsigned offset = f->offset;
Packit 4511e4
		unsigned left = sizeof(f->buffer) - offset;
Packit 4511e4
		unsigned nr = count > left ? left : count;
Packit 4511e4
		const void *data;
Packit 4511e4
Packit 4511e4
		if (f->do_crc)
Packit 4511e4
			f->crc32 = crc32(f->crc32, buf, nr);
Packit 4511e4
Packit 4511e4
		if (nr == sizeof(f->buffer)) {
Packit 4511e4
			/* process full buffer directly without copy */
Packit 4511e4
			data = buf;
Packit 4511e4
		} else {
Packit 4511e4
			memcpy(f->buffer + offset, buf, nr);
Packit 4511e4
			data = f->buffer;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		count -= nr;
Packit 4511e4
		offset += nr;
Packit 4511e4
		buf = (char *) buf + nr;
Packit 4511e4
		left -= nr;
Packit 4511e4
		if (!left) {
Packit 4511e4
			the_hash_algo->update_fn(&f->ctx, data, offset);
Packit 4511e4
			flush(f, data, offset);
Packit 4511e4
			offset = 0;
Packit 4511e4
		}
Packit 4511e4
		f->offset = offset;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
struct hashfile *hashfd(int fd, const char *name)
Packit 4511e4
{
Packit 4511e4
	return hashfd_throughput(fd, name, NULL);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
struct hashfile *hashfd_check(const char *name)
Packit 4511e4
{
Packit 4511e4
	int sink, check;
Packit 4511e4
	struct hashfile *f;
Packit 4511e4
Packit 4511e4
	sink = open("/dev/null", O_WRONLY);
Packit 4511e4
	if (sink < 0)
Packit 4511e4
		die_errno("unable to open /dev/null");
Packit 4511e4
	check = open(name, O_RDONLY);
Packit 4511e4
	if (check < 0)
Packit 4511e4
		die_errno("unable to open '%s'", name);
Packit 4511e4
	f = hashfd(sink, name);
Packit 4511e4
	f->check_fd = check;
Packit 4511e4
	return f;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
Packit 4511e4
{
Packit 4511e4
	struct hashfile *f = xmalloc(sizeof(*f));
Packit 4511e4
	f->fd = fd;
Packit 4511e4
	f->check_fd = -1;
Packit 4511e4
	f->offset = 0;
Packit 4511e4
	f->total = 0;
Packit 4511e4
	f->tp = tp;
Packit 4511e4
	f->name = name;
Packit 4511e4
	f->do_crc = 0;
Packit 4511e4
	the_hash_algo->init_fn(&f->ctx);
Packit 4511e4
	return f;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Packit 4511e4
{
Packit 4511e4
	hashflush(f);
Packit 4511e4
	checkpoint->offset = f->total;
Packit 4511e4
	checkpoint->ctx = f->ctx;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Packit 4511e4
{
Packit 4511e4
	off_t offset = checkpoint->offset;
Packit 4511e4
Packit 4511e4
	if (ftruncate(f->fd, offset) ||
Packit 4511e4
	    lseek(f->fd, offset, SEEK_SET) != offset)
Packit 4511e4
		return -1;
Packit 4511e4
	f->total = offset;
Packit 4511e4
	f->ctx = checkpoint->ctx;
Packit 4511e4
	f->offset = 0; /* hashflush() was called in checkpoint */
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void crc32_begin(struct hashfile *f)
Packit 4511e4
{
Packit 4511e4
	f->crc32 = crc32(0, NULL, 0);
Packit 4511e4
	f->do_crc = 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
uint32_t crc32_end(struct hashfile *f)
Packit 4511e4
{
Packit 4511e4
	f->do_crc = 0;
Packit 4511e4
	return f->crc32;
Packit 4511e4
}