mkdoc.zsh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/usr/bin/env zsh
  2. [ "${ZSH_VERSION:-}" = "" ] && echo >&2 "Only works with zsh" && exit 1
  3. setopt err_exit no_unset pipefail extended_glob
  4. # Simple script to update the godoc comments on all watchers so you don't need
  5. # to update the same comment 5 times.
  6. watcher=$(<<EOF
  7. // Watcher watches a set of paths, delivering events on a channel.
  8. //
  9. // A watcher should not be copied (e.g. pass it by pointer, rather than by
  10. // value).
  11. //
  12. // # Linux notes
  13. //
  14. // When a file is removed a Remove event won't be emitted until all file
  15. // descriptors are closed, and deletes will always emit a Chmod. For example:
  16. //
  17. // fp := os.Open("file")
  18. // os.Remove("file") // Triggers Chmod
  19. // fp.Close() // Triggers Remove
  20. //
  21. // This is the event that inotify sends, so not much can be changed about this.
  22. //
  23. // The fs.inotify.max_user_watches sysctl variable specifies the upper limit
  24. // for the number of watches per user, and fs.inotify.max_user_instances
  25. // specifies the maximum number of inotify instances per user. Every Watcher you
  26. // create is an "instance", and every path you add is a "watch".
  27. //
  28. // These are also exposed in /proc as /proc/sys/fs/inotify/max_user_watches and
  29. // /proc/sys/fs/inotify/max_user_instances
  30. //
  31. // To increase them you can use sysctl or write the value to the /proc file:
  32. //
  33. // # Default values on Linux 5.18
  34. // sysctl fs.inotify.max_user_watches=124983
  35. // sysctl fs.inotify.max_user_instances=128
  36. //
  37. // To make the changes persist on reboot edit /etc/sysctl.conf or
  38. // /usr/lib/sysctl.d/50-default.conf (details differ per Linux distro; check
  39. // your distro's documentation):
  40. //
  41. // fs.inotify.max_user_watches=124983
  42. // fs.inotify.max_user_instances=128
  43. //
  44. // Reaching the limit will result in a "no space left on device" or "too many open
  45. // files" error.
  46. //
  47. // # kqueue notes (macOS, BSD)
  48. //
  49. // kqueue requires opening a file descriptor for every file that's being watched;
  50. // so if you're watching a directory with five files then that's six file
  51. // descriptors. You will run in to your system's "max open files" limit faster on
  52. // these platforms.
  53. //
  54. // The sysctl variables kern.maxfiles and kern.maxfilesperproc can be used to
  55. // control the maximum number of open files, as well as /etc/login.conf on BSD
  56. // systems.
  57. //
  58. // # Windows notes
  59. //
  60. // Paths can be added as "C:\\path\\to\\dir", but forward slashes
  61. // ("C:/path/to/dir") will also work.
  62. //
  63. // When a watched directory is removed it will always send an event for the
  64. // directory itself, but may not send events for all files in that directory.
  65. // Sometimes it will send events for all times, sometimes it will send no
  66. // events, and often only for some files.
  67. //
  68. // The default ReadDirectoryChangesW() buffer size is 64K, which is the largest
  69. // value that is guaranteed to work with SMB filesystems. If you have many
  70. // events in quick succession this may not be enough, and you will have to use
  71. // [WithBufferSize] to increase the value.
  72. EOF
  73. )
  74. new=$(<<EOF
  75. // NewWatcher creates a new Watcher.
  76. EOF
  77. )
  78. newbuffered=$(<<EOF
  79. // NewBufferedWatcher creates a new Watcher with a buffered Watcher.Events
  80. // channel.
  81. //
  82. // The main use case for this is situations with a very large number of events
  83. // where the kernel buffer size can't be increased (e.g. due to lack of
  84. // permissions). An unbuffered Watcher will perform better for almost all use
  85. // cases, and whenever possible you will be better off increasing the kernel
  86. // buffers instead of adding a large userspace buffer.
  87. EOF
  88. )
  89. add=$(<<EOF
  90. // Add starts monitoring the path for changes.
  91. //
  92. // A path can only be watched once; watching it more than once is a no-op and will
  93. // not return an error. Paths that do not yet exist on the filesystem cannot be
  94. // watched.
  95. //
  96. // A watch will be automatically removed if the watched path is deleted or
  97. // renamed. The exception is the Windows backend, which doesn't remove the
  98. // watcher on renames.
  99. //
  100. // Notifications on network filesystems (NFS, SMB, FUSE, etc.) or special
  101. // filesystems (/proc, /sys, etc.) generally don't work.
  102. //
  103. // Returns [ErrClosed] if [Watcher.Close] was called.
  104. //
  105. // See [Watcher.AddWith] for a version that allows adding options.
  106. //
  107. // # Watching directories
  108. //
  109. // All files in a directory are monitored, including new files that are created
  110. // after the watcher is started. Subdirectories are not watched (i.e. it's
  111. // non-recursive).
  112. //
  113. // # Watching files
  114. //
  115. // Watching individual files (rather than directories) is generally not
  116. // recommended as many programs (especially editors) update files atomically: it
  117. // will write to a temporary file which is then moved to to destination,
  118. // overwriting the original (or some variant thereof). The watcher on the
  119. // original file is now lost, as that no longer exists.
  120. //
  121. // The upshot of this is that a power failure or crash won't leave a
  122. // half-written file.
  123. //
  124. // Watch the parent directory and use Event.Name to filter out files you're not
  125. // interested in. There is an example of this in cmd/fsnotify/file.go.
  126. EOF
  127. )
  128. addwith=$(<<EOF
  129. // AddWith is like [Watcher.Add], but allows adding options. When using Add()
  130. // the defaults described below are used.
  131. //
  132. // Possible options are:
  133. //
  134. // - [WithBufferSize] sets the buffer size for the Windows backend; no-op on
  135. // other platforms. The default is 64K (65536 bytes).
  136. EOF
  137. )
  138. remove=$(<<EOF
  139. // Remove stops monitoring the path for changes.
  140. //
  141. // Directories are always removed non-recursively. For example, if you added
  142. // /tmp/dir and /tmp/dir/subdir then you will need to remove both.
  143. //
  144. // Removing a path that has not yet been added returns [ErrNonExistentWatch].
  145. //
  146. // Returns nil if [Watcher.Close] was called.
  147. EOF
  148. )
  149. close=$(<<EOF
  150. // Close removes all watches and closes the Events channel.
  151. EOF
  152. )
  153. watchlist=$(<<EOF
  154. // WatchList returns all paths explicitly added with [Watcher.Add] (and are not
  155. // yet removed).
  156. //
  157. // Returns nil if [Watcher.Close] was called.
  158. EOF
  159. )
  160. events=$(<<EOF
  161. // Events sends the filesystem change events.
  162. //
  163. // fsnotify can send the following events; a "path" here can refer to a
  164. // file, directory, symbolic link, or special file like a FIFO.
  165. //
  166. // fsnotify.Create A new path was created; this may be followed by one
  167. // or more Write events if data also gets written to a
  168. // file.
  169. //
  170. // fsnotify.Remove A path was removed.
  171. //
  172. // fsnotify.Rename A path was renamed. A rename is always sent with the
  173. // old path as Event.Name, and a Create event will be
  174. // sent with the new name. Renames are only sent for
  175. // paths that are currently watched; e.g. moving an
  176. // unmonitored file into a monitored directory will
  177. // show up as just a Create. Similarly, renaming a file
  178. // to outside a monitored directory will show up as
  179. // only a Rename.
  180. //
  181. // fsnotify.Write A file or named pipe was written to. A Truncate will
  182. // also trigger a Write. A single "write action"
  183. // initiated by the user may show up as one or multiple
  184. // writes, depending on when the system syncs things to
  185. // disk. For example when compiling a large Go program
  186. // you may get hundreds of Write events, and you may
  187. // want to wait until you've stopped receiving them
  188. // (see the dedup example in cmd/fsnotify).
  189. //
  190. // Some systems may send Write event for directories
  191. // when the directory content changes.
  192. //
  193. // fsnotify.Chmod Attributes were changed. On Linux this is also sent
  194. // when a file is removed (or more accurately, when a
  195. // link to an inode is removed). On kqueue it's sent
  196. // when a file is truncated. On Windows it's never
  197. // sent.
  198. EOF
  199. )
  200. errors=$(<<EOF
  201. // Errors sends any errors.
  202. //
  203. // ErrEventOverflow is used to indicate there are too many events:
  204. //
  205. // - inotify: There are too many queued events (fs.inotify.max_queued_events sysctl)
  206. // - windows: The buffer size is too small; WithBufferSize() can be used to increase it.
  207. // - kqueue, fen: Not used.
  208. EOF
  209. )
  210. set-cmt() {
  211. local pat=$1
  212. local cmt=$2
  213. IFS=$'\n' local files=($(grep -n $pat backend_*~*_test.go))
  214. for f in $files; do
  215. IFS=':' local fields=($=f)
  216. local file=$fields[1]
  217. local end=$(( $fields[2] - 1 ))
  218. # Find start of comment.
  219. local start=0
  220. IFS=$'\n' local lines=($(head -n$end $file))
  221. for (( i = 1; i <= $#lines; i++ )); do
  222. local line=$lines[-$i]
  223. if ! grep -q '^[[:space:]]*//' <<<$line; then
  224. start=$(( end - (i - 2) ))
  225. break
  226. fi
  227. done
  228. head -n $(( start - 1 )) $file >/tmp/x
  229. print -r -- $cmt >>/tmp/x
  230. tail -n+$(( end + 1 )) $file >>/tmp/x
  231. mv /tmp/x $file
  232. done
  233. }
  234. set-cmt '^type Watcher struct ' $watcher
  235. set-cmt '^func NewWatcher(' $new
  236. set-cmt '^func NewBufferedWatcher(' $newbuffered
  237. set-cmt '^func (w \*Watcher) Add(' $add
  238. set-cmt '^func (w \*Watcher) AddWith(' $addwith
  239. set-cmt '^func (w \*Watcher) Remove(' $remove
  240. set-cmt '^func (w \*Watcher) Close(' $close
  241. set-cmt '^func (w \*Watcher) WatchList(' $watchlist
  242. set-cmt '^[[:space:]]*Events *chan Event$' $events
  243. set-cmt '^[[:space:]]*Errors *chan error$' $errors