| 1 | //go:build linux |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "encoding/binary" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "syscall" |
| 12 | ) |
| 13 | |
| 14 | // inotifyWatch watches what git's files ref storage changes when main |
| 15 | // moves: refs/heads, where main's loose ref is created, replaced by a |
| 16 | // rename or deleted, and the git directory, where packed-refs is replaced |
| 17 | // by a rename, as git pack-refs and the deletion of a packed ref do. Every |
| 18 | // other name in those directories, the locks git takes included, is |
| 19 | // ignored. |
| 20 | // |
| 21 | // A systemd path unit could not stand in for this: PathChanged= holds no |
| 22 | // watch while the unit it started runs, so a move during a run would be |
| 23 | // seen only at the next move. The bridge turns these moves into the |
| 24 | // request file, which a path unit's PathExists= does follow. |
| 25 | // |
| 26 | // The watches stay on the directories they were made on. So a watched |
| 27 | // directory deleted or moved away, or refs or refs/heads created, renamed |
| 28 | // or deleted in its parent, fails the watch, as does an overflow of the |
| 29 | // kernel's queue, which may have dropped any of those. The bridge then |
| 30 | // sets the watch up again. refs and refs/heads are each watched only while |
| 31 | // they exist, as in a repository whose refs are all packed. Each one's |
| 32 | // parent is watched first, so one that appears after its own watch was |
| 33 | // refused for its absence shows in its parent's watch, which fails, and the |
| 34 | // next watch is made on it. A goroutine reads the inotify descriptor |
| 35 | // through the runtime's poller, so closing it ends the read. |
| 36 | type inotifyWatch struct { |
| 37 | f *os.File |
| 38 | gitWd int32 |
| 39 | refsWd int32 // -1 while refs is absent |
| 40 | headWd int32 // -1 while refs/heads is absent |
| 41 | note chan struct{} |
| 42 | end chan error |
| 43 | } |
| 44 | |
| 45 | func watchRefs(gitDir string) (watcher, error) { |
| 46 | fd, err := syscall.InotifyInit1(syscall.IN_CLOEXEC | syscall.IN_NONBLOCK) |
| 47 | if err != nil { |
| 48 | return nil, os.NewSyscallError("inotify_init1", err) |
| 49 | } |
| 50 | w := &inotifyWatch{ |
| 51 | f: os.NewFile(uintptr(fd), "inotify"), |
| 52 | note: make(chan struct{}, 1), |
| 53 | end: make(chan error, 1), |
| 54 | } |
| 55 | const mask = syscall.IN_CREATE | syscall.IN_MOVED_TO | syscall.IN_MOVED_FROM | syscall.IN_DELETE | |
| 56 | syscall.IN_CLOSE_WRITE | syscall.IN_DELETE_SELF | syscall.IN_MOVE_SELF | syscall.IN_ONLYDIR |
| 57 | add := func(dir string) (int32, error) { |
| 58 | wd, err := syscall.InotifyAddWatch(fd, dir, mask) |
| 59 | if err != nil { |
| 60 | return -1, fmt.Errorf("%s: %w", dir, os.NewSyscallError("inotify_add_watch", err)) |
| 61 | } |
| 62 | return int32(wd), nil |
| 63 | } |
| 64 | w.refsWd, w.headWd = -1, -1 |
| 65 | w.gitWd, err = add(gitDir) |
| 66 | if err == nil { |
| 67 | w.refsWd, err = add(filepath.Join(gitDir, "refs")) |
| 68 | } |
| 69 | if err == nil { |
| 70 | w.headWd, err = add(filepath.Join(gitDir, "refs", "heads")) |
| 71 | } |
| 72 | if errors.Is(err, syscall.ENOENT) && w.gitWd >= 0 { |
| 73 | err = nil |
| 74 | } |
| 75 | if err != nil { |
| 76 | w.f.Close() |
| 77 | return nil, err |
| 78 | } |
| 79 | go w.read() |
| 80 | return w, nil |
| 81 | } |
| 82 | |
| 83 | func (w *inotifyWatch) events() <-chan struct{} { return w.note } |
| 84 | func (w *inotifyWatch) failed() <-chan error { return w.end } |
| 85 | func (w *inotifyWatch) close() { w.f.Close() } |
| 86 | |
| 87 | func (w *inotifyWatch) read() { |
| 88 | buf := make([]byte, 64*(syscall.SizeofInotifyEvent+syscall.NAME_MAX+1)) |
| 89 | for { |
| 90 | n, err := w.f.Read(buf) |
| 91 | if err != nil { |
| 92 | w.end <- fmt.Errorf("reading its events: %w", err) |
| 93 | return |
| 94 | } |
| 95 | for off := 0; off+syscall.SizeofInotifyEvent <= n; { |
| 96 | wd := int32(binary.NativeEndian.Uint32(buf[off:])) |
| 97 | mask := binary.NativeEndian.Uint32(buf[off+4:]) |
| 98 | size := int(binary.NativeEndian.Uint32(buf[off+12:])) |
| 99 | name := string(trimNUL(buf[off+syscall.SizeofInotifyEvent : min(n, off+syscall.SizeofInotifyEvent+size)])) |
| 100 | off += syscall.SizeofInotifyEvent + size |
| 101 | self := uint32(syscall.IN_IGNORED | syscall.IN_DELETE_SELF | syscall.IN_MOVE_SELF | syscall.IN_UNMOUNT) |
| 102 | switch { |
| 103 | case mask&syscall.IN_Q_OVERFLOW != 0: |
| 104 | w.end <- errDropped |
| 105 | return |
| 106 | case mask&self != 0: |
| 107 | w.end <- errors.New("a watched directory was deleted or moved") |
| 108 | return |
| 109 | case wd == w.gitWd && name == "refs": |
| 110 | w.end <- errors.New("the refs directory appeared, went or was replaced") |
| 111 | return |
| 112 | case wd == w.refsWd && name == "heads": |
| 113 | w.end <- errors.New("refs/heads appeared, went or was replaced") |
| 114 | return |
| 115 | case wd == w.gitWd && name == "packed-refs", wd == w.headWd && name == "main": |
| 116 | select { |
| 117 | case w.note <- struct{}{}: |
| 118 | default: |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func trimNUL(b []byte) []byte { |
| 126 | for len(b) > 0 && b[len(b)-1] == 0 { |
| 127 | b = b[:len(b)-1] |
| 128 | } |
| 129 | return b |
| 130 | } |