~welt/murse

ref: db85e43da90177a9ed7275eb7f2d6952407c1466 murse/files.go -rw-r--r-- 1.5 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
78
79
80
81
82
83
84
85
86
87
88
89
package main

import (
	"crypto/rsa"
	"database/sql"
	"log"
	"os"
	"path/filepath"
	"strings"
)

func init() {
	cd, err := os.UserCacheDir()
	if err != nil {
		log.Fatal(err)
	}

	if !fileExists(cd) || !fileExists(filepath.Join(cd, "murse")) {
		err = os.MkdirAll(filepath.Join(cd, "murse"), 0777)
		if err != nil {
			log.Fatal(err)
		}
	}
}

func getKey() (*rsa.PublicKey, error) {
	cd, err := os.UserCacheDir()
	if err != nil {
		return nil, err
	}

	f := filepath.Join(cd, "murse", "public.key")
	if fileExists(f) {
		b, err := os.ReadFile(f)
		if err != nil {
			return nil, err
		}

		k, err := parseKey(&b)
		if err != nil {
			return nil, err
		}

		return k, nil
	}

	k, err := downloadDevKey()
	if err != nil {
		return nil, err
	}

	return k, nil
}

// This will return the files required between two given versions
func getRevisedFiles(db *sql.DB, lrev int, nrev int) (map[string]string, error) {
	rf := make(map[string]string)
	for i := lrev; i != nrev+1; i++ {
		mp, err := filesFromSQLite(db, i)
		if err != nil {
			return nil, err
		}

		for k, v := range mp {
			rf[k] = v
		}
	}
	return rf, nil
}

func isProtected(file string) bool {
	if strings.HasPrefix(file, "cfg/") ||
		strings.HasSuffix(file, ".cfg") ||
		strings.HasSuffix(file, ".dem") ||
		strings.HasPrefix(file, "custom") ||
		strings.HasPrefix(file, "downloads") ||
		strings.HasSuffix(file, "cache") {
		return true
	}
	return false
}

func fileExists(file string) bool {
	_, err := os.Stat(file)
	if !os.IsNotExist(err) || err == nil {
		return true
	}
	return false
}