-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefox.go
107 lines (98 loc) · 2.98 KB
/
firefox.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// this firefox wrapper reduces firefox's unnecessary disk io.
// it does so by keeping the profile in /dev/shm and periodically syncing it back to disk.
// assumption 1: the profile's directory is a symlink to /dev/shm.
// assumption 2: the disk's directory name is .mozilla/firefox/${profile}.disk
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"time"
)
func run() error {
// no-op on most of my machines.
if hostname, _ := os.Hostname(); hostname != "ipi" {
cmd := exec.Command("/usr/bin/firefox", os.Args[1:]...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
return cmd.Run()
}
// extract profile name.
ffdir := filepath.Join(os.Getenv("HOME"), ".mozilla/firefox")
profiles, err := os.ReadFile(filepath.Join(ffdir, "profiles.ini"))
if err != nil {
return err
}
var profile string
profilePrefix := []byte("Default=")
for _, line := range bytes.Split(profiles, []byte("\n")) {
if bytes.HasPrefix(line, profilePrefix) {
profile = string(line[len(profilePrefix):])
break
}
}
if profile == "" {
return fmt.Errorf("couldn't find default profile in %s", ffdir)
}
diskdir := filepath.Join(ffdir, profile+".disk")
memdir := filepath.Join("/dev/shm", profile)
os.Mkdir(memdir, 0700) // in case this is the first run.
// lock the /dev/shm version.
fd, err := syscall.Open(memdir, syscall.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("open %s: %w", memdir, err)
}
if err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
return fmt.Errorf("flock %s: %w", memdir, err)
}
// run the initial sync from disk to mem.
log.Print("syncing to memory")
tomemCmd := exec.Command("/usr/bin/rsync", "--delete", "-clr", diskdir+"/", memdir)
tomemCmd.Stdout, tomemCmd.Stderr = os.Stdout, os.Stderr
if err := tomemCmd.Run(); err != nil {
return fmt.Errorf("disk to mem rsync: %w", err)
}
log.Print("sync to memory done")
// start firefox.
log.Print("starting firefox")
cmd := exec.Command("/usr/bin/firefox", os.Args[1:]...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
ffdone := make(chan error)
go func() { ffdone <- cmd.Run() }()
// periodically sync until ff quits or the user quits this wrapper.
sig, done := make(chan os.Signal), false
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for !done {
select {
case <-time.After(4 * time.Hour):
log.Print("triggering a periodic sync")
break
case <-ffdone:
log.Print("firefox exited")
done = true
case s := <-sig:
log.Print("received the %s signal", s)
cmd.Process.Kill()
time.Sleep(2 * time.Second)
done = true
}
// sync back the memory to disk.
log.Print("syncing to disk")
tomemCmd := exec.Command("/usr/bin/rsync", "--delete", "-clr", memdir+"/", diskdir)
tomemCmd.Stdout, tomemCmd.Stderr = os.Stdout, os.Stderr
if err := tomemCmd.Run(); err != nil {
log.Printf("error syncing back to disk: %v", err)
}
log.Print("sync to disk done")
}
return nil
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}