diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-26 20:25:27 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-26 20:27:11 -0600 |
commit | 8db7bed06505569c1b41a338b5131cfd5c9d1f28 (patch) | |
tree | a33404699ab627cc71c40bdbe7062ef837c9d7d3 /pkg/btrfs/btrfsvol/addr.go | |
parent | 09e3317a433add776d6539abcd7cf4f00ca984e5 (diff) |
tidy up which package things are in
Diffstat (limited to 'pkg/btrfs/btrfsvol/addr.go')
-rw-r--r-- | pkg/btrfs/btrfsvol/addr.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/pkg/btrfs/btrfsvol/addr.go b/pkg/btrfs/btrfsvol/addr.go new file mode 100644 index 0000000..e8b578a --- /dev/null +++ b/pkg/btrfs/btrfsvol/addr.go @@ -0,0 +1,45 @@ +package btrfsvol + +import ( + "fmt" + + "lukeshu.com/btrfs-tools/pkg/util" +) + +type ( + PhysicalAddr int64 + LogicalAddr int64 + AddrDelta int64 +) + +func formatAddr(addr int64, f fmt.State, verb rune) { + switch verb { + case 'v', 's', 'q': + str := fmt.Sprintf("%#016x", addr) + fmt.Fprintf(f, util.FmtStateString(f, verb), str) + default: + fmt.Fprintf(f, util.FmtStateString(f, verb), addr) + } +} + +func (a PhysicalAddr) Format(f fmt.State, verb rune) { formatAddr(int64(a), f, verb) } +func (a LogicalAddr) Format(f fmt.State, verb rune) { formatAddr(int64(a), f, verb) } +func (d AddrDelta) Format(f fmt.State, verb rune) { formatAddr(int64(d), f, verb) } + +func (a PhysicalAddr) Sub(b PhysicalAddr) AddrDelta { return AddrDelta(a - b) } +func (a LogicalAddr) Sub(b LogicalAddr) AddrDelta { return AddrDelta(a - b) } + +func (a PhysicalAddr) Add(b AddrDelta) PhysicalAddr { return a + PhysicalAddr(b) } +func (a LogicalAddr) Add(b AddrDelta) LogicalAddr { return a + LogicalAddr(b) } + +type QualifiedPhysicalAddr struct { + Dev util.UUID + Addr PhysicalAddr +} + +func (a QualifiedPhysicalAddr) Cmp(b QualifiedPhysicalAddr) int { + if d := a.Dev.Cmp(b.Dev); d != 0 { + return d + } + return int(a.Addr - b.Addr) +} |