-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
340 lines (283 loc) · 9.44 KB
/
types.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
334
335
336
337
338
339
340
/*
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright © 2023-2025 Seagate Technology LLC and/or its Affiliates
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
package common
import (
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"reflect"
"runtime"
"sync"
"time"
"github.com/JeffreyRichter/enum/enum"
)
// Standard config default values
const (
cloudfuseVersion_ = "1.9.0"
DefaultMaxLogFileSize = 512
DefaultLogFileCount = 10
FileSystemName = "cloudfuse"
DefaultConfigFilePath = "config.yaml"
MaxConcurrency = 40
DefaultConcurrency = 20
MaxDirListCount = 5000
DefaultFilePermissionBits os.FileMode = 0755
DefaultDirectoryPermissionBits os.FileMode = 0775
DefaultAllowOtherPermissionBits os.FileMode = 0777
MbToBytes = 1024 * 1024
GbToBytes = 1024 * 1024 * 1024
TbToBytes = 1024 * 1024 * 1024 * 1024
DefaultCapacityMb = TbToBytes / MbToBytes
CfuseStats = "cloudfuse_stats"
FuseAllowedFlags = "invalid FUSE options. Allowed FUSE configurations are: `-o attr_timeout=TIMEOUT`, `-o negative_timeout=TIMEOUT`, `-o entry_timeout=TIMEOUT` `-o allow_other`, `-o allow_root`, `-o umask=PERMISSIONS -o default_permissions`, `-o ro`"
UserAgentHeader = "User-Agent"
BlockCacheRWErrMsg = "Notice: The random write flow using block cache is temporarily blocked due to potential data integrity issues. This is a precautionary measure. \nIf you see this message, contact [email protected] or create a GitHub issue. We're working on a fix. More details: https://aka.ms/blobfuse2warnings."
)
var GitCommit = "**local_build**"
var CommitDate = "undated"
var GoVersion = runtime.Version()
var OsArch = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)
func FuseIgnoredFlags() []string {
return []string{"default_permissions", "rw", "dev", "nodev", "suid", "nosuid", "delay_connect", "auto", "noauto", "user", "nouser", "exec", "noexec"}
}
var CloudfuseVersion = CloudfuseVersion_()
func CloudfuseVersion_() string {
return cloudfuseVersion_
}
var DefaultWorkDir string
var DefaultLogFilePath string
var StatsConfigFilePath string
var EnableMonitoring = false
var CfsDisabled = false
func GetDefaultWorkDir() string {
val, err := os.UserHomeDir()
if err != nil {
return "./"
}
return val
}
var MountPath string
// LogLevel enum
type LogLevel int
var ELogLevel = LogLevel(0).INVALID()
func (LogLevel) INVALID() LogLevel {
return LogLevel(0)
}
func (LogLevel) LOG_OFF() LogLevel {
return LogLevel(1)
}
func (LogLevel) LOG_CRIT() LogLevel {
return LogLevel(2)
}
func (LogLevel) LOG_ERR() LogLevel {
return LogLevel(3)
}
func (LogLevel) LOG_WARNING() LogLevel {
return LogLevel(4)
}
func (LogLevel) LOG_INFO() LogLevel {
return LogLevel(5)
}
func (LogLevel) LOG_TRACE() LogLevel {
return LogLevel(6)
}
func (LogLevel) LOG_DEBUG() LogLevel {
return LogLevel(7)
}
func (l LogLevel) String() string {
return enum.StringInt(l, reflect.TypeOf(l))
}
func (l *LogLevel) Parse(s string) error {
enumVal, err := enum.ParseInt(reflect.TypeOf(l), s, true, false)
if enumVal != nil {
*l = enumVal.(LogLevel)
}
return err
}
type LogConfig struct {
Level LogLevel
MaxFileSize uint64
FileCount uint64
FilePath string
TimeTracker bool
Tag string // logging tag which can be either cloudfuse or cfusemon
}
// Flags for blocks
const (
BlockFlagUnknown uint16 = iota
DirtyBlock
TruncatedBlock
RemovedBlocks
)
type Block struct {
sync.RWMutex
StartIndex int64
EndIndex int64
Flags BitMap16
Id string
Data []byte
}
// Statfs type used by component in replace of syscall.Statfs_t
// as defined by cgofuse https://pkg.go.dev/github.com/winfsp/cgofuse/fuse#Statfs_t
type Statfs_t struct {
Bsize int64
Blocks uint64
Bfree uint64
Bavail uint64
Files uint64
Ffree uint64
Frsize int64
Flags int64
Namemax uint64
}
// Dirty : Handle is dirty or not
func (block *Block) Dirty() bool {
return block.Flags.IsSet(DirtyBlock)
}
// Truncated : block created on a truncate operation
func (block *Block) Truncated() bool {
return block.Flags.IsSet(TruncatedBlock)
}
func (block *Block) Removed() bool {
return block.Flags.IsSet(RemovedBlocks)
}
// Flags for block offset list
const (
BolFlagUnknown uint16 = iota
SmallFile
)
// list that holds blocks containing ids and corresponding offsets
type BlockOffsetList struct {
BlockList []*Block //blockId to offset mapping
Flags BitMap16
BlockIdLength int64
Size int64
Mtime time.Time
}
// Dirty : Handle is dirty or not
func (bol *BlockOffsetList) SmallFile() bool {
return bol.Flags.IsSet(SmallFile)
}
// return true if item found and index of the item
func (bol BlockOffsetList) BinarySearch(offset int64) (bool, int) {
lowerBound := 0
size := len(bol.BlockList)
higherBound := size - 1
for lowerBound <= higherBound {
middleIndex := (lowerBound + higherBound) / 2
// we found the starting block that changes are being applied to
if bol.BlockList[middleIndex].EndIndex > offset && bol.BlockList[middleIndex].StartIndex <= offset {
return true, middleIndex
// if the end index is smaller or equal then we need to increase our lower bound
} else if bol.BlockList[middleIndex].EndIndex <= offset {
lowerBound = middleIndex + 1
// if the start index is larger than the offset we need to decrease our upper bound
} else if bol.BlockList[middleIndex].StartIndex > offset {
higherBound = middleIndex - 1
}
}
// return size as this would be where the new blocks start
return false, size
}
// returns index of first mod block, size of mod data, does the new data exceed current size?, is it append only?
func (bol BlockOffsetList) FindBlocks(offset, length int64) ([]*Block, bool) {
// size of mod block list
currentBlockOffset := offset
var blocks []*Block
found, index := bol.BinarySearch(offset)
if !found {
return blocks, false
}
for _, blk := range bol.BlockList[index:] {
if blk.StartIndex > offset+length {
break
}
if currentBlockOffset >= blk.StartIndex && currentBlockOffset < blk.EndIndex && currentBlockOffset <= offset+length {
blocks = append(blocks, blk)
currentBlockOffset = blk.EndIndex
}
}
return blocks, true
}
// returns index of first mod block, size of mod data, does the new data exceed current size?, is it append only?
func (bol BlockOffsetList) FindBlocksToModify(offset, length int64) (int, int64, bool, bool) {
// size of mod block list
size := int64(0)
appendOnly := true
currentBlockOffset := offset
found, index := bol.BinarySearch(offset)
if !found {
return index, 0, true, appendOnly
}
// after the binary search just iterate to find the remaining blocks
for _, blk := range bol.BlockList[index:] {
if blk.StartIndex > offset+length {
break
}
if currentBlockOffset >= blk.StartIndex && currentBlockOffset < blk.EndIndex && currentBlockOffset <= offset+length {
appendOnly = false
blk.Flags.Set(DirtyBlock)
currentBlockOffset = blk.EndIndex
size += (blk.EndIndex - blk.StartIndex)
}
}
return index, size, offset+length >= bol.BlockList[len(bol.BlockList)-1].EndIndex, appendOnly
}
// A UUID representation compliant with specification in RFC 4122 document.
type uuid [16]byte
const reservedRFC4122 byte = 0x40
func (u uuid) Bytes() []byte {
return u[:]
}
// NewUUIDWithLength returns a new uuid using RFC 4122 algorithm with the given length.
func NewUUIDWithLength(length int64) []byte {
u := make([]byte, length)
// Set all bits to randomly (or pseudo-randomly) chosen values.
_, err := rand.Read(u[:])
if err == nil {
u[8] = (u[8] | 0x40) & 0x7F // u.setVariant(ReservedRFC4122)
var version byte = 4
u[6] = (u[6] & 0xF) | (version << 4) // u.setVersion(4)
}
return u[:]
}
// NewUUID returns a new uuid using RFC 4122 algorithm.
func NewUUID() (u uuid) {
u = uuid{}
// Set all bits to randomly (or pseudo-randomly) chosen values.
_, err := rand.Read(u[:])
if err == nil {
u[8] = (u[8] | reservedRFC4122) & 0x7F // u.setVariant(ReservedRFC4122)
var version byte = 4
u[6] = (u[6] & 0xF) | (version << 4) // u.setVersion(4)
}
return
}
func GetIdLength(id string) int64 {
existingBlockId, _ := base64.StdEncoding.DecodeString(id)
return int64(len(existingBlockId))
}
func init() {
DefaultWorkDir = JoinUnixFilepath(GetDefaultWorkDir(), ".cloudfuse")
DefaultLogFilePath = JoinUnixFilepath(DefaultWorkDir, "cloudfuse.log")
StatsConfigFilePath = JoinUnixFilepath(DefaultWorkDir, "stats_monitor.cfg")
}