Skip to content

Commit

Permalink
Don't use unkeyed structs in fen watcher
Browse files Browse the repository at this point in the history
Might as well change sendEvent() to just accept the name and Op.
  • Loading branch information
arp242 committed Oct 13, 2022
1 parent acadbbe commit 9930e10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
26 changes: 13 additions & 13 deletions backend_fen.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ func NewWatcher() (*Watcher, error) {

// sendEvent attempts to send an event to the user, returning true if the event
// was put in the channel successfully and false if the watcher has been closed.
func (w *Watcher) sendEvent(e Event) (sent bool) {
func (w *Watcher) sendEvent(name string, op Op) (sent bool) {
select {
case w.Events <- e:
case w.Events <- Event{Name: name, Op: op}:
return true
case <-w.done:
return false
Expand Down Expand Up @@ -385,13 +385,13 @@ func (w *Watcher) handleEvent(event *unix.PortEvent) error {
isWatched := watchedDir || watchedPath

if events&unix.FILE_DELETE != 0 {
if !w.sendEvent(Event{path, Remove}) {
if !w.sendEvent(path, Remove) {
return nil
}
reRegister = false
}
if events&unix.FILE_RENAME_FROM != 0 {
if !w.sendEvent(Event{path, Rename}) {
if !w.sendEvent(path, Rename) {
return nil
}
// Don't keep watching the new file name
Expand All @@ -406,7 +406,7 @@ func (w *Watcher) handleEvent(event *unix.PortEvent) error {

// inotify reports a Remove event in this case, so we simulate
// this here.
if !w.sendEvent(Event{path, Remove}) {
if !w.sendEvent(path, Remove) {
return nil
}
// Don't keep watching the file that was removed
Expand Down Expand Up @@ -439,7 +439,7 @@ func (w *Watcher) handleEvent(event *unix.PortEvent) error {
// We get a modify event of something happening inside, but by the time
// we get here, the sudirectory is already gone. Clearly we were watching this path
// but now it is gone. Let's tell the user that it was removed.
if !w.sendEvent(Event{path, Remove}) {
if !w.sendEvent(path, Remove) {
return nil
}
// Suppress extra write events on removed directories; they are not informative
Expand All @@ -453,7 +453,7 @@ func (w *Watcher) handleEvent(event *unix.PortEvent) error {
stat, err = os.Stat(path)
if err != nil {
// The symlink still exists, but the target is gone. Report the Remove similar to above.
if !w.sendEvent(Event{path, Remove}) {
if !w.sendEvent(path, Remove) {
return nil
}
// Don't return the error
Expand All @@ -467,20 +467,20 @@ func (w *Watcher) handleEvent(event *unix.PortEvent) error {
return err
}
} else {
if !w.sendEvent(Event{path, Write}) {
if !w.sendEvent(path, Write) {
return nil
}
}
} else {
if !w.sendEvent(Event{path, Write}) {
if !w.sendEvent(path, Write) {
return nil
}
}
}
if events&unix.FILE_ATTRIB != 0 && stat != nil {
// Only send Chmod if perms changed
if stat.Mode().Perm() != fmode.Perm() {
if !w.sendEvent(Event{path, Chmod}) {
if !w.sendEvent(path, Chmod) {
return nil
}
}
Expand Down Expand Up @@ -519,7 +519,7 @@ func (w *Watcher) updateDirectory(path string) error {
return nil
}
}
if !w.sendEvent(Event{path, Create}) {
if !w.sendEvent(path, Create) {
return nil
}
}
Expand Down Expand Up @@ -552,10 +552,10 @@ func (w *Watcher) associateFile(path string, stat os.FileInfo, follow bool) erro
}
}
// FILE_NOFOLLOW means we watch symlinks themselves rather than their targets
events := unix.FILE_MODIFIED|unix.FILE_ATTRIB|unix.FILE_NOFOLLOW
events := unix.FILE_MODIFIED | unix.FILE_ATTRIB | unix.FILE_NOFOLLOW
if follow {
// We *DO* follow symlinks for explicitly watched entries
events = unix.FILE_MODIFIED|unix.FILE_ATTRIB
events = unix.FILE_MODIFIED | unix.FILE_ATTRIB
}
return w.port.AssociatePath(path, stat,
events,
Expand Down
12 changes: 6 additions & 6 deletions fsnotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ var (
ErrEventOverflow = errors.New("fsnotify queue overflow")
)

func (op Op) String() string {
func (o Op) String() string {
var b strings.Builder
if op.Has(Create) {
if o.Has(Create) {
b.WriteString("|CREATE")
}
if op.Has(Remove) {
if o.Has(Remove) {
b.WriteString("|REMOVE")
}
if op.Has(Write) {
if o.Has(Write) {
b.WriteString("|WRITE")
}
if op.Has(Rename) {
if o.Has(Rename) {
b.WriteString("|RENAME")
}
if op.Has(Chmod) {
if o.Has(Chmod) {
b.WriteString("|CHMOD")
}
if b.Len() == 0 {
Expand Down

0 comments on commit 9930e10

Please sign in to comment.