~welt/murse

murse/local.go -rw-r--r-- 1.4 KiB
db85e43dwelt guiprep: adjust logging 4 months ago
                                                                                
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
}