/
fs.go
213 lines (183 loc) 路 4.77 KB
/
fs.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
// Package fs holds some utilities for manipulating the file system
package fs
import (
"bufio"
"fmt"
"io"
"os"
"os/user"
"path"
)
const defaultDirectoryPermission = 0740
const rwFilePermission = 0600
const copyChunkSize = 128 * 1024
// HomeFolder returns the home folder of the current user.
func HomeFolder() string {
u, err := user.Current()
if err != nil {
panic(err)
}
return u.HomeDir
}
// CreateSecureFolder checks if the folder exists and has the appropriate permission rights. In case of bad permission rights
// the empty string is returned. If the folder doesn't exist it, create it.
func CreateSecureFolder(folder string) string {
if exists, _ := Exists(folder); exists {
info, err := os.Lstat(folder)
if err != nil {
fmt.Fprintln(os.Stderr, "Error checking stat folder: ", err)
return ""
}
if perm := int(info.Mode().Perm()); perm != defaultDirectoryPermission {
fmt.Fprintf(os.Stderr, "Folder different permission: %#o vs %#o \n", perm, defaultDirectoryPermission)
}
return folder
}
if err := os.MkdirAll(folder, defaultDirectoryPermission); err != nil {
panic(err)
}
return folder
}
// Exists returns whether the given file or directory exists.
func Exists(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// CreateSecureFile creates a file with wr permission for user only and returns
// the file handle.
func CreateSecureFile(file string) (*os.File, error) {
fd, err := os.Create(file)
if err != nil {
return nil, err
}
fd.Close()
if err := os.Chmod(file, rwFilePermission); err != nil {
// TODO: check why we don't return the error here
return nil, nil //nolint
}
return os.OpenFile(file, os.O_RDWR, rwFilePermission)
}
// Files returns the list of file names included in the given path or error if
// any.
func Files(folderPath string) ([]string, error) {
fi, err := os.ReadDir(folderPath)
if err != nil {
return nil, err
}
var files []string
for _, f := range fi {
if !f.IsDir() {
files = append(files, path.Join(folderPath, f.Name()))
}
}
return files, nil
}
// Folders returns the list of folder names included in the given path or error if
// any.
func Folders(folderPath string) ([]string, error) {
fi, err := os.ReadDir(folderPath)
if err != nil {
return nil, err
}
var folders []string
for _, f := range fi {
if f.IsDir() {
folders = append(folders, path.Join(folderPath, f.Name()))
}
}
return folders, nil
}
// FileExists returns true if the given name is a file in the given path. name
// must be the "basename" of the file and path must be the folder where it lies.
func FileExists(filePath, name string) bool {
list, err := Files(filePath)
if err != nil {
return false
}
for _, l := range list {
if l == name {
return true
}
}
return false
}
// FolderExists returns true if the given name is a folder in the given path. name
// must be the "basename" of the file and path must be the folder where it lies.
func FolderExists(folderPath, name string) bool {
list, err := Folders(folderPath)
if err != nil {
return false
}
for _, l := range list {
if l == name {
return true
}
}
return false
}
// CopyFile copy a file or folder from one path to another
func CopyFile(origFilePath, destFilePath string) (err error) {
var src, dest *os.File
if src, err = os.Open(origFilePath); err != nil {
return err
}
// close fi on exit and check for its returned error
defer func() {
if closeErr := src.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
// make a reader buffer
srcReader := bufio.NewReader(src)
if dest, err = os.Create(destFilePath); err != nil {
return err
}
// close fo on exit and check for its returned error
defer func() {
if closeErr := dest.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
// make a writer buffer
destWriter := bufio.NewWriter(dest)
// make a buffer to keep chunks that are read
buf := make([]byte, copyChunkSize)
if _, err := io.CopyBuffer(destWriter, srcReader, buf); err != nil {
return err
}
if err := destWriter.Flush(); err != nil {
return err
}
if err := os.Chmod(destFilePath, rwFilePermission); err != nil {
return err
}
return err
}
// CopyFolder copy files inside a folder to another folder recursively
func CopyFolder(origFolderPath, destFolderPath string) error {
fi, err := os.ReadDir(origFolderPath)
if err != nil {
return err
}
for _, file := range fi {
tmp1 := path.Join(origFolderPath, file.Name())
tmp2 := path.Join(destFolderPath, file.Name())
if !file.IsDir() {
if err := CopyFile(tmp1, tmp2); err != nil {
return err
}
} else {
CreateSecureFolder(tmp2)
if err := CopyFolder(tmp1, tmp2); err != nil {
return err
}
}
}
return nil
}