forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_file_system.go
337 lines (264 loc) · 8.36 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
package system
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
osuser "os/user"
)
type osFileSystem struct {
logger boshlog.Logger
logTag string
}
func NewOsFileSystem(logger boshlog.Logger) FileSystem {
return osFileSystem{logger: logger, logTag: "File System"}
}
func (fs osFileSystem) HomeDir(username string) (homeDir string, err error) {
fs.logger.Debug(fs.logTag, "Getting HomeDir for %s", username)
user, err := osuser.Lookup(username)
if err != nil {
err = bosherr.WrapErrorf(err, "Looking up user %s", username)
return
}
homeDir = user.HomeDir
fs.logger.Debug(fs.logTag, "HomeDir is %s", homeDir)
return
}
func (fs osFileSystem) MkdirAll(path string, perm os.FileMode) (err error) {
fs.logger.Debug(fs.logTag, "Making dir %s with perm %d", path, perm)
return os.MkdirAll(path, perm)
}
func (fs osFileSystem) Chown(path, username string) (err error) {
fs.logger.Debug(fs.logTag, "Chown %s to user %s", path, username)
user, err := osuser.Lookup(username)
if err != nil {
err = bosherr.WrapErrorf(err, "Looking up user %s", username)
return
}
uid, err := strconv.Atoi(user.Uid)
if err != nil {
err = bosherr.WrapError(err, "Converting UID to integer")
return
}
gid, err := strconv.Atoi(user.Gid)
if err != nil {
err = bosherr.WrapError(err, "Converting GID to integer")
return
}
err = os.Chown(path, uid, gid)
if err != nil {
err = bosherr.WrapError(err, "Doing Chown")
return
}
return
}
func (fs osFileSystem) Chmod(path string, perm os.FileMode) (err error) {
fs.logger.Debug(fs.logTag, "Chmod %s to %d", path, perm)
return os.Chmod(path, perm)
}
func (fs osFileSystem) OpenFile(path string, flag int, perm os.FileMode) (File, error) {
return os.OpenFile(path, flag, perm)
}
func (fs osFileSystem) WriteFileString(path, content string) (err error) {
return fs.WriteFile(path, []byte(content))
}
func (fs osFileSystem) WriteFile(path string, content []byte) error {
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 := os.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()
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
}
func (fs osFileSystem) ConvergeFileContents(path string, content []byte) (bool, error) {
if fs.filesAreIdentical(content, path) {
fs.logger.Debug(fs.logTag, "Skipping writing %s because contents are identical", path)
return false, nil
}
fs.logger.Debug(fs.logTag, "File %s will be overwritten", path)
err := fs.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return true, bosherr.WrapErrorf(err, "Making dir for file %s", path)
}
file, err := os.Create(path)
if err != nil {
return true, bosherr.WrapErrorf(err, "Creating file %s", path)
}
defer file.Close()
_, err = file.Write(content)
if err != nil {
return true, bosherr.WrapErrorf(err, "Writing content to file %s", path)
}
return true, nil
}
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) ReadFile(path string) (content []byte, err error) {
fs.logger.Debug(fs.logTag, "Reading file %s", path)
file, err := os.Open(path)
if err != nil {
err = bosherr.WrapErrorf(err, "Opening file %s", path)
return
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
err = bosherr.WrapErrorf(err, "Reading file content %s", path)
return
}
content = bytes
fs.logger.DebugWithDetails(fs.logTag, "Read content", content)
return
}
func (fs osFileSystem) FileExists(path string) bool {
fs.logger.Debug(fs.logTag, "Checking if file exists %s", path)
_, err := os.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 os.Rename(oldPath, newPath)
}
func (fs osFileSystem) Symlink(oldPath, newPath string) error {
fs.logger.Debug(fs.logTag, "Symlinking oldPath %s with newPath %s", oldPath, newPath)
actualOldPath, err := filepath.EvalSymlinks(oldPath)
if err != nil {
return bosherr.WrapErrorf(err, "Evaluating symlinks for %s", oldPath)
}
existingTargetedPath, err := filepath.EvalSymlinks(newPath)
if err == nil {
if existingTargetedPath == actualOldPath {
return nil
}
err = os.Remove(newPath)
if err != nil {
return bosherr.WrapErrorf(err, "Failed to delete symlimk at %s", newPath)
}
}
containingDir := filepath.Dir(newPath)
if !fs.FileExists(containingDir) {
fs.MkdirAll(containingDir, os.FileMode(0700))
}
return os.Symlink(oldPath, newPath)
}
func (fs osFileSystem) ReadLink(symlinkPath string) (targetPath string, err error) {
targetPath, err = filepath.EvalSymlinks(symlinkPath)
return
}
func (fs osFileSystem) CopyFile(srcPath, dstPath string) error {
fs.logger.Debug(fs.logTag, "Copying file '%s' to '%s'", srcPath, dstPath)
srcFile, err := os.Open(srcPath)
if err != nil {
return bosherr.WrapError(err, "Opening source path")
}
defer srcFile.Close()
dstFile, err := os.Create(dstPath)
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 := os.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 = os.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) {
directory, err := os.Open(dirPath)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Openning dir '%s' for reading", dirPath)
}
defer directory.Close()
files, err := directory.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)
return ioutil.TempFile("", prefix)
}
func (fs osFileSystem) TempDir(prefix string) (path string, err error) {
fs.logger.Debug(fs.logTag, "Creating temp dir with prefix %s", prefix)
return ioutil.TempDir("", prefix)
}
func (fs osFileSystem) RemoveAll(fileOrDir string) (err error) {
fs.logger.Debug(fs.logTag, "Remove all %s", fileOrDir)
err = os.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) Walk(root string, walkFunc filepath.WalkFunc) error {
return filepath.Walk(root, walkFunc)
}
func (fs osFileSystem) filesAreIdentical(newContent []byte, filePath string) bool {
existingStat, err := os.Stat(filePath)
if err != nil || int64(len(newContent)) != existingStat.Size() {
return false
}
existingContent, err := fs.ReadFile(filePath)
if err != nil {
return false
}
return bytes.Compare(newContent, existingContent) == 0
}