-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
187 lines (167 loc) · 4.22 KB
/
storage.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"errors"
"io"
"io/ioutil"
"os"
"path"
)
type storage struct {
dataDir string
}
var errSeeker = errors.New("seeker can't seek")
func newStorage(dataDir string) *storage {
s := &storage{
dataDir: dataDir,
}
s.MkDir(s.dataDir)
return s
}
func (s *storage) FilePath(fileName string) string {
return path.Join(s.dataDir, fileName)
}
func (s *storage) IsEmpty(fileName string) bool {
var empty bool
if num, err := s.Size(fileName); err != nil || num == 0 || !s.Exists(fileName) {
empty = true
} else {
empty = false
}
return empty
}
func (s *storage) Exists(fileName string) bool {
filePath := path.Join(s.dataDir, fileName)
var exist = true
if _, err := os.Stat(filePath); os.IsNotExist(err) {
exist = false
}
return exist
}
func (s *storage) MkDir(dirName string) {
os.MkdirAll(dirName, 0744)
}
func (s *storage) Rm(fileName string) {
filePath := path.Join(s.dataDir, fileName)
os.Remove(filePath)
}
func (s *storage) SafeOverWrite(fileName string, data []byte) error {
filePath := path.Join(s.dataDir, fileName)
tmpFilePath := path.Join(s.dataDir, fileName+defaultTmp)
flushFilePath := path.Join(s.dataDir, fileName+defaultFlush)
defer func() {
os.Rename(filePath, tmpFilePath)
os.Rename(flushFilePath, filePath)
os.Remove(tmpFilePath)
}()
f, err := os.OpenFile(flushFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
n, err := f.Write(data)
if err != nil {
return err
} else if n < len(data) {
return io.ErrShortWrite
}
return f.Sync()
}
func (s *storage) Rename(oldName, newName string) error {
oldPath := path.Join(s.dataDir, oldName)
newPath := path.Join(s.dataDir, newName)
return os.Rename(oldPath, newPath)
}
func (s *storage) Load(fileName string) ([]byte, error) {
filePath := path.Join(s.dataDir, fileName)
return ioutil.ReadFile(filePath)
}
func (s *storage) Size(fileName string) (int64, error) {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_RDONLY, 0600)
if err != nil {
return 0, err
}
defer f.Close()
return f.Seek(0, os.SEEK_END)
}
func (s *storage) Sync(fileName string) error {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
return f.Sync()
}
func (s *storage) OverWrite(fileName string, data []byte) error {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
n, err := f.Write(data)
if err != nil {
return err
} else if n < len(data) {
return io.ErrShortWrite
}
return f.Sync()
}
func (s *storage) SeekWrite(fileName string, cursor uint64, data []byte) error {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
ret, _ := f.Seek(int64(cursor), os.SEEK_SET)
n, err := f.WriteAt(data, ret)
if err != nil {
return err
} else if n < len(data) {
return io.ErrShortWrite
}
return f.Sync()
}
func (s *storage) SeekWriteNoSync(fileName string, cursor uint64, data []byte) error {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
ret, _ := f.Seek(int64(cursor), os.SEEK_SET)
n, err := f.WriteAt(data, ret)
if err != nil {
return err
} else if n < len(data) {
return io.ErrShortWrite
}
return nil
}
func (s *storage) SeekRead(fileName string, cursor uint64, b []byte) (n int, err error) {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_RDONLY, 0600)
if err != nil {
return 0, err
}
defer f.Close()
ret, _ := f.Seek(int64(cursor), os.SEEK_SET)
return f.ReadAt(b, ret)
}
func (s *storage) Truncate(fileName string, size uint64) error {
filePath := path.Join(s.dataDir, fileName)
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
err = f.Truncate(int64(size))
if err != nil {
return err
}
return f.Sync()
}