csum-file.c 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * csum-file.c
 *
 * Copyright (C) 2005 Linus Torvalds
 *
 * Simple file write infrastructure for writing SHA1-summed
 * files. Useful when you write a file that you want to be
 * able to verify hasn't been messed with afterwards.
 */
#include "cache.h"
11
#include "progress.h"
12 13
#include "csum-file.h"

14
static void flush(struct sha1file *f, const void *buf, unsigned int count)
15
{
Junio C Hamano's avatar
Junio C Hamano committed
16 17 18 19 20 21 22 23 24 25 26 27
	if (0 <= f->check_fd && count)  {
		unsigned char check_buffer[8192];
		ssize_t ret = read_in_full(f->check_fd, check_buffer, count);

		if (ret < 0)
			die_errno("%s: sha1 file read error", f->name);
		if (ret < count)
			die("%s: sha1 file truncated", f->name);
		if (memcmp(buf, check_buffer, count))
			die("sha1 file '%s' validation error", f->name);
	}

28
	for (;;) {
29
		int ret = xwrite(f->fd, buf, count);
30
		if (ret > 0) {
31 32
			f->total += ret;
			display_throughput(f->tp, f->total);
33
			buf = (char *) buf + ret;
34 35 36
			count -= ret;
			if (count)
				continue;
37
			return;
38 39
		}
		if (!ret)
40
			die("sha1 file '%s' write error. Out of diskspace", f->name);
41
		die_errno("sha1 file '%s' write error", f->name);
42 43 44
	}
}

45
void sha1flush(struct sha1file *f)
46 47
{
	unsigned offset = f->offset;
48

49
	if (offset) {
50
		git_SHA1_Update(&f->ctx, f->buffer, offset);
Shawn O. Pearce's avatar
Shawn O. Pearce committed
51
		flush(f, f->buffer, offset);
52
		f->offset = 0;
53
	}
54 55 56 57 58 59 60
}

int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
{
	int fd;

	sha1flush(f);
61
	git_SHA1_Final(f->buffer, &f->ctx);
62 63
	if (result)
		hashcpy(result, f->buffer);
64
	if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
65
		/* write checksum and close fd */
Shawn O. Pearce's avatar
Shawn O. Pearce committed
66
		flush(f, f->buffer, 20);
67 68
		if (flags & CSUM_FSYNC)
			fsync_or_die(f->fd, f->name);
69
		if (close(f->fd))
70
			die_errno("%s: sha1 file error on close", f->name);
71 72 73
		fd = 0;
	} else
		fd = f->fd;
Junio C Hamano's avatar
Junio C Hamano committed
74 75 76 77 78 79 80 81 82 83 84
	if (0 <= f->check_fd) {
		char discard;
		int cnt = read_in_full(f->check_fd, &discard, 1);
		if (cnt < 0)
			die_errno("%s: error when reading the tail of sha1 file",
				  f->name);
		if (cnt)
			die("%s: sha1 file has trailing garbage", f->name);
		if (close(f->check_fd))
			die_errno("%s: sha1 file error on close", f->name);
	}
85
	free(f);
86
	return fd;
87 88
}

89
void sha1write(struct sha1file *f, const void *buf, unsigned int count)
90 91 92 93 94
{
	while (count) {
		unsigned offset = f->offset;
		unsigned left = sizeof(f->buffer) - offset;
		unsigned nr = count > left ? left : count;
95
		const void *data;
96 97 98 99 100 101 102 103 104 105 106

		if (f->do_crc)
			f->crc32 = crc32(f->crc32, buf, nr);

		if (nr == sizeof(f->buffer)) {
			/* process full buffer directly without copy */
			data = buf;
		} else {
			memcpy(f->buffer + offset, buf, nr);
			data = f->buffer;
		}
107 108 109

		count -= nr;
		offset += nr;
110
		buf = (char *) buf + nr;
111 112
		left -= nr;
		if (!left) {
113
			git_SHA1_Update(&f->ctx, data, offset);
Shawn O. Pearce's avatar
Shawn O. Pearce committed
114
			flush(f, data, offset);
115 116 117 118 119 120
			offset = 0;
		}
		f->offset = offset;
	}
}

121
struct sha1file *sha1fd(int fd, const char *name)
122 123 124 125
{
	return sha1fd_throughput(fd, name, NULL);
}

Junio C Hamano's avatar
Junio C Hamano committed
126 127 128 129 130 131 132
struct sha1file *sha1fd_check(const char *name)
{
	int sink, check;
	struct sha1file *f;

	sink = open("/dev/null", O_WRONLY);
	if (sink < 0)
133
		die_errno("unable to open /dev/null");
Junio C Hamano's avatar
Junio C Hamano committed
134
	check = open(name, O_RDONLY);
135 136
	if (check < 0)
		die_errno("unable to open '%s'", name);
Junio C Hamano's avatar
Junio C Hamano committed
137 138 139 140 141
	f = sha1fd(sink, name);
	f->check_fd = check;
	return f;
}

142
struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
143
{
144
	struct sha1file *f = xmalloc(sizeof(*f));
145
	f->fd = fd;
Junio C Hamano's avatar
Junio C Hamano committed
146
	f->check_fd = -1;
147
	f->offset = 0;
148
	f->total = 0;
149
	f->tp = tp;
150
	f->name = name;
151
	f->do_crc = 0;
152
	git_SHA1_Init(&f->ctx);
153 154 155
	return f;
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
void sha1file_checkpoint(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
{
	sha1flush(f);
	checkpoint->offset = f->total;
	checkpoint->ctx = f->ctx;
}

int sha1file_truncate(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
{
	off_t offset = checkpoint->offset;

	if (ftruncate(f->fd, offset) ||
	    lseek(f->fd, offset, SEEK_SET) != offset)
		return -1;
	f->total = offset;
	f->ctx = checkpoint->ctx;
	f->offset = 0; /* sha1flush() was called in checkpoint */
	return 0;
}

176 177
void crc32_begin(struct sha1file *f)
{
178
	f->crc32 = crc32(0, NULL, 0);
179 180
	f->do_crc = 1;
}
181

182 183 184 185 186
uint32_t crc32_end(struct sha1file *f)
{
	f->do_crc = 0;
	return f->crc32;
}