-
Notifications
You must be signed in to change notification settings - Fork 0
/
registryfile.go
333 lines (266 loc) · 8.35 KB
/
registryfile.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package m2mpdb
import (
"fmt"
"io"
"log"
"os"
"strconv"
)
const (
prop_name = "fname"
prop_type = "ftype"
prop_size = "fsize"
prop_ok = "getOk"
prop_block_size = "blsize"
table_registry_data = "RegistryNodeData"
default_block_size = 512 * 1024 // 512 KB
)
type RegistryFile struct {
Node *RegistryNode
path string
}
func NewRegistryFile(node *RegistryNode) *RegistryFile {
return &RegistryFile{Node: node, path: node.Path}
}
func (this *RegistryFile) Create() {
this.Node.Create()
this.Node.SetValue(".is_file", "1")
}
func (this *RegistryFile) Exists() bool {
return this.Node.Exists() && this.Node.Value(".is_file") == "1"
}
func (this *RegistryFile) SetType(mime string) error {
return this.Node.SetValue(prop_type, mime)
}
func (this *RegistryFile) Type() string {
return this.Node.Value(prop_type)
}
func (this *RegistryFile) SetName(name string) error {
return this.Node.SetValue(prop_name, name)
}
func (this *RegistryFile) Name() string {
return this.Node.Value(prop_name)
}
func (this *RegistryFile) SetSize(size int) error {
return this.Node.SetValue(prop_size, fmt.Sprintf("%d", size))
}
func (this *RegistryFile) Size() int {
if size, err := strconv.Atoi(this.Node.Value(prop_size)); err == nil {
return size
} else {
return -1
}
}
func (this *RegistryFile) SetBlockSize(size int) error {
return this.Node.SetValue(prop_block_size, fmt.Sprintf("%d", size))
}
func (this *RegistryFile) BlockSize() int {
if size, err := strconv.Atoi(this.Node.Value(prop_block_size)); err == nil {
return size
} else {
this.Node.SetValue(prop_block_size, fmt.Sprintf("%d", default_block_size))
return default_block_size
}
}
func (this *RegistryFile) SetBlock(blockNb int, data []byte) error {
return shared.session.Query("INSERT INTO "+table_registry_data+" ( path, block, data ) VALUES ( ?, ?, ? );", this.path, blockNb, data).Exec()
}
func (this *RegistryFile) DelBlock(blockNb int) error {
return shared.session.Query("DELETE FROM "+table_registry_data+" WHERE path = ? AND blockNb = ?;", this.path, blockNb).Exec()
}
func (this *RegistryFile) Block(blockNb int) ([]byte, error) {
var data []byte
err := shared.session.Query("SELECT data FROM "+table_registry_data+" WHERE path = ? AND block = ?;", this.path, blockNb).Scan(&data)
return data, err
}
func (this *RegistryFile) SetOk(ok bool) {
var value string
if ok {
value = "1"
} else {
value = "0"
}
this.Node.SetValue(prop_ok, value)
}
func (this *RegistryFile) Ok() bool {
return this.Node.Value(prop_ok) == "1"
}
const (
mode_read = os.O_RDONLY
mode_write = os.O_WRONLY
)
type RegistryFilePointer struct {
file *RegistryFile
buffer []byte
blockSize int
offset int
size int
mode int
}
func (this *RegistryFile) Writer() *RegistryFilePointer {
this.Create()
return &RegistryFilePointer{file: this, blockSize: this.BlockSize(), mode: mode_write}
}
func (this *RegistryFile) WriterEdit() *RegistryFilePointer {
this.Create()
return &RegistryFilePointer{file: this, blockSize: this.BlockSize(), size: this.Size(), mode: mode_write}
}
func (this *RegistryFile) Reader() *RegistryFilePointer {
return &RegistryFilePointer{file: this, blockSize: this.BlockSize(), size: this.Size(), mode: mode_read}
}
func (this *RegistryFile) Truncate(size int) error {
fileSize := this.Size()
blockSize := this.BlockSize()
// We cannot truncate to more than the file size
if size > fileSize {
size = fileSize
}
offset := size
for offset < fileSize {
// Current block
currentBlock := offset / blockSize
// Current block offset
blockOffset := offset - (currentBlock * blockSize)
if blockOffset == 0 {
this.DelBlock(currentBlock)
} else {
// This part isn't mandatory. It's to avoid confusion with useless data.
if data, err := this.Block(currentBlock); err == nil {
if err := this.SetBlock(currentBlock, data[0:blockOffset]); err != nil {
return err
}
} else {
return err
}
}
// We increment the offset
offset += blockSize
}
// In the end we save this as the final size
this.SetSize(size)
return nil
}
func (this *RegistryFile) Delete(forReal bool) {
if forReal {
this.Truncate(0)
}
this.Node.Delete(forReal)
}
const debug_registryfile = false
// Writer interface
func (this *RegistryFilePointer) Write(data []byte) (int, error) {
dataSize := len(data)
inputOffset := 0
var err error = nil
if debug_registryfile {
log.Printf("Write( [%d]byte );", len(data))
}
// We will write as much blocks as needed
for inputOffset < dataSize {
// Current block
currentBlock := this.offset / this.blockSize
// Current block offset
blockOffset := this.offset - (currentBlock * this.blockSize)
// The size of what we can copy in this block
copySize := this.blockSize - blockOffset
// If we don't have that much data remaining, we reduce the copySize
if remaining := dataSize - inputOffset; copySize > remaining {
copySize = remaining
}
if debug_registryfile {
log.Printf("Write: currentBlock=%d, blockOffset=%d, copySize=%d, offset=%d, blockSize=%d", currentBlock, blockOffset, copySize, this.offset, this.blockSize)
}
// If we are at the offset zero of the block or
// if existing buffer doesn't exist, we read it.
if blockOffset == 0 || this.buffer == nil { // "this.buffer == nil" will only be used when we add Skip(n int)
// We try to load any existing data in the buffer (this will be used to allow to skip data on an existing file)
if this.buffer, err = this.file.Block(currentBlock); err != nil && err.Error() != "not found" {
break
} else if len(this.buffer) != this.blockSize {
// We might need to create a bigger buffer as the last block might not
// reach its full size
newBuffer := make([]byte, this.blockSize)
copy(newBuffer, this.buffer)
this.buffer = newBuffer
}
}
// We copy the amount of data we need to copy
copy(this.buffer[blockOffset:], data[inputOffset:inputOffset+copySize])
// We write the block
if err = this.file.SetBlock(currentBlock, this.buffer[0:blockOffset+copySize]); err != nil {
// And stop if we failed
break
}
// We increment the inputOffset and the global offset
inputOffset += copySize
this.offset += copySize
// We write the file size only if the offset is superior to the current file size
if this.offset > this.size {
if err = this.file.SetSize(this.offset); err != nil {
break
}
this.size = this.offset
}
}
return inputOffset, err
}
func (this *RegistryFilePointer) Seek(offset int, whence int) {
previousBlockNb := this.offset / this.blockSize
switch whence {
case 0:
this.offset = offset
case 1:
this.offset += offset
case 2:
this.offset = this.size - offset
}
if this.offset < 0 {
this.offset = 0
} else if this.mode == mode_read && this.offset > this.size {
this.offset = this.size
}
blockNb := this.offset / this.blockSize
if previousBlockNb != blockNb {
this.buffer = nil
}
}
// Reader interface
func (this *RegistryFilePointer) Read(data []byte) (int, error) {
dataSize := len(data)
// We might not have enough data to fill the buffer
if remaining := this.size - this.offset; dataSize > remaining {
dataSize = remaining
// If we don't have anything sent, it's an EOF
if dataSize == 0 {
return 0, io.EOF
}
}
outputOffset := 0
var err error = nil
for outputOffset < dataSize {
// Current block
currentBlock := this.offset / this.blockSize
// Current block offset
blockOffset := this.offset - (currentBlock * this.blockSize)
// Data to copy from the block
copySize := this.blockSize - blockOffset
// We might not have that much data available in our target buffer
if remaining := dataSize - outputOffset; copySize > remaining {
copySize = remaining
}
// If we are at the offset zero of the block or
// if existing buffer doesn't exist, we read it.
if blockOffset == 0 || this.buffer == nil { // "this.buffer == nil" will only be used when we add Skip(n int)
// We try to load any existing data in the buffer (this will be used to allow to skip data on an existing file)
if this.buffer, err = this.file.Block(currentBlock); err != nil {
break
}
}
// We copy the amount of data we need to copy
copy(data[outputOffset:], this.buffer[blockOffset:blockOffset+copySize])
// We increment the inputOffset and the global offset
outputOffset += copySize
this.offset += copySize
}
return outputOffset, err
}