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.
10 #include "git-compat-util.h"
12 #include "csum-file.h"
16 static void verify_buffer_or_die(struct hashfile
*f
,
20 ssize_t ret
= read_in_full(f
->check_fd
, f
->check_buffer
, count
);
23 die_errno("%s: sha1 file read error", f
->name
);
25 die("%s: sha1 file truncated", f
->name
);
26 if (memcmp(buf
, f
->check_buffer
, count
))
27 die("sha1 file '%s' validation error", f
->name
);
30 static void flush(struct hashfile
*f
, const void *buf
, unsigned int count
)
32 if (0 <= f
->check_fd
&& count
)
33 verify_buffer_or_die(f
, buf
, count
);
35 if (write_in_full(f
->fd
, buf
, count
) < 0) {
37 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
38 die_errno("sha1 file '%s' write error", f
->name
);
42 display_throughput(f
->tp
, f
->total
);
45 void hashflush(struct hashfile
*f
)
47 unsigned offset
= f
->offset
;
51 the_hash_algo
->update_fn(&f
->ctx
, f
->buffer
, offset
);
52 flush(f
, f
->buffer
, offset
);
57 static void free_hashfile(struct hashfile
*f
)
60 free(f
->check_buffer
);
64 int finalize_hashfile(struct hashfile
*f
, unsigned char *result
,
65 enum fsync_component component
, unsigned int flags
)
74 the_hash_algo
->final_fn(f
->buffer
, &f
->ctx
);
77 hashcpy(result
, f
->buffer
);
78 if (flags
& CSUM_HASH_IN_STREAM
)
79 flush(f
, f
->buffer
, the_hash_algo
->rawsz
);
80 if (flags
& CSUM_FSYNC
)
81 fsync_component_or_die(component
, f
->fd
, f
->name
);
82 if (flags
& CSUM_CLOSE
) {
84 die_errno("%s: sha1 file error on close", f
->name
);
88 if (0 <= f
->check_fd
) {
90 int cnt
= read_in_full(f
->check_fd
, &discard
, 1);
92 die_errno("%s: error when reading the tail of sha1 file",
95 die("%s: sha1 file has trailing garbage", f
->name
);
96 if (close(f
->check_fd
))
97 die_errno("%s: sha1 file error on close", f
->name
);
103 void hashwrite(struct hashfile
*f
, const void *buf
, unsigned int count
)
106 unsigned left
= f
->buffer_len
- f
->offset
;
107 unsigned nr
= count
> left
? left
: count
;
110 f
->crc32
= crc32(f
->crc32
, buf
, nr
);
112 if (nr
== f
->buffer_len
) {
114 * Flush a full batch worth of data directly
115 * from the input, skipping the memcpy() to
116 * the hashfile's buffer. In this block,
117 * f->offset is necessarily zero.
120 the_hash_algo
->update_fn(&f
->ctx
, buf
, nr
);
124 * Copy to the hashfile's buffer, flushing only
127 memcpy(f
->buffer
+ f
->offset
, buf
, nr
);
135 buf
= (char *) buf
+ nr
;
139 struct hashfile
*hashfd_check(const char *name
)
144 sink
= xopen("/dev/null", O_WRONLY
);
145 check
= xopen(name
, O_RDONLY
);
146 f
= hashfd(sink
, name
);
148 f
->check_buffer
= xmalloc(f
->buffer_len
);
153 static struct hashfile
*hashfd_internal(int fd
, const char *name
,
157 struct hashfile
*f
= xmalloc(sizeof(*f
));
166 the_hash_algo
->init_fn(&f
->ctx
);
168 f
->buffer_len
= buffer_len
;
169 f
->buffer
= xmalloc(buffer_len
);
170 f
->check_buffer
= NULL
;
175 struct hashfile
*hashfd(int fd
, const char *name
)
178 * Since we are not going to use a progress meter to
179 * measure the rate of data passing through this hashfile,
180 * use a larger buffer size to reduce fsync() calls.
182 return hashfd_internal(fd
, name
, NULL
, 128 * 1024);
185 struct hashfile
*hashfd_throughput(int fd
, const char *name
, struct progress
*tp
)
188 * Since we are expecting to report progress of the
189 * write into this hashfile, use a smaller buffer
190 * size so the progress indicators arrive at a more
193 return hashfd_internal(fd
, name
, tp
, 8 * 1024);
196 void hashfile_checkpoint(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
199 checkpoint
->offset
= f
->total
;
200 the_hash_algo
->clone_fn(&checkpoint
->ctx
, &f
->ctx
);
203 int hashfile_truncate(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
205 off_t offset
= checkpoint
->offset
;
207 if (ftruncate(f
->fd
, offset
) ||
208 lseek(f
->fd
, offset
, SEEK_SET
) != offset
)
211 f
->ctx
= checkpoint
->ctx
;
212 f
->offset
= 0; /* hashflush() was called in checkpoint */
216 void crc32_begin(struct hashfile
*f
)
218 f
->crc32
= crc32(0, NULL
, 0);
222 uint32_t crc32_end(struct hashfile
*f
)
228 int hashfile_checksum_valid(const unsigned char *data
, size_t total_len
)
230 unsigned char got
[GIT_MAX_RAWSZ
];
232 size_t data_len
= total_len
- the_hash_algo
->rawsz
;
234 if (total_len
< the_hash_algo
->rawsz
)
235 return 0; /* say "too short"? */
237 the_hash_algo
->init_fn(&ctx
);
238 the_hash_algo
->update_fn(&ctx
, data
, data_len
);
239 the_hash_algo
->final_fn(got
, &ctx
);
241 return hasheq(got
, data
+ data_len
);