-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfileops_darwin.go
128 lines (103 loc) · 2.28 KB
/
fileops_darwin.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
package pitreos
import (
"fmt"
"os"
"path/filepath"
"sync"
)
type FileOps struct {
filePath string
readWrite bool
file *os.File
lock sync.Mutex
isAppendOnly bool
originalSize int64
extentsLoaded bool
}
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) wipeChunk(offset int64, length int64) error {
// TODO: fill with zeroes if the FS underneath doesn't support FIBMAP
var zerobytes = make([]byte, length)
return f.writeChunkToFile(offset, zerobytes)
}
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
}
func (f *FileOps) getLocalChunk(offset int64, size int64) (data []byte, empty bool, err error) {
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
}