Refactor blk_match(). Fold the remaining data and empty file or no blocks
authorclaudio <claudio@openbsd.org>
Wed, 28 Feb 2024 09:36:11 +0000 (09:36 +0000)
committerclaudio <claudio@openbsd.org>
Wed, 28 Feb 2024 09:36:11 +0000 (09:36 +0000)
cases together since they are kind of the same.

OK tb@

usr.bin/rsync/blocks.c

index 11f9e27..f76fb81 100644 (file)
@@ -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 <kristaps@bsd.lv>
  *
@@ -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;
 }
 
 /*