fswatch

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2015 License: BSD-3-Clause Imports: 5 Imported by: 15

README

fswatch

fswatch is a go library for watching file system changes to does not depend on inotify.

Motivation

Why not use inotify? Even though there are great libraries like fsnotify that offer cross platform file system change notifications - the approach breaks when you want to watch a lot of files or folder.

For example the default ulimit for Mac OS is set to 512. If you need to watch more files you have to increase the ulimit for open files per process. And this sucks.

Usage

Watching a single file

If you want to watch a single file use the NewFileWatcher function to create a new file watcher:

go func() {
	fileWatcher := fswatch.NewFileWatcher("Some-file").Start()

	for fileWatcher.IsRunning() {

		select {
		case <-fileWatcher.Modified:

			go func() {
				// file changed. do something.
			}()

		case <-fileWatcher.Moved:

			go func() {
				// file moved. do something.
			}()
		}

	}
}()
Watching a folder

To watch a whole folder for new, modified or deleted files you can use the NewFolderWatcher function.

Parameters:

  1. The directory path
  2. A flag indicating whether the folder shall be watched recursively
  3. An expression which decides which files are skipped
go func() {

	recurse := true

	skipNoFile := func(path string) bool {
		return false
	}	

	folderWatcher := fswatch.NewFolderWatcher("some-directory", recurse, skipNoFile).Start()

	for folderWatcher.IsRunning() {

		select {
		case <-folderWatcher.Change:

			go func() {
				// some file changed, was added, moved or deleted.
			}()

		}
	}

}()

Build Status

Build Status

Contribute

If you have an idea

  • how to reliably increase the limit for the maximum number of open files from within the application
  • how to overcome the limitations of inotify without having to resort to checking the files for changes over and over again
  • or how to make the existing code more efficient

please send me a message or a pull request. All contributions are welcome.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableDebug added in v1.0.0

func DisableDebug()

func EnableDebug added in v1.0.0

func EnableDebug() chan string

func NumberOfFileWatchers added in v1.0.0

func NumberOfFileWatchers() int

func NumberOfFolderWatchers added in v1.0.0

func NumberOfFolderWatchers() int

Types

type FileWatcher

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

func NewFileWatcher

func NewFileWatcher(filePath string, checkIntervalInSeconds int) *FileWatcher

func (*FileWatcher) IsRunning

func (fileWatcher *FileWatcher) IsRunning() bool

func (*FileWatcher) Modified

func (filewatcher *FileWatcher) Modified() chan bool

func (*FileWatcher) Moved

func (filewatcher *FileWatcher) Moved() chan bool

func (*FileWatcher) SetFile

func (fileWatcher *FileWatcher) SetFile(filePath string)

func (*FileWatcher) Start

func (fileWatcher *FileWatcher) Start()

func (*FileWatcher) Stop

func (fileWatcher *FileWatcher) Stop()

func (*FileWatcher) Stopped

func (filewatcher *FileWatcher) Stopped() chan bool

func (*FileWatcher) String

func (fileWatcher *FileWatcher) String() string

type FolderChange

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

func (*FolderChange) Modified added in v1.0.0

func (folderChange *FolderChange) Modified() []string

func (*FolderChange) Moved

func (folderChange *FolderChange) Moved() []string

func (*FolderChange) New

func (folderChange *FolderChange) New() []string

func (*FolderChange) String

func (folderChange *FolderChange) String() string

func (*FolderChange) TimeStamp

func (folderChange *FolderChange) TimeStamp() time.Time

type FolderWatcher

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

func NewFolderWatcher

func NewFolderWatcher(folderPath string, recurse bool, skipFile func(path string) bool, checkIntervalInSeconds int) *FolderWatcher

func (*FolderWatcher) ChangeDetails added in v1.0.0

func (folderWatcher *FolderWatcher) ChangeDetails() chan *FolderChange

func (*FolderWatcher) IsRunning

func (folderWatcher *FolderWatcher) IsRunning() bool

func (*FolderWatcher) Modified added in v1.0.0

func (folderWatcher *FolderWatcher) Modified() chan bool

func (*FolderWatcher) Moved added in v1.0.0

func (folderWatcher *FolderWatcher) Moved() chan bool

func (*FolderWatcher) Start

func (folderWatcher *FolderWatcher) Start()

func (*FolderWatcher) Stop

func (folderWatcher *FolderWatcher) Stop()

func (*FolderWatcher) Stopped

func (folderWatcher *FolderWatcher) Stopped() chan bool

func (*FolderWatcher) String

func (folderWatcher *FolderWatcher) String() string

type Watcher added in v1.0.0

type Watcher interface {
	Modified() chan bool
	Moved() chan bool
	Stopped() chan bool

	Start()
	Stop()
	IsRunning() bool
}

Jump to

Keyboard shortcuts

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