This repository has been archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
files_util.go
229 lines (191 loc) · 6.59 KB
/
files_util.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
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package files
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/homeport/pina-golada/pkg/files/paths"
)
// WalkFileTree iterates over each and every file instance found in the directory
func WalkFileTree(directory Directory, consumer func(file File)) {
for _, file := range directory.Files() {
consumer(file)
}
for _, dir := range directory.Directories() {
WalkFileTree(dir, consumer)
}
}
// WalkDirectoryTree walks over each sub directory found in the directory, excluding the passed directory
func WalkDirectoryTree(directory Directory, consumer func(d Directory)) {
for _, dir := range directory.Directories() {
consumer(dir)
WalkDirectoryTree(dir, consumer)
}
}
// LoadFromDisk loads the content of the paths into the directory recursively
func LoadFromDisk(directory Directory, path string) (e error) {
return loadFromDisk(directory, path)
}
// loadFromDisk loads the content of the paths into the directory recursively.
// This also returns true if the file located under the path is a directory or false
// if the file at the path is a flat file.
func loadFromDisk(directory Directory, path string) (e error) {
info, statError := os.Stat(path)
if statError != nil {
return statError
}
if info.IsDir() {
directory.WithPermission(info.Mode())
directoryContent, e := ioutil.ReadDir(path)
if e != nil {
return e
}
for _, file := range directoryContent {
if file.IsDir() {
if err := loadFromDisk(directory.NewDirectory(paths.Of(file.Name())).WithPermission(file.Mode()), filepath.Join(path, file.Name())); err != nil {
return err
}
} else {
if err := readFileInto(directory, filepath.Join(path, file.Name())); err != nil {
return err
}
}
}
return nil
}
if err := readFileInto(directory, path); err != nil {
return err
}
return nil
}
func readFileInto(directory Directory, path string) (e error) {
content, readError := ioutil.ReadFile(path)
if readError != nil {
return readError
}
info, statError := os.Stat(path)
if statError != nil {
return statError
}
return directory.NewFile(paths.Of(path).Drop()).WithPermission(info.Mode()).Write(bytes.NewBuffer(content))
}
// WriteToDisk writes a directory to the given path
// Overwriting or skipping an existing file based on the bool
func WriteToDisk(directory Directory, path string, overwrite bool) (e error) {
info, statError := os.Stat(path)
if statError != nil {
if !os.IsNotExist(statError) {
return statError
}
if err := os.MkdirAll(path, directory.PermissionSet()); err != nil {
return err
}
}
if info != nil && !info.IsDir() {
return fmt.Errorf("provided path pointed to file %s", path)
}
if directory.Parent() != nil { // If the directory is not a root directory, we want to create the directory
path = filepath.Join(path, directory.Name().String())
}
return writeDirectoryToDisk(directory, path, overwrite)
}
func writeFileToDisk(file File, directoryPath string, overwrite bool) (e error) {
path := filepath.Join(directoryPath, file.Name().String())
var fileOnDisk *os.File
info, e := os.Stat(path)
if e != nil { // Checking if statistics for path cannot be found
if !os.IsNotExist(e) { // Checking if the path does exist but couldn't be accessed
return e
}
if fileOnDisk, e = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, file.PermissionSet()); e == nil { // Creating the path given that it doesn't exist
if err := fileOnDisk.Close(); err != nil { // Close stream again
return err
}
if err := os.Chmod(path, file.PermissionSet()); err != nil { // Chmod the file
return err
}
} else {
return e
}
}
if info != nil { // Checking if the path currently exists
if info.IsDir() { // Checking if the path is a directory
return fmt.Errorf("provided path pointed to directory %s", path)
}
if !overwrite { // Checking if the file should be overwritten given it exists
return nil
}
if info.Mode() != file.PermissionSet() { // Update file permission if needed
if err := os.Chmod(path, file.PermissionSet()); err != nil {
return err
}
}
}
if fileOnDisk, e = os.OpenFile(path, os.O_RDWR|os.O_TRUNC, file.PermissionSet()); e != nil {
// Check for errors after opening the file
return e
}
if err := file.CopyContent(fileOnDisk); err != nil {
// Check for errors after writing the given file to the disk file
return err
}
if err := fileOnDisk.Close(); err != nil {
// Checking for errors while closing the disk file
return err
}
return nil
}
func writeDirectoryToDisk(directory Directory, directoryPath string, overwrite bool) (e error) {
info, e := os.Stat(directoryPath)
permissionSet := directory.PermissionSet()
if e != nil {
if !os.IsNotExist(e) {
return e
}
if e := os.MkdirAll(directoryPath, permissionSet); e != nil {
return e
}
if info, e = os.Stat(directoryPath); e != nil {
return e
}
}
if info != nil && !info.IsDir() {
return fmt.Errorf("provided path pointed to file %s", directoryPath)
}
if overwrite && info.Mode() != permissionSet && directory.Parent() != nil { // Overwrite permissions if overwrite is enabled and folder is actual folder (not root)
if err := os.Chmod(directoryPath, permissionSet); err != nil {
return err
}
}
for _, dir := range directory.Directories() {
if err := writeDirectoryToDisk(dir, filepath.Join(directoryPath, dir.Name().String()), overwrite); err != nil {
return err
}
}
for _, file := range directory.Files() {
if err := writeFileToDisk(file, directoryPath, overwrite); err != nil {
return err
}
}
return nil
}