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 verify_buffer_or_die(struct hashfile
*f
,
18 ssize_t ret
= read_in_full(f
->check_fd
, f
->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
, f
->check_buffer
, count
))
25 die("sha1 file '%s' validation error", f
->name
);
28 static void flush(struct hashfile
*f
, const void *buf
, unsigned int count
)
30 if (0 <= f
->check_fd
&& count
)
31 verify_buffer_or_die(f
, buf
, count
);
33 if (write_in_full(f
->fd
, buf
, count
) < 0) {
35 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
36 die_errno("sha1 file '%s' write error", f
->name
);
40 display_throughput(f
->tp
, f
->total
);
43 void hashflush(struct hashfile
*f
)
45 unsigned offset
= f
->offset
;
48 the_hash_algo
->update_fn(&f
->ctx
, f
->buffer
, offset
);
49 flush(f
, f
->buffer
, offset
);
54 static void free_hashfile(struct hashfile
*f
)
57 free(f
->check_buffer
);
61 int finalize_hashfile(struct hashfile
*f
, unsigned char *result
,
62 enum fsync_component component
, unsigned int flags
)
67 the_hash_algo
->final_fn(f
->buffer
, &f
->ctx
);
69 hashcpy(result
, f
->buffer
);
70 if (flags
& CSUM_HASH_IN_STREAM
)
71 flush(f
, f
->buffer
, the_hash_algo
->rawsz
);
72 if (flags
& CSUM_FSYNC
)
73 fsync_component_or_die(component
, f
->fd
, f
->name
);
74 if (flags
& CSUM_CLOSE
) {
76 die_errno("%s: sha1 file error on close", f
->name
);
80 if (0 <= f
->check_fd
) {
82 int cnt
= read_in_full(f
->check_fd
, &discard
, 1);
84 die_errno("%s: error when reading the tail of sha1 file",
87 die("%s: sha1 file has trailing garbage", f
->name
);
88 if (close(f
->check_fd
))
89 die_errno("%s: sha1 file error on close", f
->name
);
95 void hashwrite(struct hashfile
*f
, const void *buf
, unsigned int count
)
98 unsigned left
= f
->buffer_len
- f
->offset
;
99 unsigned nr
= count
> left
? left
: count
;
102 f
->crc32
= crc32(f
->crc32
, buf
, nr
);
104 if (nr
== f
->buffer_len
) {
106 * Flush a full batch worth of data directly
107 * from the input, skipping the memcpy() to
108 * the hashfile's buffer. In this block,
109 * f->offset is necessarily zero.
111 the_hash_algo
->update_fn(&f
->ctx
, buf
, nr
);
115 * Copy to the hashfile's buffer, flushing only
118 memcpy(f
->buffer
+ f
->offset
, buf
, nr
);
126 buf
= (char *) buf
+ nr
;
130 struct hashfile
*hashfd_check(const char *name
)
135 sink
= xopen("/dev/null", O_WRONLY
);
136 check
= xopen(name
, O_RDONLY
);
137 f
= hashfd(sink
, name
);
139 f
->check_buffer
= xmalloc(f
->buffer_len
);
144 static struct hashfile
*hashfd_internal(int fd
, const char *name
,
148 struct hashfile
*f
= xmalloc(sizeof(*f
));
156 the_hash_algo
->init_fn(&f
->ctx
);
158 f
->buffer_len
= buffer_len
;
159 f
->buffer
= xmalloc(buffer_len
);
160 f
->check_buffer
= NULL
;
165 struct hashfile
*hashfd(int fd
, const char *name
)
168 * Since we are not going to use a progress meter to
169 * measure the rate of data passing through this hashfile,
170 * use a larger buffer size to reduce fsync() calls.
172 return hashfd_internal(fd
, name
, NULL
, 128 * 1024);
175 struct hashfile
*hashfd_throughput(int fd
, const char *name
, struct progress
*tp
)
178 * Since we are expecting to report progress of the
179 * write into this hashfile, use a smaller buffer
180 * size so the progress indicators arrive at a more
183 return hashfd_internal(fd
, name
, tp
, 8 * 1024);
186 void hashfile_checkpoint(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
189 checkpoint
->offset
= f
->total
;
190 the_hash_algo
->clone_fn(&checkpoint
->ctx
, &f
->ctx
);
193 int hashfile_truncate(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
195 off_t offset
= checkpoint
->offset
;
197 if (ftruncate(f
->fd
, offset
) ||
198 lseek(f
->fd
, offset
, SEEK_SET
) != offset
)
201 f
->ctx
= checkpoint
->ctx
;
202 f
->offset
= 0; /* hashflush() was called in checkpoint */
206 void crc32_begin(struct hashfile
*f
)
208 f
->crc32
= crc32(0, NULL
, 0);
212 uint32_t crc32_end(struct hashfile
*f
)
218 int hashfile_checksum_valid(const unsigned char *data
, size_t total_len
)
220 unsigned char got
[GIT_MAX_RAWSZ
];
222 size_t data_len
= total_len
- the_hash_algo
->rawsz
;
224 if (total_len
< the_hash_algo
->rawsz
)
225 return 0; /* say "too short"? */
227 the_hash_algo
->init_fn(&ctx
);
228 the_hash_algo
->update_fn(&ctx
, data
, data_len
);
229 the_hash_algo
->final_fn(got
, &ctx
);
231 return hasheq(got
, data
+ data_len
);