git
/
csum-file.c
253 строки · 5.8 Кб
1/*
2* csum-file.c
3*
4* Copyright (C) 2005 Linus Torvalds
5*
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.
9*/
10
11#define USE_THE_REPOSITORY_VARIABLE12
13#include "git-compat-util.h"14#include "progress.h"15#include "csum-file.h"16#include "hash.h"17
18static void verify_buffer_or_die(struct hashfile *f,19const void *buf,20unsigned int count)21{
22ssize_t ret = read_in_full(f->check_fd, f->check_buffer, count);23
24if (ret < 0)25die_errno("%s: sha1 file read error", f->name);26if (ret != count)27die("%s: sha1 file truncated", f->name);28if (memcmp(buf, f->check_buffer, count))29die("sha1 file '%s' validation error", f->name);30}
31
32static void flush(struct hashfile *f, const void *buf, unsigned int count)33{
34if (0 <= f->check_fd && count)35verify_buffer_or_die(f, buf, count);36
37if (write_in_full(f->fd, buf, count) < 0) {38if (errno == ENOSPC)39die("sha1 file '%s' write error. Out of diskspace", f->name);40die_errno("sha1 file '%s' write error", f->name);41}42
43f->total += count;44display_throughput(f->tp, f->total);45}
46
47void hashflush(struct hashfile *f)48{
49unsigned offset = f->offset;50
51if (offset) {52if (!f->skip_hash)53the_hash_algo->update_fn(&f->ctx, f->buffer, offset);54flush(f, f->buffer, offset);55f->offset = 0;56}57}
58
59void free_hashfile(struct hashfile *f)60{
61free(f->buffer);62free(f->check_buffer);63free(f);64}
65
66int finalize_hashfile(struct hashfile *f, unsigned char *result,67enum fsync_component component, unsigned int flags)68{
69int fd;70
71hashflush(f);72
73if (f->skip_hash)74hashclr(f->buffer, the_repository->hash_algo);75else76the_hash_algo->final_fn(f->buffer, &f->ctx);77
78if (result)79hashcpy(result, f->buffer, the_repository->hash_algo);80if (flags & CSUM_HASH_IN_STREAM)81flush(f, f->buffer, the_hash_algo->rawsz);82if (flags & CSUM_FSYNC)83fsync_component_or_die(component, f->fd, f->name);84if (flags & CSUM_CLOSE) {85if (close(f->fd))86die_errno("%s: sha1 file error on close", f->name);87fd = 0;88} else89fd = f->fd;90if (0 <= f->check_fd) {91char discard;92int cnt = read_in_full(f->check_fd, &discard, 1);93if (cnt < 0)94die_errno("%s: error when reading the tail of sha1 file",95f->name);96if (cnt)97die("%s: sha1 file has trailing garbage", f->name);98if (close(f->check_fd))99die_errno("%s: sha1 file error on close", f->name);100}101free_hashfile(f);102return fd;103}
104
105void discard_hashfile(struct hashfile *f)106{
107if (0 <= f->check_fd)108close(f->check_fd);109if (0 <= f->fd)110close(f->fd);111free_hashfile(f);112}
113
114void hashwrite(struct hashfile *f, const void *buf, unsigned int count)115{
116while (count) {117unsigned left = f->buffer_len - f->offset;118unsigned nr = count > left ? left : count;119
120if (f->do_crc)121f->crc32 = crc32(f->crc32, buf, nr);122
123if (nr == f->buffer_len) {124/*125* Flush a full batch worth of data directly
126* from the input, skipping the memcpy() to
127* the hashfile's buffer. In this block,
128* f->offset is necessarily zero.
129*/
130if (!f->skip_hash)131the_hash_algo->update_fn(&f->ctx, buf, nr);132flush(f, buf, nr);133} else {134/*135* Copy to the hashfile's buffer, flushing only
136* if it became full.
137*/
138memcpy(f->buffer + f->offset, buf, nr);139f->offset += nr;140left -= nr;141if (!left)142hashflush(f);143}144
145count -= nr;146buf = (char *) buf + nr;147}148}
149
150struct hashfile *hashfd_check(const char *name)151{
152int sink, check;153struct hashfile *f;154
155sink = xopen("/dev/null", O_WRONLY);156check = xopen(name, O_RDONLY);157f = hashfd(sink, name);158f->check_fd = check;159f->check_buffer = xmalloc(f->buffer_len);160
161return f;162}
163
164static struct hashfile *hashfd_internal(int fd, const char *name,165struct progress *tp,166size_t buffer_len)167{
168struct hashfile *f = xmalloc(sizeof(*f));169f->fd = fd;170f->check_fd = -1;171f->offset = 0;172f->total = 0;173f->tp = tp;174f->name = name;175f->do_crc = 0;176f->skip_hash = 0;177the_hash_algo->init_fn(&f->ctx);178
179f->buffer_len = buffer_len;180f->buffer = xmalloc(buffer_len);181f->check_buffer = NULL;182
183return f;184}
185
186struct hashfile *hashfd(int fd, const char *name)187{
188/*189* Since we are not going to use a progress meter to
190* measure the rate of data passing through this hashfile,
191* use a larger buffer size to reduce fsync() calls.
192*/
193return hashfd_internal(fd, name, NULL, 128 * 1024);194}
195
196struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)197{
198/*199* Since we are expecting to report progress of the
200* write into this hashfile, use a smaller buffer
201* size so the progress indicators arrive at a more
202* frequent rate.
203*/
204return hashfd_internal(fd, name, tp, 8 * 1024);205}
206
207void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)208{
209hashflush(f);210checkpoint->offset = f->total;211the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx);212}
213
214int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)215{
216off_t offset = checkpoint->offset;217
218if (ftruncate(f->fd, offset) ||219lseek(f->fd, offset, SEEK_SET) != offset)220return -1;221f->total = offset;222the_hash_algo->clone_fn(&f->ctx, &checkpoint->ctx);223f->offset = 0; /* hashflush() was called in checkpoint */224return 0;225}
226
227void crc32_begin(struct hashfile *f)228{
229f->crc32 = crc32(0, NULL, 0);230f->do_crc = 1;231}
232
233uint32_t crc32_end(struct hashfile *f)234{
235f->do_crc = 0;236return f->crc32;237}
238
239int hashfile_checksum_valid(const unsigned char *data, size_t total_len)240{
241unsigned char got[GIT_MAX_RAWSZ];242git_hash_ctx ctx;243size_t data_len = total_len - the_hash_algo->rawsz;244
245if (total_len < the_hash_algo->rawsz)246return 0; /* say "too short"? */247
248the_hash_algo->init_fn(&ctx);249the_hash_algo->update_fn(&ctx, data, data_len);250the_hash_algo->final_fn(got, &ctx);251
252return hasheq(got, data + data_len, the_repository->hash_algo);253}
254