2017-04-26 17:39:16 +02:00
|
|
|
package zfs
|
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
import "fmt"
|
2017-04-26 17:39:16 +02:00
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
type DatasetFilter interface {
|
2017-08-06 13:04:29 +02:00
|
|
|
Filter(p *DatasetPath) (pass bool, err error)
|
2017-04-26 17:39:16 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 13:04:29 +02:00
|
|
|
func ZFSListMapping(filter DatasetFilter) (datasets []*DatasetPath, err error) {
|
2017-05-01 20:35:04 +02:00
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
if filter == nil {
|
|
|
|
panic("filter must not be nil")
|
2017-05-01 20:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var lines [][]string
|
|
|
|
lines, err = ZFSList([]string{"name"}, "-r", "-t", "filesystem,volume")
|
|
|
|
|
2017-08-06 13:04:29 +02:00
|
|
|
datasets = make([]*DatasetPath, 0, len(lines))
|
2017-05-01 20:35:04 +02:00
|
|
|
|
2017-05-06 23:45:33 +02:00
|
|
|
for _, line := range lines {
|
2017-05-01 20:35:04 +02:00
|
|
|
|
2017-08-06 13:04:29 +02:00
|
|
|
var path *DatasetPath
|
2017-05-01 20:35:04 +02:00
|
|
|
if path, err = NewDatasetPath(line[0]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
pass, filterErr := filter.Filter(path)
|
|
|
|
if filterErr != nil {
|
|
|
|
return nil, fmt.Errorf("error calling filter: %s", filterErr)
|
2017-05-01 20:35:04 +02:00
|
|
|
}
|
2017-08-05 21:15:37 +02:00
|
|
|
if pass {
|
2017-05-06 23:45:33 +02:00
|
|
|
datasets = append(datasets, path)
|
2017-05-01 20:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|