Skip to content

Commit

Permalink
inotify, readdcw: zero cost isdir function for Create/Delete(windows)…
Browse files Browse the repository at this point in the history
… and All(linux) events
  • Loading branch information
Pawel Knap committed Feb 12, 2015
1 parent 082bbc8 commit cc6550a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion event_inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ func (e *event) Event() Event { return e.event }
func (e *event) Path() string { return e.path }
func (e *event) Sys() interface{} { return e.sys }

// isdir TODO(ppknap): native implementation for inotify
func isdir(ei EventInfo) (bool, error) {
if _, ok := ei.Sys().(syscall.InotifyEvent); ok {
return ei.Sys().(syscall.InotifyEvent).Mask&syscall.IN_ISDIR != 0, nil
}
fi, err := os.Stat(ei.Path())
if err != nil {
return false, err
Expand Down
24 changes: 13 additions & 11 deletions event_readdcw.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,29 @@ var osestr = map[Event]string{
var ekind = map[Event]Event{}

const (
ObjectUnknown uint8 = iota
ObjectFile
ObjectDirectory
fTypeUnknown uint8 = iota
fTypeFile
fTypeDirectory
)

// TODO(ppknap) : doc.
type event struct {
pathw []uint16
name string
objtype uint8
action uint32
filter uint32
e Event
pathw []uint16
name string
ftype uint8
action uint32
filter uint32
e Event
}

func (e *event) Event() Event { return e.e }
func (e *event) Path() string { return filepath.Join(syscall.UTF16ToString(e.pathw), e.name) }
func (e *event) Sys() interface{} { return e.objtype }
func (e *event) Sys() interface{} { return e.ftype }

// isdir TODO(ppknap): native implementation for readdcw
func isdir(ei EventInfo) (bool, error) {
if ftype, ok := ei.Sys().(uint8); ok && ftype != fTypeUnknown {
return ftype == fTypeDirectory, nil
}
fi, err := os.Stat(ei.Path())
if err != nil {
return false, err
Expand Down
8 changes: 5 additions & 3 deletions watcher_inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,12 @@ func (i *inotify) loop(esch chan<- []*event) {
if err := syscall.Close(int(fd)); err != nil {
// Panic? error?
panic(err)
} else {
atomic.StoreInt32(&i.fd, invalidDescriptor)
}
i.epollclose()
atomic.StoreInt32(&i.fd, invalidDescriptor)
if err := i.epollclose(); err != nil {
// Panic? error?
panic(err)
}
close(esch)
return
}
Expand Down
6 changes: 3 additions & 3 deletions watcher_readdcw.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ func (r *readdcw) send(es []*event) {
switch Event(e.action) {
case (FileActionAdded >> 12), (FileActionRemoved >> 12):
if e.filter&uint32(dirmarker) != 0 {
e.objtype = ObjectDirectory
e.ftype = fTypeDirectory
} else {
e.objtype = ObjectFile
e.ftype = fTypeFile
}
default:
e.objtype = ObjectUnknown
e.ftype = fTypeUnknown
}
r.c <- e
}
Expand Down

0 comments on commit cc6550a

Please sign in to comment.