-
Notifications
You must be signed in to change notification settings - Fork 19
/
pergob.go
78 lines (58 loc) · 1.49 KB
/
pergob.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
package sshego
import (
"bytes"
"encoding/gob"
"fmt"
"log"
"net"
"os/exec"
"time"
"github.com/glycerine/go-unsnap-stream"
"github.com/glycerine/sshego/xendor/github.com/glycerine/xcryptossh"
)
func (s *KnownHosts) saveGobSnappy(fn string) error {
mkpath(fn)
t0 := time.Now()
gob.Register(s)
gob.Register(net.TCPAddr{})
gob.Register(new(ssh.PublicKey))
var buf bytes.Buffer
enc := gob.NewEncoder(&buf) // Will write to buf
// Encode (send) some values.
err := enc.Encode(s)
if err != nil {
panic(fmt.Sprintf("encode error: %v", err))
}
// don't blow away the last good (fn) until the new version is completely written.
fnNew := fn + ".new"
//exec.Command("mv", fn+".prev", fn+".prev.prev").Run()
exec.Command("cp", "-p", fn, fn+".prev").Run()
var file *unsnap.SnappyFile
file, err = unsnap.Create(fnNew)
if err != nil {
panic(fmt.Sprintf("problem creating s outfile '%s': %s", fn, err))
}
defer file.Close()
drainable := buf
_, err = drainable.WriteTo(file)
file.Sync()
file.Close()
exec.Command("mv", fnNew, fn).Run()
log.Printf("saveGobSnappy() took %v", time.Since(t0))
return err
}
func (s *KnownHosts) readGobSnappy(fn string) error {
f, err := unsnap.Open(fn)
if err != nil {
return err
}
defer f.Close()
log.Printf("readgob() is restoring ceptor server state from file '%s'.", fn)
// Decode (receive) and print the values.
dec := gob.NewDecoder(f)
err = dec.Decode(&s)
if err != nil {
panic(fmt.Sprintf("decode error 1: %v", err))
}
return err
}