You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
5.9 KiB

  1. # File system notifications for Go
  2. [![GoDoc](https://godoc.org/github.com/fsnotify/fsnotify?status.svg)](https://godoc.org/github.com/fsnotify/fsnotify) [![Go Report Card](https://goreportcard.com/badge/github.com/fsnotify/fsnotify)](https://goreportcard.com/report/github.com/fsnotify/fsnotify)
  3. fsnotify utilizes [golang.org/x/sys](https://godoc.org/golang.org/x/sys) rather than `syscall` from the standard library. Ensure you have the latest version installed by running:
  4. ```console
  5. go get -u golang.org/x/sys/...
  6. ```
  7. Cross platform: Windows, Linux, BSD and macOS.
  8. | Adapter | OS | Status |
  9. | --------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
  10. | inotify | Linux 2.6.27 or later, Android\* | Supported [![Build Status](https://travis-ci.org/fsnotify/fsnotify.svg?branch=master)](https://travis-ci.org/fsnotify/fsnotify) |
  11. | kqueue | BSD, macOS, iOS\* | Supported [![Build Status](https://travis-ci.org/fsnotify/fsnotify.svg?branch=master)](https://travis-ci.org/fsnotify/fsnotify) |
  12. | ReadDirectoryChangesW | Windows | Supported [![Build Status](https://travis-ci.org/fsnotify/fsnotify.svg?branch=master)](https://travis-ci.org/fsnotify/fsnotify) |
  13. | FSEvents | macOS | [Planned](https://github.com/fsnotify/fsnotify/issues/11) |
  14. | FEN | Solaris 11 | [In Progress](https://github.com/fsnotify/fsnotify/issues/12) |
  15. | fanotify | Linux 2.6.37+ | [Planned](https://github.com/fsnotify/fsnotify/issues/114) |
  16. | USN Journals | Windows | [Maybe](https://github.com/fsnotify/fsnotify/issues/53) |
  17. | Polling | *All* | [Maybe](https://github.com/fsnotify/fsnotify/issues/9) |
  18. \* Android and iOS are untested.
  19. Please see [the documentation](https://godoc.org/github.com/fsnotify/fsnotify) and consult the [FAQ](#faq) for usage information.
  20. ## API stability
  21. fsnotify is a fork of [howeyc/fsnotify](https://godoc.org/github.com/howeyc/fsnotify) with a new API as of v1.0. The API is based on [this design document](http://goo.gl/MrYxyA).
  22. All [releases](https://github.com/fsnotify/fsnotify/releases) are tagged based on [Semantic Versioning](http://semver.org/). Further API changes are [planned](https://github.com/fsnotify/fsnotify/milestones), and will be tagged with a new major revision number.
  23. Go 1.6 supports dependencies located in the `vendor/` folder. Unless you are creating a library, it is recommended that you copy fsnotify into `vendor/github.com/fsnotify/fsnotify` within your project, and likewise for `golang.org/x/sys`.
  24. ## Usage
  25. ```go
  26. package main
  27. import (
  28. "log"
  29. "github.com/fsnotify/fsnotify"
  30. )
  31. func main() {
  32. watcher, err := fsnotify.NewWatcher()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. defer watcher.Close()
  37. done := make(chan bool)
  38. go func() {
  39. for {
  40. select {
  41. case event, ok := <-watcher.Events:
  42. if !ok {
  43. return
  44. }
  45. log.Println("event:", event)
  46. if event.Op&fsnotify.Write == fsnotify.Write {
  47. log.Println("modified file:", event.Name)
  48. }
  49. case err, ok := <-watcher.Errors:
  50. if !ok {
  51. return
  52. }
  53. log.Println("error:", err)
  54. }
  55. }
  56. }()
  57. err = watcher.Add("/tmp/foo")
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. <-done
  62. }
  63. ```
  64. ## Contributing
  65. Please refer to [CONTRIBUTING][] before opening an issue or pull request.
  66. ## Example
  67. See [example_test.go](https://github.com/fsnotify/fsnotify/blob/master/example_test.go).
  68. ## FAQ
  69. **When a file is moved to another directory is it still being watched?**
  70. No (it shouldn't be, unless you are watching where it was moved to).
  71. **When I watch a directory, are all subdirectories watched as well?**
  72. No, you must add watches for any directory you want to watch (a recursive watcher is on the roadmap [#18][]).
  73. **Do I have to watch the Error and Event channels in a separate goroutine?**
  74. As of now, yes. Looking into making this single-thread friendly (see [howeyc #7][#7])
  75. **Why am I receiving multiple events for the same file on OS X?**
  76. Spotlight indexing on OS X can result in multiple events (see [howeyc #62][#62]). A temporary workaround is to add your folder(s) to the *Spotlight Privacy settings* until we have a native FSEvents implementation (see [#11][]).
  77. **How many files can be watched at once?**
  78. There are OS-specific limits as to how many watches can be created:
  79. * Linux: /proc/sys/fs/inotify/max_user_watches contains the limit, reaching this limit results in a "no space left on device" error.
  80. * BSD / OSX: sysctl variables "kern.maxfiles" and "kern.maxfilesperproc", reaching these limits results in a "too many open files" error.
  81. **Why don't notifications work with NFS filesystems or filesystem in userspace (FUSE)?**
  82. fsnotify requires support from underlying OS to work. The current NFS protocol does not provide network level support for file notifications.
  83. [#62]: https://github.com/howeyc/fsnotify/issues/62
  84. [#18]: https://github.com/fsnotify/fsnotify/issues/18
  85. [#11]: https://github.com/fsnotify/fsnotify/issues/11
  86. [#7]: https://github.com/howeyc/fsnotify/issues/7
  87. [contributing]: https://github.com/fsnotify/fsnotify/blob/master/CONTRIBUTING.md
  88. ## Related Projects
  89. * [notify](https://github.com/rjeczalik/notify)
  90. * [fsevents](https://github.com/fsnotify/fsevents)