| 1 | //go:build unix |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | // A controller owns the repository it serves. One controller serves a |
| 6 | // repository at a time: watch takes an exclusive flock on the repository's |
| 7 | // own directory before it sweeps scratch or runs a pass, and holds it until |
| 8 | // it exits. A second watch over the same repository finds the lock held and |
| 9 | // stops before it sweeps or passes. |
| 10 | // |
| 11 | // The lock is on the repository's directory rather than a file in the wake |
| 12 | // directory. The wake directory's writers may create and remove entries in |
| 13 | // it, which is how they mark it, so a lock file there could be removed and |
| 14 | // made again under a running controller, and a missing wake directory |
| 15 | // would leave nothing to lock. The repository's directory is there |
| 16 | // whenever the controller can do anything, and is the thing a second |
| 17 | // controller must not sweep or land into. |
| 18 | // |
| 19 | // A lock on a directory is a lock on the directory the path named when it |
| 20 | // was opened. So the lock counts only while the path still names that |
| 21 | // directory: it is checked after the lock is taken, before the sweep, and |
| 22 | // before every pass. A path that names another directory stops the |
| 23 | // controller, which then holds nothing the next controller has to wait |
| 24 | // for. |
| 25 | |
| 26 | import ( |
| 27 | "errors" |
| 28 | "fmt" |
| 29 | "os" |
| 30 | "syscall" |
| 31 | ) |
| 32 | |
| 33 | // ownTries is how many times ownRepo opens and locks the repository while |
| 34 | // the path comes to name another directory between the open and the check. |
| 35 | const ownTries = 3 |
| 36 | |
| 37 | // ownLocked, when set, runs once ownRepo has locked what it opened and |
| 38 | // before it checks the path. Tests set it to replace the directory there. |
| 39 | var ownLocked func() |
| 40 | |
| 41 | // repoLock is a controller's hold on the repository it serves. |
| 42 | type repoLock struct { |
| 43 | f *os.File |
| 44 | path string |
| 45 | } |
| 46 | |
| 47 | // ownRepo takes the repository's lock, or says why it cannot. Every |
| 48 | // descriptor it opens and does not return is closed. |
| 49 | func ownRepo(repo string) (*repoLock, error) { |
| 50 | for try := 1; ; try++ { |
| 51 | f, err := os.Open(repo) |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("%s: could not open it to lock it: %w", repo, err) |
| 54 | } |
| 55 | if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { |
| 56 | f.Close() |
| 57 | if errors.Is(err, syscall.EWOULDBLOCK) { |
| 58 | return nil, fmt.Errorf("%s: %w; one controller serves a repository", repo, errLockHeld) |
| 59 | } |
| 60 | return nil, fmt.Errorf("%s: could not lock it: %w", repo, err) |
| 61 | } |
| 62 | if ownLocked != nil { |
| 63 | ownLocked() |
| 64 | } |
| 65 | r := &repoLock{f: f, path: repo} |
| 66 | err = r.check() |
| 67 | if err == nil { |
| 68 | return r, nil |
| 69 | } |
| 70 | f.Close() |
| 71 | if try == ownTries { |
| 72 | return nil, fmt.Errorf("%s: another directory took its place each of %d times it was locked, so this controller does not serve it", repo, ownTries) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // check says whether the path still names the directory the lock is on. |
| 78 | func (r *repoLock) check() error { |
| 79 | if r == nil { |
| 80 | return nil |
| 81 | } |
| 82 | held, err := r.f.Stat() |
| 83 | if err != nil { |
| 84 | return fmt.Errorf("%s: could not read the directory it locked: %w", r.path, err) |
| 85 | } |
| 86 | now, err := os.Stat(r.path) |
| 87 | if err != nil || !os.SameFile(held, now) { |
| 88 | return fmt.Errorf("%s is no longer the directory this controller locked; it stops, and serves it again only from a new start", r.path) |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | func (r *repoLock) Close() error { |
| 94 | if r == nil { |
| 95 | return nil |
| 96 | } |
| 97 | return r.f.Close() |
| 98 | } |