]> git.ipfire.org Git - thirdparty/git.git/commitdiff
csum-file: introduce `hashfd_ext()`
authorPatrick Steinhardt <ps@pks.im>
Fri, 13 Mar 2026 06:45:19 +0000 (07:45 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 13 Mar 2026 15:54:15 +0000 (08:54 -0700)
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>
csum-file.c
csum-file.h

index 6e21e3cac8a636c016be59f6426b27d1dc66accc..a50416247edf2505a1b5612077e6453784ee46ed 100644 (file)
@@ -161,17 +161,16 @@ struct hashfile *hashfd_check(const struct git_hash_algo *algop,
        return f;
 }
 
-static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
-                                       int fd, const char *name,
-                                       struct progress *tp,
-                                       size_t buffer_len)
+struct hashfile *hashfd_ext(const struct git_hash_algo *algop,
+                           int fd, const char *name,
+                           const struct hashfd_options *opts)
 {
        struct hashfile *f = xmalloc(sizeof(*f));
        f->fd = fd;
        f->check_fd = -1;
        f->offset = 0;
        f->total = 0;
-       f->tp = tp;
+       f->tp = opts->progress;
        f->name = name;
        f->do_crc = 0;
        f->skip_hash = 0;
@@ -179,8 +178,8 @@ static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
        f->algop = unsafe_hash_algo(algop);
        f->algop->init_fn(&f->ctx);
 
-       f->buffer_len = buffer_len;
-       f->buffer = xmalloc(buffer_len);
+       f->buffer_len = opts->buffer_len ? opts->buffer_len : 128 * 1024;
+       f->buffer = xmalloc(f->buffer_len);
        f->check_buffer = NULL;
 
        return f;
@@ -194,7 +193,8 @@ struct hashfile *hashfd(const struct git_hash_algo *algop,
         * measure the rate of data passing through this hashfile,
         * use a larger buffer size to reduce fsync() calls.
         */
-       return hashfd_internal(algop, fd, name, NULL, 128 * 1024);
+       struct hashfd_options opts = { 0 };
+       return hashfd_ext(algop, fd, name, &opts);
 }
 
 struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
@@ -206,7 +206,11 @@ struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
         * size so the progress indicators arrive at a more
         * frequent rate.
         */
-       return hashfd_internal(algop, fd, name, tp, 8 * 1024);
+       struct hashfd_options opts = {
+               .progress = tp,
+               .buffer_len = 8 * 1024,
+       };
+       return hashfd_ext(algop, fd, name, &opts);
 }
 
 void hashfile_checkpoint_init(struct hashfile *f,
index 07ae11024afc3459bd89827a37889c41d13cf162..a03b60120d85f183ca2c7e5558ff217e00d9a86e 100644 (file)
@@ -45,6 +45,20 @@ int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
 #define CSUM_FSYNC             2
 #define CSUM_HASH_IN_STREAM    4
 
+struct hashfd_options {
+       /*
+        * Throughput progress that counts the number of bytes that have been
+        * hashed.
+        */
+       struct progress *progress;
+
+       /* The length of the buffer that shall be used read read data. */
+       size_t buffer_len;
+};
+
+struct hashfile *hashfd_ext(const struct git_hash_algo *algop,
+                           int fd, const char *name,
+                           const struct hashfd_options *opts);
 struct hashfile *hashfd(const struct git_hash_algo *algop,
                        int fd, const char *name);
 struct hashfile *hashfd_check(const struct git_hash_algo *algop,