Skip to content

Commit

Permalink
readdcw: support for non-mixed system-dependent *Name Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Knap committed Feb 24, 2015
1 parent b347e4d commit ea51631
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
27 changes: 17 additions & 10 deletions event_readdcw.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ const (

// ReadDirectoryChangesW filters.
const (
FileNotifyChangeFileName = Event(syscall.FILE_NOTIFY_CHANGE_FILE_NAME)
FileNotifyChangeDirName = Event(syscall.FILE_NOTIFY_CHANGE_DIR_NAME)
FileNotifyChangeAttributes = Event(syscall.FILE_NOTIFY_CHANGE_ATTRIBUTES)
FileNotifyChangeSize = Event(syscall.FILE_NOTIFY_CHANGE_SIZE)
FileNotifyChangeLastWrite = Event(syscall.FILE_NOTIFY_CHANGE_LAST_WRITE)
FileNotifyChangeLastAccess = Event(syscall.FILE_NOTIFY_CHANGE_LAST_ACCESS)
FileNotifyChangeCreation = Event(syscall.FILE_NOTIFY_CHANGE_CREATION)
FileNotifyChangeSecurity = Event(syscallFileNotifyChangeSecurity)
FileNotifyChangeFileName = actFRW | Event(syscall.FILE_NOTIFY_CHANGE_FILE_NAME)
FileNotifyChangeDirName = actFRW | Event(syscall.FILE_NOTIFY_CHANGE_DIR_NAME)
FileNotifyChangeAttributes = actFMV | Event(syscall.FILE_NOTIFY_CHANGE_ATTRIBUTES)
FileNotifyChangeSize = actFMV | Event(syscall.FILE_NOTIFY_CHANGE_SIZE)
FileNotifyChangeLastWrite = actFMV | Event(syscall.FILE_NOTIFY_CHANGE_LAST_WRITE)
FileNotifyChangeLastAccess = actFMV | Event(syscall.FILE_NOTIFY_CHANGE_LAST_ACCESS)
FileNotifyChangeCreation = actFMV | Event(syscall.FILE_NOTIFY_CHANGE_CREATION)
FileNotifyChangeSecurity = actFMV | Event(syscallFileNotifyChangeSecurity)
)

const fileNotifyChangeAll = 0x17f // logical sum of all FileNotifyChange* events.
// logical sum of all syscall.FILE_NOTIFY_CHANGE_* and FileAction* events.
const fileNotifyChangeAndActionAll = Event(0x17f) | actAll

// according to: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx
// this flag should be declared in: http://golang.org/src/pkg/syscall/ztypes_windows.go
Expand Down Expand Up @@ -71,6 +72,12 @@ var osestr = map[Event]string{
FileActionRenamedNewName: "notify.FileActionRenamedNewName",
}

const (
actFRW = FileActionAdded | FileActionRemoved | FileActionRenamedOldName | FileActionRenamedNewName
actFMV = FileActionModified
actAll = actFRW | actFMV
)

var ekind = map[Event]Event{}

const (
Expand All @@ -91,7 +98,7 @@ type event struct {

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.ftype }
func (e *event) Sys() interface{} { return nil }

func (e *event) isDir() (bool, error) {
if e.ftype != fTypeUnknown {
Expand Down
3 changes: 1 addition & 2 deletions notify_readdcw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package notify
import "testing"

func TestNotifySystemSpecificEvent(t *testing.T) {
t.Skip("TODO(ppknap)")
n := NewNotifyTest(t, "testdata/vfs.txt")
defer n.Close()

Expand All @@ -33,7 +32,7 @@ func TestUnknownEvent(t *testing.T) {

ch := NewChans(1)

n.WatchErr("src/github.com/rjeczalik/fs", ch[0], nil, FileActionAdded)
n.WatchErr("src/github.com/rjeczalik/fs", ch[0], nil, Event(stateRewatch))
}

func TestNotifySystemAndGlobalMix(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions watcher_readdcw.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (g *grip) readDirChanges() error {
func encode(filter uint32) uint32 {
e := Event(filter & (onlyNGlobalEvents | onlyNotifyChanges))
if e&dirmarker != 0 {
return uint32(FileNotifyChangeDirName)
return uint32(FileNotifyChangeDirName &^ actAll)
}
if e&Create != 0 {
e = (e ^ Create) | FileNotifyChangeFileName
Expand All @@ -142,7 +142,7 @@ func encode(filter uint32) uint32 {
if e&Rename != 0 {
e = (e ^ Rename) | FileNotifyChangeFileName
}
return uint32(e)
return uint32(e &^ actAll)
}

// watched is made in order to check whether an action comes from a directory or
Expand Down Expand Up @@ -273,7 +273,7 @@ func (r *readdcw) RecursiveWatch(path string, event Event) error {
// already exists, function tries to rewatch it with new filters(NOT VALID). Moreover,
// watch starts the main event loop goroutine when called for the first time.
func (r *readdcw) watch(path string, event Event, recursive bool) (err error) {
if event&^(All|fileNotifyChangeAll) != 0 {
if event&^(All|fileNotifyChangeAndActionAll) != 0 {
return errors.New("notify: unknown event")
}
r.Lock()
Expand Down Expand Up @@ -433,7 +433,7 @@ func (r *readdcw) RecursiveRewatch(oldpath, newpath string, oldevent,

// TODO : (pknap) doc.
func (r *readdcw) rewatch(path string, oldevent, newevent uint32, recursive bool) (err error) {
if Event(newevent)&^(All|fileNotifyChangeAll) != 0 {
if Event(newevent)&^(All|fileNotifyChangeAndActionAll) != 0 {
return errors.New("notify: unknown event")
}
var wd *watched
Expand Down

0 comments on commit ea51631

Please sign in to comment.