package main // This file is for local storage related stuff. We intentionally // do not store a local sqlite database for performance and file size reasons. // All that we need to store is the current revision of our installation, however // this may change in the future. Storing this as JSON ensures backwards compatability. // Oh yeah, and a lockfile. import ( "encoding/json" "log" "os" "time" ) type localManifest struct { DownloadedRevision int `json:"downloadedRevision"` // the currently installed revision } var manifest localManifest var lmd string // path to local manifest var lfd string // path to lockfile func (m *localManifest) write() error { if !fileExists(lmd) { _, err := os.Create(lmd) if err != nil { return err } } b, err := json.Marshal(&manifest) if err != nil { return err } err = os.WriteFile(lmd, b, 0777) if err != nil { return err } return nil } func (m *localManifest) read() error { b, err := os.ReadFile(lmd) if err != nil { return err } err = json.Unmarshal(b, m) if err != nil { return err } return nil } func lockInstall() error { err := os.WriteFile(lfd, []byte(time.Now().String()), 0777) return err } func unlockInstall() { err := os.Remove(lfd) if err != nil { log.Fatal("Failed to unlock installation: " + err.Error()) } } func isLocked() bool { if fileExists(lfd) { return true } return false }