fsnotify.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2012 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 !plan9
  5. // Package fsnotify provides a platform-independent interface for file system notifications.
  6. package fsnotify
  7. import (
  8. "bytes"
  9. "errors"
  10. "fmt"
  11. )
  12. // Event represents a single file system notification.
  13. type Event struct {
  14. Name string // Relative path to the file or directory.
  15. Op Op // File operation that triggered the event.
  16. }
  17. // Op describes a set of file operations.
  18. type Op uint32
  19. // These are the generalized file operations that can trigger a notification.
  20. const (
  21. Create Op = 1 << iota
  22. Write
  23. Remove
  24. Rename
  25. Chmod
  26. )
  27. func (op Op) String() string {
  28. // Use a buffer for efficient string concatenation
  29. var buffer bytes.Buffer
  30. if op&Create == Create {
  31. buffer.WriteString("|CREATE")
  32. }
  33. if op&Remove == Remove {
  34. buffer.WriteString("|REMOVE")
  35. }
  36. if op&Write == Write {
  37. buffer.WriteString("|WRITE")
  38. }
  39. if op&Rename == Rename {
  40. buffer.WriteString("|RENAME")
  41. }
  42. if op&Chmod == Chmod {
  43. buffer.WriteString("|CHMOD")
  44. }
  45. if buffer.Len() == 0 {
  46. return ""
  47. }
  48. return buffer.String()[1:] // Strip leading pipe
  49. }
  50. // String returns a string representation of the event in the form
  51. // "file: REMOVE|WRITE|..."
  52. func (e Event) String() string {
  53. return fmt.Sprintf("%q: %s", e.Name, e.Op.String())
  54. }
  55. // Common errors that can be reported by a watcher
  56. var ErrEventOverflow = errors.New("fsnotify queue overflow")