From 41ef03aabf8d6db4f926480fc5ddfec014e342d3 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 10 Jul 2022 17:47:21 -0600 Subject: lib/btrfsprogs: Don't hard-code stdout or stderr --- lib/btrfsprogs/btrfsinspect/print_tree.go | 210 +++++++++++++++--------------- lib/btrfsprogs/btrfsrepair/clearnodes.go | 9 +- 2 files changed, 110 insertions(+), 109 deletions(-) (limited to 'lib') diff --git a/lib/btrfsprogs/btrfsinspect/print_tree.go b/lib/btrfsprogs/btrfsinspect/print_tree.go index 72ff13e..a950242 100644 --- a/lib/btrfsprogs/btrfsinspect/print_tree.go +++ b/lib/btrfsprogs/btrfsinspect/print_tree.go @@ -6,7 +6,7 @@ package btrfsinspect import ( "fmt" - "os" + "io" "strings" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" @@ -16,33 +16,33 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/util" ) -func DumpTrees(fs *btrfs.FS) error { +func DumpTrees(out, errout io.Writer, fs *btrfs.FS) error { superblock, err := fs.Superblock() if err != nil { return err } if superblock.Data.RootTree != 0 { - fmt.Printf("root tree\n") - if err := printTree(fs, btrfs.ROOT_TREE_OBJECTID); err != nil { + fmt.Fprintf(out, "root tree\n") + if err := printTree(out, errout, fs, btrfs.ROOT_TREE_OBJECTID); err != nil { return err } } if superblock.Data.ChunkTree != 0 { - fmt.Printf("chunk tree\n") - if err := printTree(fs, btrfs.CHUNK_TREE_OBJECTID); err != nil { + fmt.Fprintf(out, "chunk tree\n") + if err := printTree(out, errout, fs, btrfs.CHUNK_TREE_OBJECTID); err != nil { return err } } if superblock.Data.LogTree != 0 { - fmt.Printf("log root tree\n") - if err := printTree(fs, btrfs.TREE_LOG_OBJECTID); err != nil { + fmt.Fprintf(out, "log root tree\n") + if err := printTree(out, errout, fs, btrfs.TREE_LOG_OBJECTID); err != nil { return err } } if superblock.Data.BlockGroupRoot != 0 { - fmt.Printf("block group tree\n") - if err := printTree(fs, btrfs.BLOCK_GROUP_TREE_OBJECTID); err != nil { + fmt.Fprintf(out, "block group tree\n") + if err := printTree(out, errout, fs, btrfs.BLOCK_GROUP_TREE_OBJECTID); err != nil { return err } } @@ -74,34 +74,34 @@ func DumpTrees(fs *btrfs.FS) error { if !ok { treeName = "file" } - fmt.Printf("%v tree %v \n", treeName, fmtKey(item.Head.Key)) - return printTree(fs, item.Head.Key.ObjectID) + fmt.Fprintf(out, "%v tree %v \n", treeName, fmtKey(item.Head.Key)) + return printTree(out, errout, fs, item.Head.Key.ObjectID) }, }); err != nil { return err } - fmt.Printf("total bytes %v\n", superblock.Data.TotalBytes) - fmt.Printf("bytes used %v\n", superblock.Data.BytesUsed) - fmt.Printf("uuid %v\n", superblock.Data.FSUUID) + fmt.Fprintf(out, "total bytes %v\n", superblock.Data.TotalBytes) + fmt.Fprintf(out, "bytes used %v\n", superblock.Data.BytesUsed) + fmt.Fprintf(out, "uuid %v\n", superblock.Data.FSUUID) return nil } // printTree mimics btrfs-progs // kernel-shared/print-tree.c:btrfs_print_tree() and // kernel-shared/print-tree.c:btrfs_print_leaf() -func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { +func printTree(out, errout io.Writer, fs *btrfs.FS, treeID btrfs.ObjID) error { return fs.TreeWalk(treeID, btrfs.TreeWalkHandler{ Node: func(path btrfs.TreePath, nodeRef *util.Ref[btrfsvol.LogicalAddr, btrfs.Node], err error) error { if err != nil { - fmt.Fprintf(os.Stderr, "error: %v: %v\n", path, err) + fmt.Fprintf(errout, "error: %v: %v\n", path, err) } if nodeRef != nil { - printHeaderInfo(nodeRef.Data) + printHeaderInfo(out, nodeRef.Data) } return nil }, PreKeyPointer: func(_ btrfs.TreePath, item btrfs.KeyPointer) error { - fmt.Printf("\t%v block %v gen %v\n", + fmt.Fprintf(out, "\t%v block %v gen %v\n", fmtKey(item.Key), item.BlockPtr, item.Generation) @@ -109,65 +109,65 @@ func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { }, Item: func(path btrfs.TreePath, item btrfs.Item) error { i := path[len(path)-1].ItemIdx - fmt.Printf("\titem %v %v itemoff %v itemsize %v\n", + fmt.Fprintf(out, "\titem %v %v itemoff %v itemsize %v\n", i, fmtKey(item.Head.Key), item.Head.DataOffset, item.Head.DataSize) switch body := item.Body.(type) { case btrfsitem.FreeSpaceHeader: - fmt.Printf("\t\tlocation %v\n", fmtKey(body.Location)) - fmt.Printf("\t\tcache generation %v entries %v bitmaps %v\n", + fmt.Fprintf(out, "\t\tlocation %v\n", fmtKey(body.Location)) + fmt.Fprintf(out, "\t\tcache generation %v entries %v bitmaps %v\n", body.Generation, body.NumEntries, body.NumBitmaps) case btrfsitem.Inode: - fmt.Printf(""+ + fmt.Fprintf(out, ""+ "\t\tgeneration %v transid %v size %v nbytes %v\n"+ "\t\tblock group %v mode %o links %v uid %v gid %v rdev %v\n"+ "\t\tsequence %v flags %v\n", body.Generation, body.TransID, body.Size, body.NumBytes, body.BlockGroup, body.Mode, body.NLink, body.UID, body.GID, body.RDev, body.Sequence, body.Flags) - fmt.Printf("\t\tatime %v\n", fmtTime(body.ATime)) - fmt.Printf("\t\tctime %v\n", fmtTime(body.CTime)) - fmt.Printf("\t\tmtime %v\n", fmtTime(body.MTime)) - fmt.Printf("\t\totime %v\n", fmtTime(body.OTime)) + fmt.Fprintf(out, "\t\tatime %v\n", fmtTime(body.ATime)) + fmt.Fprintf(out, "\t\tctime %v\n", fmtTime(body.CTime)) + fmt.Fprintf(out, "\t\tmtime %v\n", fmtTime(body.MTime)) + fmt.Fprintf(out, "\t\totime %v\n", fmtTime(body.OTime)) case btrfsitem.InodeRef: - fmt.Printf("\t\tindex %v namelen %v name: %s\n", + fmt.Fprintf(out, "\t\tindex %v namelen %v name: %s\n", body.Index, body.NameLen, body.Name) //case btrfsitem.INODE_EXTREF_KEY: // // TODO case btrfsitem.DirEntries: for _, dir := range body { - fmt.Printf("\t\tlocation %v type %v\n", + fmt.Fprintf(out, "\t\tlocation %v type %v\n", fmtKey(dir.Location), dir.Type) - fmt.Printf("\t\ttransid %v data_len %v name_len %v\n", + fmt.Fprintf(out, "\t\ttransid %v data_len %v name_len %v\n", dir.TransID, dir.DataLen, dir.NameLen) - fmt.Printf("\t\tname: %s\n", dir.Name) + fmt.Fprintf(out, "\t\tname: %s\n", dir.Name) if len(dir.Data) > 0 { - fmt.Printf("\t\tdata %v\n", dir.Data) + fmt.Fprintf(out, "\t\tdata %v\n", dir.Data) } } //case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY: // // TODO case btrfsitem.Root: - fmt.Printf("\t\tgeneration %v root_dirid %v bytenr %d byte_limit %v bytes_used %v\n", + fmt.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) - fmt.Printf("\t\tlast_snapshot %v flags %v refs %v\n", + fmt.Fprintf(out, "\t\tlast_snapshot %v flags %v refs %v\n", body.LastSnapshot, body.Flags, body.Refs) - fmt.Printf("\t\tdrop_progress %v drop_level %v\n", + fmt.Fprintf(out, "\t\tdrop_progress %v drop_level %v\n", fmtKey(body.DropProgress), body.DropLevel) - fmt.Printf("\t\tlevel %v generation_v2 %v\n", + fmt.Fprintf(out, "\t\tlevel %v generation_v2 %v\n", body.Level, body.GenerationV2) if body.Generation == body.GenerationV2 { - fmt.Printf("\t\tuuid %v\n", body.UUID) - fmt.Printf("\t\tparent_uuid %v\n", body.ParentUUID) - fmt.Printf("\t\treceived_uuid %v\n", body.ReceivedUUID) - fmt.Printf("\t\tctransid %v otransid %v stransid %v rtransid %v\n", + fmt.Fprintf(out, "\t\tuuid %v\n", body.UUID) + fmt.Fprintf(out, "\t\tparent_uuid %v\n", body.ParentUUID) + fmt.Fprintf(out, "\t\treceived_uuid %v\n", body.ReceivedUUID) + fmt.Fprintf(out, "\t\tctransid %v otransid %v stransid %v rtransid %v\n", body.CTransID, body.OTransID, body.STransID, body.RTransID) - fmt.Printf("\t\tctime %v\n", fmtTime(body.CTime)) - fmt.Printf("\t\totime %v\n", fmtTime(body.OTime)) - fmt.Printf("\t\tstime %v\n", fmtTime(body.STime)) - fmt.Printf("\t\trtime %v\n", fmtTime(body.RTime)) + fmt.Fprintf(out, "\t\tctime %v\n", fmtTime(body.CTime)) + fmt.Fprintf(out, "\t\totime %v\n", fmtTime(body.OTime)) + fmt.Fprintf(out, "\t\tstime %v\n", fmtTime(body.STime)) + fmt.Fprintf(out, "\t\trtime %v\n", fmtTime(body.RTime)) } case btrfsitem.RootRef: var tag string @@ -179,21 +179,21 @@ func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { default: tag = fmt.Sprintf("(error: unhandled RootRef item type: %v)", item.Head.Key.ItemType) } - fmt.Printf("\t\troot %v key dirid %v sequence %v name %s\n", + fmt.Fprintf(out, "\t\troot %v key dirid %v sequence %v name %s\n", tag, body.DirID, body.Sequence, body.Name) case btrfsitem.Extent: - fmt.Printf("\t\trefs %v gen %v flags %v\n", + fmt.Fprintf(out, "\t\trefs %v gen %v flags %v\n", body.Head.Refs, body.Head.Generation, body.Head.Flags) if body.Head.Flags.Has(btrfsitem.EXTENT_FLAG_TREE_BLOCK) { - fmt.Printf("\t\ttree block %v level %v\n", + fmt.Fprintf(out, "\t\ttree block %v level %v\n", fmtKey(body.Info.Key), body.Info.Level) } - printExtentInlineRefs(body.Refs) + printExtentInlineRefs(out, body.Refs) case btrfsitem.Metadata: - fmt.Printf("\t\trefs %v gen %v flags %v\n", + fmt.Fprintf(out, "\t\trefs %v gen %v flags %v\n", body.Head.Refs, body.Head.Generation, body.Head.Flags) - fmt.Printf("\t\ttree block skinny level %v\n", item.Head.Key.Offset) - printExtentInlineRefs(body.Refs) + fmt.Fprintf(out, "\t\ttree block skinny level %v\n", item.Head.Key.Offset) + printExtentInlineRefs(out, body.Refs) //case btrfsitem.EXTENT_DATA_REF_KEY: // // TODO //case btrfsitem.SHARED_DATA_REF_KEY: @@ -204,71 +204,71 @@ func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { start := btrfsvol.LogicalAddr(item.Head.Key.Offset) itemSize := btrfsvol.AddrDelta(len(body.Sums)) * sectorSize - fmt.Printf("\t\trange start %d end %d length %d", + fmt.Fprintf(out, "\t\trange start %d end %d length %d", start, start.Add(itemSize), itemSize) sumsPerLine := util.Max(1, len(btrfssum.CSum{})/body.ChecksumSize/2) pos := start for i, sum := range body.Sums { if i%sumsPerLine == 0 { - fmt.Printf("\n\t\t") + fmt.Fprintf(out, "\n\t\t") } else { - fmt.Printf(" ") + fmt.Fprintf(out, " ") } - fmt.Printf("[%d] 0x%s", pos, sum.Fmt(sb.Data.ChecksumType)) + fmt.Fprintf(out, "[%d] 0x%s", pos, sum.Fmt(sb.Data.ChecksumType)) pos = pos.Add(sectorSize) } - fmt.Printf("\n") + fmt.Fprintf(out, "\n") case btrfsitem.FileExtent: - fmt.Printf("\t\tgeneration %v type %v\n", + fmt.Fprintf(out, "\t\tgeneration %v type %v\n", body.Generation, body.Type) switch body.Type { case btrfsitem.FILE_EXTENT_INLINE: - fmt.Printf("\t\tinline extent data size %v ram_bytes %v compression %v\n", + fmt.Fprintf(out, "\t\tinline extent data size %v ram_bytes %v compression %v\n", len(body.BodyInline), body.RAMBytes, body.Compression) case btrfsitem.FILE_EXTENT_PREALLOC: - fmt.Printf("\t\tprealloc data disk byte %v nr %v\n", + fmt.Fprintf(out, "\t\tprealloc data disk byte %v nr %v\n", body.BodyExtent.DiskByteNr, body.BodyExtent.DiskNumBytes) - fmt.Printf("\t\tprealloc data offset %v nr %v\n", + fmt.Fprintf(out, "\t\tprealloc data offset %v nr %v\n", body.BodyExtent.Offset, body.BodyExtent.NumBytes) case btrfsitem.FILE_EXTENT_REG: - fmt.Printf("\t\textent data disk byte %d nr %d\n", + fmt.Fprintf(out, "\t\textent data disk byte %d nr %d\n", body.BodyExtent.DiskByteNr, body.BodyExtent.DiskNumBytes) - fmt.Printf("\t\textent data offset %d nr %d ram %v\n", + fmt.Fprintf(out, "\t\textent data offset %d nr %d ram %v\n", body.BodyExtent.Offset, body.BodyExtent.NumBytes, body.RAMBytes) - fmt.Printf("\t\textent compression %v\n", + fmt.Fprintf(out, "\t\textent compression %v\n", body.Compression) default: - fmt.Printf("\t\t(error) unknown file extent type %v", body.Type) + fmt.Fprintf(out, "\t\t(error) unknown file extent type %v", body.Type) } case btrfsitem.BlockGroup: - fmt.Printf("\t\tblock group used %v chunk_objectid %v flags %v\n", + fmt.Fprintf(out, "\t\tblock group used %v chunk_objectid %v flags %v\n", body.Used, body.ChunkObjectID, body.Flags) case btrfsitem.FreeSpaceInfo: - fmt.Printf("\t\tfree space info extent count %v flags %v\n", + fmt.Fprintf(out, "\t\tfree space info extent count %v flags %v\n", body.ExtentCount, body.Flags) case btrfsitem.FreeSpaceBitmap: - fmt.Printf("\t\tfree space bitmap\n") + fmt.Fprintf(out, "\t\tfree space bitmap\n") case btrfsitem.Chunk: - fmt.Printf("\t\tlength %d owner %d stripe_len %v type %v\n", + fmt.Fprintf(out, "\t\tlength %d owner %d stripe_len %v type %v\n", body.Head.Size, body.Head.Owner, body.Head.StripeLen, body.Head.Type) - fmt.Printf("\t\tio_align %v io_width %v sector_size %v\n", + fmt.Fprintf(out, "\t\tio_align %v io_width %v sector_size %v\n", body.Head.IOOptimalAlign, body.Head.IOOptimalWidth, body.Head.IOMinSize) - fmt.Printf("\t\tnum_stripes %v sub_stripes %v\n", + fmt.Fprintf(out, "\t\tnum_stripes %v sub_stripes %v\n", body.Head.NumStripes, body.Head.SubStripes) for i, stripe := range body.Stripes { - fmt.Printf("\t\t\tstripe %v devid %d offset %d\n", + fmt.Fprintf(out, "\t\t\tstripe %v devid %d offset %d\n", i, stripe.DeviceID, stripe.Offset) - fmt.Printf("\t\t\tdev_uuid %v\n", + fmt.Fprintf(out, "\t\t\tdev_uuid %v\n", stripe.DeviceUUID) } case btrfsitem.Dev: - fmt.Printf(""+ + fmt.Fprintf(out, ""+ "\t\tdevid %d total_bytes %v bytes_used %v\n"+ "\t\tio_align %v io_width %v sector_size %v type %v\n"+ "\t\tgeneration %v start_offset %v dev_group %v\n"+ @@ -282,7 +282,7 @@ func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { body.DevUUID, body.FSUUID) case btrfsitem.DevExtent: - fmt.Printf(""+ + fmt.Fprintf(out, ""+ "\t\tdev extent chunk_tree %v\n"+ "\t\tchunk_objectid %v chunk_offset %d length %d\n"+ "\t\tchunk_tree_uuid %v\n", @@ -295,49 +295,49 @@ func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { //case btrfsitem.QGROUP_LIMIT_KEY: // // TODO case btrfsitem.UUIDMap: - fmt.Printf("\t\tsubvol_id %d\n", body.ObjID) + fmt.Fprintf(out, "\t\tsubvol_id %d\n", body.ObjID) //case btrfsitem.STRING_ITEM_KEY: // // TODO case btrfsitem.DevStats: - fmt.Printf("\t\tpersistent item objectid %v offset %v\n", + fmt.Fprintf(out, "\t\tpersistent item objectid %v offset %v\n", item.Head.Key.ObjectID.Format(item.Head.Key.ItemType), item.Head.Key.Offset) switch item.Head.Key.ObjectID { case btrfs.DEV_STATS_OBJECTID: - fmt.Printf("\t\tdevice stats\n") - fmt.Printf("\t\twrite_errs %v read_errs %v flush_errs %v corruption_errs %v generation %v\n", + fmt.Fprintf(out, "\t\tdevice stats\n") + fmt.Fprintf(out, "\t\twrite_errs %v read_errs %v flush_errs %v corruption_errs %v generation %v\n", body.Values[btrfsitem.DEV_STAT_WRITE_ERRS], body.Values[btrfsitem.DEV_STAT_READ_ERRS], body.Values[btrfsitem.DEV_STAT_FLUSH_ERRS], body.Values[btrfsitem.DEV_STAT_CORRUPTION_ERRS], body.Values[btrfsitem.DEV_STAT_GENERATION_ERRS]) default: - fmt.Printf("\t\tunknown persistent item objectid %v\n", item.Head.Key.ObjectID) + fmt.Fprintf(out, "\t\tunknown persistent item objectid %v\n", item.Head.Key.ObjectID) } //case btrfsitem.TEMPORARY_ITEM_KEY: // // TODO case btrfsitem.Empty: switch item.Head.Key.ItemType { case btrfsitem.ORPHAN_ITEM_KEY: // 48 - fmt.Printf("\t\torphan item\n") + fmt.Fprintf(out, "\t\torphan item\n") case btrfsitem.TREE_BLOCK_REF_KEY: // 176 - fmt.Printf("\t\ttree block backref\n") + fmt.Fprintf(out, "\t\ttree block backref\n") case btrfsitem.SHARED_BLOCK_REF_KEY: // 182 - fmt.Printf("\t\tshared block backref\n") + fmt.Fprintf(out, "\t\tshared block backref\n") case btrfsitem.FREE_SPACE_EXTENT_KEY: // 199 - fmt.Printf("\t\tfree space extent\n") + fmt.Fprintf(out, "\t\tfree space extent\n") case btrfsitem.QGROUP_RELATION_KEY: // 246 // do nothing //case btrfsitem.EXTENT_REF_V0_KEY: - // fmt.Printf("\t\textent ref v0 (deprecated)\n") + // fmt.Fprintf(out, "\t\textent ref v0 (deprecated)\n") //case btrfsitem.CSUM_ITEM_KEY: - // fmt.Printf("\t\tcsum item\n") + // fmt.Fprintf(out, "\t\tcsum item\n") default: - fmt.Printf("\t\t(error) unhandled empty item type: %v\n", item.Head.Key.ItemType) + fmt.Fprintf(out, "\t\t(error) unhandled empty item type: %v\n", item.Head.Key.ItemType) } case btrfsitem.Error: - fmt.Printf("\t\t(error) error item: %v\n", body.Err) + fmt.Fprintf(out, "\t\t(error) error item: %v\n", body.Err) default: - fmt.Printf("\t\t(error) unhandled item type: %T\n", body) + fmt.Fprintf(out, "\t\t(error) unhandled item type: %T\n", body) } return nil }, @@ -345,66 +345,66 @@ func printTree(fs *btrfs.FS, treeID btrfs.ObjID) error { } // printHeaderInfo mimics btrfs-progs kernel-shared/print-tree.c:print_header_info() -func printHeaderInfo(node btrfs.Node) { +func printHeaderInfo(out io.Writer, node btrfs.Node) { var typename string if node.Head.Level > 0 { // internal node typename = "node" - fmt.Printf("node %v level %v items %v free space %v", + fmt.Fprintf(out, "node %v level %v items %v free space %v", node.Head.Addr, node.Head.Level, node.Head.NumItems, node.MaxItems()-node.Head.NumItems) } else { // leaf node typename = "leaf" - fmt.Printf("leaf %d items %v free space %v", + fmt.Fprintf(out, "leaf %d items %v free space %v", node.Head.Addr, node.Head.NumItems, node.LeafFreeSpace()) } - fmt.Printf(" generation %v owner %v\n", + fmt.Fprintf(out, " generation %v owner %v\n", node.Head.Generation, node.Head.Owner) - fmt.Printf("%v %d flags %v backref revision %v\n", + fmt.Fprintf(out, "%v %d flags %v backref revision %v\n", typename, node.Head.Addr, node.Head.Flags, node.Head.BackrefRev) - fmt.Printf("checksum stored %v\n", node.Head.Checksum.Fmt(node.ChecksumType)) + fmt.Fprintf(out, "checksum stored %v\n", node.Head.Checksum.Fmt(node.ChecksumType)) if calcSum, err := node.CalculateChecksum(); err != nil { - fmt.Printf("checksum calced %v\n", err) + fmt.Fprintf(out, "checksum calced %v\n", err) } else { - fmt.Printf("checksum calced %v\n", calcSum.Fmt(node.ChecksumType)) + fmt.Fprintf(out, "checksum calced %v\n", calcSum.Fmt(node.ChecksumType)) } - fmt.Printf("fs uuid %v\n", node.Head.MetadataUUID) - fmt.Printf("chunk uuid %v\n", node.Head.ChunkTreeUUID) + fmt.Fprintf(out, "fs uuid %v\n", node.Head.MetadataUUID) + fmt.Fprintf(out, "chunk uuid %v\n", node.Head.ChunkTreeUUID) } // printExtentInlineRefs mimics part of btrfs-progs kernel-shared/print-tree.c:print_extent_item() -func printExtentInlineRefs(refs []btrfsitem.ExtentInlineRef) { +func printExtentInlineRefs(out io.Writer, refs []btrfsitem.ExtentInlineRef) { for _, ref := range refs { switch subitem := ref.Body.(type) { case nil: switch ref.Type { case btrfsitem.TREE_BLOCK_REF_KEY: - fmt.Printf("\t\ttree block backref root %v\n", + fmt.Fprintf(out, "\t\ttree block backref root %v\n", btrfs.ObjID(ref.Offset)) case btrfsitem.SHARED_BLOCK_REF_KEY: - fmt.Printf("\t\tshared block backref parent %v\n", + fmt.Fprintf(out, "\t\tshared block backref parent %v\n", ref.Offset) default: - fmt.Printf("\t\t(error) unexpected empty sub-item type: %v\n", ref.Type) + fmt.Fprintf(out, "\t\t(error) unexpected empty sub-item type: %v\n", ref.Type) } case btrfsitem.ExtentDataRef: - fmt.Printf("\t\textent data backref root %v objectid %v offset %v count %v\n", + fmt.Fprintf(out, "\t\textent data backref root %v objectid %v offset %v count %v\n", subitem.Root, subitem.ObjectID, subitem.Offset, subitem.Count) case btrfsitem.SharedDataRef: - fmt.Printf("\t\tshared data backref parent %v count %v\n", + fmt.Fprintf(out, "\t\tshared data backref parent %v count %v\n", ref.Offset, subitem.Count) default: - fmt.Printf("\t\t(error) unexpected sub-item type: %T\n", subitem) + fmt.Fprintf(out, "\t\t(error) unexpected sub-item type: %T\n", subitem) } } } diff --git a/lib/btrfsprogs/btrfsrepair/clearnodes.go b/lib/btrfsprogs/btrfsrepair/clearnodes.go index 595fef0..e9cd879 100644 --- a/lib/btrfsprogs/btrfsrepair/clearnodes.go +++ b/lib/btrfsprogs/btrfsrepair/clearnodes.go @@ -7,6 +7,7 @@ package btrfsrepair import ( "errors" "fmt" + "io" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" @@ -14,7 +15,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/util" ) -func ClearBadNodes(fs *btrfs.FS) error { +func ClearBadNodes(out, errout io.Writer, fs *btrfs.FS) error { var uuidsInited bool var metadataUUID, chunkTreeUUID btrfs.UUID @@ -26,7 +27,7 @@ func ClearBadNodes(fs *btrfs.FS) error { treeID = id }, Err: func(err error) { - fmt.Printf("error: %v\n", err) + fmt.Fprintf(errout, "error: %v\n", err) }, UnsafeNodes: true, TreeWalkHandler: btrfs.TreeWalkHandler{ @@ -45,7 +46,7 @@ func ClearBadNodes(fs *btrfs.FS) error { Path: path, Err: err, } - fmt.Printf("error: %v\n", err) + fmt.Fprintf(errout, "error: %v\n", err) return nil } origErr := err @@ -82,7 +83,7 @@ func ClearBadNodes(fs *btrfs.FS) error { return err } - fmt.Printf("fixed node@%v (err was %v)\n", node.Addr, origErr) + fmt.Fprintf(out, "fixed node@%v (err was %v)\n", node.Addr, origErr) return nil }, }, -- cgit v1.2.3-2-g168b