state

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package state provides state tracking and persistence.

Index

Constants

View Source
const (
	// DefaultWatchDebounce is the default debounce duration for file watch events.
	// Multiple rapid events are coalesced into a single change notification.
	DefaultWatchDebounce = 100 * time.Millisecond

	// DefaultStopTimeout is the maximum time to wait for the event loop to exit.
	DefaultStopTimeout = 5 * time.Second
)

File watcher defaults.

Variables

View Source
var (
	ErrPathOutsideBase = pathutil.ErrPathOutsideBase
	ErrPathTraversal   = pathutil.ErrPathTraversal
)

Security-related errors.

View Source
var (
	// ErrWatcherAlreadyRunning is returned when StartWithContext is called on a running watcher.
	ErrWatcherAlreadyRunning = fmt.Errorf("file watcher is already running")

	// ErrStopTimeout is returned when Stop fails to terminate the goroutine within the timeout.
	ErrStopTimeout = fmt.Errorf("file watcher stop timed out")
)

FileWatcher errors.

Functions

This section is empty.

Types

type FileChange

type FileChange struct {
	Path       string     `json:"path"`
	ChangeType string     `json:"change_type"` // "create", "modify", "delete"
	Before     *FileState `json:"before,omitempty"`
	After      *FileState `json:"after,omitempty"`
	Timestamp  time.Time  `json:"timestamp"`
}

FileChange represents a change to a file.

type FileState

type FileState struct {
	Path    string      `json:"path"`
	Hash    string      `json:"hash"`
	Size    int64       `json:"size"`
	ModTime time.Time   `json:"mod_time"`
	Mode    os.FileMode `json:"mode"`
	Exists  bool        `json:"exists"`
	Content []byte      `json:"-"` // Not serialized by default
}

FileState represents the state of a file at a point in time.

type FileSystemStore

type FileSystemStore struct {
	// contains filtered or unexported fields
}

FileSystemStore tracks file system state and changes.

func NewFileSystemStore

func NewFileSystemStore(basePath string, opts ...FileSystemStoreOption) *FileSystemStore

NewFileSystemStore creates a new file system store with optional path enforcement.

func (*FileSystemStore) CaptureFile

func (s *FileSystemStore) CaptureFile(path string) (*FileState, error)

CaptureFile captures the current state of a file. This is the public API that handles path validation.

func (*FileSystemStore) CaptureFileWithContent

func (s *FileSystemStore) CaptureFileWithContent(path string) (*FileState, error)

CaptureFileWithContent captures state including file content.

func (*FileSystemStore) CaptureFileWithContext added in v0.3.0

func (s *FileSystemStore) CaptureFileWithContext(ctx context.Context, path string) (*FileState, error)

CaptureFileWithContext captures the current state of a file with context cancellation. This is the public API that handles path validation and respects context cancellation.

func (*FileSystemStore) Clear

func (s *FileSystemStore) Clear()

Clear resets the store.

func (*FileSystemStore) CreateSnapshot

func (s *FileSystemStore) CreateSnapshot(message string) *Snapshot

CreateSnapshot creates a snapshot of current tracked files.

func (*FileSystemStore) DetectChanges

func (s *FileSystemStore) DetectChanges() ([]*FileChange, error)

DetectChanges checks for changes since tracking started.

func (*FileSystemStore) GetChanges

func (s *FileSystemStore) GetChanges() []*FileChange

GetChanges returns recorded changes.

func (*FileSystemStore) GetChangesSince

func (s *FileSystemStore) GetChangesSince(snapshotID string) []*FileChange

GetChangesSince returns changes since a snapshot.

func (*FileSystemStore) GetSnapshot

func (s *FileSystemStore) GetSnapshot(id string) *Snapshot

GetSnapshot retrieves a snapshot by ID.

func (*FileSystemStore) ListSnapshots

func (s *FileSystemStore) ListSnapshots() []*Snapshot

ListSnapshots returns all snapshots.

func (*FileSystemStore) RecordChange

func (s *FileSystemStore) RecordChange(path string, changeType string) error

RecordChange manually records a file change.

func (*FileSystemStore) Rollback

func (s *FileSystemStore) Rollback(snapshotID string) error

Rollback restores files to a previous snapshot.

func (*FileSystemStore) RollbackChanges

func (s *FileSystemStore) RollbackChanges(count int) error

RollbackChanges undoes recent changes.

func (*FileSystemStore) Track

func (s *FileSystemStore) Track(path string) error

Track starts tracking a file for changes.

func (*FileSystemStore) TrackDir

func (s *FileSystemStore) TrackDir(dir string, patterns ...string) error

TrackDir tracks all files in a directory.

type FileSystemStoreOption

type FileSystemStoreOption func(*FileSystemStore)

FileSystemStoreOption configures FileSystemStore.

func WithEnforceBasePath

func WithEnforceBasePath(enforce bool) FileSystemStoreOption

WithEnforceBasePath enables strict base path enforcement for all operations.

func WithMaxChanges

func WithMaxChanges(max int) FileSystemStoreOption

WithMaxChanges limits the number of changes tracked (oldest evicted first).

func WithMaxSnapshots

func WithMaxSnapshots(max int) FileSystemStoreOption

WithMaxSnapshots limits the number of snapshots kept (oldest evicted first).

type FileWatcher

type FileWatcher struct {
	// contains filtered or unexported fields
}

FileWatcher watches files for changes using fsnotify. This provides event-driven file watching that scales to >10k files without the CPU overhead of polling.

func NewFileWatcher

func NewFileWatcher(config *WatchConfig, store *FileSystemStore) *FileWatcher

NewFileWatcher creates a file watcher.

func (*FileWatcher) OnChange

func (w *FileWatcher) OnChange(fn func([]*FileChange)) *FileWatcher

OnChange sets the callback for file changes. Must be called before Start() or behavior is undefined.

func (*FileWatcher) OnError

func (w *FileWatcher) OnError(fn func(error)) *FileWatcher

OnError sets the callback for watcher errors. If not set, errors are logged to stderr. Must be called before Start() or behavior is undefined.

func (*FileWatcher) Start

func (w *FileWatcher) Start() error

Start begins watching for changes. Deprecated: Use StartWithContext for proper context cancellation support.

func (*FileWatcher) StartWithContext added in v0.3.0

func (w *FileWatcher) StartWithContext(ctx context.Context) error

StartWithContext begins watching for changes with context cancellation support. The watcher will stop when the context is cancelled. Returns ErrWatcherAlreadyRunning if the watcher is already running.

func (*FileWatcher) Stop

func (w *FileWatcher) Stop() error

Stop stops watching and cleans up resources. Blocks until the event loop goroutine exits or the timeout expires. Returns ErrStopTimeout if the goroutine doesn't exit within DefaultStopTimeout.

type Snapshot

type Snapshot struct {
	ID        string                `json:"id"`
	Timestamp time.Time             `json:"timestamp"`
	Files     map[string]*FileState `json:"files"`
	Message   string                `json:"message,omitempty"`
}

Snapshot represents the state of multiple files at a point in time.

type WatchConfig

type WatchConfig struct {
	Paths     []string
	Patterns  []string
	Recursive bool
	Debounce  time.Duration
}

WatchConfig configures file watching.

Jump to

Keyboard shortcuts

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