csum-file: introduce `hashfd_ext()`

Introduce a new `hashfd_ext()` function that takes an options structure. This function will replace `hashd_throughput()` in the next commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 13, 2026 at 07:45 UTC a1118c0a44606e0b71e515b05112ff38fef989c0
2 files changed +27 -9
csum-file.c
+13 -9
@@ -161,17 +161,16 @@ struct hashfile *hashfd_check(const struct git_hash_algo *algop,
161 return f;
162 }
163
164 -static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
165 - int fd, const char *name,
166 - struct progress *tp,
167 - size_t buffer_len)
164 +struct hashfile *hashfd_ext(const struct git_hash_algo *algop,
165 + int fd, const char *name,
166 + const struct hashfd_options *opts)
167 {
168 struct hashfile *f = xmalloc(sizeof(*f));
169 f->fd = fd;
170 f->check_fd = -1;
171 f->offset = 0;
172 f->total = 0;
174 - f->tp = tp;
173 + f->tp = opts->progress;
174 f->name = name;
175 f->do_crc = 0;
176 f->skip_hash = 0;
@@ -179,8 +178,8 @@ static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
178 f->algop = unsafe_hash_algo(algop);
179 f->algop->init_fn(&f->ctx);
180
182 - f->buffer_len = buffer_len;
183 - f->buffer = xmalloc(buffer_len);
181 + f->buffer_len = opts->buffer_len ? opts->buffer_len : 128 * 1024;
182 + f->buffer = xmalloc(f->buffer_len);
183 f->check_buffer = NULL;
184
185 return f;
@@ -194,7 +193,8 @@ struct hashfile *hashfd(const struct git_hash_algo *algop,
193 * measure the rate of data passing through this hashfile,
194 * use a larger buffer size to reduce fsync() calls.
195 */
197 - return hashfd_internal(algop, fd, name, NULL, 128 * 1024);
196 + struct hashfd_options opts = { 0 };
197 + return hashfd_ext(algop, fd, name, &opts);
198 }
199
200 struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
@@ -206,7 +206,11 @@ struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
206 * size so the progress indicators arrive at a more
207 * frequent rate.
208 */
209 - return hashfd_internal(algop, fd, name, tp, 8 * 1024);
209 + struct hashfd_options opts = {
210 + .progress = tp,
211 + .buffer_len = 8 * 1024,
212 + };
213 + return hashfd_ext(algop, fd, name, &opts);
214 }
215
216 void hashfile_checkpoint_init(struct hashfile *f,
csum-file.h
+14
@@ -45,6 +45,20 @@ int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
45 #define CSUM_FSYNC 2
46 #define CSUM_HASH_IN_STREAM 4
47
48 +struct hashfd_options {
49 + /*
50 + * Throughput progress that counts the number of bytes that have been
51 + * hashed.
52 + */
53 + struct progress *progress;
54 +
55 + /* The length of the buffer that shall be used read read data. */
56 + size_t buffer_len;
57 +};
58 +
59 +struct hashfile *hashfd_ext(const struct git_hash_algo *algop,
60 + int fd, const char *name,
61 + const struct hashfd_options *opts);
62 struct hashfile *hashfd(const struct git_hash_algo *algop,
63 int fd, const char *name);
64 struct hashfile *hashfd_check(const struct git_hash_algo *algop,