inotify

package module
v0.0.0-...-410e225 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 5 Imported by: 0

README

Documentation

Package inotify provides a no-frills, fully exposed, Linux inotify implementation in go.

There are several other packages available which already provide inotify functionality, but many of them provide abstractions which restrict what you can do. This package is meant to fully expose inotify, allowing as much control as desired. There are no hidden struct members, and the code is extremely simple.

In addition, the package does not utilize Cgo, and should thus work with the native go compiler, avoiding all the little gotchas that Cgo brings.

import "github.com/phemmer/go-inotify"

func main() {
  in, err := inotify.New()
  // handle error

  w, err := in.AddWatch("/path/to/foo", inotify.IN_CREATE|inotify.IN_DELETE)
  // handle error

  in.SetReadDeadline(time.Now().Add(time.Second))
  event, err := in.Read()
  // handle error
  // do something with event

  err := in.RemoveWatch(w)
  // moar error!

  in.Close()
}

Documentation

Overview

Package inotify provides a no-frills, fully exposed, Linux inotify implementation in go.

There are several other packages available which already provide inotify functionality, but many of them provide abstractions which restrict what you can do. This package is meant to fully expose inotify, allowing as much control as desired. There are no hidden struct members, and the code is extremely simple.

In addition, the package does not utilize Cgo, and should thus work with the native go compiler, avoiding all the little gotchas that Cgo brings.

Example
in, err := inotify.New()
if err != nil {
	fmt.Printf("Error: %s\n", err)
	return
}

w, err := in.AddWatch("/tmp", inotify.IN_CREATE|inotify.IN_DELETE)
if err != nil {
	fmt.Printf("Error: %s\n", err)
	return
}

// Test IN_CREATE
tf, _ := ioutil.TempFile("/tmp", "")
tf.Close()
event, err := in.Read()
if err != nil {
	fmt.Printf("Error: %s\n", err)
	return
}
fmt.Printf("Event: %+v\n", event)

// Test IN_DELETE
os.Remove(tf.Name())
event, err = in.Read()
if err != nil {
	fmt.Printf("Error: %s\n", err)
	return
}
fmt.Printf("Event: %+v\n", event)

// Stop watching the specific watch
in.RemoveWatch(w)

// Shut down entirely
in.Close()
Output:

Index

Examples

Constants

View Source
const (
	IN_ACCESS        Mask = unix.IN_ACCESS
	IN_ATTRIB        Mask = unix.IN_ATTRIB
	IN_CLOSE_WRITE   Mask = unix.IN_CLOSE_WRITE
	IN_CLOSE_NOWRITE Mask = unix.IN_CLOSE_NOWRITE
	IN_CREATE        Mask = unix.IN_CREATE
	IN_DELETE        Mask = unix.IN_DELETE
	IN_DELETE_SELF   Mask = unix.IN_DELETE_SELF
	IN_MODIFY        Mask = unix.IN_MODIFY
	IN_MOVE_SELF     Mask = unix.IN_MOVE_SELF
	IN_MOVED_FROM    Mask = unix.IN_MOVED_FROM
	IN_MOVED_TO      Mask = unix.IN_MOVED_TO
	IN_OPEN          Mask = unix.IN_OPEN
	IN_MOVE          Mask = unix.IN_MOVE
	IN_CLOSE         Mask = unix.IN_CLOSE
	IN_ALL_EVENTS    Mask = unix.IN_ALL_EVENTS

	IN_DONT_FOLLOW Mask = unix.IN_DONT_FOLLOW
	IN_MASK_ADD    Mask = unix.IN_MASK_ADD
	IN_ONESHOT     Mask = unix.IN_ONESHOT
	IN_ONLYDIR     Mask = unix.IN_ONLYDIR
	IN_MASK_CREATE Mask = unix.IN_MASK_CREATE

	IN_IGNORED    uint32 = unix.IN_IGNORED
	IN_ISDIR      uint32 = unix.IN_ISDIR
	IN_Q_OVERFLOW uint32 = unix.IN_Q_OVERFLOW
	IN_UNMOUNT    uint32 = unix.IN_UNMOUNT
)

These are duplicated locally and type converted for convenience.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// type InotifyEvent struct {
	//    Wd     int32
	//    Mask   uint32
	//    Cookie uint32
	//    Len    uint32
	// }
	unix.InotifyEvent
	Name string
}

type Inotify

type Inotify struct {
	Buffer *bytes.Buffer
	// contains filtered or unexported fields
}

func New

func New() (*Inotify, error)

New constructs a new Inotify watcher.

func (*Inotify) AddWatch

func (in *Inotify) AddWatch(pathname string, mask Mask) (InotifyWatchDesc, error)

AddWatch adds the given path to the watch list and returns a new watch descriptor. The watch descriptor can be provided to RemoveWatch() to stop watching.

func (*Inotify) Close

func (in *Inotify) Close() error

func (*Inotify) Read

func (in *Inotify) Read() (Event, error)

Read reads the next event, blocking if none is available.

func (*Inotify) RemoveWatch

func (in *Inotify) RemoveWatch(watchDesc InotifyWatchDesc) error

Removes the given watch descriptor from the watch list.

type InotifyWatchDesc

type InotifyWatchDesc int32

type Mask

type Mask = uint32

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL