* fix: use os.Lstat for symlink-safe mtime and permission checks On NixOS and other systems using symlinks heavily (e.g., home-manager), os.Stat follows symlinks and returns the target's metadata. This causes: 1. False staleness detection when JSONL is symlinked - mtime of target changes unpredictably when symlinks are recreated 2. os.Chmod failing or changing wrong file's permissions when target is in read-only location (e.g., /nix/store) 3. os.Chtimes modifying target's times instead of the symlink itself Changes: - autoimport.go: Use Lstat for JSONL mtime in CheckStaleness() - import.go: Use Lstat in TouchDatabaseFile() for JSONL mtime - export.go: Skip chmod for symlinked files - multirepo.go: Use Lstat for JSONL mtime cache - multirepo_export.go: Use Lstat for mtime, skip chmod for symlinks - doctor/fix/permissions.go: Skip permission fixes for symlinked paths These changes are safe cross-platform: - On systems without symlinks, Lstat behaves identically to Stat - Symlink permission bits are ignored on Unix anyway - The extra Lstat syscall overhead is negligible Fixes symlink-related data loss on NixOS. See GitHub issue #379. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * test: add symlink behavior tests for NixOS compatibility Add tests that verify symlink handling behavior: - TestCheckStaleness_SymlinkedJSONL: verifies mtime detection uses symlink's own mtime (os.Lstat), not target's mtime (os.Stat) - TestPermissions_SkipsSymlinkedBeadsDir: verifies chmod is skipped for symlinked .beads directories - TestPermissions_SkipsSymlinkedDatabase: verifies chmod is skipped for symlinked database files while still fixing .beads dir perms Also adds devShell to flake.nix for local development with go, gopls, golangci-lint, and sqlite tools. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package fix
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Permissions fixes file permission issues in the .beads directory
|
|
func Permissions(path string) error {
|
|
// Validate workspace
|
|
if err := validateBeadsWorkspace(path); err != nil {
|
|
return err
|
|
}
|
|
|
|
beadsDir := filepath.Join(path, ".beads")
|
|
|
|
// Check if .beads/ directory exists
|
|
// Use Lstat to detect symlinks - we shouldn't chmod symlinked directories
|
|
// as this would change the target's permissions (problematic on NixOS).
|
|
info, err := os.Lstat(beadsDir)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to stat .beads directory: %w", err)
|
|
}
|
|
|
|
// Skip permission fixes for symlinked .beads directories (common on NixOS with home-manager)
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
return nil // Symlink permissions are not meaningful on Unix
|
|
}
|
|
|
|
// Ensure .beads directory has exactly 0700 permissions (owner rwx only)
|
|
expectedDirMode := os.FileMode(0700)
|
|
if info.Mode().Perm() != expectedDirMode {
|
|
if err := os.Chmod(beadsDir, expectedDirMode); err != nil {
|
|
return fmt.Errorf("failed to fix .beads directory permissions: %w", err)
|
|
}
|
|
}
|
|
|
|
// Fix permissions on database file if it exists
|
|
// Use Lstat to detect symlinks - skip chmod for symlinked database files
|
|
dbPath := filepath.Join(beadsDir, "beads.db")
|
|
if dbInfo, err := os.Lstat(dbPath); err == nil {
|
|
// Skip permission fixes for symlinked database files (NixOS)
|
|
if dbInfo.Mode()&os.ModeSymlink != 0 {
|
|
return nil
|
|
}
|
|
|
|
// Ensure database has exactly 0600 permissions (owner rw only)
|
|
expectedFileMode := os.FileMode(0600)
|
|
currentPerms := dbInfo.Mode().Perm()
|
|
requiredPerms := os.FileMode(0600)
|
|
|
|
// Check if we have both read and write for owner
|
|
if currentPerms&requiredPerms != requiredPerms {
|
|
if err := os.Chmod(dbPath, expectedFileMode); err != nil {
|
|
return fmt.Errorf("failed to fix database permissions: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|