diff --git a/cmd/nativeprobe/main_ceph.go b/cmd/nativeprobe/main_ceph.go index 4f7b050..f3245b4 100644 --- a/cmd/nativeprobe/main_ceph.go +++ b/cmd/nativeprobe/main_ceph.go @@ -1,15 +1,20 @@ //go:build ceph && cgo +// Command nativeprobe exercises the libcephfs binding against a lab cluster: +// it stats a path and optionally prints the snapdiff between two snapshots. +// It is only built with the ceph build tag and cgo. package main import ( "encoding/json" + "errors" "flag" "fmt" - "git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative" - "git.zb-server.de/Sebi/CephfsMig/internal/storage" "io" "os" + + "git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative" + "git.zb-server.de/Sebi/CephfsMig/internal/storage" ) func main() { @@ -19,39 +24,49 @@ func main() { a := flag.String("from", "", "previous snapshot") b := flag.String("to", "", "current snapshot") flag.Parse() - data, err := os.ReadFile(*config) + if err := run(os.Stdout, *config, *root, *rel, *a, *b); err != nil { + fmt.Fprintln(os.Stderr, "nativeprobe:", err) + os.Exit(1) + } +} + +func run(out io.Writer, config, root, rel, from, to string) error { + data, err := os.ReadFile(config) if err != nil { - panic(err) + return err } var c struct{ Source storage.Options } if err = json.Unmarshal(data, &c); err != nil { - panic(err) + return fmt.Errorf("config: %w", err) } fs, err := cephfsnative.Open(c.Source) if err != nil { - panic(err) + return err } defer fs.Close() - n, err := fs.Stat(*root) + n, err := fs.Stat(root) if err != nil { - panic(err) + return err } - json.NewEncoder(os.Stdout).Encode(n) - if *a != "" { - d, err := fs.SnapDiff(*root, *rel, *a, *b) + if err = json.NewEncoder(out).Encode(n); err != nil { + return err + } + if from == "" { + return nil + } + d, err := fs.SnapDiff(root, rel, from, to) + if err != nil { + return err + } + defer d.Close() + for { + name, err := d.Next() + if errors.Is(err, io.EOF) { + return nil + } if err != nil { - panic(err) - } - defer d.Close() - for { - name, err := d.Next() - if err == io.EOF { - break - } - if err != nil { - panic(err) - } - fmt.Printf("diff: %q\n", name) + return err } + fmt.Fprintf(out, "diff: %q\n", name) } }