-
Notifications
You must be signed in to change notification settings - Fork 11
/
files.go
192 lines (167 loc) · 4.08 KB
/
files.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
/*
* Copyright (c) 2024-present unTill Software Development Group B.V.
* @author Denis Gribanov
*/
package coreutils
import (
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"slices"
)
// copies the specified dir from the provided FS to disk to path specified by dst
// use "." src to copy the entire srcFS content
func CopyDirFS(srcFS IReadFS, src, dst string, optFuncs ...CopyOpt) error {
opts := ©Opts{}
for _, optFunc := range optFuncs {
optFunc(opts)
}
return copyDirFSOpts(srcFS, src, dst, opts)
}
// copies the specified file from the provided FS to disk to path specified by dstDir
func CopyFileFS(srcFS fs.FS, srcFileName, dstDir string, optFuncs ...CopyOpt) error {
opts := ©Opts{}
for _, optFunc := range optFuncs {
optFunc(opts)
}
return copyFileFSOpts(srcFS, srcFileName, dstDir, opts)
}
func CopyFile(src, dstDir string, optFuncs ...CopyOpt) error {
dir, file := filepath.Split(filepath.Clean(src))
if len(dir) == 0 {
dir = "."
}
return CopyFileFS(os.DirFS(dir), file, dstDir, optFuncs...)
}
func CopyDir(src, dst string, optFuncs ...CopyOpt) error {
readDirFS := os.DirFS(src).(IReadFS)
return CopyDirFS(readDirFS, ".", dst, optFuncs...)
}
func copyDirFSOpts(srcFS IReadFS, src, dst string, opts *copyOpts) error {
srcinfo, err := fs.Stat(srcFS, src)
if err != nil {
// notest
return err
}
// TODO: src is "." -> srcinfo.Mode() is weak -> permission deined on create dst within temp dir created with more strong FileMode
_ = srcinfo
if err = os.MkdirAll(dst, FileMode_rwxrwxrwx); err != nil {
return err
}
dirEntries, err := srcFS.ReadDir(src)
if err != nil {
// notest
return err
}
for _, dirEntry := range dirEntries {
srcFilePath := path.Join(src, dirEntry.Name()) // '/' separator must be used for fs.FS,
if dirEntry.IsDir() {
dstFilePath := filepath.Join(dst, dirEntry.Name())
err = copyDirFSOpts(srcFS, srcFilePath, dstFilePath, opts)
} else {
err = copyFileFSOpts(srcFS, srcFilePath, dst, opts)
}
if err != nil {
return err
}
}
return nil
}
func copyFileFSOpts(srcFS fs.FS, srcFileName, dstDir string, opts *copyOpts) error {
if len(opts.files) > 0 && !slices.Contains(opts.files, srcFileName) {
return nil
}
srcF, err := srcFS.Open(srcFileName)
if err != nil {
return err
}
defer srcF.Close()
targetFileName := filepath.Base(srcFileName)
if len(opts.targetFileName) > 0 {
targetFileName = opts.targetFileName
}
dstFileNameWithPath := filepath.Join(dstDir, targetFileName)
existsDstFile, err := Exists(dstFileNameWithPath)
if err != nil {
// notest
return err
}
if existsDstFile {
if opts.skipExisting {
return nil
}
return fmt.Errorf("file %s already exists: %w", dstDir, os.ErrExist)
}
existsDstDir, err := Exists(dstDir)
if err != nil {
// notest
return err
}
if !existsDstDir {
if err := os.MkdirAll(dstDir, FileMode_rwxrwxrwx); err != nil {
// notest
return err
}
}
dstF, err := os.Create(dstFileNameWithPath)
if err != nil {
return err
}
defer dstF.Close()
if _, err = io.Copy(dstF, srcF); err != nil {
// notest
return err
}
if err = dstF.Sync(); err != nil {
// notest
return err
}
if opts.fm > 0 {
return os.Chmod(dstDir, opts.fm)
}
srcinfo, err := fs.Stat(srcFS, srcFileName)
if err != nil {
// notest
return err
}
return os.Chmod(dstFileNameWithPath, srcinfo.Mode())
}
func Exists(filePath string) (exists bool, err error) {
if _, err = os.Stat(filePath); err == nil {
return true, nil
}
if os.IsNotExist(err) {
err = nil
}
return false, err
}
type copyOpts struct {
fm fs.FileMode
skipExisting bool
files []string
targetFileName string
}
type CopyOpt func(co *copyOpts)
func WithFileMode(fm fs.FileMode) CopyOpt {
return func(co *copyOpts) {
co.fm = fm
}
}
func WithSkipExisting() CopyOpt {
return func(co *copyOpts) {
co.skipExisting = true
}
}
func WithNewName(fileName string) CopyOpt {
return func(co *copyOpts) {
co.targetFileName = fileName
}
}
func WithFilterFilesWithRelativePaths(filesWithRelativePaths []string) CopyOpt {
return func(co *copyOpts) {
co.files = filesWithRelativePaths
}
}