-
Notifications
You must be signed in to change notification settings - Fork 1
/
IO.go
170 lines (151 loc) · 4.6 KB
/
IO.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
package fsEngine
import (
"fmt"
"github.com/fanap-infra/fsEngine/internal/constants"
)
func (fse *FSEngine) writeInBlock(data []byte, blockIndex uint32) (int, error) {
// fse.log.Infov("FSEngine write in block", "blockIndex", blockIndex,
// "maxNumberOfBlocks", fse.maxNumberOfBlocks, "len(data)", len(data))
// fse.WMux.Lock()
// defer fse.WMux.Unlock()
if blockIndex >= fse.maxNumberOfBlocks {
return 0, constants.ErrBlockIndexOutOFRange
}
if uint32(len(data)) > fse.blockSize {
return 0, fmt.Errorf("size of data is larger than block size, size of data: %v,"+
" but size of block is %v", len(data), fse.blockSize)
}
n, err := fse.file.WriteAt(data, int64(blockIndex)*int64(fse.blockSize))
if err != nil {
fse.log.Errorv("Error Writing to file", "err", err.Error(),
"file", fse.file.Name(), "blockIndex", blockIndex)
return n, err
}
return n, nil
}
func (fse *FSEngine) ReadBlock(blockIndex uint32, fileID uint32) ([]byte, error) {
fse.RMux.Lock()
defer fse.RMux.Unlock()
// fse.log.Infov("FSEngine read in block", "blockIndex", blockIndex)
if blockIndex >= fse.maxNumberOfBlocks {
return nil, constants.ErrBlockIndexOutOFRange
}
var err error
buf := make([]byte, fse.blockSize)
n, err := fse.file.ReadAt(buf, int64(blockIndex)*int64(fse.blockSize))
if err != nil {
return nil, err
}
if n != int(fse.blockSize) {
return buf, constants.ErrDataBlockMismatch
}
data, err := fse.parseBlock(buf, blockIndex, fileID)
if err != nil {
return nil, err
}
return data, nil
}
func (fse *FSEngine) ReadAt(data []byte, off int64, fileID uint32) (int, error) {
// fse.rIBlockMux.Lock()
// defer fse.rIBlockMux.Unlock()
// ToDo: implement it
return 0, nil
}
func (fse *FSEngine) Read(data []byte, fileID uint32) (int, error) {
return 0, fmt.Errorf("please impkement me")
}
//func (fse *FSEngine) WriteAt(b []byte, off int64, fileID uint32) (n int, err error) {
// // ToDo: complete it
// n, err = fse.file.WriteAt(b, off)
//
// return
//}
func (fse *FSEngine) Write(data []byte, fileID uint32, previousBlock uint32) (int, []uint32, error) {
fse.WMux.Lock()
defer fse.WMux.Unlock()
dataSize := len(data)
if dataSize == 0 {
return 0, []uint32{}, fmt.Errorf("data siz is zero, file ID: %v ", fileID)
}
//vfInfo, ok := fse.openFiles[fileID]
//if !ok {
// return 0, 0, fmt.Errorf("this file ID: %v did not opened", fileID)
//}
n := 0
var err error
var blocksID []uint32
if fse.cleaning == 0 {
blmArrayLen := len(fse.header.GetBLMArray())
if blmArrayLen > int(float64(fse.maxNumberOfBlocks)*0.9) {
fse.log.Infov("Cleaning Begin due to Space requirement",
"blmArrayLen", blmArrayLen)
// atomic.StoreUint32(&fse.cleaning, 1)
fse.NoSpace()
}
}
for {
if n >= dataSize {
if n == dataSize {
return n, blocksID, nil
}
fse.log.Errorv("data is written more than dataSize", "dataSize", dataSize, "n", n)
return n, []uint32{}, fmt.Errorf("it is wirtten more, dataSize: %v, n = %v", dataSize, n)
}
// previousBlock := vfInfo.vfs[0].GetLastBlock()
blockID := fse.header.FindNextFreeBlockAndAllocate()
var d []byte
if dataSize >= n+int(fse.blockSizeUsable) {
d, err = fse.prepareBlock(data[n:n+int(fse.blockSizeUsable)], fileID, previousBlock, blockID)
if err != nil {
return 0, []uint32{}, err
}
} else {
d, err = fse.prepareBlock(data[n:], fileID, previousBlock, blockID)
if err != nil {
return 0, []uint32{}, err
}
}
m, err := fse.writeInBlock(d, blockID)
if err != nil {
return 0, []uint32{}, err
}
err = fse.header.SetBlockAsAllocated(blockID)
if err != nil {
fse.log.Errorv("can not set block id in header",
"blockID", blockID, "fileID", fileID)
return 0, []uint32{}, err
}
blocksID = append(blocksID, blockID)
if m != len(d) {
return 0, blocksID, fmt.Errorf("block with size: %v did not write correctly, n = %v", m, len(d))
}
n = n + m - constants.BlockHeaderSize
}
}
// It is event handler
func (fse *FSEngine) Closed(fileID uint32) error {
fse.WMux.Lock()
defer fse.WMux.Unlock()
fse.RMux.Lock()
defer fse.RMux.Unlock()
fse.crudMutex.Lock()
defer fse.crudMutex.Unlock()
err := fse.header.UpdateFSHeader()
if err != nil {
fse.log.Warnv("Can not updateHeader", "err", err.Error())
}
vfInfo, ok := fse.openFiles[fileID]
if !ok {
return fmt.Errorf("this file ID: %v did not opened", fileID)
}
vfInfo.numberOfOpened = vfInfo.numberOfOpened - 1
fse.log.Infov("file closed", "fileID", fileID, "numberOfOpened", vfInfo.numberOfOpened)
if vfInfo.numberOfOpened == 0 {
delete(fse.openFiles, fileID)
}
err = fse.file.Sync()
if err != nil {
fse.log.Warnv("Can not sync file", "err", err.Error())
}
return nil
}