From 7d622695ae19e518fded6aa5fbf001dae4652211 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 14 Mar 2026 10:13:55 -0400 Subject: back: add qver package to track qid versions --- back/qver/update.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 back/qver/update.go (limited to 'back/qver/update.go') 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 + } +} -- cgit v1.2.3