diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-03 19:06:22 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-04 19:31:54 -0700 |
commit | 47f66c084291040fb11212956df9033bf263a892 (patch) | |
tree | 988624ae776fa172ba1882c18a7039032af88c16 /lib/btrfs/btrfssum/sumrun.go | |
parent | ab4296c75bfecbce1c2f68820ec43b82e524d617 (diff) |
btrfssum: Split shortsum.go in to several files
Diffstat (limited to 'lib/btrfs/btrfssum/sumrun.go')
-rw-r--r-- | lib/btrfs/btrfssum/sumrun.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/btrfs/btrfssum/sumrun.go b/lib/btrfs/btrfssum/sumrun.go new file mode 100644 index 0000000..1000e7a --- /dev/null +++ b/lib/btrfs/btrfssum/sumrun.go @@ -0,0 +1,58 @@ +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package btrfssum + +import ( + "context" + "io" + + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" +) + +type SumRun[Addr btrfsvol.IntAddr[Addr]] struct { + // How big a ShortSum is in this Run. + ChecksumSize int `json:",omitempty"` + // Base address where this run starts. + Addr Addr `json:",omitempty"` + // All of the ShortSums in this run, concatenated together. + Sums ShortSum +} + +func (run SumRun[Addr]) NumSums() int { + return len(run.Sums) / run.ChecksumSize +} + +func (run SumRun[Addr]) Size() btrfsvol.AddrDelta { + return btrfsvol.AddrDelta(run.NumSums()) * BlockSize +} + +// Get implements diskio.Sequence[int, ShortSum] +func (run SumRun[Addr]) Get(sumIdx int64) (ShortSum, error) { + if sumIdx < 0 || int(sumIdx) >= run.NumSums() { + return "", io.EOF + } + off := int(sumIdx) * run.ChecksumSize + return run.Sums[off : off+run.ChecksumSize], nil +} + +func (run SumRun[Addr]) SumForAddr(addr Addr) (ShortSum, bool) { + if addr < run.Addr || addr >= run.Addr.Add(run.Size()) { + return "", false + } + off := int((addr-run.Addr)/BlockSize) * run.ChecksumSize + return run.Sums[off : off+run.ChecksumSize], true +} + +func (run SumRun[Addr]) Walk(ctx context.Context, fn func(Addr, ShortSum) error) error { + for addr, off := run.Addr, 0; off < len(run.Sums); addr, off = addr+BlockSize, off+run.ChecksumSize { + if err := ctx.Err(); err != nil { + return err + } + if err := fn(addr, run.Sums[off:off+run.ChecksumSize]); err != nil { + return err + } + } + return nil +} |