-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_rpi.go
80 lines (67 loc) · 1.37 KB
/
update_rpi.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
package main
import (
"io"
"net/http"
"os"
"syscall"
)
func updateStage1(url string) error {
wd, err := os.Getwd()
if err != nil {
return err
}
// TODO: check directory writable
// TODO: check executability in directory (ie. if FS is mounted with noexec or SELinux is blocking something)
// TODO: check if the executable itself is writable
err = download(url, wd+string(os.PathSeparator)+"seismosensor-new")
if err != nil {
return err
}
procname, err := os.Executable()
if err != nil {
return err
}
args := []string{"seismosensor-new", "-stage2update", procname}
env := os.Environ()
return syscall.Exec(wd+string(os.PathSeparator)+"seismosensor-new", args, env)
}
func updateStage2(filename string) error {
err := os.Remove(filename)
if err != nil {
return err
}
out, err := os.Create(filename)
if err != nil {
return err
}
in, err := os.Open(filename)
if err != nil {
return err
}
_, err = io.Copy(out, in)
if err != nil {
return err
}
in.Close()
out.Close()
args := []string{filename}
env := os.Environ()
return syscall.Exec(filename, args, env)
}
func download(url string, saveTo string) error {
out, err := os.Create(saveTo)
if err != nil {
return err
}
resp, err := http.Get(url)
if err != nil {
return err
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
resp.Body.Close()
out.Close()
return nil
}