diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-01 00:00:19 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-01 00:00:19 -0600 |
commit | aee0fa4cf09ef5af90e28441d673ce440e4c2c16 (patch) | |
tree | 5a87486e5bd4f0af400d633d3040edd5ed03bf5c /cmd/btrfs-fsck/pass0.go | |
parent | df3e7ef9c5fd0ceb2e89d5afd4e981652f9a8bdd (diff) |
add open/close utility functions
Diffstat (limited to 'cmd/btrfs-fsck/pass0.go')
-rw-r--r-- | cmd/btrfs-fsck/pass0.go | 18 |
1 files changed, 13 insertions, 5 deletions
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 } |