devbox/internal/hub/sharename.go
shoemoney 5bfccdb297 fix(M7): review fixes — pidfile O_EXCL, publish race, backoff reset 🐛
Code-review pass on M6.5+M7:
- pidfile: claim via O_CREATE|O_EXCL and arbitrate live-vs-stale, so two
  `devbox start` invocations can't both win the guard and run concurrently
- hub: serialize publish (publishMu) so a case-clash check + create is atomic;
  reject share names ending in '.' (Windows strips it → silent collision)
- daemon: reset SSE backoff only after a genuinely healthy stream (2m), not the
  30s ceiling, so a hub that recycles connections doesn't pin a 30s event lag
- document that restore/deploy's delete pass is ignore/guard-filtered on purpose
  (never delete a user's gitignored runtime files or local secrets)
- test: pidfile claim/refuse/stale-reclaim arbitration
2026-06-23 01:37:58 -05:00

56 lines
2.1 KiB
Go

package hub
import (
"fmt"
"strings"
)
// reservedNames are Windows device names a share must not use (case-insensitive):
// a share name can become a directory on a Windows client, where these are magic.
var reservedNames = func() map[string]bool {
m := map[string]bool{"con": true, "prn": true, "aux": true, "nul": true}
for i := 1; i <= 9; i++ {
m[fmt.Sprintf("com%d", i)] = true
m[fmt.Sprintf("lpt%d", i)] = true
}
return m
}()
// ValidateShareName rejects share names that are unsafe as a path component or
// would collide with an existing share on a case-insensitive filesystem (macOS,
// Windows). existing is the set of names already on the hub; an exact match is
// allowed (publish is idempotent) — only a different-case clash is rejected.
func ValidateShareName(name string, existing []string) error {
switch {
case name == "":
return fmt.Errorf("share name is empty")
case strings.TrimSpace(name) != name:
return fmt.Errorf("share name %q has leading/trailing whitespace", name)
case len(name) > 128:
return fmt.Errorf("share name %q is too long (max 128 bytes)", name)
case name == "." || name == "..":
return fmt.Errorf("share name %q is reserved", name)
case strings.HasPrefix(name, ".") || strings.HasPrefix(name, "-"):
return fmt.Errorf("share name %q must not start with '.' or '-'", name)
case strings.HasSuffix(name, "."):
return fmt.Errorf("share name %q must not end with '.' (Windows strips it)", name)
}
for _, r := range name {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9':
case r == '.', r == '_', r == '-':
default:
return fmt.Errorf("share name %q has invalid character %q (allowed: letters, digits, . _ -)", name, r)
}
}
if reservedNames[strings.ToLower(name)] {
return fmt.Errorf("share name %q is a reserved device name", name)
}
lower := strings.ToLower(name)
for _, e := range existing {
if e != name && strings.ToLower(e) == lower {
return fmt.Errorf("share name %q case-clashes with existing share %q (collides on case-insensitive filesystems)", name, e)
}
}
return nil
}