forked from u-root/u-root
/
cp.go
313 lines (273 loc) · 6.67 KB
/
cp.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2016-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copy files.
//
// Synopsis:
// cp [-rRfivwP] FROM... TO
//
// Options:
// -w n: number of worker goroutines
// -R: copy file hierarchies
// -r: alias to -R recursive mode
// -i: prompt about overwriting file
// -f: force overwrite files
// -v: verbose copy mode
// -P: don't follow symlinks
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
flag "github.com/spf13/pflag"
)
// buffSize is the length of buffer during
// the parallel copy using worker function
const buffSize = 8192
var (
flags struct {
recursive bool
ask bool
force bool
verbose bool
symlink bool
nwork int
}
input = bufio.NewReader(os.Stdin)
// offchan is a channel used for indicate the nextbuffer to read with worker()
offchan = make(chan int64, 0)
// zerochan is a channel used for indicate the start of a new read file
zerochan = make(chan int, 0)
)
func init() {
defUsage := flag.Usage
flag.Usage = func() {
os.Args[0] = "cp [-wRrifvP] file[s] ... dest"
defUsage()
}
flag.IntVarP(&flags.nwork, "workers", "w", runtime.NumCPU(), "number of worker goroutines")
flag.BoolVarP(&flags.recursive, "RECURSIVE", "R", false, "copy file hierarchies")
flag.BoolVarP(&flags.recursive, "recursive", "r", false, "alias to -R recursive mode")
flag.BoolVarP(&flags.ask, "interactive", "i", false, "prompt about overwriting file")
flag.BoolVarP(&flags.force, "force", "f", false, "force overwrite files")
flag.BoolVarP(&flags.verbose, "verbose", "v", false, "verbose copy mode")
flag.BoolVarP(&flags.symlink, "no-dereference", "P", false, "don't follow symlinks")
go nextOff()
}
// promptOverwrite ask if the user wants overwrite file
func promptOverwrite(dst string) (bool, error) {
fmt.Printf("cp: overwrite %q? ", dst)
answer, err := input.ReadString('\n')
if err != nil {
return false, err
}
if strings.ToLower(answer)[0] != 'y' {
return false, nil
}
return true, nil
}
// copyFile copies file between src (source) and dst (destination)
// todir: if true insert src INTO dir dst
func copyFile(src, dst string, todir bool) error {
if todir {
file := filepath.Base(src)
dst = filepath.Join(dst, file)
}
srcb, err := os.Lstat(src)
if err != nil {
return fmt.Errorf("can't stat %v: %v", src, err)
}
// don't follow symlinks, copy symlink
if L := os.ModeSymlink; flags.symlink && srcb.Mode()&L == L {
linkPath, err := filepath.EvalSymlinks(src)
if err != nil {
return fmt.Errorf("can't eval symlink %v: %v", src, err)
}
return os.Symlink(linkPath, dst)
}
if srcb.IsDir() {
if flags.recursive {
return copyDir(src, dst)
}
return fmt.Errorf("%q is a directory, try use recursive option", src)
}
dstb, err := os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("%q: can't handle error %v", dst, err)
}
if dstb != nil {
if sameFile(srcb.Sys(), dstb.Sys()) {
return fmt.Errorf("%q and %q are the same file", src, dst)
}
if flags.ask && !flags.force {
overwrite, err := promptOverwrite(dst)
if err != nil {
return err
}
if !overwrite {
return nil
}
}
}
mode := srcb.Mode() & 0777
s, err := os.Open(src)
if err != nil {
return fmt.Errorf("can't open %q: %v", src, err)
}
defer s.Close()
d, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return fmt.Errorf("can't create %q: %v", dst, err)
}
defer d.Close()
return copyOneFile(s, d, src, dst)
}
// copyOneFile copy the content between two files
func copyOneFile(s *os.File, d *os.File, src, dst string) error {
zerochan <- 0
fail := make(chan error, flags.nwork)
for i := 0; i < flags.nwork; i++ {
go worker(s, d, fail)
}
// iterate the errors from channel
for i := 0; i < flags.nwork; i++ {
err := <-fail
if err != nil {
return err
}
}
if flags.verbose {
fmt.Printf("%q -> %q\n", src, dst)
}
return nil
}
// createDir populate dir destination if not exists
// if exists verify is not a dir: return error if is file
// cannot overwrite: dir -> file
func createDir(src, dst string) error {
dstInfo, err := os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
if !dstInfo.IsDir() {
return fmt.Errorf("can't overwrite non-dir %q with dir %q", dst, src)
}
return nil
}
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
if err := os.Mkdir(dst, srcInfo.Mode()); err != nil {
return err
}
if flags.verbose {
fmt.Printf("%q -> %q\n", src, dst)
}
return nil
}
// copyDir copy the file hierarchies
// used at cp when -r or -R flag is true
func copyDir(src, dst string) error {
if err := createDir(src, dst); err != nil {
return err
}
// list files from destination
files, err := ioutil.ReadDir(src)
if err != nil {
return fmt.Errorf("can't list files from %q: %q", src, err)
}
// copy recursively the src -> dst
for _, file := range files {
fname := file.Name()
fpath := filepath.Join(src, fname)
newDst := filepath.Join(dst, fname)
copyFile(fpath, newDst, false)
}
return err
}
// worker is a concurrent copy, used to copy part of the files
// in parallel
func worker(s *os.File, d *os.File, fail chan error) {
var buf [buffSize]byte
var bp []byte
l := len(buf)
bp = buf[0:]
o := <-offchan
for {
n, err := s.ReadAt(bp, o)
if err != nil && err != io.EOF {
fail <- fmt.Errorf("reading %s at %v: %v", s.Name(), o, err)
return
}
if n == 0 {
break
}
nb := bp[0:n]
n, err = d.WriteAt(nb, o)
if err != nil {
fail <- fmt.Errorf("writing %s: %v", d.Name(), err)
return
}
bp = buf[n:]
o += int64(n)
l -= n
if l == 0 {
l = len(buf)
bp = buf[0:]
o = <-offchan
}
}
fail <- nil
}
// nextOff handler for next buffers and sync goroutines
// zerochan imply the init of file
// offchan is the next buffer part to read
func nextOff() {
off := int64(0)
for {
select {
case <-zerochan:
off = 0
case offchan <- off:
off += buffSize
}
}
}
// cp is a function whose eval the args
// and make decisions for copyfiles
func cp(args []string) (lastErr error) {
todir := false
from, to := args[:len(args)-1], args[len(args)-1]
toStat, err := os.Stat(to)
if err == nil {
todir = toStat.IsDir()
}
if flag.NArg() > 2 && todir == false {
log.Fatalf("is not a directory: %s\n", to)
}
for _, file := range from {
if err := copyFile(file, to, todir); err != nil {
log.Printf("cp: %v\n", err)
lastErr = err
}
}
return err
}
func main() {
flag.Parse()
if flag.NArg() < 2 {
flag.Usage()
os.Exit(1)
}
if err := cp(flag.Args()); err != nil {
os.Exit(1)
}
}