From 042f4afa40d29bdcc08939fbc9651c1f3177a3d5 Mon Sep 17 00:00:00 2001 From: claudio Date: Wed, 28 Feb 2024 09:36:11 +0000 Subject: [PATCH] Refactor blk_match(). Fold the remaining data and empty file or no blocks cases together since they are kind of the same. OK tb@ --- usr.bin/rsync/blocks.c | 95 +++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/usr.bin/rsync/blocks.c b/usr.bin/rsync/blocks.c index 11f9e2740f0..f76fb81b621 100644 --- a/usr.bin/rsync/blocks.c +++ b/usr.bin/rsync/blocks.c @@ -1,4 +1,4 @@ -/* $OpenBSD: blocks.c,v 1.22 2024/02/27 11:28:30 claudio Exp $ */ +/* $OpenBSD: blocks.c,v 1.23 2024/02/28 09:36:11 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -243,7 +243,7 @@ void blk_match(struct sess *sess, const struct blkset *blks, const char *path, struct blkstat *st) { - off_t last, end, sz; + off_t last, end = 0, sz; int32_t tok; size_t i; const struct blk *blk; @@ -265,66 +265,55 @@ blk_match(struct sess *sess, const struct blkset *blks, */ end = st->mapsz + 1 - blks->blks[blks->blksz - 1].len; - last = st->offs; - - for (i = 0; st->offs < end; st->offs++, i++) { - blk = blk_find(sess, st, blks, path, i == 0); - if (blk == NULL) - continue; - - sz = st->offs - last; - st->dirty += sz; - st->total += sz; - LOG4("%s: flushing %jd B before %zu B block %zu", - path, (intmax_t)sz, - blk->len, blk->idx); - tok = -(blk->idx + 1); - - hash_file_buf(&st->ctx, st->map + last, sz + blk->len); - - /* - * Write the data we have, then follow it with - * the tag of the block that matches. - */ - - st->curpos = last; - st->curlen = st->curpos + sz; - st->curtok = tok; - assert(st->curtok != 0); - st->curst = sz ? BLKSTAT_DATA : BLKSTAT_TOK; - st->total += blk->len; - st->offs += blk->len; - st->hint = blk->idx + 1; - - return; - } - - /* Emit remaining data and send terminator token. */ + } - sz = st->mapsz - last; - LOG4("%s: flushing remaining %jd B", - path, (intmax_t)sz); + last = st->offs; + for (i = 0; st->offs < end; st->offs++, i++) { + blk = blk_find(sess, st, blks, path, i == 0); + if (blk == NULL) + continue; - st->total += sz; + sz = st->offs - last; st->dirty += sz; + st->total += sz; + LOG4("%s: flushing %jd B before %zu B block %zu", + path, (intmax_t)sz, + blk->len, blk->idx); + tok = -(blk->idx + 1); + + hash_file_buf(&st->ctx, st->map + last, sz + blk->len); + + /* + * Write the data we have, then follow it with + * the tag of the block that matches. + */ + st->curpos = last; st->curlen = st->curpos + sz; - st->curtok = 0; + st->curtok = tok; + assert(st->curtok != 0); st->curst = sz ? BLKSTAT_DATA : BLKSTAT_TOK; + st->total += blk->len; + st->offs += blk->len; + st->hint = blk->idx + 1; - hash_file_buf(&st->ctx, st->map + st->curpos, sz); - } else { - st->curpos = 0; - st->curlen = st->mapsz; - st->curtok = 0; - st->curst = st->mapsz ? BLKSTAT_DATA : BLKSTAT_TOK; - st->dirty = st->total = st->mapsz; + return; + } - hash_file_buf(&st->ctx, st->map, st->mapsz); + /* Emit remaining data and send terminator token. */ - LOG4("%s: flushing whole file %zu B", - path, st->mapsz); - } + sz = st->mapsz - last; + LOG4("%s: flushing %s %jd B", path, + last == 0 ? "whole" : "remaining", (intmax_t)sz); + + hash_file_buf(&st->ctx, st->map + last, sz); + + st->total += sz; + st->dirty += sz; + st->curpos = last; + st->curlen = st->curpos + sz; + st->curtok = 0; + st->curst = sz ? BLKSTAT_DATA : BLKSTAT_TOK; } /* -- 2.20.1