From aee0fa4cf09ef5af90e28441d673ce440e4c2c16 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 1 Jul 2022 00:00:19 -0600 Subject: add open/close utility functions --- cmd/btrfs-fsck/pass0.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'cmd/btrfs-fsck/pass0.go') diff --git a/cmd/btrfs-fsck/pass0.go b/cmd/btrfs-fsck/pass0.go index 2a8e20a..c0c872b 100644 --- a/cmd/btrfs-fsck/pass0.go +++ b/cmd/btrfs-fsck/pass0.go @@ -8,19 +8,27 @@ import ( "lukeshu.com/btrfs-tools/pkg/util" ) -func pass0(imgfiles ...*os.File) (*btrfs.FS, *util.Ref[btrfs.PhysicalAddr, btrfs.Superblock], error) { +func pass0(filenames ...string) (*btrfs.FS, *util.Ref[btrfs.PhysicalAddr, btrfs.Superblock], error) { fmt.Printf("\nPass 0: init and superblocks...\n") fs := new(btrfs.FS) - for _, imgfile := range imgfiles { - fmt.Printf("Pass 0: ... adding device %q...\n", imgfile.Name()) - if err := fs.AddDevice(&btrfs.Device{File: imgfile}); err != nil { - fmt.Printf("Pass 0: ... add device %q: error: %v\n", imgfile.Name(), err) + for _, filename := range filenames { + fmt.Printf("Pass 0: ... adding device %q...\n", filename) + + fh, err := os.OpenFile(filename, os.O_RDWR, 0) + if err != nil { + _ = fs.Close() + return nil, nil, fmt.Errorf("device %q: %w", filename, err) + } + + if err := fs.AddDevice(&btrfs.Device{File: fh}); err != nil { + fmt.Printf("Pass 0: ... add device %q: error: %v\n", filename, err) } } sb, err := fs.Superblock() if err != nil { + _ = fs.Close() return nil, nil, err } -- cgit v1.2.3-2-g168b