summaryrefslogtreecommitdiff
path: root/inotify
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-12-19 02:06:19 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-12-19 02:06:19 -0500
commit9dbaa991819fde8a0afed727506290ee993d2eef (patch)
tree62bf9ae885c6130f3659aac889e6e4eeb8a8fcf8 /inotify
parentfc5d64a5fc886e0b8519e8c19d311ebfb0df59db (diff)
Use x/sys/unix instead of the deprecated syscall.
Diffstat (limited to 'inotify')
-rw-r--r--inotify/inotify.go54
-rw-r--r--inotify/syscall.go23
2 files changed, 41 insertions, 36 deletions
diff --git a/inotify/inotify.go b/inotify/inotify.go
index 1c0c3c6..bc54f7b 100644
--- a/inotify/inotify.go
+++ b/inotify/inotify.go
@@ -19,10 +19,11 @@
package inotify
import (
+ "os"
"sync"
- "syscall"
"unsafe"
- "os"
+
+ "golang.org/x/sys/unix"
)
// Most of the complexity here is that we syncronize around access to
@@ -30,13 +31,13 @@ import (
// closed and re-used between the time the fd is read from the os.File
// and the time the system call is actually dispatched.
//
-// A B
-// in.method() in.Close(); syscall.Open()
-// ---------------------------------------------------
+// A B
+// in.method() in.Close(); unix.Open()
+// -----------------------------------------
// fd = in.fd
-// syscall.Close()
-// syscall.Open()
-// syscall(fd)
+// unix.Close()
+// unix.Open()
+// unix.Syscall(fd)
//
// And you're wondering "should we really protoect against that; after
// all: share by communicating, don't communicate by sharing" and "why
@@ -57,9 +58,12 @@ type Inotify struct {
fdLock sync.RWMutex
fdBlock bool
- buffFull [4096]byte // 4KiB is a good size, but the bare
- // minimum is `sizeof(struct
- // inotify_event) + NAME_MAX + 1`
+ // The bare binimum size of the buffer is
+ //
+ // unix.SizeofInotifyEvent + unix.NAME_MAX + 1
+ //
+ // But we don't want the bare minimu. 4KiB is a page size.
+ buffFull [4096]byte
buff []byte
buffLock sync.Mutex
buffErr error
@@ -87,9 +91,9 @@ func newInotify(fd inFd, blocking bool) *Inotify {
return nil
}
in := &Inotify{
- fd: fd,
+ fd: fd,
fdBlock: blocking,
- ch: make(chan chev),
+ ch: make(chan chev),
}
in.buff = in.buffFull[:0]
go in.worker()
@@ -133,7 +137,7 @@ func InotifyInit() (*Inotify, error) {
// extra functionality.
func InotifyInit1(flags int) (*Inotify, error) {
fd, err := sys_inotify_init1(flags &^ IN_NONBLOCK)
- return newInotify(fd, flags & IN_NONBLOCK == 0), err
+ return newInotify(fd, flags&IN_NONBLOCK == 0), err
}
// AddWatch adds a watch to the inotify instance, or modifies an
@@ -160,9 +164,9 @@ func (in *Inotify) Close() (err error) {
//
// Choices for the error:
// - Linux: EBADF
- // - os.File: syscall.EINVAL
+ // - os.File: unix.EINVAL
// - net.netFD: net.errClosing = errors.New(...)
- err = os.NewSyscallError("close", syscall.EBADF)
+ err = os.NewSyscallError("close", unix.EBADF)
}
}()
close(in.ch) // will panic if already closed; hence above
@@ -198,31 +202,31 @@ func (in *Inotify) read() (Event, error) {
in.buff = in.buffFull[0:n]
}
- if len(in.buff) < syscall.SizeofInotifyEvent {
+ if len(in.buff) < unix.SizeofInotifyEvent {
// Either Linux screwed up (and we have no chance of
// handling that sanely), or this Inotify came from an
// existing FD that wasn't really an inotify instance.
- in.buffErr = syscall.EBADF
+ in.buffErr = unix.EBADF
return Event{Wd: -1}, in.buffErr
}
- raw := (*syscall.InotifyEvent)(unsafe.Pointer(&in.buff[0]))
+ raw := (*unix.InotifyEvent)(unsafe.Pointer(&in.buff[0]))
ret := Event{
Wd: Wd(raw.Wd),
Mask: Mask(raw.Mask),
Cookie: raw.Cookie,
Name: nil,
}
- if int64(len(in.buff)) < syscall.SizeofInotifyEvent+int64(raw.Len) {
+ if int64(len(in.buff)) < unix.SizeofInotifyEvent+int64(raw.Len) {
// Same as above.
- in.buffErr = syscall.EBADF
+ in.buffErr = unix.EBADF
return Event{Wd: -1}, in.buffErr
}
if raw.Len > 0 {
- bytes := (*[syscall.NAME_MAX]byte)(unsafe.Pointer(&in.buff[syscall.SizeofInotifyEvent]))
+ bytes := (*[unix.NAME_MAX]byte)(unsafe.Pointer(&in.buff[unix.SizeofInotifyEvent]))
name := string(bytes[:raw.Len-1])
ret.Name = &name
}
- in.buff = in.buff[0 : syscall.SizeofInotifyEvent+raw.Len]
+ in.buff = in.buff[0 : unix.SizeofInotifyEvent+raw.Len]
return ret, nil
}
@@ -253,7 +257,7 @@ func (in *Inotify) Read() (Event, error) {
func (in *Inotify) ReadBlock() (Event, error) {
e, ok := <-in.ch
if !ok {
- return Event{Wd: -1}, os.NewSyscallError("read", syscall.EBADF)
+ return Event{Wd: -1}, os.NewSyscallError("read", unix.EBADF)
}
return e.Ev, e.Err
}
@@ -271,7 +275,7 @@ func (in *Inotify) ReadNonblock() (Event, error) {
select {
case e, ok := <-in.ch:
if !ok {
- return Event{Wd: -1}, os.NewSyscallError("read", syscall.EBADF)
+ return Event{Wd: -1}, os.NewSyscallError("read", unix.EBADF)
}
return e.Ev, e.Err
default:
diff --git a/inotify/syscall.go b/inotify/syscall.go
index 33a10e2..a6f203f 100644
--- a/inotify/syscall.go
+++ b/inotify/syscall.go
@@ -18,7 +18,8 @@ package inotify
import (
"os"
- "syscall"
+
+ "golang.org/x/sys/unix"
)
func newPathError(op string, path string, err error) error {
@@ -30,26 +31,26 @@ func newPathError(op string, path string, err error) error {
// Create and initialize inotify instance.
func sys_inotify_init() (inFd, error) {
- fd, err := syscall.InotifyInit()
+ 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 := syscall.InotifyInit1(flags)
+ 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 := syscall.InotifyAddWatch(int(fd), name, uint32(mask))
+ 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 := syscall.InotifyRmWatch(int(fd), uint32(wd))
+ success, err := unix.InotifyRmWatch(int(fd), uint32(wd))
switch success {
case -1:
if err == nil {
@@ -66,16 +67,16 @@ func sys_inotify_rm_watch(fd inFd, wd Wd) error {
}
func sys_close(fd inFd) error {
- return os.NewSyscallError("close", syscall.Close(int(fd)))
+ return os.NewSyscallError("close", unix.Close(int(fd)))
}
func sys_read(fd inFd, p []byte) (int, error) {
- n, err := syscall.Read(int(fd), p)
+ n, err := unix.Read(int(fd), p)
return n, os.NewSyscallError("read", err)
}
func sys_dupfd_cloexec(fd inFd) (inFd, error) {
- n, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_DUPFD_CLOEXEC, 0)
+ n, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), unix.F_DUPFD_CLOEXEC, 0)
if errno != 0 {
return -1, os.NewSyscallError("dupfd_cloexec", errno)
}
@@ -83,14 +84,14 @@ func sys_dupfd_cloexec(fd inFd) (inFd, error) {
}
func sys_setnonblock(fd inFd, nonblocking bool) error {
- err := syscall.SetNonblock(int(fd), nonblocking)
+ err := unix.SetNonblock(int(fd), nonblocking)
return os.NewSyscallError("setnonblock", err)
}
func sys_getnonblock(fd inFd) (bool, error) {
- flag, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_GETFL, 0)
+ flag, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), unix.F_GETFL, 0)
if errno != 0 {
return false, os.NewSyscallError("getnonblock", errno)
}
- return flag & syscall.O_NONBLOCK != 0, nil
+ return flag&unix.O_NONBLOCK != 0, nil
}