This commit is contained in:
Jesse Newland
2019-02-09 20:39:09 -06:00
parent de62a2eece
commit 3198627879
19 changed files with 1077 additions and 19 deletions

2
vendor/github.com/andreaskoch/go-fswatch/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,2 @@
language: go
go: 1.1

27
vendor/github.com/andreaskoch/go-fswatch/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,27 @@
"New BSD License"
Copyright (c) 2013, Andreas Koch "Andyk"
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

90
vendor/github.com/andreaskoch/go-fswatch/README.md generated vendored Normal file
View File

@@ -0,0 +1,90 @@
# fswatch
fswatch is a go library for watching file system changes to **does not** depend on inotify.
## Motivation
Why not use [inotify](http://en.wikipedia.org/wiki/Inotify)? Even though there are great libraries like [fsnotify](https://github.com/howeyc/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
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
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](https://travis-ci.org/andreaskoch/go-fswatch.png?branch=master)](https://travis-ci.org/andreaskoch/go-fswatch)
## 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.

33
vendor/github.com/andreaskoch/go-fswatch/debug.go generated vendored Normal file
View File

@@ -0,0 +1,33 @@
// Copyright 2013 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fswatch
import (
"fmt"
)
var (
debugIsEnabled = false
debugMessages chan string
)
func EnableDebug() chan string {
debugIsEnabled = true
debugMessages = make(chan string, 10)
return debugMessages
}
func DisableDebug() {
debugIsEnabled = false
close(debugMessages)
}
func log(format string, v ...interface{}) {
if !debugIsEnabled {
return
}
debugMessages <- fmt.Sprint(fmt.Sprintf(format, v...))
}

192
vendor/github.com/andreaskoch/go-fswatch/file.go generated vendored Normal file
View File

@@ -0,0 +1,192 @@
// Copyright 2013 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fswatch
import (
"fmt"
"os"
"time"
)
var numberOfFileWatchers int
func init() {
numberOfFolderWatchers = 0
}
func NumberOfFileWatchers() int {
return numberOfFileWatchers
}
type FileWatcher struct {
modified chan bool
moved chan bool
stopped chan bool
file string
running bool
wasStopped bool
checkInterval time.Duration
previousModTime time.Time
}
func NewFileWatcher(filePath string, checkIntervalInSeconds int) *FileWatcher {
if checkIntervalInSeconds < 1 {
panic(fmt.Sprintf("Cannot create a file watcher with a check interval of %v seconds.", checkIntervalInSeconds))
}
return &FileWatcher{
modified: make(chan bool),
moved: make(chan bool),
stopped: make(chan bool),
file: filePath,
checkInterval: time.Duration(checkIntervalInSeconds),
}
}
func (fileWatcher *FileWatcher) String() string {
return fmt.Sprintf("Filewatcher %q", fileWatcher.file)
}
func (fileWatcher *FileWatcher) SetFile(filePath string) {
fileWatcher.file = filePath
}
func (filewatcher *FileWatcher) Modified() chan bool {
return filewatcher.modified
}
func (filewatcher *FileWatcher) Moved() chan bool {
return filewatcher.moved
}
func (filewatcher *FileWatcher) Stopped() chan bool {
return filewatcher.stopped
}
func (fileWatcher *FileWatcher) Start() {
fileWatcher.running = true
sleepInterval := time.Second * fileWatcher.checkInterval
go func() {
// increment watcher count
numberOfFileWatchers++
var modTime time.Time
previousModTime := fileWatcher.getPreviousModTime()
if timeIsSet(previousModTime) {
modTime = previousModTime
} else {
currentModTime, err := getLastModTimeFromFile(fileWatcher.file)
if err != nil {
// send out the notification
log("File %q has been moved or is inaccessible.", fileWatcher.file)
go func() {
fileWatcher.moved <- true
}()
// stop this file watcher
fileWatcher.Stop()
} else {
modTime = currentModTime
}
}
for fileWatcher.wasStopped == false {
newModTime, err := getLastModTimeFromFile(fileWatcher.file)
if err != nil {
// send out the notification
log("File %q has been moved.", fileWatcher.file)
go func() {
fileWatcher.moved <- true
}()
// stop this file watcher
fileWatcher.Stop()
continue
}
// detect changes
if modTime.Before(newModTime) {
// send out the notification
log("File %q has been modified.", fileWatcher.file)
go func() {
fileWatcher.modified <- true
}()
} else {
log("File %q has not changed.", fileWatcher.file)
}
// assign the new modtime
modTime = newModTime
time.Sleep(sleepInterval)
}
fileWatcher.running = false
// capture the entry list for a restart
fileWatcher.captureModTime(modTime)
// inform channel-subscribers
go func() {
fileWatcher.stopped <- true
}()
// decrement the watch counter
numberOfFileWatchers--
// final log message
log("Stopped file watcher %q", fileWatcher.String())
}()
}
func (fileWatcher *FileWatcher) Stop() {
log("Stopping file watcher %q", fileWatcher.String())
fileWatcher.wasStopped = true
}
func (fileWatcher *FileWatcher) IsRunning() bool {
return fileWatcher.running
}
func (fileWatcher *FileWatcher) getPreviousModTime() time.Time {
return fileWatcher.previousModTime
}
// Remember the last mod time for a later restart
func (fileWatcher *FileWatcher) captureModTime(modTime time.Time) {
fileWatcher.previousModTime = modTime
}
func getLastModTimeFromFile(file string) (time.Time, error) {
fileInfo, err := os.Stat(file)
if err != nil {
return time.Time{}, err
}
return fileInfo.ModTime(), nil
}
func timeIsSet(t time.Time) bool {
return time.Time{} == t
}

250
vendor/github.com/andreaskoch/go-fswatch/folder.go generated vendored Normal file
View File

@@ -0,0 +1,250 @@
// Copyright 2013 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fswatch
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
)
var numberOfFolderWatchers int
func init() {
numberOfFolderWatchers = 0
}
func NumberOfFolderWatchers() int {
return numberOfFolderWatchers
}
type FolderWatcher struct {
changeDetails chan *FolderChange
modified chan bool
moved chan bool
stopped chan bool
recurse bool
skipFile func(path string) bool
debug bool
folder string
running bool
wasStopped bool
checkInterval time.Duration
previousEntries []string
}
func NewFolderWatcher(folderPath string, recurse bool, skipFile func(path string) bool, checkIntervalInSeconds int) *FolderWatcher {
if checkIntervalInSeconds < 1 {
panic(fmt.Sprintf("Cannot create a folder watcher with a check interval of %v seconds.", checkIntervalInSeconds))
}
return &FolderWatcher{
modified: make(chan bool),
moved: make(chan bool),
stopped: make(chan bool),
changeDetails: make(chan *FolderChange),
recurse: recurse,
skipFile: skipFile,
debug: true,
folder: folderPath,
checkInterval: time.Duration(checkIntervalInSeconds),
}
}
func (folderWatcher *FolderWatcher) String() string {
return fmt.Sprintf("Folderwatcher %q", folderWatcher.folder)
}
func (folderWatcher *FolderWatcher) Modified() chan bool {
return folderWatcher.modified
}
func (folderWatcher *FolderWatcher) Moved() chan bool {
return folderWatcher.moved
}
func (folderWatcher *FolderWatcher) Stopped() chan bool {
return folderWatcher.stopped
}
func (folderWatcher *FolderWatcher) ChangeDetails() chan *FolderChange {
return folderWatcher.changeDetails
}
func (folderWatcher *FolderWatcher) Start() {
folderWatcher.running = true
sleepInterval := time.Second * folderWatcher.checkInterval
go func() {
// get existing entries
var entryList []string
directory := folderWatcher.folder
previousEntryList := folderWatcher.getPreviousEntryList()
if previousEntryList != nil {
// use the entry list from a previous run
entryList = previousEntryList
} else {
// use a new entry list
newEntryList, _ := getFolderEntries(directory, folderWatcher.recurse, folderWatcher.skipFile)
entryList = newEntryList
}
// increment watcher count
numberOfFolderWatchers++
for folderWatcher.wasStopped == false {
// get new entries
updatedEntryList, _ := getFolderEntries(directory, folderWatcher.recurse, folderWatcher.skipFile)
// check for new items
newItems := make([]string, 0)
modifiedItems := make([]string, 0)
for _, entry := range updatedEntryList {
if isNewItem := !sliceContainsElement(entryList, entry); isNewItem {
// entry is new
newItems = append(newItems, entry)
continue
}
// check if the file changed
if newModTime, err := getLastModTimeFromFile(entry); err == nil {
// check if file has been modified
timeOfLastCheck := time.Now().Add(sleepInterval * -1)
if timeOfLastCheck.Before(newModTime) {
// existing entry has been modified
modifiedItems = append(modifiedItems, entry)
}
}
}
// check for moved items
movedItems := make([]string, 0)
for _, entry := range entryList {
isMoved := !sliceContainsElement(updatedEntryList, entry)
if isMoved {
movedItems = append(movedItems, entry)
}
}
// assign the new list
entryList = updatedEntryList
// sleep
time.Sleep(sleepInterval)
// check if something happened
if len(newItems) > 0 || len(movedItems) > 0 || len(modifiedItems) > 0 {
// send out change
go func() {
folderWatcher.modified <- true
}()
go func() {
log("Folder %q changed", directory)
folderWatcher.changeDetails <- newFolderChange(newItems, movedItems, modifiedItems)
}()
} else {
log("No change in folder %q", directory)
}
}
folderWatcher.running = false
// capture the entry list for a restart
folderWatcher.captureEntryList(entryList)
// inform channel-subscribers
go func() {
folderWatcher.stopped <- true
}()
// decrement the watch counter
numberOfFolderWatchers--
// final log message
log("Stopped folder watcher %q", folderWatcher.String())
}()
}
func (folderWatcher *FolderWatcher) Stop() {
log("Stopping folder watcher %q", folderWatcher.String())
folderWatcher.wasStopped = true
}
func (folderWatcher *FolderWatcher) IsRunning() bool {
return folderWatcher.running
}
func (folderWatcher *FolderWatcher) getPreviousEntryList() []string {
return folderWatcher.previousEntries
}
// Remember the entry list for a later restart
func (folderWatcher *FolderWatcher) captureEntryList(list []string) {
folderWatcher.previousEntries = list
}
func getFolderEntries(directory string, recurse bool, skipFile func(path string) bool) ([]string, error) {
// the return array
entries := make([]string, 0)
// read the entries of the specified directory
directoryEntries, err := ioutil.ReadDir(directory)
if err != nil {
return entries, err
}
for _, entry := range directoryEntries {
// get the full path
subEntryPath := filepath.Join(directory, entry.Name())
// recurse or append
if recurse && entry.IsDir() {
// recurse (ignore errors, unreadable sub directories don't hurt much)
subFolderEntries, _ := getFolderEntries(subEntryPath, recurse, skipFile)
entries = append(entries, subFolderEntries...)
} else {
// check if the enty shall be ignored
if skipFile(subEntryPath) {
continue
}
// append entry
entries = append(entries, subEntryPath)
}
}
return entries, nil
}

View File

@@ -0,0 +1,46 @@
// Copyright 2013 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fswatch
import (
"fmt"
"time"
)
type FolderChange struct {
timeStamp time.Time
newItems []string
movedItems []string
modifiedItems []string
}
func newFolderChange(newItems, movedItems, modifiedItems []string) *FolderChange {
return &FolderChange{
timeStamp: time.Now(),
newItems: newItems,
movedItems: movedItems,
modifiedItems: modifiedItems,
}
}
func (folderChange *FolderChange) String() string {
return fmt.Sprintf("Folderchange (timestamp: %s, new: %d, moved: %d)", folderChange.timeStamp, len(folderChange.New()), len(folderChange.Moved()))
}
func (folderChange *FolderChange) TimeStamp() time.Time {
return folderChange.timeStamp
}
func (folderChange *FolderChange) New() []string {
return folderChange.newItems
}
func (folderChange *FolderChange) Moved() []string {
return folderChange.movedItems
}
func (folderChange *FolderChange) Modified() []string {
return folderChange.modifiedItems
}

14
vendor/github.com/andreaskoch/go-fswatch/util.go generated vendored Normal file
View File

@@ -0,0 +1,14 @@
// Copyright 2013 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fswatch
func sliceContainsElement(list []string, elem string) bool {
for _, t := range list {
if t == elem {
return true
}
}
return false
}

15
vendor/github.com/andreaskoch/go-fswatch/watcher.go generated vendored Normal file
View File

@@ -0,0 +1,15 @@
// Copyright 2013 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fswatch
type Watcher interface {
Modified() chan bool
Moved() chan bool
Stopped() chan bool
Start()
Stop()
IsRunning() bool
}