-
Notifications
You must be signed in to change notification settings - Fork 19
/
filedb.go
144 lines (124 loc) · 2.71 KB
/
filedb.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package sshego
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"strings"
"github.com/glycerine/greenpack/msgp"
)
//go:generate greenpack
var hostDbKey = "host-db"
type Filedb struct {
fd *os.File
filepath string
HostDb *HostDb `zid:"0"`
}
func (b *Filedb) Close() {
if b != nil && b.fd != nil {
b.fd.Close()
b.fd = nil
}
}
func convertSlashes(s string) string {
return strings.Replace(s, "/", "\\", -1)
}
func NewFiledb(filepath string) (*Filedb, error) {
if len(filepath) == 0 {
return nil, fmt.Errorf("filepath must not be empty string")
}
if filepath[0] != '/' && filepath[0] != '.' {
if runtime.GOOS != "windows" {
// help back-compat with old prefix style argument
filepath = "./" + filepath
}
}
if runtime.GOOS == "windows" {
filepath = convertSlashes(filepath)
}
b := &Filedb{
filepath: filepath,
}
sz := int64(0)
if fileExists(filepath) {
fi, err := os.Stat(filepath)
if err != nil {
return nil, fmt.Errorf("stat of filepath='%v' gave err='%v'", filepath, err)
}
sz = fi.Size()
_ = sz
} else {
return b, nil
}
if sz == 0 {
return b, nil
}
// maybe windows doesn't report the size?
//if sz == 0 {
//return nil, fmt.Errorf("database file present but empty! '%v'", filepath)
//}
// Open the my.db data file in your current directory.
// It will be created if it doesn't exist.
fd, err := os.OpenFile(b.filepath, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
wd, _ := os.Getwd()
// probably already open by another process.
return nil, fmt.Errorf("error opening Filedb: '%v' "+
"upon trying to open path '%s' in cwd '%s'", err, filepath, wd)
}
if err != nil {
return nil, err
}
defer fd.Close()
err = msgp.Decode(fd, b)
if err != nil {
return nil, fmt.Errorf("msgp.Decode returned err='%v'", err)
}
p("FILEDB opened successfully '%s'", filepath)
return b, nil
}
func (b *Filedb) SaveToDisk() error {
p("Filedb.SaveToDisk is saving to b.filepath='%s'", b.filepath)
fd, err := os.OpenFile(b.filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
fdJson, err := os.OpenFile(b.filepath+".json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer fdJson.Close()
defer fd.Close()
by, err := b.MarshalMsg(nil)
if err != nil {
return err
}
src := bytes.NewBuffer(by)
_, err = msgp.CopyToJSON(fdJson, src)
if err != nil {
return err
}
err = writeFull(fd, by)
if err != nil {
return err
}
return nil
}
func writeFull(w io.Writer, b []byte) error {
totw := 0
n := len(b)
for totw < n {
nw, err := w.Write(b[totw:])
if err != nil {
panic(err)
return err
}
totw += nw
}
return nil
}
func (b *Filedb) storeHostDb(h *HostDb) error {
b.HostDb = h
return b.SaveToDisk()
}