fsnotify.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //go:build !plan9
  2. // +build !plan9
  3. // Package fsnotify provides a cross-platform interface for file system
  4. // notifications.
  5. package fsnotify
  6. import (
  7. "errors"
  8. "fmt"
  9. "strings"
  10. )
  11. // Event represents a file system notification.
  12. type Event struct {
  13. // Path to the file or directory.
  14. //
  15. // Paths are relative to the input; for example with Add("dir") the Name
  16. // will be set to "dir/file" if you create that file, but if you use
  17. // Add("/path/to/dir") it will be "/path/to/dir/file".
  18. Name string
  19. // File operation that triggered the event.
  20. //
  21. // This is a bitmask and some systems may send multiple operations at once.
  22. // Use the Event.Has() method instead of comparing with ==.
  23. Op Op
  24. }
  25. // Op describes a set of file operations.
  26. type Op uint32
  27. // The operations fsnotify can trigger; see the documentation on [Watcher] for a
  28. // full description, and check them with [Event.Has].
  29. const (
  30. Create Op = 1 << iota
  31. Write
  32. Remove
  33. Rename
  34. Chmod
  35. )
  36. // Common errors that can be reported by a watcher
  37. var (
  38. ErrNonExistentWatch = errors.New("can't remove non-existent watcher")
  39. ErrEventOverflow = errors.New("fsnotify queue overflow")
  40. )
  41. func (op Op) String() string {
  42. var b strings.Builder
  43. if op.Has(Create) {
  44. b.WriteString("|CREATE")
  45. }
  46. if op.Has(Remove) {
  47. b.WriteString("|REMOVE")
  48. }
  49. if op.Has(Write) {
  50. b.WriteString("|WRITE")
  51. }
  52. if op.Has(Rename) {
  53. b.WriteString("|RENAME")
  54. }
  55. if op.Has(Chmod) {
  56. b.WriteString("|CHMOD")
  57. }
  58. if b.Len() == 0 {
  59. return "[no events]"
  60. }
  61. return b.String()[1:]
  62. }
  63. // Has reports if this operation has the given operation.
  64. func (o Op) Has(h Op) bool { return o&h == h }
  65. // Has reports if this event has the given operation.
  66. func (e Event) Has(op Op) bool { return e.Op.Has(op) }
  67. // String returns a string representation of the event with their path.
  68. func (e Event) String() string {
  69. return fmt.Sprintf("%-13s %q", e.Op.String(), e.Name)
  70. }