2017-04-26 17:39:16 +02:00
|
|
|
package zfs
|
|
|
|
|
2018-02-18 13:28:46 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
2018-02-18 13:28:46 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
rchan := make(chan ZFSListResult)
|
|
|
|
go ZFSListChan(ctx, rchan, []string{"name"}, "-r", "-t", "filesystem,volume")
|
2017-05-01 20:35:04 +02:00
|
|
|
|
2018-02-18 13:28:46 +01:00
|
|
|
datasets = make([]*DatasetPath, 0)
|
|
|
|
for r := range rchan {
|
2017-05-01 20:35:04 +02:00
|
|
|
|
2018-02-18 13:28:46 +01:00
|
|
|
if r.err != nil {
|
|
|
|
err = r.err
|
|
|
|
return
|
|
|
|
}
|
2017-05-01 20:35:04 +02:00
|
|
|
|
2017-08-06 13:04:29 +02:00
|
|
|
var path *DatasetPath
|
2018-02-18 13:28:46 +01:00
|
|
|
if path, err = NewDatasetPath(r.fields[0]); err != nil {
|
2017-05-01 20:35:04 +02:00
|
|
|
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
|
|
|
|
}
|