style: add blank lines between declarations and statement blocks
Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec0297fd23
commit
a6fda8f8d3
25 files changed
+929
-41
No files matched your search
@@ -41,22 +41,27 @@ func ReadConfig(path string) (Config, error) {
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
st, err := f.Stat()
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
if !st.Mode().IsRegular() || st.Size() > 1<<20 {
|
||||
return c, errors.New("config must be a regular file of at most 1 MiB")
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(io.LimitReader(f, 1<<20))
|
||||
dec.DisallowUnknownFields()
|
||||
if err = dec.Decode(&c); err != nil {
|
||||
return c, fmt.Errorf("config: %w", err)
|
||||
}
|
||||
|
||||
if err = dec.Decode(new(any)); !errors.Is(err, io.EOF) {
|
||||
return c, errors.New("config must contain exactly one JSON object")
|
||||
}
|
||||
|
||||
return c, c.Validate()
|
||||
}
|
||||
|
||||
@@ -71,9 +76,11 @@ func (c Config) Validate() error {
|
||||
if c.Backend != "" && c.Backend != "rsync" && c.Backend != "native" {
|
||||
return errors.New("backend must be native or rsync")
|
||||
}
|
||||
|
||||
if c.Workers < 0 || c.Workers > 64 {
|
||||
return errors.New("workers must be between 0 and 64 (0 selects the default)")
|
||||
}
|
||||
|
||||
for _, p := range []struct {
|
||||
name string
|
||||
e Endpoint
|
||||
@@ -81,30 +88,37 @@ func (c Config) Validate() error {
|
||||
if p.e.User != "" && (!strings.HasPrefix(p.e.User, "client.") || !validName(p.e.User)) {
|
||||
return fmt.Errorf("%s: user must be a client identity", p.name)
|
||||
}
|
||||
|
||||
for _, s := range []string{p.e.Filesystem, p.e.Subvolume} {
|
||||
if !validName(s) {
|
||||
return fmt.Errorf("%s: invalid filesystem or subvolume name", p.name)
|
||||
}
|
||||
}
|
||||
|
||||
if p.e.Group != "" && !validName(p.e.Group) {
|
||||
return fmt.Errorf("%s: invalid group", p.name)
|
||||
}
|
||||
|
||||
paths := []string{p.e.Config, p.e.Keyring}
|
||||
if c.Backend != "native" || p.e.Mount != "" {
|
||||
paths = append(paths, p.e.Mount)
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
if !filepath.IsAbs(path) || filepath.Clean(path) != path {
|
||||
return fmt.Errorf("%s: paths must be absolute and clean: %q", p.name, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.BandwidthKiB < 0 || c.BandwidthKiB > (1<<63-1)/1024 {
|
||||
return errors.New("bandwidth_kib must be nonnegative and fit in bytes per second")
|
||||
}
|
||||
|
||||
if c.Backend != "native" && overlaps(c.Source.Mount, c.Target.Mount) {
|
||||
return errors.New("source and target mounts must not overlap")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -112,6 +126,7 @@ func beneath(root, path string) bool {
|
||||
rel, err := filepath.Rel(root, path)
|
||||
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
|
||||
}
|
||||
|
||||
func overlaps(a, b string) bool { return beneath(a, b) || beneath(b, a) }
|
||||
|
||||
// dataPath resolves cephPath to a real directory below the filesystem mount
|
||||
@@ -120,20 +135,25 @@ func dataPath(root, cephPath string) (string, error) {
|
||||
if !filepath.IsAbs(cephPath) || filepath.Clean(cephPath) != cephPath || cephPath == "/" {
|
||||
return "", fmt.Errorf("invalid Ceph path %q", cephPath)
|
||||
}
|
||||
|
||||
p := filepath.Join(root, strings.TrimPrefix(cephPath, "/"))
|
||||
resolved, err := filepath.EvalSymlinks(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if resolved != p || !beneath(root, p) {
|
||||
return "", fmt.Errorf("path escapes mount or contains symlinks: %q", p)
|
||||
}
|
||||
|
||||
st, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !st.IsDir() {
|
||||
return "", fmt.Errorf("not a directory: %s", p)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
Reference in new issue
Block a user