-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxpisync.go
More file actions
85 lines (69 loc) · 1.92 KB
/
xpisync.go
File metadata and controls
85 lines (69 loc) · 1.92 KB
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
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"log"
"os"
"path/filepath"
)
// syncXPI silently checks if the installed .xpi matches the embedded version.
// If the versions differ, it rebuilds and overwrites the .xpi and clears the startup cache.
func syncXPI() {
profiles, err := findThunderbirdProfiles()
if err != nil {
log.Printf("XPI sync: cannot find profiles: %v", err)
return
}
embeddedVersion := addonVersion()
for _, profile := range profiles {
xpiPath := filepath.Join(profile, "extensions", extensionID+".xpi")
installedVersion := getInstalledXPIVersion(xpiPath)
if installedVersion == embeddedVersion {
log.Printf("XPI sync: %s is up to date (v%s)", filepath.Base(profile), embeddedVersion)
continue
}
log.Printf("XPI sync: updating %s from v%s to v%s", filepath.Base(profile), installedVersion, embeddedVersion)
xpiData, err := buildXPI()
if err != nil {
log.Printf("XPI sync: failed to build XPI: %v", err)
continue
}
if err := installXPI(profile, xpiData); err != nil {
log.Printf("XPI sync: failed to install XPI to %s: %v", profile, err)
continue
}
// Clear startup cache
os.RemoveAll(filepath.Join(profile, "startupCache"))
log.Printf("XPI sync: updated %s successfully", filepath.Base(profile))
}
}
// getInstalledXPIVersion reads the version from an installed .xpi file.
func getInstalledXPIVersion(xpiPath string) string {
data, err := os.ReadFile(xpiPath)
if err != nil {
return ""
}
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return ""
}
for _, f := range r.File {
if f.Name == "manifest.json" {
rc, err := f.Open()
if err != nil {
return ""
}
defer rc.Close()
var manifest map[string]interface{}
if err := json.NewDecoder(rc).Decode(&manifest); err != nil {
return ""
}
if v, ok := manifest["version"].(string); ok {
return v
}
return ""
}
}
return ""
}