1package store
2
3import (
4 "fmt"
5 "os"
6 "strings"
7 "time"
8
9 "github.com/mjl-/mox/mox-"
10 "github.com/mjl-/mox/moxvar"
11 "github.com/mjl-/mox/updates"
12)
13
14// StoreLastKnown stores the the last known version. Future update checks compare
15// against it, or the currently running version, whichever is newer.
16func StoreLastKnown(v updates.Version) error {
17 return os.WriteFile(mox.DataDirPath("lastknownversion"), []byte(v.String()), 0660)
18}
19
20// LastKnown returns the last known version that has been mentioned in an update
21// email, or the current application.
22func LastKnown() (current, lastknown updates.Version, mtime time.Time, rerr error) {
23 curv, curerr := updates.ParseVersion(moxvar.VersionBare)
24
25 p := mox.DataDirPath("lastknownversion")
26 fi, _ := os.Stat(p)
27 if fi != nil {
28 mtime = fi.ModTime()
29 }
30
31 vbuf, err := os.ReadFile(p)
32 if err != nil && !os.IsNotExist(err) {
33 return curv, updates.Version{}, mtime, err
34 }
35
36 lastknown, lasterr := updates.ParseVersion(strings.TrimSpace(string(vbuf)))
37
38 if curerr == nil && lasterr == nil {
39 if curv.After(lastknown) {
40 return curv, curv, mtime, nil
41 }
42 return curv, lastknown, mtime, nil
43 } else if curerr == nil {
44 return curv, curv, mtime, nil
45 } else if lasterr == nil {
46 return curv, lastknown, mtime, nil
47 }
48 if strings.HasPrefix(moxvar.Version, "(devel)") {
49 return curv, updates.Version{}, mtime, fmt.Errorf("development version")
50 }
51 return curv, updates.Version{}, mtime, fmt.Errorf("parsing version: %w", err)
52}
53