-
Notifications
You must be signed in to change notification settings - Fork 37
/
os_file_system.go
428 lines (344 loc) · 11.2 KB
/
os_file_system.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package system
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"errors"
"github.com/bmatcuk/doublestar"
fsWrapper "github.com/charlievieth/fs"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type osFileSystem struct {
logger boshlog.Logger
logTag string
tempRoot string
requiresTempRoot bool
}
func NewOsFileSystem(logger boshlog.Logger) FileSystem {
return &osFileSystem{logger: logger, logTag: "File System", requiresTempRoot: false}
}
func NewOsFileSystemWithStrictTempRoot(logger boshlog.Logger) FileSystem {
return &osFileSystem{logger: logger, logTag: "File System", requiresTempRoot: true}
}
func (fs *osFileSystem) HomeDir(username string) (string, error) {
fs.logger.Debug(fs.logTag, "Getting HomeDir for %s", username)
dir, err := fs.homeDir(username)
if err != nil {
return "", err
}
fs.logger.Debug(fs.logTag, "HomeDir is %s", dir)
return dir, nil
}
func (fs *osFileSystem) ExpandPath(path string) (string, error) {
fs.logger.Debug(fs.logTag, "Expanding path for '%s'", path)
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
return "", bosherr.WrapError(err, "Getting current user home dir")
}
path = filepath.Join(home, path[1:])
}
path, err := filepath.Abs(path)
if err != nil {
return "", bosherr.WrapError(err, "Getting absolute path")
}
return path, nil
}
func (fs *osFileSystem) MkdirAll(path string, perm os.FileMode) (err error) {
fs.logger.Debug(fs.logTag, "Making dir %s with perm %#o", path, perm)
return fsWrapper.MkdirAll(path, perm)
}
func (fs *osFileSystem) Chown(path, username string) error {
fs.logger.Debug(fs.logTag, "Chown %s to user %s", path, username)
return fs.chown(path, username)
}
func (fs *osFileSystem) Chmod(path string, perm os.FileMode) (err error) {
fs.logger.Debug(fs.logTag, "Chmod %s to %d", path, perm)
return fsWrapper.Chmod(path, perm)
}
func (fs *osFileSystem) openFile(path string, flag int, perm os.FileMode) (*os.File, error) {
return fsWrapper.OpenFile(path, flag, perm)
}
func (fs *osFileSystem) OpenFile(path string, flag int, perm os.FileMode) (File, error) {
return fs.openFile(path, flag, perm)
}
type StatOpts struct {
Quiet bool
}
func (fs *osFileSystem) StatWithOpts(path string, opts StatOpts) (os.FileInfo, error) {
if !opts.Quiet {
fs.logger.Debug(fs.logTag, "Stat '%s'", path)
}
return fsWrapper.Stat(path)
}
func (fs *osFileSystem) Stat(path string) (os.FileInfo, error) {
return fs.StatWithOpts(path, StatOpts{})
}
func (fs *osFileSystem) Lstat(path string) (os.FileInfo, error) {
fs.logger.Debug(fs.logTag, "Lstat '%s'", path)
return fsWrapper.Lstat(path)
}
func (fs *osFileSystem) WriteFileString(path, content string) (err error) {
return fs.WriteFile(path, []byte(content))
}
func (fs *osFileSystem) WriteFileQuietly(path string, content []byte) error {
return fs.writeFileHelper(path, content, false)
}
func (fs *osFileSystem) WriteFile(path string, content []byte) error {
return fs.writeFileHelper(path, content, true)
}
func (fs *osFileSystem) writeFileHelper(path string, content []byte, logDebug bool) error {
if logDebug {
fs.logger.Debug(fs.logTag, "Writing %s", path)
}
err := fs.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return bosherr.WrapError(err, "Creating dir to write file")
}
file, err := fs.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return bosherr.WrapErrorf(err, "Creating file %s", path)
}
defer file.Close()
if logDebug {
fs.logger.DebugWithDetails(fs.logTag, "Write content", content)
}
_, err = file.Write(content)
if err != nil {
return bosherr.WrapErrorf(err, "Writing content to file %s", path)
}
return nil
}
type ConvergeFileContentsOpts struct {
DryRun bool
}
func (fs *osFileSystem) ConvergeFileContents(path string, content []byte, opts ...ConvergeFileContentsOpts) (bool, error) {
actuallyConverge := true
if len(opts) > 0 {
actuallyConverge = !opts[0].DryRun
}
fi, err := fs.Stat(path)
if err != nil || fi.Size() != int64(len(content)) {
if actuallyConverge {
return true, fs.WriteFile(path, content)
}
return true, nil
}
file, err := fs.openFile(path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return true, bosherr.WrapErrorf(err, "Creating file %s", path)
}
defer file.Close()
src, err := ioutil.ReadAll(file)
if err != nil {
return true, bosherr.WrapErrorf(err, "Reading file %s", path)
}
if bytes.Equal(src, content) {
fs.logger.Debug(fs.logTag, "Skipping writing %s because contents are identical", path)
return false, nil
}
if actuallyConverge {
fs.logger.Debug(fs.logTag, "File %s will be overwritten", path)
file.Close()
return true, fs.WriteFile(path, content)
}
return true, nil
}
type ReadOpts struct {
Quiet bool
}
func (fs *osFileSystem) ReadFileString(path string) (content string, err error) {
bytes, err := fs.ReadFile(path)
if err != nil {
return
}
content = string(bytes)
return
}
func (fs *osFileSystem) ReadFileWithOpts(path string, opts ReadOpts) (content []byte, err error) {
if !opts.Quiet {
fs.logger.Debug(fs.logTag, "Reading file %s", path)
}
file, err := fs.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
err = bosherr.WrapErrorf(err, "Opening file %s", path)
return
}
defer file.Close()
content, err = ioutil.ReadAll(file)
if err != nil {
err = bosherr.WrapErrorf(err, "Reading file content %s", path)
return
}
if !opts.Quiet {
fs.logger.DebugWithDetails(fs.logTag, "Read content", content)
}
return
}
func (fs *osFileSystem) ReadFile(path string) (content []byte, err error) {
return fs.ReadFileWithOpts(path, ReadOpts{})
}
func (fs *osFileSystem) FileExists(path string) bool {
fs.logger.Debug(fs.logTag, "Checking if file exists %s", path)
_, err := fs.Stat(path)
if err != nil {
return !os.IsNotExist(err)
}
return true
}
func (fs *osFileSystem) Rename(oldPath, newPath string) (err error) {
fs.logger.Debug(fs.logTag, "Renaming %s to %s", oldPath, newPath)
fs.RemoveAll(newPath)
return fsWrapper.Rename(oldPath, newPath)
}
func (fs *osFileSystem) Symlink(oldPath, newPath string) error {
fs.logger.Debug(fs.logTag, "Symlinking oldPath %s with newPath %s", oldPath, newPath)
source, target, err := fs.symlinkPaths(oldPath, newPath)
if err != nil {
bosherr.WrapErrorf(err, "Getting absolute paths for target and path links: %s %s", oldPath, newPath)
}
if fi, err := fs.Lstat(target); err == nil {
if fi.Mode()&os.ModeSymlink != 0 {
// Symlink
new, err := fs.Readlink(target)
if err != nil {
return bosherr.WrapErrorf(err, "Reading link for %s", target)
}
if filepath.Clean(source) == filepath.Clean(new) {
return nil
}
}
if err := fs.RemoveAll(target); err != nil {
return bosherr.WrapErrorf(err, "Removing new path at %s", target)
}
}
containingDir := filepath.Dir(target)
if !fs.FileExists(containingDir) {
fs.MkdirAll(containingDir, os.FileMode(0700))
}
return fsWrapper.Symlink(source, target)
}
func (fs *osFileSystem) ReadAndFollowLink(symlinkPath string) (targetPath string, err error) {
return filepath.EvalSymlinks(symlinkPath)
}
func (fs *osFileSystem) Readlink(symlinkPath string) (targetPath string, err error) {
return fsWrapper.Readlink(symlinkPath)
}
func (fs *osFileSystem) CopyFile(srcPath, dstPath string) error {
fs.logger.Debug(fs.logTag, "Copying file '%s' to '%s'", srcPath, dstPath)
srcFile, err := fs.OpenFile(srcPath, os.O_RDONLY, 0)
if err != nil {
return bosherr.WrapError(err, "Opening source path")
}
defer srcFile.Close()
srcFi, err := fs.Stat(srcPath)
if err != nil {
return bosherr.WrapError(err, "Stating source path")
}
dstFile, err := fs.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcFi.Mode())
if err != nil {
return bosherr.WrapError(err, "Creating destination file")
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return bosherr.WrapError(err, "Copying file")
}
return nil
}
func (fs *osFileSystem) CopyDir(srcPath, dstPath string) error {
fs.logger.Debug(fs.logTag, "Copying dir '%s' to '%s'", srcPath, dstPath)
sourceInfo, err := fs.Stat(srcPath)
if err != nil {
return bosherr.WrapErrorf(err, "Reading dir stats for '%s'", srcPath)
}
// create destination dir with same permissions as source dir
err = fs.MkdirAll(dstPath, sourceInfo.Mode())
if err != nil {
return bosherr.WrapErrorf(err, "Making destination dir '%s'", dstPath)
}
files, err := fs.listDirContents(srcPath)
if err != nil {
return bosherr.WrapErrorf(err, "Listing contents of source dir '%s", srcPath)
}
for _, file := range files {
fileSrcPath := filepath.Join(srcPath, file.Name())
fileDstPath := filepath.Join(dstPath, file.Name())
if file.IsDir() {
err = fs.CopyDir(fileSrcPath, fileDstPath)
if err != nil {
return bosherr.WrapErrorf(err, "Copying sub-dir '%s' to '%s'", fileSrcPath, fileDstPath)
}
} else {
err = fs.CopyFile(fileSrcPath, fileDstPath)
if err != nil {
return bosherr.WrapErrorf(err, "Copying file '%s' to '%s'", fileSrcPath, fileDstPath)
}
}
}
return nil
}
func (fs *osFileSystem) listDirContents(dirPath string) ([]os.FileInfo, error) {
file, err := fs.openFile(dirPath, os.O_RDONLY, 0)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Opening dir '%s' for reading", dirPath)
}
defer file.Close()
files, err := file.Readdir(-1)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading dir '%s' contents", dirPath)
}
return files, nil
}
func (fs *osFileSystem) TempFile(prefix string) (file File, err error) {
fs.logger.Debug(fs.logTag, "Creating temp file with prefix %s", prefix)
if fs.tempRoot == "" && fs.requiresTempRoot {
return nil, errors.New("Set a temp directory root with ChangeTempRoot before making temp files")
}
return ioutil.TempFile(fs.tempRoot, prefix)
}
func (fs *osFileSystem) TempDir(prefix string) (path string, err error) {
fs.logger.Debug(fs.logTag, "Creating temp dir with prefix %s", prefix)
if fs.tempRoot == "" && fs.requiresTempRoot {
return "", errors.New("Set a temp directory root with ChangeTempRoot before making temp directories")
}
return ioutil.TempDir(fs.tempRoot, prefix)
}
func (fs *osFileSystem) ChangeTempRoot(tempRootPath string) error {
err := fs.MkdirAll(tempRootPath, os.ModePerm)
if err != nil {
return err
}
fs.tempRoot = tempRootPath
return nil
}
func (fs *osFileSystem) RemoveAll(fileOrDir string) (err error) {
fs.logger.Debug(fs.logTag, "Remove all %s", fileOrDir)
err = fsWrapper.RemoveAll(fileOrDir)
return
}
func (fs *osFileSystem) Glob(pattern string) (matches []string, err error) {
fs.logger.Debug(fs.logTag, "Glob '%s'", pattern)
return filepath.Glob(pattern)
}
func (fs *osFileSystem) RecursiveGlob(pattern string) (matches []string, err error) {
fs.logger.Debug(fs.logTag, "RecursiveGlob '%s'", pattern)
return doublestar.Glob(pattern)
}
func (fs *osFileSystem) Walk(root string, walkFunc filepath.WalkFunc) error {
return filepath.Walk(root, walkFunc)
}
func (fs *osFileSystem) runCommand(cmd string) (string, error) {
var stdout bytes.Buffer
shCmd := exec.Command("sh", "-c", cmd)
shCmd.Stdout = &stdout
if err := shCmd.Run(); err != nil {
return "", err
}
return strings.TrimSpace(stdout.String()), nil
}