summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-10-02 01:29:50 +0700
committerShulhan <ms@kilabit.info>2021-10-02 01:29:50 +0700
commita4d62261e82d3b7dd036cc83be407b73b321ddae (patch)
treed31d2c9f4d3337e7be9f747558ee160fb201bff8
parent8be5543e70932d4af49c784c08cfa11809c705a9 (diff)
downloadpakakeh.go-a4d62261e82d3b7dd036cc83be407b73b321ddae.tar.xz
lib/io: separate FileState for updated mode and content
Previously, when content of file being watched is modified, it will trigger the callback with State set to FileStateModified. When the mode of file is modified, it will also trigger the callback with the same state. This changes separated those state into two kind: FileStateUpdateMode for update on file mode, and FileStateUpdateContent for update on file content.
-rw-r--r--lib/io/dirwatcher.go2
-rw-r--r--lib/io/dirwatcher_test.go2
-rw-r--r--lib/io/filestate.go13
-rw-r--r--lib/io/watcher.go34
-rw-r--r--lib/io/watcher_test.go4
5 files changed, 24 insertions, 31 deletions
diff --git a/lib/io/dirwatcher.go b/lib/io/dirwatcher.go
index 0b2e15be..b2fd10ae 100644
--- a/lib/io/dirwatcher.go
+++ b/lib/io/dirwatcher.go
@@ -297,7 +297,7 @@ func (dw *DirWatcher) onModified(node *memfs.Node, newDirInfo os.FileInfo) {
ns := &NodeState{
Node: node,
- State: FileStateModified,
+ State: FileStateUpdateMode,
}
dw.Callback(ns)
diff --git a/lib/io/dirwatcher_test.go b/lib/io/dirwatcher_test.go
index b5546683..449453b2 100644
--- a/lib/io/dirwatcher_test.go
+++ b/lib/io/dirwatcher_test.go
@@ -39,7 +39,7 @@ func TestDirWatcher(t *testing.T) {
state: FileStateCreated,
path: "/",
}, {
- state: FileStateModified,
+ state: FileStateUpdateMode,
path: "/",
}, {
state: FileStateCreated,
diff --git a/lib/io/filestate.go b/lib/io/filestate.go
index fecb5b93..3c9cdac4 100644
--- a/lib/io/filestate.go
+++ b/lib/io/filestate.go
@@ -5,14 +5,13 @@
package io
// FileState define the state of file.
-// There are three state of file: created, modified, or deleted.
+// There are four states of file: created, updated on mode, updated on content
+// or deleted.
type FileState byte
const (
- // FileStateCreated indicate that the file has been created.
- FileStateCreated FileState = iota
- // FileStateModified indicate that the file has been modified.
- FileStateModified
- // FileStateDeleted indicate that the file has been deleted.
- FileStateDeleted
+ FileStateCreated FileState = iota // New file is created.
+ FileStateUpdateContent // The content of file is modified.
+ FileStateUpdateMode // The mode of file is modified.
+ FileStateDeleted // The file has been deleted.
)
diff --git a/lib/io/watcher.go b/lib/io/watcher.go
index 88f16546..daa202fe 100644
--- a/lib/io/watcher.go
+++ b/lib/io/watcher.go
@@ -93,58 +93,52 @@ func NewWatcher(path string, d time.Duration, cb WatchCallback) (w *Watcher, err
}
func (w *Watcher) start() {
+ logp := "Watcher"
if debug.Value >= 2 {
- fmt.Printf("lib/io: Watcher watching %q\n", w.path)
+ fmt.Printf("%s: %s: watching for changes\n", logp, w.path)
}
for range w.ticker.C {
+ ns := &NodeState{
+ Node: w.node,
+ }
+
newInfo, err := os.Stat(w.path)
if err != nil {
if !os.IsNotExist(err) {
- log.Println("lib/io: Watcher: " + err.Error())
+ log.Printf("%s: %s", logp, err.Error())
continue
}
if debug.Value >= 2 {
- fmt.Printf("lib/io: Watcher: deleted %q\n", w.node.SysPath)
+ fmt.Printf("%s: %s: deleted\n", logp, w.node.SysPath)
}
- ns := &NodeState{
- Node: w.node,
- State: FileStateDeleted,
- }
+ ns.State = FileStateDeleted
w.cb(ns)
w.node = nil
return
}
+
if w.node.Mode() != newInfo.Mode() {
if debug.Value >= 2 {
- fmt.Printf("lib/io: Watcher: mode modified %q\n", w.node.SysPath)
+ fmt.Printf("%s: %s: mode modified\n", logp, w.node.SysPath)
}
-
+ ns.State = FileStateUpdateMode
w.node.SetMode(newInfo.Mode())
- ns := &NodeState{
- Node: w.node,
- State: FileStateModified,
- }
w.cb(ns)
continue
}
if w.node.ModTime().Equal(newInfo.ModTime()) {
continue
}
-
if debug.Value >= 2 {
- fmt.Printf("lib/io: Watcher: content modified %q\n", w.node.SysPath)
+ fmt.Printf("%s: %s: content modified\n", logp, w.node.SysPath)
}
w.node.SetModTime(newInfo.ModTime())
w.node.SetSize(newInfo.Size())
- ns := &NodeState{
- Node: w.node,
- State: FileStateModified,
- }
-
+ ns.State = FileStateUpdateContent
w.cb(ns)
}
}
diff --git a/lib/io/watcher_test.go b/lib/io/watcher_test.go
index b5c2166b..e158c5a8 100644
--- a/lib/io/watcher_test.go
+++ b/lib/io/watcher_test.go
@@ -28,10 +28,10 @@ func TestWatcher(t *testing.T) {
mode os.FileMode
size int64
}{{
- state: FileStateModified,
+ state: FileStateUpdateMode,
mode: 0700,
}, {
- state: FileStateModified,
+ state: FileStateUpdateContent,
mode: 0700,
size: 13,
}, {