watcher

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package watcher monitors a directory tree for filesystem changes and delivers batched, debounced events to callers.

Watcher is the interface; New selects an implementation based on config.Config.WatchMode: "fsnotify" for kernel-level notifications, "poll" for periodic directory scanning, or "auto" which tries fsnotify and falls back to polling. Event carries the changed path and Op type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsWatchLimitError

func IsWatchLimitError(err error) bool

IsWatchLimitError reports whether err is a OS-level watch-limit error. On Linux this is ENOSPC (inotify watch table full); on macOS it is EMFILE (per-process file descriptor limit) or ENFILE (system-wide limit).

Types

type Event

type Event struct {
	Path string
	Op   Op
}

Event describes a single filesystem change.

type Op

type Op int

Op is the type of filesystem change.

const (
	// Create signals a new file was created.
	Create Op = iota
	// Write signals an existing file was modified.
	Write
	// Remove signals a file was deleted.
	Remove
	// Rename signals a file was renamed or moved.
	Rename
)

func (Op) String

func (o Op) String() string

type Watcher

type Watcher interface {
	// Watch returns a channel of batched, debounced events.
	// The channel is closed when ctx is cancelled or Close is called.
	// Watch must be called exactly once per Watcher instance.
	Watch(ctx context.Context) <-chan []Event
	Close() error
}

Watcher monitors a directory tree for file changes. Watch must be called exactly once; calling it a second time is not safe.

func New

func New(cfg *config.Config, matchFn func(string) bool, fsys fs.FS) (Watcher, error)

New creates a Watcher based on cfg.WatchMode:

  • "poll": polling watcher
  • "fsnotify": fsnotify watcher (errors if unavailable)
  • "auto" or "": attempts fsnotify; falls back to polling on error or watch limit

matchFn, when non-nil, is called to decide whether a file path should produce events. Pass nil to use the include/exclude pattern logic only. fsys is the filesystem to walk; pass w.FS() (the walker's os.Root-scoped FS) for production use to prevent symlink-escape attacks.

Jump to

Keyboard shortcuts

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