forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp.go
163 lines (145 loc) · 3.65 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
// 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.
// cp copies 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"
"log"
"os"
"path/filepath"
"strings"
flag "github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/cp"
)
var (
flags struct {
recursive bool
ask bool
force bool
verbose bool
noFollowSymlinks bool
}
input = bufio.NewReader(os.Stdin)
)
func init() {
defUsage := flag.Usage
flag.Usage = func() {
os.Args[0] = "cp [-wRrifvP] file[s] ... dest"
defUsage()
}
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.noFollowSymlinks, "no-dereference", "P", false, "don't follow symlinks")
}
// 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
}
// cpArgs is a function whose eval the args
// and make decisions for copyfiles
func cpArgs(args []string) 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 {
log.Fatalf("is not a directory: %s\n", to)
}
opts := cp.Options{
NoFollowSymlinks: flags.noFollowSymlinks,
// cp the command makes sure that
//
// (1) the files it's copying aren't already the same,
// (2) the user is asked about overwriting an existing file if
// one is already there.
PreCallback: func(src, dst string, srcfi os.FileInfo) error {
// check if src is dir
if !flags.recursive && srcfi.IsDir() {
log.Printf("cp: -r not specified, omitting directory %s", src)
return cp.ErrSkip
}
dstfi, err := os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
log.Printf("cp: %q: can't handle error %v", dst, err)
return cp.ErrSkip
} else if err != nil {
// dst does not exist.
return nil
}
// dst does exist.
if os.SameFile(srcfi, dstfi) {
log.Printf("cp: %q and %q are the same file", src, dst)
return cp.ErrSkip
}
if flags.ask && !flags.force {
overwrite, err := promptOverwrite(dst)
if err != nil {
return err
}
if !overwrite {
return cp.ErrSkip
}
}
return nil
},
PostCallback: func(src, dst string) {
if flags.verbose {
fmt.Printf("%q -> %q\n", src, dst)
}
},
}
var lastErr error
for _, file := range from {
dst := to
if todir {
dst = filepath.Join(dst, filepath.Base(file))
}
if flags.recursive {
err = opts.CopyTree(file, dst)
} else {
err = opts.Copy(file, dst)
}
if err != nil {
log.Printf("cp: %v\n", err)
lastErr = err
}
}
return lastErr
}
func main() {
flag.Parse()
if flag.NArg() < 2 {
flag.Usage()
os.Exit(1)
}
if err := cpArgs(flag.Args()); err != nil {
os.Exit(1)
}
}