-
Notifications
You must be signed in to change notification settings - Fork 14
/
fileops_linux.go
177 lines (142 loc) · 3.28 KB
/
fileops_linux.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
package pitreos
import (
"fmt"
"os"
"path/filepath"
"sync"
fibmap "github.com/frostschutz/go-fibmap"
)
type FileOps struct {
filePath string
readWrite bool
file *os.File
lock sync.Mutex
isAppendOnly bool
originalSize int64
extentsLoaded bool
extents []fibmap.Extent
}
func NewFileOps(filePath string, readWrite bool) *FileOps {
return &FileOps{
readWrite: readWrite,
filePath: filePath,
}
}
func (f *FileOps) Open() error {
perms := os.O_RDONLY
if f.readWrite {
perms = os.O_RDWR | os.O_CREATE
}
fl, err := os.OpenFile(f.filePath, perms, 0644)
if err != nil {
return err
}
f.file = fl
return nil
}
func (f *FileOps) Close() error {
if f.file == nil {
return fmt.Errorf("file not currently open, %q", f.filePath)
}
return f.file.Close()
}
func (f *FileOps) Truncate(size int64) error { return f.file.Truncate(size) }
func (f *FileOps) getSparseFileExtents() ([]fibmap.Extent, error) {
ff := fibmap.NewFibmapFile(f.file)
extents, errno := ff.Fiemap(9999999)
if errno != 0 {
return nil, errno
}
return extents, nil
}
func (f *FileOps) wipeChunk(offset int64, length int64) error {
// TODO: fill with zeroes if the FS underneath doesn't support FIBMAP
ff := fibmap.NewFibmapFile(f.file)
return ff.PunchHole(offset, length)
}
func (f *FileOps) writeChunkToFile(offset int64, s []byte) error {
f.lock.Lock()
defer f.lock.Unlock()
_, err := f.file.Seek(offset, 0)
if err != nil {
return err
}
_, err = f.file.Write(s)
return err
}
// isRangeInSparseExtent reports whether data is PRESENT in that range.
func (f *FileOps) hasDataInRange(startIndex, size int64) bool {
endIndex := startIndex + size - 1
f.lock.Lock()
if f.extentsLoaded == false {
exts, err := f.getSparseFileExtents()
if err != nil {
zlog.Warn("sparse check failed, cannot optimize based on sparse file holes, prepare for large backups")
} else {
f.extents = exts
}
f.extentsLoaded = true
}
f.lock.Unlock()
if f.extents == nil {
return true
}
for _, ex := range f.extents {
if uint64(startIndex) <= ex.Logical+ex.Length-1 && ex.Logical <= uint64(endIndex) {
return true
}
}
return false
}
func (f *FileOps) getLocalChunk(offset int64, size int64) (data []byte, empty bool, err error) {
hasData := f.hasDataInRange(offset, size)
if !hasData {
empty = true
return
}
data = make([]byte, size)
f.lock.Lock()
defer f.lock.Unlock()
_, err = f.file.Seek(offset, 0)
if err != nil {
return data, empty, fmt.Errorf("seek error: %s", err)
}
_, err = f.file.Read(data)
if err != nil {
return data, empty, fmt.Errorf("read error: %s", err)
}
if isEmptyChunk(data) {
empty = true
}
return data, empty, nil
}
func isEmptyChunk(s []byte) bool {
for _, v := range s {
if v != 0 {
return false
}
}
return true
}
func getDirFiles(directory string) (fileNames []string, err error) {
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return err
}
// FIXME: Add support for symbolic links, other special files like FIFO and weird oddities.
fileNames = append(fileNames, path)
return nil
})
return
}
func stringarrayContains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}