4 * Copyright (C) 2005 Linus Torvalds
6 * Simple file write infrastructure for writing SHA1-summed
7 * files. Useful when you write a file that you want to be
8 * able to verify hasn't been messed with afterwards.
12 #include "csum-file.h"
14 static void flush(struct hashfile *f, const void *buf, unsigned int count)
16 if (0 <= f->check_fd && count) {
17 unsigned char check_buffer[8192];
18 ssize_t ret = read_in_full(f->check_fd, check_buffer, count);
21 die_errno("%s: sha1 file read error", f->name);
23 die("%s: sha1 file truncated", f->name);
24 if (memcmp(buf, check_buffer, count))
25 die("sha1 file '%s' validation error", f->name);
29 int ret = xwrite(f->fd, buf, count);
32 display_throughput(f->tp, f->total);
33 buf = (char *) buf + ret;
40 die("sha1 file '%s' write error. Out of diskspace", f->name);
41 die_errno("sha1 file '%s' write error", f->name);
45 void hashflush(struct hashfile *f)
47 unsigned offset = f->offset;
50 the_hash_algo->update_fn(&f->ctx, f->buffer, offset);
51 flush(f, f->buffer, offset);
56 int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags)
61 the_hash_algo->final_fn(f->buffer, &f->ctx);
63 hashcpy(result, f->buffer);
64 if (flags & CSUM_HASH_IN_STREAM)
65 flush(f, f->buffer, the_hash_algo->rawsz);
66 if (flags & CSUM_FSYNC)
67 fsync_or_die(f->fd, f->name);
68 if (flags & CSUM_CLOSE) {
70 die_errno("%s: sha1 file error on close", f->name);
74 if (0 <= f->check_fd) {
76 int cnt = read_in_full(f->check_fd, &discard, 1);
78 die_errno("%s: error when reading the tail of sha1 file",
81 die("%s: sha1 file has trailing garbage", f->name);
82 if (close(f->check_fd))
83 die_errno("%s: sha1 file error on close", f->name);
89 void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
92 unsigned offset = f->offset;
93 unsigned left = sizeof(f->buffer) - offset;
94 unsigned nr = count > left ? left : count;
98 f->crc32 = crc32(f->crc32, buf, nr);
100 if (nr == sizeof(f->buffer)) {
101 /* process full buffer directly without copy */
104 memcpy(f->buffer + offset, buf, nr);
110 buf = (char *) buf + nr;
113 the_hash_algo->update_fn(&f->ctx, data, offset);
114 flush(f, data, offset);
121 struct hashfile *hashfd(int fd, const char *name)
123 return hashfd_throughput(fd, name, NULL);
126 struct hashfile *hashfd_check(const char *name)
131 sink = open("/dev/null", O_WRONLY);
133 die_errno("unable to open /dev/null");
134 check = open(name, O_RDONLY);
136 die_errno("unable to open '%s'", name);
137 f = hashfd(sink, name);
142 struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
144 struct hashfile *f = xmalloc(sizeof(*f));
152 the_hash_algo->init_fn(&f->ctx);
156 void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
159 checkpoint->offset = f->total;
160 the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx);
163 int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
165 off_t offset = checkpoint->offset;
167 if (ftruncate(f->fd, offset) ||
168 lseek(f->fd, offset, SEEK_SET) != offset)
171 f->ctx = checkpoint->ctx;
172 f->offset = 0; /* hashflush() was called in checkpoint */
176 void crc32_begin(struct hashfile *f)
178 f->crc32 = crc32(0, NULL, 0);
182 uint32_t crc32_end(struct hashfile *f)