| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package notify |
| 8 |
|
| 9 |
import ( |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"syscall" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
const ( |
| 17 |
osSpecificCreate Event = 1 << (20 + iota) |
| 18 |
osSpecificRemove |
| 19 |
osSpecificWrite |
| 20 |
osSpecificRename |
| 21 |
|
| 22 |
recursive |
| 23 |
|
| 24 |
|
| 25 |
omit |
| 26 |
|
| 27 |
dirmarker |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
const ( |
| 36 |
FileNotifyChangeFileName = Event(syscall.FILE_NOTIFY_CHANGE_FILE_NAME) |
| 37 |
FileNotifyChangeDirName = Event(syscall.FILE_NOTIFY_CHANGE_DIR_NAME) |
| 38 |
FileNotifyChangeAttributes = Event(syscall.FILE_NOTIFY_CHANGE_ATTRIBUTES) |
| 39 |
FileNotifyChangeSize = Event(syscall.FILE_NOTIFY_CHANGE_SIZE) |
| 40 |
FileNotifyChangeLastWrite = Event(syscall.FILE_NOTIFY_CHANGE_LAST_WRITE) |
| 41 |
FileNotifyChangeLastAccess = Event(syscall.FILE_NOTIFY_CHANGE_LAST_ACCESS) |
| 42 |
FileNotifyChangeCreation = Event(syscall.FILE_NOTIFY_CHANGE_CREATION) |
| 43 |
FileNotifyChangeSecurity = Event(syscallFileNotifyChangeSecurity) |
| 44 |
) |
| 45 |
|
| 46 |
const ( |
| 47 |
fileNotifyChangeAll = 0x17f |
| 48 |
fileNotifyChangeModified = fileNotifyChangeAll &^ (FileNotifyChangeFileName | FileNotifyChangeDirName) |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
const syscallFileNotifyChangeSecurity = 0x00000100 |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
const ( |
| 63 |
FileActionAdded = Event(syscall.FILE_ACTION_ADDED) << 12 |
| 64 |
FileActionRemoved = Event(syscall.FILE_ACTION_REMOVED) << 12 |
| 65 |
FileActionModified = Event(syscall.FILE_ACTION_MODIFIED) << 14 |
| 66 |
FileActionRenamedOldName = Event(syscall.FILE_ACTION_RENAMED_OLD_NAME) << 15 |
| 67 |
FileActionRenamedNewName = Event(syscall.FILE_ACTION_RENAMED_NEW_NAME) << 16 |
| 68 |
) |
| 69 |
|
| 70 |
const fileActionAll = 0x7f000 |
| 71 |
|
| 72 |
var osestr = map[Event]string{ |
| 73 |
FileNotifyChangeFileName: "notify.FileNotifyChangeFileName", |
| 74 |
FileNotifyChangeDirName: "notify.FileNotifyChangeDirName", |
| 75 |
FileNotifyChangeAttributes: "notify.FileNotifyChangeAttributes", |
| 76 |
FileNotifyChangeSize: "notify.FileNotifyChangeSize", |
| 77 |
FileNotifyChangeLastWrite: "notify.FileNotifyChangeLastWrite", |
| 78 |
FileNotifyChangeLastAccess: "notify.FileNotifyChangeLastAccess", |
| 79 |
FileNotifyChangeCreation: "notify.FileNotifyChangeCreation", |
| 80 |
FileNotifyChangeSecurity: "notify.FileNotifyChangeSecurity", |
| 81 |
|
| 82 |
FileActionAdded: "notify.FileActionAdded", |
| 83 |
FileActionRemoved: "notify.FileActionRemoved", |
| 84 |
FileActionModified: "notify.FileActionModified", |
| 85 |
FileActionRenamedOldName: "notify.FileActionRenamedOldName", |
| 86 |
FileActionRenamedNewName: "notify.FileActionRenamedNewName", |
| 87 |
} |
| 88 |
|
| 89 |
const ( |
| 90 |
fTypeUnknown uint8 = iota |
| 91 |
fTypeFile |
| 92 |
fTypeDirectory |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
type event struct { |
| 97 |
pathw []uint16 |
| 98 |
name string |
| 99 |
ftype uint8 |
| 100 |
action uint32 |
| 101 |
filter uint32 |
| 102 |
e Event |
| 103 |
} |
| 104 |
|
| 105 |
func (e *event) Event() Event { return e.e } |
| 106 |
func (e *event) Path() string { return filepath.Join(syscall.UTF16ToString(e.pathw), e.name) } |
| 107 |
func (e *event) Sys() interface{} { return e.ftype } |
| 108 |
|
| 109 |
func (e *event) isDir() (bool, error) { |
| 110 |
if e.ftype != fTypeUnknown { |
| 111 |
return e.ftype == fTypeDirectory, nil |
| 112 |
} |
| 113 |
fi, err := os.Stat(e.Path()) |
| 114 |
if err != nil { |
| 115 |
return false, err |
| 116 |
} |
| 117 |
return fi.IsDir(), nil |
| 118 |
} |