package worktree import ( "context" "errors" "fmt" "io/fs" "os" "path/filepath" "strings" "time" "golang.org/x/sys/unix" ) const mirrorName = ".mirror" // lockMirror takes the exclusive lock on mirror, which every Manager for the // same work directory shares because it is a file, not process memory. With // wait false a held lock fails with unix.EWOULDBLOCK instead of blocking. Taking it // to clone marks the mirror used, which PruneMirrors measures idleness by. func lockMirror(mirror string, wait bool) (unlock func(), err error) { path := mirror + ".lock" if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return nil, fmt.Errorf("create mirror parent: %w", err) } file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600) if err != nil { return nil, fmt.Errorf("open mirror lock: %w", err) } how := unix.LOCK_EX if !wait { how |= unix.LOCK_NB } if err := unix.Flock(int(file.Fd()), how); err != nil { _ = file.Close() return nil, fmt.Errorf("lock mirror: %w", err) } if wait { now := time.Now() _ = os.Chtimes(path, now, now) } return func() { _ = unix.Flock(int(file.Fd()), unix.LOCK_UN) _ = file.Close() }, nil } // PruneMirrors removes every repository mirror under root that no task clone // borrows from and that no clone has used for idle. A mirror is in use while // any other entry sits beside it, since task clones live next to their mirror. // It returns the mirrors removed. func PruneMirrors(ctx context.Context, root string, idle time.Duration, now time.Time) ([]string, error) { root, err := filepath.Abs(root) if err != nil { return nil, err } var removed []string err = filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { if err != nil { if errors.Is(err, fs.ErrNotExist) { return nil } return err } if err := ctx.Err(); err != nil { return err } if !entry.IsDir() || path == root { return nil } name := entry.Name() if name != mirrorName { // Task clones are deep trees; nothing to find inside one. if strings.HasPrefix(name, "issue-") || strings.HasPrefix(name, ".") { return filepath.SkipDir } return nil } done, err := pruneMirror(path, idle, now) if err != nil { return err } if done { removed = append(removed, path) } return filepath.SkipDir }) return removed, err } // pruneMirror removes mirror when it is idle and alone in its directory. func pruneMirror(mirror string, idle time.Duration, now time.Time) (bool, error) { unlock, err := lockMirror(mirror, false) if errors.Is(err, unix.EWOULDBLOCK) { return false, nil // a clone is using it right now } if err != nil { return false, err } defer unlock() siblings, err := os.ReadDir(filepath.Dir(mirror)) if err != nil { return false, err } for _, sibling := range siblings { if name := sibling.Name(); name != mirrorName && name != mirrorName+".lock" { return false, nil } } info, err := os.Stat(mirror + ".lock") if err != nil { return false, err } if now.Sub(info.ModTime()) < idle { return false, nil } // The lock file stays: removing it while held would let a later clone // lock a new file while another process still holds the old one. if err := os.RemoveAll(mirror); err != nil { return false, fmt.Errorf("remove mirror: %w", err) } return true, nil }