forked from gokcehan/lf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.go
More file actions
186 lines (164 loc) · 4.08 KB
/
copy.go
File metadata and controls
186 lines (164 loc) · 4.08 KB
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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/djherbis/times"
)
type ProgressWriter struct {
writer io.Writer
nums chan<- int64
}
func NewProgressWriter(writer io.Writer, nums chan<- int64) *ProgressWriter {
return &ProgressWriter{
writer: writer,
nums: nums,
}
}
func (progressWriter *ProgressWriter) Write(b []byte) (int, error) {
n, err := progressWriter.writer.Write(b)
progressWriter.nums <- int64(n)
return n, err
}
func copySize(srcs []string) (int64, error) {
var total int64
for _, src := range srcs {
_, err := os.Lstat(src)
if os.IsNotExist(err) {
return total, fmt.Errorf("src does not exist: %q", src)
}
err = filepath.Walk(src, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walk: %w", err)
}
total += info.Size()
return nil
})
if err != nil {
return total, err
}
}
return total, nil
}
func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan<- int64, errs chan<- error) {
r, err := os.Open(src)
if err != nil {
errs <- err
return
}
defer r.Close()
var dstMode os.FileMode = 0o666
if slices.Contains(preserve, "mode") {
dstMode = info.Mode()
}
w, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, dstMode)
if err != nil {
errs <- err
return
}
if _, err := io.Copy(NewProgressWriter(w, nums), r); err != nil {
errs <- err
w.Close()
if err = os.Remove(dst); err != nil {
errs <- err
}
return
}
if err := w.Close(); err != nil {
errs <- err
if err = os.Remove(dst); err != nil {
errs <- err
}
return
}
if slices.Contains(preserve, "timestamps") {
atime := times.Get(info).AccessTime()
mtime := info.ModTime()
if err := os.Chtimes(dst, atime, mtime); err != nil {
errs <- err
if err = os.Remove(dst); err != nil {
errs <- err
}
return
}
}
}
func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64, errs chan error) {
nums = make(chan int64, 1024)
errs = make(chan error, 1024)
go func() {
dirInfos := make(map[string]os.FileInfo)
for _, src := range srcs {
file := filepath.Base(src)
dst := filepath.Join(dstDir, file)
if lstat, err := os.Lstat(dst); err == nil {
ext := getFileExtension(lstat)
basename := file[:len(file)-len(ext)]
var newPath string
for i := 1; !os.IsNotExist(err); i++ {
file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext)
file = strings.ReplaceAll(file, "%b", basename)
file = strings.ReplaceAll(file, "%e", ext)
file = strings.ReplaceAll(file, "%n", strconv.Itoa(i))
newPath = filepath.Join(dstDir, file)
_, err = os.Lstat(newPath)
}
dst = newPath
}
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
errs <- fmt.Errorf("walk: %w", err)
return nil
}
rel, err := filepath.Rel(src, path)
if err != nil {
errs <- fmt.Errorf("relative: %w", err)
return nil
}
newPath := filepath.Join(dst, rel)
switch {
case info.IsDir():
dstMode := os.ModePerm
if slices.Contains(preserve, "mode") {
dstMode = info.Mode()
}
if err := os.MkdirAll(newPath, dstMode); err != nil {
errs <- fmt.Errorf("mkdir: %w", err)
}
if slices.Contains(preserve, "timestamps") {
dirInfos[newPath] = info
}
nums <- info.Size()
case info.Mode()&os.ModeSymlink != 0:
if rlink, err := os.Readlink(path); err != nil {
errs <- fmt.Errorf("symlink: %w", err)
} else {
if err := os.Symlink(rlink, newPath); err != nil {
errs <- fmt.Errorf("symlink: %w", err)
}
}
nums <- info.Size()
default:
copyFile(path, newPath, preserve, info, nums, errs)
}
return nil
})
if err != nil {
errs <- fmt.Errorf("walk: %w", err)
}
}
for path, info := range dirInfos {
atime := times.Get(info).AccessTime()
mtime := info.ModTime()
if err := os.Chtimes(path, atime, mtime); err != nil {
errs <- fmt.Errorf("chtimes: %w", err)
}
}
close(errs)
}()
return nums, errs
}