state

package
v0.2.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: 11 Imported by: 0

Documentation

Overview

Package state provides state tracking and persistence.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPathOutsideBase = errors.New("path outside allowed base directory")
	ErrPathTraversal   = errors.New("path traversal detected")
)

Security-related 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) 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.

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.

func (*FileWatcher) Start

func (w *FileWatcher) Start() error

Start begins watching for changes.

func (*FileWatcher) Stop

func (w *FileWatcher) Stop()

Stop stops watching and cleans up resources.

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