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.
11 #define USE_THE_REPOSITORY_VARIABLE
13 #include "git-compat-util.h"
15 #include "csum-file.h"
18 static void verify_buffer_or_die(struct hashfile
*f
,
22 ssize_t ret
= read_in_full(f
->check_fd
, f
->check_buffer
, count
);
25 die_errno("%s: sha1 file read error", f
->name
);
27 die("%s: sha1 file truncated", f
->name
);
28 if (memcmp(buf
, f
->check_buffer
, count
))
29 die("sha1 file '%s' validation error", f
->name
);
32 static void flush(struct hashfile
*f
, const void *buf
, unsigned int count
)
34 if (0 <= f
->check_fd
&& count
)
35 verify_buffer_or_die(f
, buf
, count
);
37 if (write_in_full(f
->fd
, buf
, count
) < 0) {
39 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
40 die_errno("sha1 file '%s' write error", f
->name
);
44 display_throughput(f
->tp
, f
->total
);
47 void hashflush(struct hashfile
*f
)
49 unsigned offset
= f
->offset
;
53 the_hash_algo
->update_fn(&f
->ctx
, f
->buffer
, offset
);
54 flush(f
, f
->buffer
, offset
);
59 static void free_hashfile(struct hashfile
*f
)
62 free(f
->check_buffer
);
66 int finalize_hashfile(struct hashfile
*f
, unsigned char *result
,
67 enum fsync_component component
, unsigned int flags
)
74 hashclr(f
->buffer
, the_repository
->hash_algo
);
76 the_hash_algo
->final_fn(f
->buffer
, &f
->ctx
);
79 hashcpy(result
, f
->buffer
, the_repository
->hash_algo
);
80 if (flags
& CSUM_HASH_IN_STREAM
)
81 flush(f
, f
->buffer
, the_hash_algo
->rawsz
);
82 if (flags
& CSUM_FSYNC
)
83 fsync_component_or_die(component
, f
->fd
, f
->name
);
84 if (flags
& CSUM_CLOSE
) {
86 die_errno("%s: sha1 file error on close", f
->name
);
90 if (0 <= f
->check_fd
) {
92 int cnt
= read_in_full(f
->check_fd
, &discard
, 1);
94 die_errno("%s: error when reading the tail of sha1 file",
97 die("%s: sha1 file has trailing garbage", f
->name
);
98 if (close(f
->check_fd
))
99 die_errno("%s: sha1 file error on close", f
->name
);
105 void hashwrite(struct hashfile
*f
, const void *buf
, unsigned int count
)
108 unsigned left
= f
->buffer_len
- f
->offset
;
109 unsigned nr
= count
> left
? left
: count
;
112 f
->crc32
= crc32(f
->crc32
, buf
, nr
);
114 if (nr
== f
->buffer_len
) {
116 * Flush a full batch worth of data directly
117 * from the input, skipping the memcpy() to
118 * the hashfile's buffer. In this block,
119 * f->offset is necessarily zero.
122 the_hash_algo
->update_fn(&f
->ctx
, buf
, nr
);
126 * Copy to the hashfile's buffer, flushing only
129 memcpy(f
->buffer
+ f
->offset
, buf
, nr
);
137 buf
= (char *) buf
+ nr
;
141 struct hashfile
*hashfd_check(const char *name
)
146 sink
= xopen("/dev/null", O_WRONLY
);
147 check
= xopen(name
, O_RDONLY
);
148 f
= hashfd(sink
, name
);
150 f
->check_buffer
= xmalloc(f
->buffer_len
);
155 static struct hashfile
*hashfd_internal(int fd
, const char *name
,
159 struct hashfile
*f
= xmalloc(sizeof(*f
));
168 the_hash_algo
->init_fn(&f
->ctx
);
170 f
->buffer_len
= buffer_len
;
171 f
->buffer
= xmalloc(buffer_len
);
172 f
->check_buffer
= NULL
;
177 struct hashfile
*hashfd(int fd
, const char *name
)
180 * Since we are not going to use a progress meter to
181 * measure the rate of data passing through this hashfile,
182 * use a larger buffer size to reduce fsync() calls.
184 return hashfd_internal(fd
, name
, NULL
, 128 * 1024);
187 struct hashfile
*hashfd_throughput(int fd
, const char *name
, struct progress
*tp
)
190 * Since we are expecting to report progress of the
191 * write into this hashfile, use a smaller buffer
192 * size so the progress indicators arrive at a more
195 return hashfd_internal(fd
, name
, tp
, 8 * 1024);
198 void hashfile_checkpoint(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
201 checkpoint
->offset
= f
->total
;
202 the_hash_algo
->clone_fn(&checkpoint
->ctx
, &f
->ctx
);
205 int hashfile_truncate(struct hashfile
*f
, struct hashfile_checkpoint
*checkpoint
)
207 off_t offset
= checkpoint
->offset
;
209 if (ftruncate(f
->fd
, offset
) ||
210 lseek(f
->fd
, offset
, SEEK_SET
) != offset
)
213 the_hash_algo
->clone_fn(&f
->ctx
, &checkpoint
->ctx
);
214 f
->offset
= 0; /* hashflush() was called in checkpoint */
218 void crc32_begin(struct hashfile
*f
)
220 f
->crc32
= crc32(0, NULL
, 0);
224 uint32_t crc32_end(struct hashfile
*f
)
230 int hashfile_checksum_valid(const unsigned char *data
, size_t total_len
)
232 unsigned char got
[GIT_MAX_RAWSZ
];
234 size_t data_len
= total_len
- the_hash_algo
->rawsz
;
236 if (total_len
< the_hash_algo
->rawsz
)
237 return 0; /* say "too short"? */
239 the_hash_algo
->init_fn(&ctx
);
240 the_hash_algo
->update_fn(&ctx
, data
, data_len
);
241 the_hash_algo
->final_fn(got
, &ctx
);
243 return hasheq(got
, data
+ data_len
, the_repository
->hash_algo
);