From fc5d64a5fc886e0b8519e8c19d311ebfb0df59db Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 18 Dec 2016 15:51:39 -0500 Subject: add inotify.NewInotify --- inotify/inotify.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'inotify/inotify.go') diff --git a/inotify/inotify.go b/inotify/inotify.go index 2db414c..1c0c3c6 100644 --- a/inotify/inotify.go +++ b/inotify/inotify.go @@ -96,6 +96,32 @@ func newInotify(fd inFd, blocking bool) *Inotify { return in } +// NewInotify creates an Inotify object that wraps an inotify instance +// on an already open file descriptor, much like net.FileListener. +// +// Closing file does not affect the Inotify, and closing Inotify does +// not affect file. +func NewInotify(file *os.File) (*Inotify, error) { + dup, err := sys_dupfd_cloexec(inFd(file.Fd())) + if err != nil { + return nil, err + } + + nonblock, err := sys_getnonblock(dup) + if err != nil { + sys_close(dup) + return nil, err + } + + err = sys_setnonblock(dup, false) + if err != nil { + sys_close(dup) + return nil, err + } + + return newInotify(dup, !nonblock), nil +} + // InotifyInit creates an inotify instance. The variant // InotifyInit1() allows flags to access extra functionality. func InotifyInit() (*Inotify, error) { -- cgit v1.2.3