gf/os/gfsnotify/gfsnotify.go

132 lines
4.5 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-04-17 18:58:22 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-04-17 18:58:22 +08:00
2019-01-15 23:27:47 +08:00
// Package gfsnotify provides a platform-independent interface for file system notifications.
2018-04-17 18:58:22 +08:00
package gfsnotify
import (
2019-06-19 09:06:52 +08:00
"errors"
"fmt"
2019-10-31 23:37:33 +08:00
"github.com/gogf/gf/container/gset"
"time"
2019-07-02 23:57:49 +08:00
"github.com/fsnotify/fsnotify"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/glist"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/container/gqueue"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/os/gcache"
2018-04-17 18:58:22 +08:00
)
2019-10-31 23:37:33 +08:00
// Watcher is the monitor for file changes.
2018-04-17 18:58:22 +08:00
type Watcher struct {
2019-10-31 23:37:33 +08:00
watcher *fsnotify.Watcher // Underlying fsnotify object.
events *gqueue.Queue // Used for internal event management.
cache *gcache.Cache // Used for repeated event filter.
nameSet *gset.StrSet // Used for AddOnce feature.
callbacks *gmap.StrAnyMap // Path(file/folder) to callbacks mapping.
closeChan chan struct{} // Used for watcher closing notification.
}
2019-10-31 23:37:33 +08:00
// Callback is the callback function for Watcher.
type Callback struct {
2019-10-31 23:37:33 +08:00
Id int // Unique id for callback object.
Func func(event *Event) // Callback function.
Path string // Bound file path (absolute).
name string // Registered name for AddOnce.
elem *glist.Element // Element in the callbacks of watcher.
recursive bool // Is bound to path recursively or not.
2018-04-17 18:58:22 +08:00
}
2019-10-31 23:37:33 +08:00
// Event is the event produced by underlying fsnotify.
2018-04-17 18:58:22 +08:00
type Event struct {
2019-10-31 23:37:33 +08:00
event fsnotify.Event // Underlying event.
Path string // Absolute file path.
Op Op // File operation.
Watcher *Watcher // Parent watcher.
2018-04-17 18:58:22 +08:00
}
2019-10-31 23:37:33 +08:00
// Op is the bits union for file operations.
2018-04-17 18:58:22 +08:00
type Op uint32
const (
2019-06-19 09:06:52 +08:00
CREATE Op = 1 << iota
WRITE
REMOVE
RENAME
CHMOD
2018-04-17 18:58:22 +08:00
)
const (
2019-10-31 23:37:33 +08:00
REPEAT_EVENT_FILTER_DURATION = time.Millisecond // Duration for repeated event filter.
gFSNOTIFY_EVENT_EXIT = "exit" // Custom exit event for internal usage.
)
var (
2019-10-31 23:37:33 +08:00
defaultWatcher, _ = New() // Default watcher.
callbackIdMap = gmap.NewIntAnyMap(true) // Id to callback mapping.
callbackIdGenerator = gtype.NewInt() // Atomic id generator for callback.
)
2019-10-31 23:37:33 +08:00
// New creates and returns a new watcher.
// Note that the watcher number is limited by the file handle setting of the system.
// Eg: fs.inotify.max_user_instances system variable in linux systems.
func New() (*Watcher, error) {
2019-06-19 09:06:52 +08:00
w := &Watcher{
cache: gcache.New(),
events: gqueue.New(),
2019-10-31 23:37:33 +08:00
nameSet: gset.NewStrSet(true),
2019-06-19 09:06:52 +08:00
closeChan: make(chan struct{}),
callbacks: gmap.NewStrAnyMap(true),
2019-06-19 09:06:52 +08:00
}
if watcher, err := fsnotify.NewWatcher(); err == nil {
w.watcher = watcher
} else {
return nil, err
}
w.startWatchLoop()
w.startEventLoop()
return w, nil
2018-04-17 18:58:22 +08:00
}
2019-10-31 23:37:33 +08:00
// Add monitors <path> using default watcher with callback function <callbackFunc>.
// The optional parameter <recursive> specifies whether monitoring the <path> recursively, which is true in default.
2019-06-19 09:06:52 +08:00
func Add(path string, callbackFunc func(event *Event), recursive ...bool) (callback *Callback, err error) {
return defaultWatcher.Add(path, callbackFunc, recursive...)
}
2019-10-31 23:37:33 +08:00
// AddOnce monitors <path> using default watcher with callback function <callbackFunc> only once using unique name <name>.
// If AddOnce is called multiple times with the same <name> parameter, <path> is only added to monitor once. It returns error
// if it's called twice with the same <name>.
//
// The optional parameter <recursive> specifies whether monitoring the <path> recursively, which is true in default.
func AddOnce(name, path string, callbackFunc func(event *Event), recursive ...bool) (callback *Callback, err error) {
return defaultWatcher.AddOnce(name, path, callbackFunc, recursive...)
}
// Remove removes all monitoring callbacks of given <path> from watcher recursively.
func Remove(path string) error {
2019-06-19 09:06:52 +08:00
return defaultWatcher.Remove(path)
}
2019-10-31 23:37:33 +08:00
// RemoveCallback removes specified callback with given id from watcher.
func RemoveCallback(callbackId int) error {
2019-06-19 09:06:52 +08:00
callback := (*Callback)(nil)
if r := callbackIdMap.Get(callbackId); r != nil {
callback = r.(*Callback)
}
if callback == nil {
return errors.New(fmt.Sprintf(`callback for id %d not found`, callbackId))
}
defaultWatcher.RemoveCallback(callbackId)
return nil
2018-04-18 09:16:08 +08:00
}
2019-03-14 23:28:56 +08:00
2019-10-31 23:37:33 +08:00
// Exit is only used in the callback function, which can be used to remove current callback from the watcher.
2019-03-14 23:28:56 +08:00
func Exit() {
2019-06-19 09:06:52 +08:00
panic(gFSNOTIFY_EVENT_EXIT)
2019-03-14 23:28:56 +08:00
}