-
Notifications
You must be signed in to change notification settings - Fork 1
/
posix_backend.go
204 lines (165 loc) · 4.71 KB
/
posix_backend.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
package main
import (
"bufio"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"time"
)
type PosixDatastore struct {
id string
mountPath string
canWrite bool
skipFilesNewer int
skipFilesOlder int
purgeFilesOlder int
purgeFoldersOlder int
elasticIndex string
recogniseTypes bool
noGroup bool
skipPaths []string
priority uint8
purgeDryRun bool
useCtimePurge bool
purgeToTrash bool
}
func (l PosixDatastore) GetPriority() uint8 {
return l.priority
}
func (l PosixDatastore) GetId() string {
return l.id
}
func (l PosixDatastore) GetElasticIndex() string {
return l.elasticIndex
}
func (l PosixDatastore) GetPurgeDryRun() bool {
return l.purgeDryRun
}
func (l PosixDatastore) GetMountPath() string {
return l.mountPath
}
func (l PosixDatastore) GetSkipFilesNewer() int {
return l.skipFilesNewer
}
func (l PosixDatastore) GetPurgeFilesOlder() int {
return l.purgeFilesOlder
}
func (l PosixDatastore) GetPurgeFoldersOlder() int {
return l.purgeFoldersOlder
}
func (l PosixDatastore) IsRecogniseTypes() bool {
return l.recogniseTypes
}
func (l PosixDatastore) IsNoGroup() bool {
return l.noGroup
}
func (l PosixDatastore) IsUseCtimePurge() bool {
return l.useCtimePurge
}
func (l PosixDatastore) IsPurgeToTrash() bool {
return l.purgeToTrash
}
func (l PosixDatastore) GetSkipPaths() []string {
return l.skipPaths
}
func (l PosixDatastore) GetSkipFilesOlder() int {
return l.skipFilesOlder
}
func (l PosixDatastore) GetLocalFilepath(filePath string) string {
return path.Join(l.mountPath, filePath)
}
func (l PosixDatastore) GetMetadata(filePath string) (os.FileInfo, error) {
return os.Lstat(path.Join(l.mountPath, filePath))
}
func (l PosixDatastore) Readlink(filePath string) (string, error) {
return os.Readlink(path.Join(l.mountPath, filePath))
}
func (l PosixDatastore) Symlink(pointTo, filePath string) error {
return os.Symlink(pointTo, path.Join(l.mountPath, filePath))
}
func (l PosixDatastore) Remove(filePath string) error {
return os.Remove(path.Join(l.mountPath, filePath))
}
func (l PosixDatastore) RemoveAll(filePath string) error {
return os.RemoveAll(path.Join(l.mountPath, filePath))
}
func (l PosixDatastore) Trash(filePath string) error {
os.MkdirAll(path.Join(l.mountPath, ".trash", filepath.Dir(filePath)), os.ModePerm)
return os.Rename(path.Join(l.mountPath, filePath), path.Join(l.mountPath, ".trash", filePath))
}
func (l PosixDatastore) Open(filePath string) (io.ReadCloser, error) {
return os.Open(path.Join(l.mountPath, filePath))
}
func (l PosixDatastore) Create(filePath string, meta os.FileInfo) (io.WriteCloser, error) {
return os.OpenFile(path.Join(l.mountPath, filePath), os.O_CREATE|os.O_RDWR|os.O_TRUNC, meta.Mode())
}
func (l PosixDatastore) Lchown(filePath string, uid, gid int) error {
return os.Lchown(path.Join(l.mountPath, filePath), uid, gid)
}
func (l PosixDatastore) Chmod(filePath string, perm os.FileMode) error {
return os.Chmod(path.Join(l.mountPath, filePath), perm)
}
func (l PosixDatastore) Mkdir(dirPath string, perm os.FileMode) error {
return os.Mkdir(path.Join(l.mountPath, dirPath), perm)
}
func (l PosixDatastore) Chtimes(dirPath string, atime time.Time, mtime time.Time) error {
return os.Chtimes(path.Join(l.mountPath, dirPath), atime, mtime)
}
func (l PosixDatastore) ListDir(dirPath string, listFiles bool) (chan []string, error) {
outchan := make(chan []string)
curDir := path.Join(l.mountPath, dirPath)
cmdName := "find"
cmdArgs := []string{curDir, "-mindepth", "1", "-maxdepth", "1", "!", "-type", "d"}
if !listFiles {
cmdArgs = []string{curDir, "-mindepth", "1", "-maxdepth", "1", "-type", "d"}
}
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
logger.Errorf("Error running find: %v", err)
return nil, err
}
scanner := bufio.NewScanner(cmdReader)
err = cmd.Start()
if err != nil {
return nil, err
}
go func(outchan chan []string) {
defer close(outchan)
if !listFiles {
for scanner.Scan() {
folder := scanner.Text()
if folder != curDir {
rel, err := filepath.Rel(l.mountPath, folder)
if err != nil {
logger.Errorf("Error resolving folder %s: %v", folder, err)
continue
}
outchan <- []string{rel}
}
}
} else {
var filesBuf []string
for scanner.Scan() {
if len(filesBuf) == FileChunks {
outchan <- filesBuf
filesBuf = nil
}
file := scanner.Text()
rel, err := filepath.Rel(l.mountPath, file)
if err != nil {
logger.Errorf("Error resolving file %s: %v", file, err)
continue
}
filesBuf = append(filesBuf, rel)
}
if len(filesBuf) > 0 {
outchan <- filesBuf
}
}
cmd.Wait()
}(outchan)
return outchan, nil
}