disk: add ForEachFilesystem helper

Add helper that iterates over all filesystems in the partition
table and calls the callback on each one.
Add simple checks for it as well.
This commit is contained in:
Christian Kellner 2021-11-10 18:37:59 +01:00 committed by Tom Gundersen
parent 74f6c59c93
commit 37d912529c
2 changed files with 122 additions and 4 deletions

View file

@ -5,6 +5,7 @@
package disk
import (
"errors"
"sort"
osbuild "github.com/osbuild/osbuild-composer/internal/osbuild1"
@ -176,19 +177,52 @@ func (pt *PartitionTable) BootPartitionIndex() int {
return rootIdx
}
// Returns the Filesystem instance for a given mountpoint, if it exists.
func (pt *PartitionTable) FindFilesystemForMountpoint(mountpoint string) *Filesystem {
// StopIter is used as a return value from iterator function to indicate
// the iteration should not continue. Not an actual error and thus not
// returned by iterator function.
var StopIter = errors.New("stop the iteration")
// ForEachFileSystemFunc is a type of function called by ForEachFilesystem
// to iterate over every filesystem in the partition table.
//
// If the function returns an error, the iteration stops.
type ForEachFileSystemFunc func(fs *Filesystem) error
// Iterates over all filesystems in the partition table and calls the
// callback on each one. The iteration continues as long as the callback
// does not return an error.
func (pt *PartitionTable) ForEachFilesystem(cb ForEachFileSystemFunc) error {
for _, part := range pt.Partitions {
if part.Filesystem == nil {
continue
}
if part.Filesystem.Mountpoint == mountpoint {
return part.Filesystem
if err := cb(part.Filesystem); err != nil {
if err == StopIter {
return nil
}
return err
}
}
return nil
}
// Returns the Filesystem instance for a given mountpoint, if it exists.
func (pt *PartitionTable) FindFilesystemForMountpoint(mountpoint string) *Filesystem {
var res *Filesystem
_ = pt.ForEachFilesystem(func(fs *Filesystem) error {
if fs.Mountpoint == mountpoint {
res = fs
return StopIter
}
return nil
})
return res
}
// Returns the Filesystem instance that corresponds to the root
// filesystem, i.e. the filesystem whose mountpoint is '/'.
func (pt *PartitionTable) RootFilesystem() *Filesystem {