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