diff options
Diffstat (limited to 'inotify/inotify.go')
-rw-r--r-- | inotify/inotify.go | 26 |
1 files changed, 26 insertions, 0 deletions
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) { |