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 /pkg/btrfsmisc/open.go | |
parent | df3e7ef9c5fd0ceb2e89d5afd4e981652f9a8bdd (diff) |
add open/close utility functions
Diffstat (limited to 'pkg/btrfsmisc/open.go')
-rw-r--r-- | pkg/btrfsmisc/open.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/pkg/btrfsmisc/open.go b/pkg/btrfsmisc/open.go new file mode 100644 index 0000000..bf72670 --- /dev/null +++ b/pkg/btrfsmisc/open.go @@ -0,0 +1,24 @@ +package btrfsmisc + +import ( + "fmt" + "os" + + "lukeshu.com/btrfs-tools/pkg/btrfs" +) + +func Open(flag int, filenames ...string) (*btrfs.FS, error) { + fs := new(btrfs.FS) + for _, filename := range filenames { + fh, err := os.OpenFile(filename, flag, 0) + if err != nil { + _ = fs.Close() + return nil, fmt.Errorf("file %q: %w", filename, err) + } + if err := fs.AddDevice(&btrfs.Device{File: fh}); err != nil { + _ = fs.Close() + return nil, fmt.Errorf("file %q: %w", filename, err) + } + } + return fs, nil +} |