71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type SnapraidConfig struct {
|
|
Conf string
|
|
DataDirs []string
|
|
ParityDir string
|
|
ScrubPlan int
|
|
}
|
|
|
|
func BuildSnapraidArgs(kind string, cfg SnapraidConfig) ([]string, error) {
|
|
if cfg.Conf == "" {
|
|
return nil, fmt.Errorf("snapraid conf file is required")
|
|
}
|
|
args := []string{"snapraid", "-c", cfg.Conf}
|
|
switch kind {
|
|
case "snapraid_diff":
|
|
args = append(args, "diff")
|
|
case "snapraid_sync":
|
|
args = append(args, "sync")
|
|
case "snapraid_scrub":
|
|
args = append(args, "scrub", "-p", strconv.Itoa(cfg.ScrubPlan))
|
|
case "snapraid_check":
|
|
args = append(args, "check")
|
|
default:
|
|
return nil, fmt.Errorf("unknown snapraid kind: %s", kind)
|
|
}
|
|
return args, nil
|
|
}
|
|
|
|
func ValidateSnapraidConf(conf string) error {
|
|
if conf == "" {
|
|
return fmt.Errorf("snapraid conf file is required")
|
|
}
|
|
info, err := os.Stat(conf)
|
|
if err != nil {
|
|
return fmt.Errorf("snapraid conf %s: %w", conf, err)
|
|
}
|
|
if info.IsDir() {
|
|
return fmt.Errorf("snapraid conf %s: is a directory, not a file", conf)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateScrubPlan(plan int) error {
|
|
if plan < 1 || plan > 99 {
|
|
return fmt.Errorf("scrub plan must be between 1 and 99, got %d", plan)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ParseSnapraidDataDirs(raw string) []string {
|
|
if raw == "" {
|
|
return nil
|
|
}
|
|
var dirs []string
|
|
for _, d := range strings.Split(raw, ",") {
|
|
d = strings.TrimSpace(d)
|
|
if d != "" {
|
|
dirs = append(dirs, d)
|
|
}
|
|
}
|
|
return dirs
|
|
}
|