mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 03:34:50 +00:00
refactor complex code into smaller pieces
This commit is contained in:
@@ -45,43 +45,65 @@ func (fs *FSWatcher) Init(rdirs, dirs []string) (chan error, chan struct{}) {
|
||||
doneCh := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
defer close(doneCh)
|
||||
|
||||
err := fs.i.Init()
|
||||
if err != nil {
|
||||
errCh <- fmt.Errorf("setting up inotify: %v", err)
|
||||
return
|
||||
}
|
||||
for _, dir := range rdirs {
|
||||
fs.addWatchers(dir, -1, errCh)
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
fs.addWatchers(dir, 0, errCh)
|
||||
}
|
||||
close(doneCh)
|
||||
|
||||
fs.addWatchers(rdirs, dirs, errCh)
|
||||
}()
|
||||
|
||||
return errCh, doneCh
|
||||
}
|
||||
|
||||
func (fs *FSWatcher) addWatchers(dir string, depth int, errCh chan error) {
|
||||
func (fs *FSWatcher) addWatchers(rdirs, dirs []string, errCh chan error) {
|
||||
for _, dir := range rdirs {
|
||||
fs.addWatchersToDir(dir, -1, errCh)
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
fs.addWatchersToDir(dir, 0, errCh)
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *FSWatcher) addWatchersToDir(dir string, depth int, errCh chan error) {
|
||||
dirCh, walkErrCh, doneCh := fs.w.Walk(dir, depth)
|
||||
|
||||
for {
|
||||
if fs.maxWatchers > 0 && fs.i.NumWatchers() >= fs.maxWatchers {
|
||||
if fs.maximumWatchersExceeded() {
|
||||
close(doneCh)
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-walkErrCh:
|
||||
errCh <- fmt.Errorf("adding inotift watchers: %v", err)
|
||||
case dir, ok := <-dirCh:
|
||||
if !ok {
|
||||
done, err := fs.handleNextWalkerResult(dirCh, walkErrCh)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
if done {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *FSWatcher) maximumWatchersExceeded() bool {
|
||||
return fs.maxWatchers > 0 && fs.i.NumWatchers() >= fs.maxWatchers
|
||||
}
|
||||
|
||||
func (fs *FSWatcher) handleNextWalkerResult(dirCh chan string, walkErrCh chan error) (bool, error) {
|
||||
select {
|
||||
case err := <-walkErrCh:
|
||||
return false, fmt.Errorf("adding inotify watchers: %v", err)
|
||||
case dir, ok := <-dirCh:
|
||||
if !ok {
|
||||
return true, nil // finished
|
||||
}
|
||||
if err := fs.i.Watch(dir); err != nil {
|
||||
errCh <- fmt.Errorf("Can't create watcher: %v", err)
|
||||
}
|
||||
return false, fmt.Errorf("Can't create watcher: %v", err)
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (fs *FSWatcher) Run() (chan struct{}, chan string, chan error) {
|
||||
@@ -111,6 +133,11 @@ func (fs *FSWatcher) observe(triggerCh chan struct{}, dataCh chan []byte, errCh
|
||||
|
||||
func (fs *FSWatcher) parseEvents(dataCh chan []byte, eventCh chan string, errCh chan error) {
|
||||
for buf := range dataCh {
|
||||
fs.handleChunk(buf, eventCh, errCh)
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *FSWatcher) handleChunk(buf []byte, eventCh chan string, errCh chan error) {
|
||||
var ptr uint32
|
||||
for len(buf[ptr:]) > 0 {
|
||||
event, size, err := fs.i.ParseNextEvent(buf[ptr:])
|
||||
@@ -122,4 +149,3 @@ func (fs *FSWatcher) parseEvents(dataCh chan []byte, eventCh chan string, errCh
|
||||
eventCh <- fmt.Sprintf("%20s | %s", event.Op, event.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user