summaryrefslogtreecommitdiffstats
path: root/back/qver/update.go
blob: afbe51cc17192b62e29333333292cfaa5304cea6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
	}
}