fsnotify.go 1.4 KB

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