A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

inotify.go 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build linux
  5. package fsnotify
  6. import (
  7. "errors"
  8. "fmt"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "sync"
  14. "unsafe"
  15. "golang.org/x/sys/unix"
  16. )
  17. // Watcher watches a set of files, delivering events to a channel.
  18. type Watcher struct {
  19. Events chan Event
  20. Errors chan error
  21. mu sync.Mutex // Map access
  22. fd int
  23. poller *fdPoller
  24. watches map[string]*watch // Map of inotify watches (key: path)
  25. paths map[int]string // Map of watched paths (key: watch descriptor)
  26. done chan struct{} // Channel for sending a "quit message" to the reader goroutine
  27. doneResp chan struct{} // Channel to respond to Close
  28. }
  29. // NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
  30. func NewWatcher() (*Watcher, error) {
  31. // Create inotify fd
  32. fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
  33. if fd == -1 {
  34. return nil, errno
  35. }
  36. // Create epoll
  37. poller, err := newFdPoller(fd)
  38. if err != nil {
  39. unix.Close(fd)
  40. return nil, err
  41. }
  42. w := &Watcher{
  43. fd: fd,
  44. poller: poller,
  45. watches: make(map[string]*watch),
  46. paths: make(map[int]string),
  47. Events: make(chan Event),
  48. Errors: make(chan error),
  49. done: make(chan struct{}),
  50. doneResp: make(chan struct{}),
  51. }
  52. go w.readEvents()
  53. return w, nil
  54. }
  55. func (w *Watcher) isClosed() bool {
  56. select {
  57. case <-w.done:
  58. return true
  59. default:
  60. return false
  61. }
  62. }
  63. // Close removes all watches and closes the events channel.
  64. func (w *Watcher) Close() error {
  65. if w.isClosed() {
  66. return nil
  67. }
  68. // Send 'close' signal to goroutine, and set the Watcher to closed.
  69. close(w.done)
  70. // Wake up goroutine
  71. w.poller.wake()
  72. // Wait for goroutine to close
  73. <-w.doneResp
  74. return nil
  75. }
  76. // Add starts watching the named file or directory (non-recursively).
  77. func (w *Watcher) Add(name string) error {
  78. name = filepath.Clean(name)
  79. if w.isClosed() {
  80. return errors.New("inotify instance already closed")
  81. }
  82. const agnosticEvents = unix.IN_MOVED_TO | unix.IN_MOVED_FROM |
  83. unix.IN_CREATE | unix.IN_ATTRIB | unix.IN_MODIFY |
  84. unix.IN_MOVE_SELF | unix.IN_DELETE | unix.IN_DELETE_SELF
  85. var flags uint32 = agnosticEvents
  86. w.mu.Lock()
  87. defer w.mu.Unlock()
  88. watchEntry := w.watches[name]
  89. if watchEntry != nil {
  90. flags |= watchEntry.flags | unix.IN_MASK_ADD
  91. }
  92. wd, errno := unix.InotifyAddWatch(w.fd, name, flags)
  93. if wd == -1 {
  94. return errno
  95. }
  96. if watchEntry == nil {
  97. w.watches[name] = &watch{wd: uint32(wd), flags: flags}
  98. w.paths[wd] = name
  99. } else {
  100. watchEntry.wd = uint32(wd)
  101. watchEntry.flags = flags
  102. }
  103. return nil
  104. }
  105. // Remove stops watching the named file or directory (non-recursively).
  106. func (w *Watcher) Remove(name string) error {
  107. name = filepath.Clean(name)
  108. // Fetch the watch.
  109. w.mu.Lock()
  110. defer w.mu.Unlock()
  111. watch, ok := w.watches[name]
  112. // Remove it from inotify.
  113. if !ok {
  114. return fmt.Errorf("can't remove non-existent inotify watch for: %s", name)
  115. }
  116. // We successfully removed the watch if InotifyRmWatch doesn't return an
  117. // error, we need to clean up our internal state to ensure it matches
  118. // inotify's kernel state.
  119. delete(w.paths, int(watch.wd))
  120. delete(w.watches, name)
  121. // inotify_rm_watch will return EINVAL if the file has been deleted;
  122. // the inotify will already have been removed.
  123. // watches and pathes are deleted in ignoreLinux() implicitly and asynchronously
  124. // by calling inotify_rm_watch() below. e.g. readEvents() goroutine receives IN_IGNORE
  125. // so that EINVAL means that the wd is being rm_watch()ed or its file removed
  126. // by another thread and we have not received IN_IGNORE event.
  127. success, errno := unix.InotifyRmWatch(w.fd, watch.wd)
  128. if success == -1 {
  129. // TODO: Perhaps it's not helpful to return an error here in every case.
  130. // the only two possible errors are:
  131. // EBADF, which happens when w.fd is not a valid file descriptor of any kind.
  132. // EINVAL, which is when fd is not an inotify descriptor or wd is not a valid watch descriptor.
  133. // Watch descriptors are invalidated when they are removed explicitly or implicitly;
  134. // explicitly by inotify_rm_watch, implicitly when the file they are watching is deleted.
  135. return errno
  136. }
  137. return nil
  138. }
  139. type watch struct {
  140. wd uint32 // Watch descriptor (as returned by the inotify_add_watch() syscall)
  141. flags uint32 // inotify flags of this watch (see inotify(7) for the list of valid flags)
  142. }
  143. // readEvents reads from the inotify file descriptor, converts the
  144. // received events into Event objects and sends them via the Events channel
  145. func (w *Watcher) readEvents() {
  146. var (
  147. buf [unix.SizeofInotifyEvent * 4096]byte // Buffer for a maximum of 4096 raw events
  148. n int // Number of bytes read with read()
  149. errno error // Syscall errno
  150. ok bool // For poller.wait
  151. )
  152. defer close(w.doneResp)
  153. defer close(w.Errors)
  154. defer close(w.Events)
  155. defer unix.Close(w.fd)
  156. defer w.poller.close()
  157. for {
  158. // See if we have been closed.
  159. if w.isClosed() {
  160. return
  161. }
  162. ok, errno = w.poller.wait()
  163. if errno != nil {
  164. select {
  165. case w.Errors <- errno:
  166. case <-w.done:
  167. return
  168. }
  169. continue
  170. }
  171. if !ok {
  172. continue
  173. }
  174. n, errno = unix.Read(w.fd, buf[:])
  175. // If a signal interrupted execution, see if we've been asked to close, and try again.
  176. // http://man7.org/linux/man-pages/man7/signal.7.html :
  177. // "Before Linux 3.8, reads from an inotify(7) file descriptor were not restartable"
  178. if errno == unix.EINTR {
  179. continue
  180. }
  181. // unix.Read might have been woken up by Close. If so, we're done.
  182. if w.isClosed() {
  183. return
  184. }
  185. if n < unix.SizeofInotifyEvent {
  186. var err error
  187. if n == 0 {
  188. // If EOF is received. This should really never happen.
  189. err = io.EOF
  190. } else if n < 0 {
  191. // If an error occurred while reading.
  192. err = errno
  193. } else {
  194. // Read was too short.
  195. err = errors.New("notify: short read in readEvents()")
  196. }
  197. select {
  198. case w.Errors <- err:
  199. case <-w.done:
  200. return
  201. }
  202. continue
  203. }
  204. var offset uint32
  205. // We don't know how many events we just read into the buffer
  206. // While the offset points to at least one whole event...
  207. for offset <= uint32(n-unix.SizeofInotifyEvent) {
  208. // Point "raw" to the event in the buffer
  209. raw := (*unix.InotifyEvent)(unsafe.Pointer(&buf[offset]))
  210. mask := uint32(raw.Mask)
  211. nameLen := uint32(raw.Len)
  212. if mask&unix.IN_Q_OVERFLOW != 0 {
  213. select {
  214. case w.Errors <- ErrEventOverflow:
  215. case <-w.done:
  216. return
  217. }
  218. }
  219. // If the event happened to the watched directory or the watched file, the kernel
  220. // doesn't append the filename to the event, but we would like to always fill the
  221. // the "Name" field with a valid filename. We retrieve the path of the watch from
  222. // the "paths" map.
  223. w.mu.Lock()
  224. name, ok := w.paths[int(raw.Wd)]
  225. // IN_DELETE_SELF occurs when the file/directory being watched is removed.
  226. // This is a sign to clean up the maps, otherwise we are no longer in sync
  227. // with the inotify kernel state which has already deleted the watch
  228. // automatically.
  229. if ok && mask&unix.IN_DELETE_SELF == unix.IN_DELETE_SELF {
  230. delete(w.paths, int(raw.Wd))
  231. delete(w.watches, name)
  232. }
  233. w.mu.Unlock()
  234. if nameLen > 0 {
  235. // Point "bytes" at the first byte of the filename
  236. bytes := (*[unix.PathMax]byte)(unsafe.Pointer(&buf[offset+unix.SizeofInotifyEvent]))
  237. // The filename is padded with NULL bytes. TrimRight() gets rid of those.
  238. name += "/" + strings.TrimRight(string(bytes[0:nameLen]), "\000")
  239. }
  240. event := newEvent(name, mask)
  241. // Send the events that are not ignored on the events channel
  242. if !event.ignoreLinux(mask) {
  243. select {
  244. case w.Events <- event:
  245. case <-w.done:
  246. return
  247. }
  248. }
  249. // Move to the next event in the buffer
  250. offset += unix.SizeofInotifyEvent + nameLen
  251. }
  252. }
  253. }
  254. // Certain types of events can be "ignored" and not sent over the Events
  255. // channel. Such as events marked ignore by the kernel, or MODIFY events
  256. // against files that do not exist.
  257. func (e *Event) ignoreLinux(mask uint32) bool {
  258. // Ignore anything the inotify API says to ignore
  259. if mask&unix.IN_IGNORED == unix.IN_IGNORED {
  260. return true
  261. }
  262. // If the event is not a DELETE or RENAME, the file must exist.
  263. // Otherwise the event is ignored.
  264. // *Note*: this was put in place because it was seen that a MODIFY
  265. // event was sent after the DELETE. This ignores that MODIFY and
  266. // assumes a DELETE will come or has come if the file doesn't exist.
  267. if !(e.Op&Remove == Remove || e.Op&Rename == Rename) {
  268. _, statErr := os.Lstat(e.Name)
  269. return os.IsNotExist(statErr)
  270. }
  271. return false
  272. }
  273. // newEvent returns an platform-independent Event based on an inotify mask.
  274. func newEvent(name string, mask uint32) Event {
  275. e := Event{Name: name}
  276. if mask&unix.IN_CREATE == unix.IN_CREATE || mask&unix.IN_MOVED_TO == unix.IN_MOVED_TO {
  277. e.Op |= Create
  278. }
  279. if mask&unix.IN_DELETE_SELF == unix.IN_DELETE_SELF || mask&unix.IN_DELETE == unix.IN_DELETE {
  280. e.Op |= Remove
  281. }
  282. if mask&unix.IN_MODIFY == unix.IN_MODIFY {
  283. e.Op |= Write
  284. }
  285. if mask&unix.IN_MOVE_SELF == unix.IN_MOVE_SELF || mask&unix.IN_MOVED_FROM == unix.IN_MOVED_FROM {
  286. e.Op |= Rename
  287. }
  288. if mask&unix.IN_ATTRIB == unix.IN_ATTRIB {
  289. e.Op |= Chmod
  290. }
  291. return e
  292. }