summaryrefslogtreecommitdiffstats
path: root/back/qver/update.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2026-03-14 10:13:55 -0400
committerSam Anthony <sam@samanthony.xyz>2026-03-14 10:13:55 -0400
commit7d622695ae19e518fded6aa5fbf001dae4652211 (patch)
treecca62a7c6213c824adc0ad8447ff189cb106fcf0 /back/qver/update.go
parent3b8368d665c8818b84557f54681c5ebab35ba22e (diff)
downloadbuth-7d622695ae19e518fded6aa5fbf001dae4652211.zip
back: add qver package to track qid versions
Diffstat (limited to 'back/qver/update.go')
-rw-r--r--back/qver/update.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/back/qver/update.go b/back/qver/update.go
new file mode 100644
index 0000000..afbe51c
--- /dev/null
+++ b/back/qver/update.go
@@ -0,0 +1,29 @@
+package qver
+
+import (
+ "os"
+ "time"
+)
+
+// An UpdateFunc updates the version and state variables. It is called
+// by Version.Get(). If there is an error, it should return the old
+// version and state along with the error.
+type UpdateFunc func(version uint32, state interface{}) (uint32, interface{}, error)
+
+// UpdateOnFileMod returns a state variable and an UpdateFunc that
+// bumps the version when a file or directory is modified. The return
+// values are to be passed to the Update() option.
+func UpdateOnFileMod(path string) (state interface{}, f UpdateFunc) {
+ return time.Now(), func(ver uint32, state interface{}) (uint32, interface{}, error) {
+ mtime := state.(time.Time)
+ info, err := os.Stat(path)
+ if err != nil {
+ return ver, mtime, err
+ }
+ if info.ModTime().After(mtime) {
+ mtime = info.ModTime()
+ ver++
+ }
+ return ver, mtime, err
+ }
+}