-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_utils.go
258 lines (220 loc) · 5.04 KB
/
file_utils.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
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sync/atomic"
"syscall"
"time"
)
type FileInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
Mode string `json:"mode"`
ModTime time.Time `json:"mod_time"`
UID uint `json:"uid"`
GID uint `json:"gid"`
IsDir bool `json:"is_dir"`
}
/*
* When some function work for both file and directory, it should be named as Path
* Just refer that with `File` in the function name
* We shouldn't use `Dir` in the function name, unless it's specifically for directory
*/
func ExistsPath(path string) bool {
_, err := os.Stat(path)
if err != nil {
return false
}
return !os.IsNotExist(err)
}
func RemovePath(path string) {
// Silently try to remove directory and all contents
_ = os.RemoveAll(path)
}
func CreateDirectoryWithOptions(path string, removeIfExists bool, perm os.FileMode) error {
// Check if directory exists and remove if requested
if removeIfExists {
if ExistsPath(path) {
if err := os.RemoveAll(path); err != nil {
return err
}
}
}
return os.MkdirAll(path, perm)
}
func WriteFile(path string, data []byte, perm os.FileMode, uid int, gid int) error {
// Open file for writing
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, perm)
if err != nil {
return err
}
defer file.Close()
// Write data to file
_, err = file.Write(data)
if err != nil {
return err
}
// Change the ownership of the file
if err := os.Chown(path, uid, gid); err != nil {
return err
}
return nil
}
func CopyFile(src, dst string) error {
// Open source file
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// Create destination file
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
// Copy contents of source file to destination file
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
// Fix permissions of destination file
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
if err := os.Chmod(dst, srcInfo.Mode()); err != nil {
return err
}
// Fix ownership of destination file
srcSysInfo := srcInfo.Sys().(*syscall.Stat_t)
if err := os.Chown(dst, int(srcSysInfo.Uid), int(srcSysInfo.Gid)); err != nil {
return err
}
return nil
}
func MoveFile(src, dst string) error {
// Fix ownership of destination file
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
srcSysInfo := srcInfo.Sys().(*syscall.Stat_t)
// Move file
if err := os.Rename(src, dst); err != nil {
return err
}
// Fix ownership of destination file
if err := os.Chown(dst, int(srcSysInfo.Uid), int(srcSysInfo.Gid)); err != nil {
return err
}
return nil
}
func ModifyFile(path string, data []byte) error {
// Get the current file info
info, err := os.Stat(path)
if err != nil {
return err
}
// Open file for writing
file, err := os.OpenFile(path, os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return err
}
defer file.Close()
// Write data to file
_, err = file.Write(data)
if err != nil {
return err
}
// Change the ownership of the file to match the original
if err := os.Chown(path, int(info.Sys().(*syscall.Stat_t).Uid), int(info.Sys().(*syscall.Stat_t).Gid)); err != nil {
return err
}
return nil
}
func FetchFileSize(path string) (int64, error) {
// Create a channel to receive file sizes
sizeChan := make(chan int64)
errChan := make(chan error)
done := make(chan bool)
var totalSize int64
go func() {
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
sizeChan <- info.Size()
}
return nil
})
if err != nil {
errChan <- err
}
done <- true
}()
// Collect results
for {
select {
case size := <-sizeChan:
atomic.AddInt64(&totalSize, size)
case err := <-errChan:
return 0, err
case <-done:
return totalSize, nil
}
}
}
func ListFiles(fullPath string) ([]FileInfo, error) {
var fileInfos []FileInfo
files, err := os.ReadDir(fullPath)
if err != nil {
return nil, err
}
for _, file := range files {
info, err := file.Info()
if err != nil {
return nil, err
}
sysInfo := info.Sys().(*syscall.Stat_t)
fileInfos = append(fileInfos, FileInfo{
Name: info.Name(),
Size: info.Size(),
Mode: fmt.Sprintf("%o", info.Mode().Perm()),
ModTime: info.ModTime(),
UID: uint(sysInfo.Uid),
GID: uint(sysInfo.Gid),
IsDir: info.IsDir(),
})
}
return fileInfos, nil
}
// DownloadFile downloads a file from the given URL and saves it to the specified path
func DownloadFile(url string, path string) error {
// Create the file
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Write the data to the file
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}