常量

三个 error

  1. var (
  2. // ErrDurationTooShort occurs when calling the watcher's Start
  3. // method with a duration that's less than 1 nanosecond.
  4. ErrDurationTooShort = errors.New("error: duration is less than 1ns")
  5. // ErrWatcherRunning occurs when trying to call the watcher's
  6. // Start method and the polling cycle is still already running
  7. // from previously calling Start and not yet calling Close.
  8. ErrWatcherRunning = errors.New("error: watcher is already running")
  9. // ErrWatchedFileDeleted is an error that occurs when a file or folder that was
  10. // being watched has been deleted.
  11. ErrWatchedFileDeleted = errors.New("error: watched file or folder deleted")
  12. )

六个可以检测的操作

  1. // An Op is a type that is used to describe what type
  2. // of event has occurred during the watching process.
  3. type Op uint32
  4. // Ops
  5. const (
  6. Create Op = iota
  7. Write
  8. Remove
  9. Rename
  10. Chmod
  11. Move
  12. )
  13. var ops = map[Op]string{
  14. Create: "CREATE",
  15. Write: "WRITE",
  16. Remove: "REMOVE",
  17. Rename: "RENAME",
  18. Chmod: "CHMOD",
  19. Move: "MOVE",
  20. }
  21. // String prints the string version of the Op consts
  22. func (e Op) String() string {
  23. if op, found := ops[e]; found {
  24. return op
  25. }
  26. return "???"
  27. }

结构

Event

  1. // An Event describes an event that is received when files or directory
  2. // changes occur. It includes the os.FileInfo of the changed file or
  3. // directory and the type of event that's occurred and the full path of the file.
  4. type Event struct {
  5. Op
  6. Path string
  7. os.FileInfo
  8. }
  9. // String returns a string depending on what type of event occurred and the
  10. // file name associated with the event.
  11. func (e Event) String() string {
  12. if e.FileInfo != nil {
  13. pathType := "FILE"
  14. if e.IsDir() {
  15. pathType = "DIRECTORY"
  16. }
  17. return fmt.Sprintf("%s %q %s [%s]", pathType, e.Name(), e.Op, e.Path)
  18. }
  19. return "???"
  20. }

Watcher

  1. // Watcher describes a process that watches files for changes.
  2. type Watcher struct {
  3. Event chan Event
  4. Error chan error
  5. Closed chan struct{}
  6. close chan struct{}
  7. wg *sync.WaitGroup
  8. // mu protects the following.
  9. mu *sync.Mutex
  10. running bool
  11. names map[string]bool // bool for recursive or not.
  12. files map[string]os.FileInfo // map of files.
  13. ignored map[string]struct{} // ignored files or directories.
  14. ops map[Op]struct{} // Op filtering.
  15. ignoreHidden bool // ignore hidden files or not.
  16. maxEvents int // max sent events per cycle
  17. }

fileInfo

  1. // fileInfo is an implementation of os.FileInfo that can be used
  2. // as a mocked os.FileInfo when triggering an event when the specified
  3. // os.FileInfo is nil.
  4. type fileInfo struct {
  5. name string
  6. size int64
  7. mode os.FileMode
  8. modTime time.Time
  9. sys interface{}
  10. dir bool
  11. }
  12. func (fs *fileInfo) IsDir() bool {
  13. return fs.dir
  14. }
  15. func (fs *fileInfo) ModTime() time.Time {
  16. return fs.modTime
  17. }
  18. func (fs *fileInfo) Mode() os.FileMode {
  19. return fs.mode
  20. }
  21. func (fs *fileInfo) Name() string {
  22. return fs.name
  23. }
  24. func (fs *fileInfo) Size() int64 {
  25. return fs.size
  26. }
  27. func (fs *fileInfo) Sys() interface{} {
  28. return fs.sys
  29. }