|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package filesystem |
| 18 | + |
| 19 | +import ( |
| 20 | + "io" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "github.com/spf13/afero" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + createOrUpdate = os.O_WRONLY | os.O_CREATE | os.O_TRUNC |
| 29 | + |
| 30 | + defaultDirectoryPermission os.FileMode = 0700 |
| 31 | + defaultFilePermission os.FileMode = 0600 |
| 32 | +) |
| 33 | + |
| 34 | +// FileSystem is an IO wrapper to create files |
| 35 | +type FileSystem interface { |
| 36 | + // Exists checks if the file exists |
| 37 | + Exists(path string) (bool, error) |
| 38 | + |
| 39 | + // Create creates the directory and file and returns a self-closing |
| 40 | + // io.Writer pointing to that file. If the file exists, it truncates it. |
| 41 | + Create(path string) (io.Writer, error) |
| 42 | +} |
| 43 | + |
| 44 | +// fileSystem implements FileSystem |
| 45 | +type fileSystem struct { |
| 46 | + fs afero.Fs |
| 47 | + dirPerm os.FileMode |
| 48 | + filePerm os.FileMode |
| 49 | + fileMode int |
| 50 | +} |
| 51 | + |
| 52 | +// New returns a new FileSystem |
| 53 | +func New(options ...Options) FileSystem { |
| 54 | + // Default values |
| 55 | + fs := fileSystem{ |
| 56 | + fs: afero.NewOsFs(), |
| 57 | + dirPerm: defaultDirectoryPermission, |
| 58 | + filePerm: defaultFilePermission, |
| 59 | + fileMode: createOrUpdate, |
| 60 | + } |
| 61 | + |
| 62 | + // Apply options |
| 63 | + for _, option := range options { |
| 64 | + option(&fs) |
| 65 | + } |
| 66 | + |
| 67 | + return fs |
| 68 | +} |
| 69 | + |
| 70 | +// Options configure FileSystem |
| 71 | +type Options func(system *fileSystem) |
| 72 | + |
| 73 | +// DirectoryPermissions makes FileSystem.Create use the provided directory |
| 74 | +// permissions |
| 75 | +func DirectoryPermissions(dirPerm os.FileMode) Options { |
| 76 | + return func(fs *fileSystem) { |
| 77 | + fs.dirPerm = dirPerm |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// FilePermissions makes FileSystem.Create use the provided file permissions |
| 82 | +func FilePermissions(filePerm os.FileMode) Options { |
| 83 | + return func(fs *fileSystem) { |
| 84 | + fs.filePerm = filePerm |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Exists implements FileSystem.Exists |
| 89 | +func (fs fileSystem) Exists(path string) (bool, error) { |
| 90 | + return afero.Exists(fs.fs, path) |
| 91 | +} |
| 92 | + |
| 93 | +// Create implements FileSystem.Create |
| 94 | +func (fs fileSystem) Create(path string) (io.Writer, error) { |
| 95 | + // Create the directory if needed |
| 96 | + if err := fs.fs.MkdirAll(filepath.Dir(path), fs.dirPerm); err != nil { |
| 97 | + return nil, createDirectoryError{path, err} |
| 98 | + } |
| 99 | + |
| 100 | + // Create or truncate the file |
| 101 | + wc, err := fs.fs.OpenFile(path, fs.fileMode, fs.filePerm) |
| 102 | + if err != nil { |
| 103 | + return nil, createFileError{path, err} |
| 104 | + } |
| 105 | + |
| 106 | + return &file{path, wc}, nil |
| 107 | +} |
| 108 | + |
| 109 | +// file implements io.Writer |
| 110 | +type file struct { |
| 111 | + path string |
| 112 | + io.WriteCloser |
| 113 | +} |
| 114 | + |
| 115 | +// Write implements io.Writer.Write |
| 116 | +func (f *file) Write(content []byte) (n int, err error) { |
| 117 | + // Close the file when we end writing |
| 118 | + defer func() { |
| 119 | + if closeErr := f.Close(); err == nil && closeErr != nil { |
| 120 | + err = closeFileError{f.path, err} |
| 121 | + } |
| 122 | + }() |
| 123 | + |
| 124 | + // Write the content |
| 125 | + n, err = f.WriteCloser.Write(content) |
| 126 | + if err != nil { |
| 127 | + return n, writeFileError{f.path, err} |
| 128 | + } |
| 129 | + |
| 130 | + return n, nil |
| 131 | +} |
0 commit comments