// Copyright 2015-2016 Luke Shumaker . // // This is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of // the License, or (at your option) any later version. // // This software is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this manual; if not, see // . package inotify import ( "os" "golang.org/x/sys/unix" ) func newPathError(op string, path string, err error) error { if err == nil { return nil } return &os.PathError{Op: op, Path: path, Err: err} } // Create and initialize inotify instance. func sys_inotify_init() (inFd, error) { fd, err := unix.InotifyInit() return inFd(fd), os.NewSyscallError("inotify_init", err) } // Create and initialize inotify instance. func sys_inotify_init1(flags int) (inFd, error) { fd, err := unix.InotifyInit1(flags) return inFd(fd), os.NewSyscallError("inotify_init1", err) } // Add watch of object NAME to inotify instance FD. Notify about // events specified by MASK. func sys_inotify_add_watch(fd inFd, name string, mask Mask) (Wd, error) { wd, err := unix.InotifyAddWatch(int(fd), name, uint32(mask)) return Wd(wd), newPathError("inotify_add_watch", name, err) } // Remove the watch specified by WD from the inotify instance FD. func sys_inotify_rm_watch(fd inFd, wd Wd) error { success, err := unix.InotifyRmWatch(int(fd), uint32(wd)) switch success { case -1: if err == nil { panic("should never happen") } os.NewSyscallError("inotify_rm_watch", err) case 0: if err != nil { panic("should never happen") } return nil } panic("should never happen") } func sys_close(fd inFd) error { return os.NewSyscallError("close", unix.Close(int(fd))) } func sys_read(fd inFd, p []byte) (int, error) { n, err := unix.Read(int(fd), p) return n, os.NewSyscallError("read", err) } func sys_dupfd_cloexec(fd inFd) (inFd, error) { n, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), unix.F_DUPFD_CLOEXEC, 0) if errno != 0 { return -1, os.NewSyscallError("dupfd_cloexec", errno) } return inFd(n), nil } func sys_setnonblock(fd inFd, nonblocking bool) error { err := unix.SetNonblock(int(fd), nonblocking) return os.NewSyscallError("setnonblock", err) } func sys_getnonblock(fd inFd) (bool, error) { flag, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), unix.F_GETFL, 0) if errno != 0 { return false, os.NewSyscallError("getnonblock", errno) } return flag&unix.O_NONBLOCK != 0, nil }