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"
15 static void verify_buffer_or_die(struct hashfile
*f
,
19 ssize_t ret
= read_in_full(f
->check_fd
, f
->check_buffer
, count
);
22 die_errno("%s: sha1 file read error", f
->name
);
24 die("%s: sha1 file truncated", f
->name
);
25 if (memcmp(buf
, f
->check_buffer
, count
))
26 die("sha1 file '%s' validation error", f
->name
);
29 static void flush(struct hashfile
*f
, const void *buf
, unsigned int count
)
31 if (0 <= f
->check_fd
&& count
)
32 verify_buffer_or_die(f
, buf
, count
);
34 if (write_in_full(f
->fd
, buf
, count
) < 0) {
36 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
37 die_errno("sha1 file '%s' write error", f
->name
);
41 display_throughput(f
->tp
, f
->total
);
44 void hashflush(struct hashfile
*f
)
46 unsigned offset
= f
->offset
;
50 the_hash_algo
->update_fn(&f
->ctx
, f
->buffer
, offset
);
51 flush(f
, f
->buffer
, offset
);
56 static void free_hashfile(struct hashfile
*f
)
59 free(f
->check_buffer
);
63 int finalize_hashfile(struct hashfile
*f
, unsigned char *result
,
64 enum fsync_component component
, unsigned int flags
)
73 the_hash_algo
->final_fn(f
->buffer
, &f
->ctx
);
76 hashcpy(result
, f
->buffer
);
77 if (flags
& CSUM_HASH_IN_STREAM
)
78 flush(f
, f
->buffer
, the_hash_algo
->rawsz
);
79 if (flags
& CSUM_FSYNC
)
80 fsync_component_or_die(component
, f
->fd
, f
->name
);
81 if (flags
& CSUM_CLOSE
) {
83 die_errno("%s: sha1 file error on close", f
->name
);
87 if (0 <= f
->check_fd
) {
89 int cnt
= read_in_full(f
->check_fd
, &discard
, 1);
91 die_errno("%s: error when reading the tail of sha1 file",
94 die("%s: sha1 file has trailing garbage", f
->name
);
95 if (close(f
->check_fd
))
96 die_errno("%s: sha1 file error on close", f
->name
);
102 void hashwrite(struct hashfile
*f
, const void *buf
, unsigned int count
)
105 unsigned left
= f
->buffer_len
- f
->offset
;
106 unsigned nr
= count
> left
? left
: count
;
109 f
->crc32
= crc32(f
->crc32
, buf
, nr
);
111 if (nr
== f
->buffer_len
) {
113 * Flush a full batch worth of data directly
114 * from the input, skipping the memcpy() to
115 * the hashfile's buffer. In this block,
116 * f->offset is necessarily zero.
119 the_hash_algo
->update_fn(&f
->ctx
, buf
, nr
);
123 * Copy to the hashfile's buffer, flushing only
126 memcpy(f
->buffer
+ f
->offset
, buf
, nr
);
134 buf
= (char *) buf
+ nr
;
138 struct hashfile
*hashfd_check(const char *name
)
143 sink
= xopen("/dev/null", O_WRONLY
);
144 check
= xopen(name
, O_RDONLY
);
145 f
= hashfd(sink
, name
);
147 f
->check_buffer
= xmalloc(f
->buffer_len
);
152 static struct hashfile
*hashfd_internal(int fd
, const char *name
,
156 struct hashfile
*f
= xmalloc(sizeof(*f
));
165 the_hash_algo
->init_fn(&f
->ctx
);
167 f
->buffer_len
= buffer_len
;
168 f
->buffer
= xmalloc(buffer_len
);
169 f
->check_buffer
= NULL
;
174 struct hashfile
*hashfd(int fd
, const char *name
)
177 * Since we are not going to use a progress meter to
178 * measure the rate of data passing through this hashfile,
179 * use a larger buffer size to reduce fsync() calls.
181 return hashfd_internal(fd
, name
, NULL
, 128 * 1024);
184 struct hashfile
*hashfd_throughput(int fd
, const char *name
, struct progress
*tp
)
187 * Since we are expecting to report progress of the
188 * write into this hashfile, use a smaller buffer
189 * size so the progress indicators arrive at a more
192 return hashfd_internal(fd
, name
, tp
, 8 * 1024);
195 void hashfile_checkpoint(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
198 checkpoint
->offset
= f
->total
;
199 the_hash_algo
->clone_fn(&checkpoint
->ctx
, &f
->ctx
);
202 int hashfile_truncate(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
204 off_t offset
= checkpoint
->offset
;
206 if (ftruncate(f
->fd
, offset
) ||
207 lseek(f
->fd
, offset
, SEEK_SET
) != offset
)
210 f
->ctx
= checkpoint
->ctx
;
211 f
->offset
= 0; /* hashflush() was called in checkpoint */
215 void crc32_begin(struct hashfile
*f
)
217 f
->crc32
= crc32(0, NULL
, 0);
221 uint32_t crc32_end(struct hashfile
*f
)
227 int hashfile_checksum_valid(const unsigned char *data
, size_t total_len
)
229 unsigned char got
[GIT_MAX_RAWSZ
];
231 size_t data_len
= total_len
- the_hash_algo
->rawsz
;
233 if (total_len
< the_hash_algo
->rawsz
)
234 return 0; /* say "too short"? */
236 the_hash_algo
->init_fn(&ctx
);
237 the_hash_algo
->update_fn(&ctx
, data
, data_len
);
238 the_hash_algo
->final_fn(got
, &ctx
);
240 return hasheq(got
, data
+ data_len
);