From 9dbaa991819fde8a0afed727506290ee993d2eef Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 19 Dec 2016 02:06:19 -0500 Subject: Use x/sys/unix instead of the deprecated syscall. --- inotify/inotify.go | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'inotify/inotify.go') 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: -- cgit v1.2.3