From 45f676e42c2e136e7d337f15762a50da2ae8c633 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 17:45:11 -0700 Subject: lint: Tighten up the nolintlint configuration --- cmd/btrfs-rec/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index 13ae886..3f662bb 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -43,7 +43,7 @@ func main() { SilenceErrors: true, // main() will handle this after .ExecuteContext() returns SilenceUsage: true, // our FlagErrorFunc will handle it - CompletionOptions: cobra.CompletionOptions{ //nolint:exhaustivestruct + CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: true, }, } -- cgit v1.2.3-54-g00ecf From c307e7048da3c02e1e540eab227def7fec7340cc Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 19:27:49 -0700 Subject: lint: Turn on gocritic --- .golangci.yml | 4 +- cmd/btrfs-rec/inspect_lstrees.go | 8 ++-- lib/btrfs/btrfstree/ops.go | 14 +++---- lib/btrfs/btrfstree/types_node.go | 9 ++-- lib/btrfs/btrfstree/types_node_test.go | 5 +-- lib/btrfs/btrfsvol/lvm.go | 8 ++-- lib/btrfsprogs/btrfsinspect/mount.go | 4 +- lib/btrfsprogs/btrfsinspect/print_tree.go | 48 +++++++++++----------- .../btrfsinspect/rebuildmappings/fuzzymatchsums.go | 11 ++--- .../btrfsinspect/rebuildnodes/rebuild.go | 4 +- .../btrfsinspect/rebuildnodes/rebuild_graph.go | 14 +++---- lib/containers/intervaltree.go | 4 +- lib/containers/rbtree.go | 7 ++-- lib/containers/rbtree_test.go | 10 +---- 14 files changed, 72 insertions(+), 78 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/.golangci.yml b/.golangci.yml index ad53616..953b512 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -31,7 +31,6 @@ linters: - gochecknoglobals - gochecknoinits - gocognit - - gocritic - gocyclo - godot - godox @@ -67,6 +66,9 @@ linters-settings: - standard - default - prefix(git.lukeshu.com/btrfs-progs-ng) + gocritic: + disabled-checks: + - appendAssign gofmt: simplify: true nolintlint: diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go index e92c544..a6d86eb 100644 --- a/cmd/btrfs-rec/inspect_lstrees.go +++ b/cmd/btrfs-rec/inspect_lstrees.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -79,12 +79,12 @@ func init() { }, Item: func(_ btrfstree.TreePath, item btrfstree.Item) error { typ := item.Key.ItemType - treeItemCnt[typ] = treeItemCnt[typ] + 1 + treeItemCnt[typ]++ return nil }, BadItem: func(_ btrfstree.TreePath, item btrfstree.Item) error { typ := item.Key.ItemType - treeItemCnt[typ] = treeItemCnt[typ] + 1 + treeItemCnt[typ]++ return nil }, }, @@ -113,7 +113,7 @@ func init() { } for _, item := range node.Data.BodyLeaf { typ := item.Key.ItemType - treeItemCnt[typ] = treeItemCnt[typ] + 1 + treeItemCnt[typ]++ } } } diff --git a/lib/btrfs/btrfstree/ops.go b/lib/btrfs/btrfstree/ops.go index f2eb6f0..ddab630 100644 --- a/lib/btrfs/btrfstree/ops.go +++ b/lib/btrfs/btrfstree/ops.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -156,14 +156,12 @@ func (fs TreeOperatorImpl) treeWalk(ctx context.Context, path TreePath, errHandl } if err != nil { errHandle(&TreeError{Path: path, Err: err}) - } else { - if cbs.Node != nil { - if err := cbs.Node(path, node); err != nil { - if errors.Is(err, iofs.SkipDir) { - return - } - errHandle(&TreeError{Path: path, Err: err}) + } else if cbs.Node != nil { + if err := cbs.Node(path, node); err != nil { + if errors.Is(err, iofs.SkipDir) { + return } + errHandle(&TreeError{Path: path, Err: err}) } } if ctx.Err() != nil { diff --git a/lib/btrfs/btrfstree/types_node.go b/lib/btrfs/btrfstree/types_node.go index 299c433..d2e91de 100644 --- a/lib/btrfs/btrfstree/types_node.go +++ b/lib/btrfs/btrfstree/types_node.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -212,10 +212,11 @@ func (node Node) MarshalBinary() ([]byte, error) { if bs, err := binstruct.Marshal(node.Head); err != nil { return buf, err - } else if len(bs) != binstruct.StaticSize(NodeHeader{}) { - return nil, fmt.Errorf("header is %v bytes but expected %v", - len(bs), binstruct.StaticSize(NodeHeader{})) } else { + if len(bs) != binstruct.StaticSize(NodeHeader{}) { + return nil, fmt.Errorf("header is %v bytes but expected %v", + len(bs), binstruct.StaticSize(NodeHeader{})) + } copy(buf, bs) } diff --git a/lib/btrfs/btrfstree/types_node_test.go b/lib/btrfs/btrfstree/types_node_test.go index 040b90c..80855d8 100644 --- a/lib/btrfs/btrfstree/types_node_test.go +++ b/lib/btrfs/btrfstree/types_node_test.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -23,13 +23,12 @@ func FuzzRoundTripNode(f *testing.F) { n, err := binstruct.Unmarshal(inDat, &node) if err != nil { t.Logf("err=%v", err) - //require.Equal(t, 0, n) } else { require.Equal(t, len(inDat), n) outDat, err := binstruct.Marshal(node) require.NoError(t, err) - require.Equal(t, inDat[:], outDat) + require.Equal(t, inDat, outDat) } }) } diff --git a/lib/btrfs/btrfsvol/lvm.go b/lib/btrfs/btrfsvol/lvm.go index c7551fc..605524b 100644 --- a/lib/btrfs/btrfsvol/lvm.go +++ b/lib/btrfs/btrfsvol/lvm.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -335,10 +335,8 @@ func (lv *LogicalVolume[PhysicalVolume]) maybeShortReadAt(dat []byte, laddr Logi } if first { copy(dat, buf) - } else { - if !bytes.Equal(dat, buf) { - return 0, fmt.Errorf("inconsistent stripes at laddr=%v len=%v", laddr, len(dat)) - } + } else if !bytes.Equal(dat, buf) { + return 0, fmt.Errorf("inconsistent stripes at laddr=%v len=%v", laddr, len(dat)) } } return len(dat), nil diff --git a/lib/btrfsprogs/btrfsinspect/mount.go b/lib/btrfsprogs/btrfsinspect/mount.go index 2a0b232..29193cb 100644 --- a/lib/btrfsprogs/btrfsinspect/mount.go +++ b/lib/btrfsprogs/btrfsinspect/mount.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -213,7 +213,7 @@ func (sv *subvolume) StatFS(_ context.Context, op *fuseops.StatFSOp) error { op.IoSize = sb.SectorSize op.BlockSize = sb.SectorSize op.Blocks = sb.TotalBytes / uint64(sb.SectorSize) // TODO: adjust for RAID type - //op.BlocksFree = TODO + // op.BlocksFree = TODO // btrfs doesn't have a fixed number of inodes op.Inodes = 0 diff --git a/lib/btrfsprogs/btrfsinspect/print_tree.go b/lib/btrfsprogs/btrfsinspect/print_tree.go index 6c31350..62d1d7b 100644 --- a/lib/btrfsprogs/btrfsinspect/print_tree.go +++ b/lib/btrfsprogs/btrfsinspect/print_tree.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -142,8 +142,8 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfspri textui.Fprintf(out, "\t\tindex %v namelen %v name: %s\n", ref.Index, ref.NameLen, ref.Name) } - //case btrfsitem.INODE_EXTREF_KEY: - // // TODO + // case btrfsitem.INODE_EXTREF_KEY: + // // TODO case btrfsitem.DirEntry: textui.Fprintf(out, "\t\tlocation %v type %v\n", fmtKey(body.Location), body.Type) @@ -153,8 +153,8 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfspri if len(body.Data) > 0 { textui.Fprintf(out, "\t\tdata %v\n", body.Data) } - //case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY: - // // TODO + // case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY: + // // TODO case btrfsitem.Root: textui.Fprintf(out, "\t\tgeneration %v root_dirid %v bytenr %d byte_limit %v bytes_used %v\n", body.Generation, body.RootDirID, body.ByteNr, body.ByteLimit, body.BytesUsed) @@ -200,10 +200,10 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfspri body.Head.Refs, body.Head.Generation, body.Head.Flags) textui.Fprintf(out, "\t\ttree block skinny level %v\n", item.Key.Offset) printExtentInlineRefs(out, body.Refs) - //case btrfsitem.EXTENT_DATA_REF_KEY: - // // TODO - //case btrfsitem.SHARED_DATA_REF_KEY: - // // TODO + // case btrfsitem.EXTENT_DATA_REF_KEY: + // // TODO + // case btrfsitem.SHARED_DATA_REF_KEY: + // // TODO case btrfsitem.ExtentCSum: start := btrfsvol.LogicalAddr(item.Key.Offset) textui.Fprintf(out, "\t\trange start %d end %d length %d", @@ -291,16 +291,16 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfspri "\t\tchunk_tree_uuid %v\n", body.ChunkTree, body.ChunkObjectID, body.ChunkOffset, body.Length, body.ChunkTreeUUID) - //case btrfsitem.QGROUP_STATUS_KEY: - // // TODO - //case btrfsitem.QGROUP_INFO_KEY: - // // TODO - //case btrfsitem.QGROUP_LIMIT_KEY: - // // TODO + // case btrfsitem.QGROUP_STATUS_KEY: + // // TODO + // case btrfsitem.QGROUP_INFO_KEY: + // // TODO + // case btrfsitem.QGROUP_LIMIT_KEY: + // // TODO case btrfsitem.UUIDMap: textui.Fprintf(out, "\t\tsubvol_id %d\n", body.ObjID) - //case btrfsitem.STRING_ITEM_KEY: - // // TODO + // case btrfsitem.STRING_ITEM_KEY: + // // TODO case btrfsitem.DevStats: textui.Fprintf(out, "\t\tpersistent item objectid %v offset %v\n", item.Key.ObjectID.Format(item.Key.ItemType), item.Key.Offset) @@ -316,8 +316,8 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfspri default: textui.Fprintf(out, "\t\tunknown persistent item objectid %v\n", item.Key.ObjectID) } - //case btrfsitem.TEMPORARY_ITEM_KEY: - // // TODO + // case btrfsitem.TEMPORARY_ITEM_KEY: + // // TODO case btrfsitem.Empty: switch item.Key.ItemType { case btrfsitem.ORPHAN_ITEM_KEY: // 48 @@ -330,10 +330,10 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfspri textui.Fprintf(out, "\t\tfree space extent\n") case btrfsitem.QGROUP_RELATION_KEY: // 246 // do nothing - //case btrfsitem.EXTENT_REF_V0_KEY: - // textui.Fprintf(out, "\t\textent ref v0 (deprecated)\n") - //case btrfsitem.CSUM_ITEM_KEY: - // textui.Fprintf(out, "\t\tcsum item\n") + // case btrfsitem.EXTENT_REF_V0_KEY: + // textui.Fprintf(out, "\t\textent ref v0 (deprecated)\n") + // case btrfsitem.CSUM_ITEM_KEY: + // textui.Fprintf(out, "\t\tcsum item\n") default: textui.Fprintf(out, "\t\t(error) unhandled empty item type: %v\n", item.Key.ItemType) } @@ -426,7 +426,7 @@ func fmtKey(key btrfsprim.Key) string { var out strings.Builder textui.Fprintf(&out, "key (%v %v", key.ObjectID.Format(key.ItemType), key.ItemType) switch key.ItemType { - case btrfsitem.QGROUP_RELATION_KEY: //TODO, btrfsitem.QGROUP_INFO_KEY, btrfsitem.QGROUP_LIMIT_KEY: + case btrfsitem.QGROUP_RELATION_KEY: // TODO, btrfsitem.QGROUP_INFO_KEY, btrfsitem.QGROUP_LIMIT_KEY: panic("TODO: printing qgroup items not yet implemented") case btrfsitem.UUID_SUBVOL_KEY, btrfsitem.UUID_RECEIVED_SUBVOL_KEY: textui.Fprintf(&out, " %#08x)", key.Offset) diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go index ae83513..b1be7ba 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -78,7 +78,7 @@ func fuzzyMatchBlockGroupSums(ctx context.Context, Dev: paddr.Dev, Addr: paddr.Addr.Add(-off), } - matches[key] = matches[key] + 1 + matches[key]++ } return nil }); err != nil { @@ -145,11 +145,12 @@ type lowestN[T containers.Ordered[T]] struct { } func (l *lowestN[T]) Insert(v T) { - if len(l.Dat) < l.N { + switch { + case len(l.Dat) < l.N: l.Dat = append(l.Dat, v) - } else if v.Cmp(l.Dat[0]) < 0 { + case v.Cmp(l.Dat[0]) < 0: l.Dat[0] = v - } else { + default: return } sort.Slice(l.Dat, func(i, j int) bool { diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go index a7fe5c7..3057e75 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -317,7 +317,7 @@ func (o *rebuilder) resolveTreeAugments(ctx context.Context, listsWithDistances counts := make(map[btrfsvol.LogicalAddr]int) for _, list := range lists { for item := range list { - counts[item] = counts[item] + 1 + counts[item]++ } } diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_graph.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_graph.go index df5ae71..bf4c95d 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_graph.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild_graph.go @@ -112,13 +112,13 @@ func handleItem(o rebuildCallbacks, ctx context.Context, treeID btrfsprim.ObjID, case btrfsitem.Empty: // nothing case btrfsitem.Extent: - //if body.Head.Flags.Has(btrfsitem.EXTENT_FLAG_TREE_BLOCK) { - // // Supposedly this flag indicates that - // // body.Info.Key identifies a node by the - // // first key in the node. But nothing in the - // // kernel ever reads this, so who knows if it - // // always gets updated correctly? - //} + // if body.Head.Flags.Has(btrfsitem.EXTENT_FLAG_TREE_BLOCK) { + // // Supposedly this flag indicates that + // // body.Info.Key identifies a node by the + // // first key in the node. But nothing in the + // // kernel ever reads this, so who knows if it + // // always gets updated correctly? + // } for i, ref := range body.Refs { switch refBody := ref.Body.(type) { case nil: diff --git a/lib/containers/intervaltree.go b/lib/containers/intervaltree.go index 424b297..16bc916 100644 --- a/lib/containers/intervaltree.go +++ b/lib/containers/intervaltree.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -139,4 +139,4 @@ func (t *IntervalTree[K, V]) SearchAll(fn func(K) int) []V { return ret } -//func (t *IntervalTree[K, V]) Walk(fn func(*RBNode[V]) error) error +// TODO: func (t *IntervalTree[K, V]) Walk(fn func(*RBNode[V]) error) error diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go index 17bb3e3..f922a7b 100644 --- a/lib/containers/rbtree.go +++ b/lib/containers/rbtree.go @@ -338,11 +338,12 @@ func (t *RBTree[K, V]) Insert(val V) { Parent: parent, Value: val, } - if parent == nil { + switch { + case parent == nil: t.root = node - } else if key.Cmp(t.KeyFn(parent.Value)) < 0 { + case key.Cmp(t.KeyFn(parent.Value)) < 0: parent.Left = node - } else { + default: parent.Right = node } t.updateAttr(node) diff --git a/lib/containers/rbtree_test.go b/lib/containers/rbtree_test.go index a487c52..e42410e 100644 --- a/lib/containers/rbtree_test.go +++ b/lib/containers/rbtree_test.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -39,13 +39,7 @@ func (node *RBNode[V]) asciiArt(w io.Writer, u, m, l string) { } node.Right.asciiArt(w, u+" ", u+" ,--", u+" | ") - - if node.Color == Red { - fmt.Fprintf(w, "%s%v\n", m, node) - } else { - fmt.Fprintf(w, "%s%v\n", m, node) - } - + fmt.Fprintf(w, "%s%v\n", m, node) node.Left.asciiArt(w, l+" | ", l+" `--", l+" ") } -- cgit v1.2.3-54-g00ecf From c971b863a0a1f9feb75f31657729db3a03da114c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 19:46:28 -0700 Subject: cmd/btrfs-rec: Add a type-agnostic utility function to write JSON out --- cmd/btrfs-rec/inspect_rebuildmappings.go | 26 ++++++-------------------- cmd/btrfs-rec/inspect_rebuildnodes.go | 27 +++++---------------------- cmd/btrfs-rec/inspect_scandevices.go | 26 ++++++-------------------- cmd/btrfs-rec/util.go | 13 ++++++++++++- 4 files changed, 29 insertions(+), 63 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/cmd/btrfs-rec/inspect_rebuildmappings.go b/cmd/btrfs-rec/inspect_rebuildmappings.go index da7d12e..b805fc3 100644 --- a/cmd/btrfs-rec/inspect_rebuildmappings.go +++ b/cmd/btrfs-rec/inspect_rebuildmappings.go @@ -1,12 +1,10 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later package main import ( - "bufio" - "io" "os" "git.lukeshu.com/go/lowmemjson" @@ -49,7 +47,11 @@ func init() { } dlog.Infof(ctx, "Writing reconstructed mappings to stdout...") - if err := writeMappingsJSON(os.Stdout, fs); err != nil { + if err := writeJSONFile(os.Stdout, fs, lowmemjson.ReEncoder{ + Indent: "\t", + ForceTrailingNewlines: true, + CompactIfUnder: 120, + }); err != nil { return err } dlog.Info(ctx, "... done writing") @@ -58,19 +60,3 @@ func init() { }, }) } - -func writeMappingsJSON(w io.Writer, fs *btrfs.FS) (err error) { - buffer := bufio.NewWriter(w) - defer func() { - if _err := buffer.Flush(); err == nil && _err != nil { - err = _err - } - }() - return lowmemjson.Encode(&lowmemjson.ReEncoder{ - Out: buffer, - - Indent: "\t", - ForceTrailingNewlines: true, - CompactIfUnder: 120, - }, fs.LV.Mappings()) -} diff --git a/cmd/btrfs-rec/inspect_rebuildnodes.go b/cmd/btrfs-rec/inspect_rebuildnodes.go index 0f3d60e..e61e6d2 100644 --- a/cmd/btrfs-rec/inspect_rebuildnodes.go +++ b/cmd/btrfs-rec/inspect_rebuildnodes.go @@ -1,12 +1,10 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later package main import ( - "bufio" - "io" "os" "git.lukeshu.com/go/lowmemjson" @@ -15,11 +13,8 @@ import ( "github.com/spf13/cobra" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" - "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim" - "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect" "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildnodes" - "git.lukeshu.com/btrfs-progs-ng/lib/containers" ) func init() { @@ -44,7 +39,10 @@ func init() { } dlog.Info(ctx, "Writing re-built nodes to stdout...") - if err := writeNodesJSON(os.Stdout, rebuiltNodes); err != nil { + if err := writeJSONFile(os.Stdout, rebuiltNodes, lowmemjson.ReEncoder{ + Indent: "\t", + ForceTrailingNewlines: true, + }); err != nil { return err } dlog.Info(ctx, "... done writing") @@ -53,18 +51,3 @@ func init() { }, }) } - -func writeNodesJSON(w io.Writer, rebuiltNodes map[btrfsprim.ObjID]containers.Set[btrfsvol.LogicalAddr]) (err error) { - buffer := bufio.NewWriter(w) - defer func() { - if _err := buffer.Flush(); err == nil && _err != nil { - err = _err - } - }() - return lowmemjson.Encode(&lowmemjson.ReEncoder{ - Out: buffer, - - Indent: "\t", - ForceTrailingNewlines: true, - }, rebuiltNodes) -} diff --git a/cmd/btrfs-rec/inspect_scandevices.go b/cmd/btrfs-rec/inspect_scandevices.go index 7235e45..bca1b13 100644 --- a/cmd/btrfs-rec/inspect_scandevices.go +++ b/cmd/btrfs-rec/inspect_scandevices.go @@ -1,12 +1,10 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later package main import ( - "bufio" - "io" "os" "git.lukeshu.com/go/lowmemjson" @@ -33,7 +31,11 @@ func init() { } dlog.Info(ctx, "Writing scan results to stdout...") - if err := writeScanResults(os.Stdout, results); err != nil { + if err := writeJSONFile(os.Stdout, results, lowmemjson.ReEncoder{ + Indent: "\t", + ForceTrailingNewlines: true, + CompactIfUnder: 16, + }); err != nil { return err } dlog.Info(ctx, "... done writing") @@ -42,19 +44,3 @@ func init() { }, }) } - -func writeScanResults(w io.Writer, results btrfsinspect.ScanDevicesResult) (err error) { - buffer := bufio.NewWriter(w) - defer func() { - if _err := buffer.Flush(); err == nil && _err != nil { - err = _err - } - }() - return lowmemjson.Encode(&lowmemjson.ReEncoder{ - Out: buffer, - - Indent: "\t", - ForceTrailingNewlines: true, - CompactIfUnder: 16, - }, results) -} diff --git a/cmd/btrfs-rec/util.go b/cmd/btrfs-rec/util.go index adfe97e..ed1b75d 100644 --- a/cmd/btrfs-rec/util.go +++ b/cmd/btrfs-rec/util.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -81,3 +81,14 @@ func readJSONFile[T any](ctx context.Context, filename string) (T, error) { _ = buf.Close() return ret, nil } + +func writeJSONFile(w io.Writer, obj any, cfg lowmemjson.ReEncoder) (err error) { + buffer := bufio.NewWriter(w) + defer func() { + if _err := buffer.Flush(); err == nil && _err != nil { + err = _err + } + }() + cfg.Out = buffer + return lowmemjson.Encode(&cfg, obj) +} -- cgit v1.2.3-54-g00ecf From b261c2a4f5f028c9d83cef208ccc7d829f841bad Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 31 Dec 2022 11:59:09 -0700 Subject: tree-wide: Annotate values that I might want to be tuning --- cmd/btrfs-rec/util.go | 2 +- lib/btrfs/io4_fs.go | 44 +++++++++++++--------- .../btrfsinspect/rebuildmappings/fuzzymatchsums.go | 2 +- .../rebuildnodes/btrees/rebuilt_btrees.go | 6 +-- .../btrfsinspect/rebuildnodes/graph/graph.go | 4 +- .../btrfsinspect/rebuildnodes/keyio/keyio.go | 3 +- .../btrfsinspect/rebuildnodes/rebuild.go | 4 +- lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go | 2 +- lib/btrfsprogs/btrfsinspect/scandevices.go | 2 +- lib/btrfsprogs/btrfsutil/open.go | 5 ++- lib/btrfsprogs/btrfsutil/skinny_paths.go | 3 +- lib/containers/lru.go | 26 ++----------- lib/textui/log_memstats.go | 2 +- lib/textui/tunable.go | 13 +++++++ 14 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 lib/textui/tunable.go (limited to 'cmd/btrfs-rec') diff --git a/cmd/btrfs-rec/util.go b/cmd/btrfs-rec/util.go index ed1b75d..ffc03cc 100644 --- a/cmd/btrfs-rec/util.go +++ b/cmd/btrfs-rec/util.go @@ -34,7 +34,7 @@ func newRuneScanner(ctx context.Context, fh *os.File) (*runeScanner, error) { progress: textui.Portion[int64]{ D: fi.Size(), }, - progressWriter: textui.NewProgress[textui.Portion[int64]](ctx, dlog.LogLevelInfo, 1*time.Second), + progressWriter: textui.NewProgress[textui.Portion[int64]](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)), reader: bufio.NewReader(fh), closer: fh, } diff --git a/lib/btrfs/io4_fs.go b/lib/btrfs/io4_fs.go index 3848ef1..41df3a8 100644 --- a/lib/btrfs/io4_fs.go +++ b/lib/btrfs/io4_fs.go @@ -22,6 +22,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/maps" "git.lukeshu.com/btrfs-progs-ng/lib/slices" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) type BareInode struct { @@ -69,32 +70,37 @@ type Subvolume struct { TreeID btrfsprim.ObjID NoChecksums bool - rootOnce sync.Once - rootVal btrfsitem.Root - rootErr error + initOnce sync.Once - bareInodeCache containers.LRUCache[btrfsprim.ObjID, *BareInode] - fullInodeCache containers.LRUCache[btrfsprim.ObjID, *FullInode] - dirCache containers.LRUCache[btrfsprim.ObjID, *Dir] - fileCache containers.LRUCache[btrfsprim.ObjID, *File] + rootVal btrfsitem.Root + rootErr error + + bareInodeCache *containers.LRUCache[btrfsprim.ObjID, *BareInode] + fullInodeCache *containers.LRUCache[btrfsprim.ObjID, *FullInode] + dirCache *containers.LRUCache[btrfsprim.ObjID, *Dir] + fileCache *containers.LRUCache[btrfsprim.ObjID, *File] } func (sv *Subvolume) init() { - sv.rootOnce.Do(func() { + sv.initOnce.Do(func() { root, err := sv.FS.TreeSearch(btrfsprim.ROOT_TREE_OBJECTID, btrfstree.RootItemSearchFn(sv.TreeID)) if err != nil { sv.rootErr = err - return + } else { + switch rootBody := root.Body.(type) { + case btrfsitem.Root: + sv.rootVal = rootBody + case btrfsitem.Error: + sv.rootErr = fmt.Errorf("FS_TREE ROOT_ITEM has malformed body: %w", rootBody.Err) + default: + panic(fmt.Errorf("should not happen: ROOT_ITEM has unexpected item type: %T", rootBody)) + } } - switch rootBody := root.Body.(type) { - case btrfsitem.Root: - sv.rootVal = rootBody - case btrfsitem.Error: - sv.rootErr = fmt.Errorf("FS_TREE ROOT_ITEM has malformed body: %w", rootBody.Err) - default: - panic(fmt.Errorf("should not happen: ROOT_ITEM has unexpected item type: %T", rootBody)) - } + sv.bareInodeCache = containers.NewLRUCache[btrfsprim.ObjID, *BareInode](textui.Tunable(128)) + sv.fullInodeCache = containers.NewLRUCache[btrfsprim.ObjID, *FullInode](textui.Tunable(128)) + sv.dirCache = containers.NewLRUCache[btrfsprim.ObjID, *Dir](textui.Tunable(128)) + sv.fileCache = containers.NewLRUCache[btrfsprim.ObjID, *File](textui.Tunable(128)) }) } @@ -104,6 +110,7 @@ func (sv *Subvolume) GetRootInode() (btrfsprim.ObjID, error) { } func (sv *Subvolume) LoadBareInode(inode btrfsprim.ObjID) (*BareInode, error) { + sv.init() val := sv.bareInodeCache.GetOrElse(inode, func() (val *BareInode) { val = &BareInode{ Inode: inode, @@ -136,6 +143,7 @@ func (sv *Subvolume) LoadBareInode(inode btrfsprim.ObjID) (*BareInode, error) { } func (sv *Subvolume) LoadFullInode(inode btrfsprim.ObjID) (*FullInode, error) { + sv.init() val := sv.fullInodeCache.GetOrElse(inode, func() (val *FullInode) { val = &FullInode{ BareInode: BareInode{ @@ -191,6 +199,7 @@ func (sv *Subvolume) LoadFullInode(inode btrfsprim.ObjID) (*FullInode, error) { } func (sv *Subvolume) LoadDir(inode btrfsprim.ObjID) (*Dir, error) { + sv.init() val := sv.dirCache.GetOrElse(inode, func() (val *Dir) { val = new(Dir) fullInode, err := sv.LoadFullInode(inode) @@ -326,6 +335,7 @@ func (dir *Dir) AbsPath() (string, error) { } func (sv *Subvolume) LoadFile(inode btrfsprim.ObjID) (*File, error) { + sv.init() val := sv.fileCache.GetOrElse(inode, func() (val *File) { val = new(File) fullInode, err := sv.LoadFullInode(inode) diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go index b1be7ba..a8d05eb 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go @@ -19,7 +19,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) -const minFuzzyPct = 0.5 +var minFuzzyPct = textui.Tunable(0.5) type fuzzyRecord struct { PAddr btrfsvol.QualifiedPhysicalAddr diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/rebuilt_btrees.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/rebuilt_btrees.go index 33eb352..5e8883a 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/rebuilt_btrees.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/rebuilt_btrees.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -191,7 +191,7 @@ func (ts *RebuiltTrees) AddRoot(ctx context.Context, treeID btrfsprim.ObjID, roo var stats rootStats stats.Leafs.D = len(tree.leafToRoots) - progressWriter := textui.NewProgress[rootStats](ctx, dlog.LogLevelInfo, 1*time.Second) + progressWriter := textui.NewProgress[rootStats](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) for i, leaf := range maps.SortedKeys(tree.leafToRoots) { stats.Leafs.N = i progressWriter.Set(stats) @@ -297,7 +297,7 @@ func (tree *rebuiltTree) indexLeafs(ctx context.Context, graph pkggraph.Graph) { var stats textui.Portion[int] stats.D = len(graph.Nodes) - progressWriter := textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, 1*time.Second) + progressWriter := textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) progress := func() { stats.N = len(nodeToRoots) progressWriter.Set(stats) diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/graph/graph.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/graph/graph.go index c4ed675..cf86d74 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/graph/graph.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/graph/graph.go @@ -197,7 +197,7 @@ func (g Graph) FinalCheck(ctx context.Context, fs diskio.File[btrfsvol.LogicalAd ctx = dlog.WithField(_ctx, "btrfsinspect.rebuild-nodes.read.substep", "check-keypointers") dlog.Info(_ctx, "Checking keypointers for dead-ends...") - progressWriter := textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, 1*time.Second) + progressWriter := textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) stats.D = len(g.EdgesTo) progressWriter.Set(stats) for laddr := range g.EdgesTo { @@ -221,7 +221,7 @@ func (g Graph) FinalCheck(ctx context.Context, fs diskio.File[btrfsvol.LogicalAd dlog.Info(_ctx, "Checking for btree loops...") stats.D = len(g.Nodes) stats.N = 0 - progressWriter = textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, 1*time.Second) + progressWriter = textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) progressWriter.Set(stats) visited := make(containers.Set[btrfsvol.LogicalAddr], len(g.Nodes)) numLoops := 0 diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go index b1e68f9..24c3dcf 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go @@ -17,6 +17,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildnodes/graph" "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/diskio" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) type ItemPtr struct { @@ -50,7 +51,7 @@ func NewHandle(file diskio.File[btrfsvol.LogicalAddr], sb btrfstree.Superblock) Sizes: make(map[ItemPtr]SizeAndErr), - cache: containers.NewLRUCache[btrfsvol.LogicalAddr, *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]](8), + cache: containers.NewLRUCache[btrfsvol.LogicalAddr, *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]](textui.Tunable(8)), } } diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go index 5d0804c..0a3ccdf 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go @@ -126,7 +126,7 @@ func (o *rebuilder) rebuild(_ctx context.Context) error { o.itemQueue = nil var progress textui.Portion[int] progress.D = len(itemQueue) - progressWriter := textui.NewProgress[textui.Portion[int]](stepCtx, dlog.LogLevelInfo, 1*time.Second) + progressWriter := textui.NewProgress[textui.Portion[int]](stepCtx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) stepCtx = dlog.WithField(stepCtx, "btrfsinspect.rebuild-nodes.rebuild.substep.progress", &progress) for i, key := range itemQueue { itemCtx := dlog.WithField(stepCtx, "btrfsinspect.rebuild-nodes.rebuild.process.item", key) @@ -160,7 +160,7 @@ func (o *rebuilder) rebuild(_ctx context.Context) error { progress.D += len(resolvedAugments[treeID]) } o.augmentQueue = make(map[btrfsprim.ObjID][]map[btrfsvol.LogicalAddr]int) - progressWriter = textui.NewProgress[textui.Portion[int]](stepCtx, dlog.LogLevelInfo, 1*time.Second) + progressWriter = textui.NewProgress[textui.Portion[int]](stepCtx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) stepCtx = dlog.WithField(stepCtx, "btrfsinspect.rebuild-nodes.rebuild.substep.progress", &progress) for _, treeID := range maps.SortedKeys(resolvedAugments) { treeCtx := dlog.WithField(stepCtx, "btrfsinspect.rebuild-nodes.rebuild.augment.tree", treeID) diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go index 7a112b4..7e96e29 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go @@ -32,7 +32,7 @@ func ScanDevices(ctx context.Context, fs *btrfs.FS, scanResults btrfsinspect.Sca stats.D = countNodes(scanResults) progressWriter := textui.NewProgress[textui.Portion[int]]( dlog.WithField(ctx, "btrfsinspect.rebuild-nodes.read.substep", "read-nodes"), - dlog.LogLevelInfo, 1*time.Second) + dlog.LogLevelInfo, textui.Tunable(1*time.Second)) nodeGraph := graph.New(*sb) keyIO := keyio.NewHandle(fs, *sb) diff --git a/lib/btrfsprogs/btrfsinspect/scandevices.go b/lib/btrfsprogs/btrfsinspect/scandevices.go index 4058663..7668a83 100644 --- a/lib/btrfsprogs/btrfsinspect/scandevices.go +++ b/lib/btrfsprogs/btrfsinspect/scandevices.go @@ -126,7 +126,7 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfstree.Superblo var sums strings.Builder sums.Grow(numSums * csumSize) - progressWriter := textui.NewProgress[scanStats](ctx, dlog.LogLevelInfo, 1*time.Second) + progressWriter := textui.NewProgress[scanStats](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)) progress := func(pos btrfsvol.PhysicalAddr) { progressWriter.Set(scanStats{ Portion: textui.Portion[btrfsvol.PhysicalAddr]{ diff --git a/lib/btrfsprogs/btrfsutil/open.go b/lib/btrfsprogs/btrfsutil/open.go index 9491a20..441d4e2 100644 --- a/lib/btrfsprogs/btrfsutil/open.go +++ b/lib/btrfsprogs/btrfsutil/open.go @@ -14,6 +14,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/diskio" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) func Open(ctx context.Context, flag int, filenames ...string) (*btrfs.FS, error) { @@ -30,8 +31,8 @@ func Open(ctx context.Context, flag int, filenames ...string) (*btrfs.FS, error) } bufFile := diskio.NewBufferedFile[btrfsvol.PhysicalAddr]( typedFile, - 16384, // block size: 16KiB - 1024, // number of blocks to buffer; total of 16MiB + textui.Tunable[btrfsvol.PhysicalAddr](16384), // block size: 16KiB + textui.Tunable(1024), // number of blocks to buffer; total of 16MiB ) devFile := &btrfs.Device{ File: bufFile, diff --git a/lib/btrfsprogs/btrfsutil/skinny_paths.go b/lib/btrfsprogs/btrfsutil/skinny_paths.go index 2f71968..6a51739 100644 --- a/lib/btrfsprogs/btrfsutil/skinny_paths.go +++ b/lib/btrfsprogs/btrfsutil/skinny_paths.go @@ -11,6 +11,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/diskio" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) type skinnyItem struct { @@ -42,7 +43,7 @@ func (a *SkinnyPathArena) init() { // which is a good number for me. Then I tought it to do a // better job of recovering trees, and so the memory grew, and I // cut it to 64K. Then to 8K. Then grew it to 128K. - a.fatItems = containers.NewLRUCache[skinnyItem, btrfstree.TreePathElem](128 * 1024) + a.fatItems = containers.NewLRUCache[skinnyItem, btrfstree.TreePathElem](textui.Tunable(128 * 1024)) } } diff --git a/lib/containers/lru.go b/lib/containers/lru.go index bfda361..aa372ed 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -5,45 +5,30 @@ package containers import ( - "sync" - lru "github.com/hashicorp/golang-lru" ) // LRUCache is a least-recently-used(ish) cache. A zero LRUCache is -// usable and has a cache size of 128 items; use NewLRUCache to set a -// different size. +// not usable; it must be initialized with NewLRUCache. type LRUCache[K comparable, V any] struct { - initOnce sync.Once - inner *lru.ARCCache + inner *lru.ARCCache } func NewLRUCache[K comparable, V any](size int) *LRUCache[K, V] { c := new(LRUCache[K, V]) - c.initOnce.Do(func() { - c.inner, _ = lru.NewARC(size) - }) + c.inner, _ = lru.NewARC(size) return c } -func (c *LRUCache[K, V]) init() { - c.initOnce.Do(func() { - c.inner, _ = lru.NewARC(128) - }) -} - func (c *LRUCache[K, V]) Add(key K, value V) { - c.init() c.inner.Add(key, value) } func (c *LRUCache[K, V]) Contains(key K) bool { - c.init() return c.inner.Contains(key) } func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { - c.init() _value, ok := c.inner.Get(key) if ok { //nolint:forcetypeassert // Typed wrapper around untyped lib. @@ -53,7 +38,6 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { } func (c *LRUCache[K, V]) Keys() []K { - c.init() untyped := c.inner.Keys() typed := make([]K, len(untyped)) for i := range untyped { @@ -64,12 +48,10 @@ func (c *LRUCache[K, V]) Keys() []K { } func (c *LRUCache[K, V]) Len() int { - c.init() return c.inner.Len() } func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { - c.init() _value, ok := c.inner.Peek(key) if ok { //nolint:forcetypeassert // Typed wrapper around untyped lib. @@ -79,12 +61,10 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { } func (c *LRUCache[K, V]) Purge() { - c.init() c.inner.Purge() } func (c *LRUCache[K, V]) Remove(key K) { - c.init() c.inner.Remove(key) } diff --git a/lib/textui/log_memstats.go b/lib/textui/log_memstats.go index 39733c6..6e3c3a1 100644 --- a/lib/textui/log_memstats.go +++ b/lib/textui/log_memstats.go @@ -19,7 +19,7 @@ type LiveMemUse struct { var _ fmt.Stringer = (*LiveMemUse)(nil) -const liveMemUseUpdateInterval = 1 * time.Second +var liveMemUseUpdateInterval = Tunable(1 * time.Second) func (o *LiveMemUse) String() string { o.mu.Lock() diff --git a/lib/textui/tunable.go b/lib/textui/tunable.go new file mode 100644 index 0000000..7e5f311 --- /dev/null +++ b/lib/textui/tunable.go @@ -0,0 +1,13 @@ +// Copyright (C) 2022 Luke Shumaker +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package textui + +// Tunable annotates a value as something that might want to be tuned +// as the program gets optimized. +// +// TODO(lukeshu): Have Tunable be runtime-configurable. +func Tunable[T any](x T) T { + return x +} -- cgit v1.2.3-54-g00ecf From 97fa22c161056c289a9978f20e3fb6b05d779a23 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 20:48:22 -0700 Subject: lint: Turn on gomnd --- .golangci.yml | 7 +- cmd/btrfs-rec/inspect_lstrees.go | 2 +- cmd/btrfs-rec/inspect_rebuildmappings.go | 2 +- cmd/btrfs-rec/inspect_scandevices.go | 2 +- lib/binstruct/binint/builtins.go | 117 +++++++++++---------- lib/binstruct/size.go | 15 ++- lib/btrfs/btrfsitem/statmode.go | 4 +- lib/btrfs/btrfsprim/objid.go | 3 +- lib/btrfs/btrfsprim/uuid.go | 3 +- lib/btrfs/btrfssum/csum.go | 3 +- lib/btrfs/btrfssum/shortsum.go | 5 +- lib/btrfs/btrfstree/types_node.go | 10 +- lib/btrfsprogs/btrfsinspect/mount.go | 2 +- .../btrfsinspect/rebuildmappings/fuzzymatchsums.go | 4 +- .../btrfsinspect/rebuildmappings/matchsums.go | 7 +- .../rebuildmappings/rebuildmappings.go | 9 +- lib/btrfsprogs/btrfsutil/open.go | 7 +- 17 files changed, 115 insertions(+), 87 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/.golangci.yml b/.golangci.yml index edb8499..f412141 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,7 +35,6 @@ linters: - godot - godox - goerr113 - - gomnd - gomoddirectives - ireturn - lll @@ -70,6 +69,12 @@ linters-settings: - appendAssign gofmt: simplify: true + gomnd: + ignored-numbers: + - '2' + ignored-functions: + - 'binutil.NeedNBytes' + - 'textui.Tunable' nolintlint: require-explanation: true require-specific: true diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go index a6d86eb..0f70cd1 100644 --- a/cmd/btrfs-rec/inspect_lstrees.go +++ b/cmd/btrfs-rec/inspect_lstrees.go @@ -54,7 +54,7 @@ func init() { } numWidth := len(strconv.Itoa(slices.Max(treeErrCnt, totalItems))) - table := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) + table := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) //nolint:gomnd // This is what looks Nice. textui.Fprintf(table, " errors\t% *s\n", numWidth, strconv.Itoa(treeErrCnt)) for _, typ := range maps.SortedKeys(treeItemCnt) { textui.Fprintf(table, " %v items\t% *s\n", typ, numWidth, strconv.Itoa(treeItemCnt[typ])) diff --git a/cmd/btrfs-rec/inspect_rebuildmappings.go b/cmd/btrfs-rec/inspect_rebuildmappings.go index b805fc3..4555e58 100644 --- a/cmd/btrfs-rec/inspect_rebuildmappings.go +++ b/cmd/btrfs-rec/inspect_rebuildmappings.go @@ -50,7 +50,7 @@ func init() { if err := writeJSONFile(os.Stdout, fs, lowmemjson.ReEncoder{ Indent: "\t", ForceTrailingNewlines: true, - CompactIfUnder: 120, + CompactIfUnder: 120, //nolint:gomnd // This is what looks Nice. }); err != nil { return err } diff --git a/cmd/btrfs-rec/inspect_scandevices.go b/cmd/btrfs-rec/inspect_scandevices.go index bca1b13..410fa4f 100644 --- a/cmd/btrfs-rec/inspect_scandevices.go +++ b/cmd/btrfs-rec/inspect_scandevices.go @@ -34,7 +34,7 @@ func init() { if err := writeJSONFile(os.Stdout, results, lowmemjson.ReEncoder{ Indent: "\t", ForceTrailingNewlines: true, - CompactIfUnder: 16, + CompactIfUnder: 16, //nolint:gomnd // This is what looks Nice. }); err != nil { return err } diff --git a/lib/binstruct/binint/builtins.go b/lib/binstruct/binint/builtins.go index e4be2a6..cfd0fc2 100644 --- a/lib/binstruct/binint/builtins.go +++ b/lib/binstruct/binint/builtins.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -10,242 +10,249 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil" ) +const ( + sizeof8 = 1 + sizeof16 = 2 + sizeof32 = 4 + sizeof64 = 8 +) + // unsigned type U8 uint8 -func (U8) BinaryStaticSize() int { return 1 } +func (U8) BinaryStaticSize() int { return sizeof8 } func (x U8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil } func (x *U8) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 1); err != nil { + if err := binutil.NeedNBytes(dat, sizeof8); err != nil { return 0, err } *x = U8(dat[0]) - return 1, nil + return sizeof8, nil } // unsigned little endian type U16le uint16 -func (U16le) BinaryStaticSize() int { return 2 } +func (U16le) BinaryStaticSize() int { return sizeof16 } func (x U16le) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.LittleEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *U16le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = U16le(binary.LittleEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type U32le uint32 -func (U32le) BinaryStaticSize() int { return 4 } +func (U32le) BinaryStaticSize() int { return sizeof32 } func (x U32le) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.LittleEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *U32le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = U32le(binary.LittleEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type U64le uint64 -func (U64le) BinaryStaticSize() int { return 8 } +func (U64le) BinaryStaticSize() int { return sizeof64 } func (x U64le) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.LittleEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *U64le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = U64le(binary.LittleEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } // unsigned big endian type U16be uint16 -func (U16be) BinaryStaticSize() int { return 2 } +func (U16be) BinaryStaticSize() int { return sizeof16 } func (x U16be) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.BigEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *U16be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = U16be(binary.BigEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type U32be uint32 -func (U32be) BinaryStaticSize() int { return 4 } +func (U32be) BinaryStaticSize() int { return sizeof32 } func (x U32be) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.BigEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *U32be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = U32be(binary.BigEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type U64be uint64 -func (U64be) BinaryStaticSize() int { return 8 } +func (U64be) BinaryStaticSize() int { return sizeof64 } func (x U64be) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.BigEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *U64be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = U64be(binary.BigEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } // signed type I8 int8 -func (I8) BinaryStaticSize() int { return 1 } +func (I8) BinaryStaticSize() int { return sizeof8 } func (x I8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil } func (x *I8) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 1); err != nil { + if err := binutil.NeedNBytes(dat, sizeof8); err != nil { return 0, err } *x = I8(dat[0]) - return 1, nil + return sizeof8, nil } // signed little endian type I16le int16 -func (I16le) BinaryStaticSize() int { return 2 } +func (I16le) BinaryStaticSize() int { return sizeof16 } func (x I16le) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.LittleEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *I16le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = I16le(binary.LittleEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type I32le int32 -func (I32le) BinaryStaticSize() int { return 4 } +func (I32le) BinaryStaticSize() int { return sizeof32 } func (x I32le) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.LittleEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *I32le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = I32le(binary.LittleEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type I64le int64 -func (I64le) BinaryStaticSize() int { return 8 } +func (I64le) BinaryStaticSize() int { return sizeof64 } func (x I64le) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.LittleEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *I64le) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = I64le(binary.LittleEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } // signed big endian type I16be int16 -func (I16be) BinaryStaticSize() int { return 2 } +func (I16be) BinaryStaticSize() int { return sizeof16 } func (x I16be) MarshalBinary() ([]byte, error) { - var buf [2]byte + var buf [sizeof16]byte binary.BigEndian.PutUint16(buf[:], uint16(x)) return buf[:], nil } func (x *I16be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 2); err != nil { + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { return 0, err } *x = I16be(binary.BigEndian.Uint16(dat)) - return 2, nil + return sizeof16, nil } type I32be int32 -func (I32be) BinaryStaticSize() int { return 4 } +func (I32be) BinaryStaticSize() int { return sizeof32 } func (x I32be) MarshalBinary() ([]byte, error) { - var buf [4]byte + var buf [sizeof32]byte binary.BigEndian.PutUint32(buf[:], uint32(x)) return buf[:], nil } func (x *I32be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 4); err != nil { + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { return 0, err } *x = I32be(binary.BigEndian.Uint32(dat)) - return 4, nil + return sizeof32, nil } type I64be int64 -func (I64be) BinaryStaticSize() int { return 8 } +func (I64be) BinaryStaticSize() int { return sizeof64 } func (x I64be) MarshalBinary() ([]byte, error) { - var buf [8]byte + var buf [sizeof64]byte binary.BigEndian.PutUint64(buf[:], uint64(x)) return buf[:], nil } func (x *I64be) UnmarshalBinary(dat []byte) (int, error) { - if err := binutil.NeedNBytes(dat, 8); err != nil { + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { return 0, err } *x = I64be(binary.BigEndian.Uint64(dat)) - return 8, nil + return sizeof64, nil } diff --git a/lib/binstruct/size.go b/lib/binstruct/size.go index 52fa380..d6d70c6 100644 --- a/lib/binstruct/size.go +++ b/lib/binstruct/size.go @@ -28,6 +28,13 @@ var ( unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() ) +const ( + sizeof8 = 1 + sizeof16 = 2 + sizeof32 = 4 + sizeof64 = 8 +) + func staticSize(typ reflect.Type) (int, error) { if typ.Implements(staticSizerType) { //nolint:forcetypeassert // Already did a type check via reflection. @@ -43,13 +50,13 @@ func staticSize(typ reflect.Type) (int, error) { } switch typ.Kind() { case reflect.Uint8, reflect.Int8: - return 1, nil + return sizeof8, nil case reflect.Uint16, reflect.Int16: - return 2, nil + return sizeof16, nil case reflect.Uint32, reflect.Int32: - return 4, nil + return sizeof32, nil case reflect.Uint64, reflect.Int64: - return 8, nil + return sizeof64, nil case reflect.Ptr: return staticSize(typ.Elem()) case reflect.Array: diff --git a/lib/btrfs/btrfsitem/statmode.go b/lib/btrfs/btrfsitem/statmode.go index a1302ee..557b688 100644 --- a/lib/btrfs/btrfsitem/statmode.go +++ b/lib/btrfs/btrfsitem/statmode.go @@ -1,5 +1,5 @@ // Copyright (C) 2020-2021 Ambassador Labs -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: Apache-2.0 // @@ -9,7 +9,6 @@ package btrfsitem type StatMode uint32 -//nolint:deadcode,varcheck // not all of these modes will be used const ( // 16 bits = 5⅓ octal characters @@ -73,6 +72,7 @@ func (mode StatMode) IsRegular() bool { // 's' (GNU `ls` behavior; though POSIX notes that many // implementations use '=' for sockets). func (mode StatMode) String() string { + //nolint:gomnd // Magic numbers is all this is. buf := [10]byte{ // type: This string is easy; it directly pairs with // the above ModeFmtXXX list above; the character in diff --git a/lib/btrfs/btrfsprim/objid.go b/lib/btrfs/btrfsprim/objid.go index 17a0eeb..5ba213d 100644 --- a/lib/btrfs/btrfsprim/objid.go +++ b/lib/btrfs/btrfsprim/objid.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -68,6 +68,7 @@ func (id ObjID) Format(typ ItemType) string { case DEV_EXTENT_KEY: return fmt.Sprintf("%d", int64(id)) case QGROUP_RELATION_KEY: + //nolint:gomnd // TODO: I'm not sure what the 48/16 bit split means. return fmt.Sprintf("%d/%d", uint64(id)>>48, uint64(id)&((1<<48)-1)) diff --git a/lib/btrfs/btrfsprim/uuid.go b/lib/btrfs/btrfsprim/uuid.go index 4e3fd6b..0103ee4 100644 --- a/lib/btrfs/btrfsprim/uuid.go +++ b/lib/btrfs/btrfsprim/uuid.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -56,6 +56,7 @@ func (a UUID) Cmp(b UUID) int { return 0 } +//nolint:gomnd // This is all magic numbers. func ParseUUID(str string) (UUID, error) { var ret UUID j := 0 diff --git a/lib/btrfs/btrfssum/csum.go b/lib/btrfs/btrfssum/csum.go index 770f6ea..6df9efd 100644 --- a/lib/btrfs/btrfssum/csum.go +++ b/lib/btrfs/btrfssum/csum.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -70,6 +70,7 @@ func (typ CSumType) String() string { } func (typ CSumType) Size() int { + //nolint:gomnd // This is where we define the magic numbers. sizes := map[CSumType]int{ TYPE_CRC32: 4, TYPE_XXHASH: 8, diff --git a/lib/btrfs/btrfssum/shortsum.go b/lib/btrfs/btrfssum/shortsum.go index 6fd0c68..aaf7a89 100644 --- a/lib/btrfs/btrfssum/shortsum.go +++ b/lib/btrfs/btrfssum/shortsum.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -56,10 +56,11 @@ func (sum ShortSum) EncodeJSON(w io.Writer) error { } func deHex(r rune) (byte, bool) { - if r > 0xff { + if r > math.MaxUint8 { return 0, false } c := byte(r) + //nolint:gomnd // Hex conversion. switch { case '0' <= c && c <= '9': return c - '0', true diff --git a/lib/btrfs/btrfstree/types_node.go b/lib/btrfs/btrfstree/types_node.go index b709d34..a26215b 100644 --- a/lib/btrfs/btrfstree/types_node.go +++ b/lib/btrfs/btrfstree/types_node.go @@ -23,21 +23,23 @@ import ( type NodeFlags uint64 +const sizeofNodeFlags = 7 + func (NodeFlags) BinaryStaticSize() int { - return 7 + return sizeofNodeFlags } func (f NodeFlags) MarshalBinary() ([]byte, error) { var bs [8]byte binary.LittleEndian.PutUint64(bs[:], uint64(f)) - return bs[:7], nil + return bs[:sizeofNodeFlags], nil } func (f *NodeFlags) UnmarshalBinary(dat []byte) (int, error) { var bs [8]byte - copy(bs[:7], dat[:7]) + copy(bs[:sizeofNodeFlags], dat[:sizeofNodeFlags]) *f = NodeFlags(binary.LittleEndian.Uint64(bs[:])) - return 7, nil + return sizeofNodeFlags, nil } var ( diff --git a/lib/btrfsprogs/btrfsinspect/mount.go b/lib/btrfsprogs/btrfsinspect/mount.go index f061526..d10037d 100644 --- a/lib/btrfsprogs/btrfsinspect/mount.go +++ b/lib/btrfsprogs/btrfsinspect/mount.go @@ -260,7 +260,7 @@ func (sv *subvolume) LookUpInode(_ context.Context, op *fuseops.LookUpInodeOp) e Child: 2, // an inode number that a real file will never have Attributes: fuseops.InodeAttributes{ Nlink: 1, - Mode: uint32(btrfsitem.ModeFmtDir | 0o700), + Mode: uint32(btrfsitem.ModeFmtDir | 0o700), //nolint:gomnd // TODO }, } return nil diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go index a8d05eb..9e6b864 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go @@ -112,8 +112,8 @@ func fuzzyMatchBlockGroupSums(ctx context.Context, if apply { lvl = dlog.LogLevelInfo } - dlog.Logf(ctx, lvl, "(%v/%v) blockgroup[laddr=%v] matches=[%s]; bestpossible=%v%% (based on %v runs)", - i+1, numBlockgroups, bgLAddr, matchesStr, int(100*bgRun.PctFull()), len(bgRun.Runs)) + dlog.Logf(ctx, lvl, "(%v/%v) blockgroup[laddr=%v] matches=[%s]; bestpossible=%v (based on %v runs)", + i+1, numBlockgroups, bgLAddr, matchesStr, number.Percent(bgRun.PctFull()), len(bgRun.Runs)) if !apply { continue } diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/matchsums.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/matchsums.go index be82f87..02c657f 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/matchsums.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/matchsums.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -8,6 +8,7 @@ import ( "context" "github.com/datawire/dlib/dlog" + "golang.org/x/text/number" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum" @@ -55,8 +56,8 @@ func matchBlockGroupSums(ctx context.Context, if len(matches) == 1 { lvl = dlog.LogLevelInfo } - dlog.Logf(ctx, lvl, "(%v/%v) blockgroup[laddr=%v] has %v matches based on %v%% coverage from %v runs", - i+1, numBlockgroups, bgLAddr, len(matches), int(100*bgRun.PctFull()), len(bgRun.Runs)) + dlog.Logf(ctx, lvl, "(%v/%v) blockgroup[laddr=%v] has %v matches based on %v coverage from %v runs", + i+1, numBlockgroups, bgLAddr, len(matches), number.Percent(bgRun.PctFull()), len(bgRun.Runs)) if len(matches) != 1 { continue } diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/rebuildmappings.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/rebuildmappings.go index 7311aca..665bc96 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/rebuildmappings.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/rebuildmappings.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -15,6 +15,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect" "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/maps" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) func getNodeSize(fs *btrfs.FS) (btrfsvol.AddrDelta, error) { @@ -189,20 +190,20 @@ func RebuildMappings(ctx context.Context, fs *btrfs.FS, scanResults btrfsinspect unmappedPhysical += region.End.Sub(region.Beg) } } - dlog.Infof(ctx, "... %d KiB of unmapped physical space (across %d regions)", int(unmappedPhysical/1024), numUnmappedPhysical) + dlog.Infof(ctx, "... %d of unmapped physical space (across %d regions)", textui.IEC(unmappedPhysical, "B"), numUnmappedPhysical) unmappedLogicalRegions := ListUnmappedLogicalRegions(fs, logicalSums) var unmappedLogical btrfsvol.AddrDelta for _, region := range unmappedLogicalRegions { unmappedLogical += region.Size() } - dlog.Infof(ctx, "... %d KiB of unmapped summed logical space (across %d regions)", int(unmappedLogical/1024), len(unmappedLogicalRegions)) + dlog.Infof(ctx, "... %d of unmapped summed logical space (across %d regions)", textui.IEC(unmappedLogical, "B"), len(unmappedLogicalRegions)) var unmappedBlockGroups btrfsvol.AddrDelta for _, bg := range bgs { unmappedBlockGroups += bg.Size } - dlog.Infof(ctx, "... %d KiB of unmapped block groups (across %d groups)", int(unmappedBlockGroups/1024), len(bgs)) + dlog.Infof(ctx, "... %d of unmapped block groups (across %d groups)", textui.IEC(unmappedBlockGroups, "B"), len(bgs)) dlog.Info(_ctx, "detailed report:") for _, devID := range maps.SortedKeys(unmappedPhysicalRegions) { diff --git a/lib/btrfsprogs/btrfsutil/open.go b/lib/btrfsprogs/btrfsutil/open.go index 441d4e2..c5ee314 100644 --- a/lib/btrfsprogs/btrfsutil/open.go +++ b/lib/btrfsprogs/btrfsutil/open.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -31,8 +31,9 @@ func Open(ctx context.Context, flag int, filenames ...string) (*btrfs.FS, error) } bufFile := diskio.NewBufferedFile[btrfsvol.PhysicalAddr]( typedFile, - textui.Tunable[btrfsvol.PhysicalAddr](16384), // block size: 16KiB - textui.Tunable(1024), // number of blocks to buffer; total of 16MiB + //nolint:gomnd // False positive: gomnd.ignored-functions=[textui.Tunable] doesn't support type params. + textui.Tunable[btrfsvol.PhysicalAddr](16*1024), // block size: 16KiB + textui.Tunable(1024), // number of blocks to buffer; total of 16MiB ) devFile := &btrfs.Device{ File: bufFile, -- cgit v1.2.3-54-g00ecf From 6912dad34102be1ddea5f3b7710abd0cc8922d20 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 22:13:14 -0700 Subject: lint: Turn on stylecheck --- .golangci.yml | 8 +++- cmd/btrfs-rec/main.go | 2 +- lib/binstruct/structs.go | 4 +- lib/btrfs/btrfsprim/misc.go | 6 +-- lib/btrfs/io4_fs.go | 90 ++++++++++++++++++++++----------------------- 5 files changed, 58 insertions(+), 52 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/.golangci.yml b/.golangci.yml index e8b7678..57f58a1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -42,7 +42,6 @@ linters: - nlreturn - nonamedreturns - revive - - stylecheck - tagliatelle - testpackage - thelper @@ -77,10 +76,17 @@ linters-settings: require-specific: true allow-no-explanation: - dupword + stylecheck: + checks: + - "all" + - "-ST1003" # CONST_VAL names for consistency with other btrfs code issues: exclude-rules: # Ignore false positives that don't actually have any words. - linters: [dupword] source: "^[^a-zA-Z]*$" + # https://github.com/dominikh/go-tools/issues/1347 + - linters: [stylecheck] + text: '^ST1016: methods on the same type should have the same receiver name \(seen 1x "(a|dst)", \d+x "[^"]+"\)$' max-issues-per-linter: 0 max-same-issues: 0 diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index 3f662bb..d9ab485 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -62,7 +62,7 @@ func main() { panic(err) } - var openFlag int = os.O_RDONLY + openFlag := os.O_RDONLY argparserInspect := &cobra.Command{ Use: "inspect {[flags]|SUBCOMMAND}", diff --git a/lib/binstruct/structs.go b/lib/binstruct/structs.go index 9bc556c..7eea600 100644 --- a/lib/binstruct/structs.go +++ b/lib/binstruct/structs.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -120,7 +120,7 @@ func genStructHandler(structInfo reflect.Type) (structHandler, error) { var curOffset, endOffset int for i := 0; i < structInfo.NumField(); i++ { - var fieldInfo reflect.StructField = structInfo.Field(i) + fieldInfo := structInfo.Field(i) if fieldInfo.Anonymous && fieldInfo.Type != endType { err := fmt.Errorf("binstruct does not support embedded fields") diff --git a/lib/btrfs/btrfsprim/misc.go b/lib/btrfs/btrfsprim/misc.go index 9b3a6f8..22939bf 100644 --- a/lib/btrfs/btrfsprim/misc.go +++ b/lib/btrfs/btrfsprim/misc.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -22,8 +22,8 @@ type Key struct { binstruct.End `bin:"off=0x11"` } -func (k Key) String() string { - return fmt.Sprintf("{%v %v %v}", k.ObjectID, k.ItemType, k.Offset) +func (key Key) String() string { + return fmt.Sprintf("{%v %v %v}", key.ObjectID, key.ItemType, key.Offset) } var MaxKey = Key{ diff --git a/lib/btrfs/io4_fs.go b/lib/btrfs/io4_fs.go index 41df3a8..799c865 100644 --- a/lib/btrfs/io4_fs.go +++ b/lib/btrfs/io4_fs.go @@ -218,16 +218,16 @@ func (sv *Subvolume) LoadDir(inode btrfsprim.ObjID) (*Dir, error) { return val, nil } -func (ret *Dir) populate() { - ret.ChildrenByName = make(map[string]btrfsitem.DirEntry) - ret.ChildrenByIndex = make(map[uint64]btrfsitem.DirEntry) - for _, item := range ret.OtherItems { +func (dir *Dir) populate() { + dir.ChildrenByName = make(map[string]btrfsitem.DirEntry) + dir.ChildrenByIndex = make(map[uint64]btrfsitem.DirEntry) + for _, item := range dir.OtherItems { switch item.Key.ItemType { case btrfsitem.INODE_REF_KEY: switch body := item.Body.(type) { case btrfsitem.InodeRefs: if len(body) != 1 { - ret.Errs = append(ret.Errs, fmt.Errorf("INODE_REF item with %d entries on a directory", + dir.Errs = append(dir.Errs, fmt.Errorf("INODE_REF item with %d entries on a directory", len(body))) continue } @@ -235,15 +235,15 @@ func (ret *Dir) populate() { Inode: btrfsprim.ObjID(item.Key.Offset), InodeRef: body[0], } - if ret.DotDot != nil { - if !reflect.DeepEqual(ref, *ret.DotDot) { - ret.Errs = append(ret.Errs, fmt.Errorf("multiple INODE_REF items on a directory")) + if dir.DotDot != nil { + if !reflect.DeepEqual(ref, *dir.DotDot) { + dir.Errs = append(dir.Errs, fmt.Errorf("multiple INODE_REF items on a directory")) } continue } - ret.DotDot = &ref + dir.DotDot = &ref case btrfsitem.Error: - ret.Errs = append(ret.Errs, fmt.Errorf("malformed INODE_REF: %w", body.Err)) + dir.Errs = append(dir.Errs, fmt.Errorf("malformed INODE_REF: %w", body.Err)) default: panic(fmt.Errorf("should not happen: INODE_REF has unexpected item type: %T", body)) } @@ -252,19 +252,19 @@ func (ret *Dir) populate() { case btrfsitem.DirEntry: namehash := btrfsitem.NameHash(entry.Name) if namehash != item.Key.Offset { - ret.Errs = append(ret.Errs, fmt.Errorf("direntry crc32c mismatch: key=%#x crc32c(%q)=%#x", + dir.Errs = append(dir.Errs, fmt.Errorf("direntry crc32c mismatch: key=%#x crc32c(%q)=%#x", item.Key.Offset, entry.Name, namehash)) continue } - if other, exists := ret.ChildrenByName[string(entry.Name)]; exists { + if other, exists := dir.ChildrenByName[string(entry.Name)]; exists { if !reflect.DeepEqual(entry, other) { - ret.Errs = append(ret.Errs, fmt.Errorf("multiple instances of direntry name %q", entry.Name)) + dir.Errs = append(dir.Errs, fmt.Errorf("multiple instances of direntry name %q", entry.Name)) } continue } - ret.ChildrenByName[string(entry.Name)] = entry + dir.ChildrenByName[string(entry.Name)] = entry case btrfsitem.Error: - ret.Errs = append(ret.Errs, fmt.Errorf("malformed DIR_ITEM: %w", entry.Err)) + dir.Errs = append(dir.Errs, fmt.Errorf("malformed DIR_ITEM: %w", entry.Err)) default: panic(fmt.Errorf("should not happen: DIR_ITEM has unexpected item type: %T", entry)) } @@ -272,15 +272,15 @@ func (ret *Dir) populate() { index := item.Key.Offset switch entry := item.Body.(type) { case btrfsitem.DirEntry: - if other, exists := ret.ChildrenByIndex[index]; exists { + if other, exists := dir.ChildrenByIndex[index]; exists { if !reflect.DeepEqual(entry, other) { - ret.Errs = append(ret.Errs, fmt.Errorf("multiple instances of direntry index %v", index)) + dir.Errs = append(dir.Errs, fmt.Errorf("multiple instances of direntry index %v", index)) } continue } - ret.ChildrenByIndex[index] = entry + dir.ChildrenByIndex[index] = entry case btrfsitem.Error: - ret.Errs = append(ret.Errs, fmt.Errorf("malformed DIR_INDEX: %w", entry.Err)) + dir.Errs = append(dir.Errs, fmt.Errorf("malformed DIR_INDEX: %w", entry.Err)) default: panic(fmt.Errorf("should not happen: DIR_INDEX has unexpected item type: %T", entry)) } @@ -290,23 +290,23 @@ func (ret *Dir) populate() { } entriesWithIndexes := make(containers.Set[string]) nextIndex := uint64(2) - for index, entry := range ret.ChildrenByIndex { + for index, entry := range dir.ChildrenByIndex { if index+1 > nextIndex { nextIndex = index + 1 } entriesWithIndexes.Insert(string(entry.Name)) - if other, exists := ret.ChildrenByName[string(entry.Name)]; !exists { - ret.Errs = append(ret.Errs, fmt.Errorf("missing by-name direntry for %q", entry.Name)) - ret.ChildrenByName[string(entry.Name)] = entry + if other, exists := dir.ChildrenByName[string(entry.Name)]; !exists { + dir.Errs = append(dir.Errs, fmt.Errorf("missing by-name direntry for %q", entry.Name)) + dir.ChildrenByName[string(entry.Name)] = entry } else if !reflect.DeepEqual(entry, other) { - ret.Errs = append(ret.Errs, fmt.Errorf("direntry index %v and direntry name %q disagree", index, entry.Name)) - ret.ChildrenByName[string(entry.Name)] = entry + dir.Errs = append(dir.Errs, fmt.Errorf("direntry index %v and direntry name %q disagree", index, entry.Name)) + dir.ChildrenByName[string(entry.Name)] = entry } } - for _, name := range maps.SortedKeys(ret.ChildrenByName) { + for _, name := range maps.SortedKeys(dir.ChildrenByName) { if !entriesWithIndexes.Has(name) { - ret.Errs = append(ret.Errs, fmt.Errorf("missing by-index direntry for %q", name)) - ret.ChildrenByIndex[nextIndex] = ret.ChildrenByName[name] + dir.Errs = append(dir.Errs, fmt.Errorf("missing by-index direntry for %q", name)) + dir.ChildrenByIndex[nextIndex] = dir.ChildrenByName[name] nextIndex++ } } @@ -354,20 +354,20 @@ func (sv *Subvolume) LoadFile(inode btrfsprim.ObjID) (*File, error) { return val, nil } -func (ret *File) populate() { - for _, item := range ret.OtherItems { +func (file *File) populate() { + for _, item := range file.OtherItems { switch item.Key.ItemType { case btrfsitem.INODE_REF_KEY: // TODO case btrfsitem.EXTENT_DATA_KEY: switch itemBody := item.Body.(type) { case btrfsitem.FileExtent: - ret.Extents = append(ret.Extents, FileExtent{ + file.Extents = append(file.Extents, FileExtent{ OffsetWithinFile: int64(item.Key.Offset), FileExtent: itemBody, }) case btrfsitem.Error: - ret.Errs = append(ret.Errs, fmt.Errorf("malformed EXTENT_DATA: %w", itemBody.Err)) + file.Errs = append(file.Errs, fmt.Errorf("malformed EXTENT_DATA: %w", itemBody.Err)) default: panic(fmt.Errorf("should not happen: EXTENT_DATA has unexpected item type: %T", itemBody)) } @@ -379,34 +379,34 @@ func (ret *File) populate() { // These should already be sorted, because of the nature of // the btree; but this is a recovery tool for corrupt // filesystems, so go ahead and ensure that it's sorted. - sort.Slice(ret.Extents, func(i, j int) bool { - return ret.Extents[i].OffsetWithinFile < ret.Extents[j].OffsetWithinFile + sort.Slice(file.Extents, func(i, j int) bool { + return file.Extents[i].OffsetWithinFile < file.Extents[j].OffsetWithinFile }) pos := int64(0) - for _, extent := range ret.Extents { + for _, extent := range file.Extents { if extent.OffsetWithinFile != pos { if extent.OffsetWithinFile > pos { - ret.Errs = append(ret.Errs, fmt.Errorf("extent gap from %v to %v", + file.Errs = append(file.Errs, fmt.Errorf("extent gap from %v to %v", pos, extent.OffsetWithinFile)) } else { - ret.Errs = append(ret.Errs, fmt.Errorf("extent overlap from %v to %v", + file.Errs = append(file.Errs, fmt.Errorf("extent overlap from %v to %v", extent.OffsetWithinFile, pos)) } } size, err := extent.Size() if err != nil { - ret.Errs = append(ret.Errs, fmt.Errorf("extent %v: %w", extent.OffsetWithinFile, err)) + file.Errs = append(file.Errs, fmt.Errorf("extent %v: %w", extent.OffsetWithinFile, err)) } pos += size } - if ret.InodeItem != nil && pos != ret.InodeItem.NumBytes { - if ret.InodeItem.NumBytes > pos { - ret.Errs = append(ret.Errs, fmt.Errorf("extent gap from %v to %v", - pos, ret.InodeItem.NumBytes)) + if file.InodeItem != nil && pos != file.InodeItem.NumBytes { + if file.InodeItem.NumBytes > pos { + file.Errs = append(file.Errs, fmt.Errorf("extent gap from %v to %v", + pos, file.InodeItem.NumBytes)) } else { - ret.Errs = append(ret.Errs, fmt.Errorf("extent mapped past end of file from %v to %v", - ret.InodeItem.NumBytes, pos)) + file.Errs = append(file.Errs, fmt.Errorf("extent mapped past end of file from %v to %v", + file.InodeItem.NumBytes, pos)) } } } @@ -451,7 +451,7 @@ func (file *File) maybeShortReadAt(dat []byte, off int64) (int, error) { if err != nil { return 0, err } - var beg btrfsvol.LogicalAddr = extent.BodyExtent.DiskByteNr. + beg := extent.BodyExtent.DiskByteNr. Add(extent.BodyExtent.Offset). Add(btrfsvol.AddrDelta(offsetWithinExt)) var block [btrfssum.BlockSize]byte -- cgit v1.2.3-54-g00ecf From 6e1a9fbb1e9a943e04902ed3a4958f6821e39456 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 22:41:34 -0700 Subject: lint: Set exclude-use-default=false --- .golangci.yml | 11 +++++++++++ cmd/btrfs-rec/inspect_lsfiles.go | 8 ++++++-- cmd/btrfs-rec/inspect_lstrees.go | 2 +- cmd/btrfs-rec/inspect_spewitems.go | 6 +++--- lib/textui/log.go | 8 ++++---- lib/textui/text.go | 8 ++++---- 6 files changed, 29 insertions(+), 14 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/.golangci.yml b/.golangci.yml index d1c1798..6b9a830 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -55,6 +55,9 @@ linters: - varnamelen - wrapcheck linters-settings: + errcheck: + exclude-functions: + - "git.lukeshu.com/btrfs-progs-ng/lib/textui.Fprintf" gci: sections: - standard @@ -74,6 +77,10 @@ linters-settings: gomoddirectives: replace-allow-list: - github.com/jacobsa/fuse + gosec: + excludes: + - G104 # duplicates errcheck + - G304 # this program opens arbitrary files nolintlint: require-explanation: true require-specific: true @@ -83,12 +90,16 @@ linters-settings: checks: - "all" - "-ST1003" # CONST_VAL names for consistency with other btrfs code + - "-ST1000" # TODO: get this to pass + - "-ST1020" # TODO: get this to pass + - "-ST1021" # TODO: get this to pass tagliatelle: case: use-field-name: true rules: json: pascal issues: + exclude-use-default: false exclude-rules: # Ignore false positives that don't actually have any words. - linters: [dupword] diff --git a/cmd/btrfs-rec/inspect_lsfiles.go b/cmd/btrfs-rec/inspect_lsfiles.go index 502d91d..395f60a 100644 --- a/cmd/btrfs-rec/inspect_lsfiles.go +++ b/cmd/btrfs-rec/inspect_lsfiles.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -34,7 +34,11 @@ func init() { }, RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { out := bufio.NewWriter(os.Stdout) - defer out.Flush() + defer func() { + if _err := out.Flush(); _err != nil && err == nil { + err = _err + } + }() defer func() { if r := derror.PanicToError(recover()); r != nil { textui.Fprintf(out, "\n\n%+v\n", r) diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go index 0f70cd1..f74956a 100644 --- a/cmd/btrfs-rec/inspect_lstrees.go +++ b/cmd/btrfs-rec/inspect_lstrees.go @@ -60,7 +60,7 @@ func init() { textui.Fprintf(table, " %v items\t% *s\n", typ, numWidth, strconv.Itoa(treeItemCnt[typ])) } textui.Fprintf(table, " total items\t% *s\n", numWidth, strconv.Itoa(totalItems)) - table.Flush() + _ = table.Flush() } visitedNodes := make(containers.Set[btrfsvol.LogicalAddr]) btrfsutil.WalkAllTrees(ctx, fs, btrfsutil.WalkAllTreesHandler{ diff --git a/cmd/btrfs-rec/inspect_spewitems.go b/cmd/btrfs-rec/inspect_spewitems.go index 8d71797..3b79e8b 100644 --- a/cmd/btrfs-rec/inspect_spewitems.go +++ b/cmd/btrfs-rec/inspect_spewitems.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -39,13 +39,13 @@ func init() { Item: func(path btrfstree.TreePath, item btrfstree.Item) error { textui.Fprintf(os.Stdout, "%s = ", path) spew.Dump(item) - os.Stdout.WriteString("\n") + _, _ = os.Stdout.WriteString("\n") return nil }, BadItem: func(path btrfstree.TreePath, item btrfstree.Item) error { textui.Fprintf(os.Stdout, "%s = ", path) spew.Dump(item) - os.Stdout.WriteString("\n") + _, _ = os.Stdout.WriteString("\n") return nil }, }, diff --git a/lib/textui/log.go b/lib/textui/log.go index 0fe03e5..d2373f8 100644 --- a/lib/textui/log.go +++ b/lib/textui/log.go @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2022 Ambassador Labs +// Copyright (C) 2019-2022-2023 Ambassador Labs // Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: Apache-2.0 @@ -134,21 +134,21 @@ func (l *logger) Log(lvl dlog.LogLevel, msg string) { // UnformattedLog implements dlog.OptimizedLogger. func (l *logger) UnformattedLog(lvl dlog.LogLevel, args ...any) { l.log(lvl, func(w io.Writer) { - printer.Fprint(w, args...) + _, _ = printer.Fprint(w, args...) }) } // UnformattedLogln implements dlog.OptimizedLogger. func (l *logger) UnformattedLogln(lvl dlog.LogLevel, args ...any) { l.log(lvl, func(w io.Writer) { - printer.Fprintln(w, args...) + _, _ = printer.Fprintln(w, args...) }) } // UnformattedLogf implements dlog.OptimizedLogger. func (l *logger) UnformattedLogf(lvl dlog.LogLevel, format string, args ...any) { l.log(lvl, func(w io.Writer) { - printer.Fprintf(w, format, args...) + _, _ = printer.Fprintf(w, format, args...) }) } diff --git a/lib/textui/text.go b/lib/textui/text.go index 615516d..c0a3c5e 100644 --- a/lib/textui/text.go +++ b/lib/textui/text.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -53,7 +53,7 @@ var ( // String implements fmt.Formatter. func (h humanized) Format(f fmt.State, verb rune) { - printer.Fprintf(f, fmtutil.FmtStateString(f, verb), h.val) + _, _ = printer.Fprintf(f, fmtutil.FmtStateString(f, verb), h.val) } // String implements fmt.Stringer. @@ -144,7 +144,7 @@ func (v metric[T]) Format(f fmt.State, verb rune) { if v.Val < 0 { y = -y } - printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s", + _, _ = printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s", y, prefix, v.Unit) } @@ -192,7 +192,7 @@ func (v iec[T]) Format(f fmt.State, verb rune) { if v.Val < 0 { y = -y } - printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s", + _, _ = printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s", number.Decimal(y), prefix, v.Unit) } -- cgit v1.2.3-54-g00ecf