本文整理汇总了Golang中syscall.InotifyAddWatch函数的典型用法代码示例。如果您正苦于以下问题:Golang InotifyAddWatch函数的具体用法?Golang InotifyAddWatch怎么用?Golang InotifyAddWatch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了InotifyAddWatch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
fd, err := syscall.InotifyInit()
if err != nil {
log.Fatal(err)
}
defer syscall.Close(fd)
wd, err := syscall.InotifyAddWatch(fd, "test1.log", syscall.IN_ALL_EVENTS)
_, err = syscall.InotifyAddWatch(fd, "../test2.log", syscall.IN_ALL_EVENTS)
//_, err = syscall.InotifyAddWatch(fd, ".", syscall.IN_ALL_EVENTS)
if err != nil {
log.Fatal(err)
}
defer syscall.InotifyRmWatch(fd, uint32(wd))
fmt.Printf("WD is %d\n", wd)
for {
// Room for at least 128 events
buffer := make([]byte, syscall.SizeofInotifyEvent*128)
bytesRead, err := syscall.Read(fd, buffer)
if err != nil {
log.Fatal(err)
}
if bytesRead < syscall.SizeofInotifyEvent {
// No point trying if we don't have at least one event
continue
}
fmt.Printf("Size of InotifyEvent is %s\n", syscall.SizeofInotifyEvent)
fmt.Printf("Bytes read: %d\n", bytesRead)
offset := 0
for offset < bytesRead-syscall.SizeofInotifyEvent {
event := (*syscall.InotifyEvent)(unsafe.Pointer(&buffer[offset]))
fmt.Printf("%+v\n", event)
if (event.Mask & syscall.IN_ACCESS) > 0 {
fmt.Printf("Saw IN_ACCESS for %+v\n", event)
}
// We need to account for the length of the name
offset += syscall.SizeofInotifyEvent + int(event.Len)
}
}
}
示例2: watch
// watch adds a new watcher to the set of watched objects or modifies the existing
// one. If called for the first time, this function initializes inotify filesystem
// monitor and starts producer-consumers goroutines.
func (i *inotify) watch(path string, e Event) (err error) {
if e&^(All|Event(syscall.IN_ALL_EVENTS)) != 0 {
return errors.New("notify: unknown event")
}
if err = i.lazyinit(); err != nil {
return
}
iwd, err := syscall.InotifyAddWatch(int(i.fd), path, encode(e))
if err != nil {
return
}
i.RLock()
wd := i.m[int32(iwd)]
i.RUnlock()
if wd == nil {
i.Lock()
if i.m[int32(iwd)] == nil {
i.m[int32(iwd)] = &watched{path: path, mask: uint32(e)}
}
i.Unlock()
} else {
i.Lock()
wd.mask = uint32(e)
i.Unlock()
}
return nil
}
示例3: Add
// Add starts watching the named file or directory (non-recursively).
func (w *Watcher) Add(name string) error {
name = filepath.Clean(name)
if w.isClosed() {
return errors.New("inotify instance already closed")
}
const agnosticEvents = syscall.IN_MOVED_TO | syscall.IN_MOVED_FROM |
syscall.IN_CREATE | syscall.IN_ATTRIB | syscall.IN_MODIFY |
syscall.IN_MOVE_SELF | syscall.IN_DELETE | syscall.IN_DELETE_SELF
var flags uint32 = agnosticEvents
w.mu.Lock()
watchEntry, found := w.watches[name]
w.mu.Unlock()
if found {
watchEntry.flags |= flags
flags |= syscall.IN_MASK_ADD
}
wd, errno := syscall.InotifyAddWatch(w.fd, name, flags)
if wd == -1 {
return errno
}
w.mu.Lock()
w.watches[name] = &watch{wd: uint32(wd), flags: flags}
w.paths[wd] = name
w.mu.Unlock()
return nil
}
示例4: monitor
func monitor(root string, action chan uint32) {
fd, err := syscall.InotifyInit()
if fd == -1 || err != nil {
end <- true
return
}
flags := syscall.IN_MODIFY | syscall.IN_CREATE | syscall.IN_DELETE // 2/128/512
wd, _ := syscall.InotifyAddWatch(fd, root, uint32(flags))
if wd == -1 {
end <- true
return
}
var (
buf [syscall.SizeofInotifyEvent * 10]byte
n int
)
for {
n, _ = syscall.Read(fd, buf[0:])
if n > syscall.SizeofInotifyEvent {
var offset = 0
for offset < n {
raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
mask := uint32(raw.Mask)
offset = offset + int(raw.Len) + syscall.SizeofInotifyEvent
action <- mask
log.Println("action:", mask)
}
}
}
}
示例5: add_watch_r
func add_watch_r(inotify_fd int, f string, paths map[int]string) {
fi, err := os.Open(f)
if err != nil {
log.Fatal(err)
}
defer fi.Close()
n, err := syscall.InotifyAddWatch(inotify_fd, f, syscall.IN_ALL_EVENTS)
paths[n] = f
if err != nil {
log.Fatal(err)
}
s, _ := fi.Stat()
if s.IsDir() {
files, _ := ioutil.ReadDir(f)
for _, file := range files {
if file.IsDir() {
f := path.Join(f, file.Name())
add_watch_r(inotify_fd, f, paths)
// log.Print(path.Join(f, file.Name()))
}
}
}
}
示例6: AddWatch
// AddWatch adds path to the watched file set.
// The flags are interpreted as described in inotify_add_watch(2).
func (w *Watcher) AddWatch(path string, flags uint32) error {
if w.isClosed {
return errors.New("inotify instance already closed")
}
watchEntry, found := w.watches[path]
if found {
watchEntry.flags |= flags
flags |= syscall.IN_MASK_ADD
}
w.mu.Lock() // synchronize with readEvents goroutine
wd, err := syscall.InotifyAddWatch(w.fd, path, flags)
if err != nil {
w.mu.Unlock()
return &os.PathError{
Op: "inotify_add_watch",
Path: path,
Err: err,
}
}
if !found {
w.watches[path] = &watch{wd: uint32(wd), flags: flags}
w.paths[wd] = path
}
w.mu.Unlock()
return nil
}
示例7: addFilesToInotify
//Add directories recursively to the tracking list
func addFilesToInotify(fd int, dirPath string) {
dir, err := os.Stat(dirPath)
if err != nil {
log.Fatal("error getting info on dir: ", err)
return
}
if dir.IsDir() && dir.Name() != ".git" {
log.Print("adding: ", dirPath)
_, err = syscall.InotifyAddWatch(fd, dirPath, syscall.IN_CLOSE_WRITE|syscall.IN_DELETE)
if err != nil {
log.Fatal("error adding watch: ", err)
return
}
fileList, err := ioutil.ReadDir(dirPath)
if err != nil {
log.Fatal("error reading dir: ", err)
return
}
for _, file := range fileList {
newPath := dirPath + "/" + file.Name()
if file.IsDir() && file.Name() != ".git" {
addFilesToInotify(fd, newPath)
}
}
}
}
示例8: add
func (w *inotify) add(id Id, path string, flags uint32) error {
wd, err := syscall.InotifyAddWatch(w.watchfd, path, flags)
if err != nil {
return err
}
watch := int32(wd)
w.watches[id] = watch
w.ids[watch] = id
return nil
}
示例9: inotify_add_watch
func inotify_add_watch(name string) {
wd, err := syscall.InotifyAddWatch(inotify_fd, name,
syscall.IN_MODIFY|syscall.IN_DELETE_SELF|syscall.IN_MOVE_SELF)
if -1 == wd {
if err == syscall.ENOENT {
// Dirty hack.
return
}
tabby_log("InotifyAddWatch failed, changes of file " + name +
" outside of tabby will remain unnoticed, errno = " + err.Error())
return
}
name_by_wd[int32(wd)] = name
wd_by_name[name] = int32(wd)
}
示例10: eventProcessor
// eventProcessor processes events from the file system
func (pin *Pin) eventProcessor() error {
fd, err := syscall.InotifyInit()
pin.Fd = fd
if err != nil {
return err
}
_, err = syscall.InotifyAddWatch(fd, pin.ValuePath, syscall.IN_MODIFY)
if err != nil {
return err
}
go pin.watcher()
return nil
}
示例11: doAdd
func (this *watcher) doAdd(path string, flags uint32) error {
//Check that file exists
if _, err := os.Lstat(path); err != nil {
return err
}
if wd, errno := syscall.InotifyAddWatch(this.fd, path, flags); wd < 0 {
return os.NewSyscallError("inotify_add_watch", errno)
} else {
this.wd = wd
this.root = path
}
return nil
}
示例12: AddWatch
// AddWatch adds path to the watched file set.
// The flags are interpreted as described in inotify_add_watch(2).
func (w *Watcher) AddWatch(path string, flags uint32) error {
if w.isClosed {
return errors.New("inotify instance already closed")
}
watchEntry, found := w.watches[path]
if found {
watchEntry.flags |= flags
flags |= syscall.IN_MASK_ADD
}
wd, err := syscall.InotifyAddWatch(w.fd, path, flags)
if err != nil {
return &os.PathError{"inotify_add_watch", path, err}
}
if !found {
w.watches[path] = &watch{wd: uint32(wd), flags: flags}
w.paths[wd] = path
}
return nil
}
示例13: AddWatch
func (w *Watcher) AddWatch(path string, flags uint32) error {
if w.isClose {
log.Fatal("监控已经关闭")
}
//判断是否已经监控了
if found := w.wm.find(path); found != nil {
f := found.(watch)
f.flags |= flags
flags |= syscall.IN_MASK_ADD
}
wd, err := syscall.InotifyAddWatch(w.fd, path, flags)
fmt.Println("开始监控目录:", path, wd)
if wd == -1 {
return err
}
w.wm.add(path, wd, flags)
return nil
}
示例14: AddWatch
// AddWatch adds path to the watched file set.
// The flags are interpreted as described in inotify_add_watch(2).
func (w *Watcher) AddWatch(path string, flags uint32) os.Error {
if w.isClosed {
return os.NewError("inotify instance already closed")
}
watchEntry, found := w.watches[path]
if found {
watchEntry.flags |= flags
flags |= syscall.IN_MASK_ADD
}
wd, errno := syscall.InotifyAddWatch(w.fd, path, flags)
if wd == -1 {
return os.NewSyscallError("inotify_add_watch", errno)
}
if !found {
w.watches[path] = &watch{wd: uint32(wd), flags: flags}
w.paths[wd] = path
}
return nil
}
示例15: addWatch
// AddWatch adds path to the watched file set.
// The flags are interpreted as described in inotify_add_watch(2).
func (w *Watcher) addWatch(path string, flags uint32) error {
if w.isClosed {
return errors.New("inotify instance already closed")
}
watchEntry, found := w.watches[path]
if found {
watchEntry.flags |= flags
flags |= syscall.IN_MASK_ADD
}
wd, errno := syscall.InotifyAddWatch(w.fd, path, flags)
if wd == -1 {
return errno
}
w.mu.Lock()
w.watches[path] = &watch{wd: uint32(wd), flags: flags}
w.paths[wd] = path
w.mu.Unlock()
return nil
}