/
files.go
165 lines (141 loc) · 3.37 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
/*
* 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 dst
func CopyFileFS(srcFS fs.FS, src, dst string, optFuncs ...CopyOpt) error {
opts := ©Opts{}
for _, optFunc := range optFuncs {
optFunc(opts)
}
return copyFileFSOpts(srcFS, src, dst, opts)
}
func CopyFile(src, dst string, optFuncs ...CopyOpt) error {
return CopyFileFS(os.DirFS(filepath.Clean(src)), src, dst, 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,
dstFilePath := filepath.Join(dst, dirEntry.Name())
if dirEntry.IsDir() {
err = copyDirFSOpts(srcFS, srcFilePath, dstFilePath, opts)
} else {
err = copyFileFSOpts(srcFS, srcFilePath, dstFilePath, opts)
}
if err != nil {
return err
}
}
return nil
}
func copyFileFSOpts(srcFS fs.FS, src, dst string, opts *copyOpts) error {
if len(opts.files) > 0 && !slices.Contains(opts.files, src) {
return nil
}
srcF, err := srcFS.Open(src)
if err != nil {
return err
}
defer srcF.Close()
exists, err := Exists(dst)
if err != nil {
// notest
return err
}
if exists {
if opts.skipExisting {
return nil
}
return fmt.Errorf("file %s already exists: %w", dst, os.ErrExist)
}
dstF, err := os.Create(dst)
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(dst, opts.fm)
}
srcinfo, err := fs.Stat(srcFS, src)
if err != nil {
// notest
return err
}
return os.Chmod(dst, 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
}
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 WithFilterFilesWithRelativePaths(filesWithRelativePaths []string) CopyOpt {
return func(co *copyOpts) {
co.files = filesWithRelativePaths
}
}