forked from laher/someutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp.go
154 lines (142 loc) · 2.81 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
package someutils
import (
"errors"
"fmt"
"github.com/laher/uggo"
"io"
"os"
"path/filepath"
)
type CpOptions struct {
Recursive bool
}
func init() {
Register(Util{
"cp",
Cp})
}
func Cp(call []string) error {
options := CpOptions{}
flagSet := uggo.NewFlagSetDefault("cp", "[options] [src...] [dest]", VERSION)
flagSet.AliasedBoolVar(&options.Recursive, []string{"R", "r", "recursive"}, false, "Recurse into directories")
err := flagSet.Parse(call[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Flag error: %v\n\n", err.Error())
flagSet.Usage()
return err
}
if flagSet.ProcessHelpOrVersion() {
return nil
}
args := flagSet.Args()
if len(args) < 2 {
flagSet.Usage()
return errors.New("Not enough args")
}
srcGlobs := args[0 : len(args)-1]
dest := args[len(args)-1]
for _, srcGlob := range srcGlobs {
srces, err := filepath.Glob(srcGlob)
if err != nil {
return err
}
for _, src := range srces {
err = copyFile(src, dest, options)
if err != nil {
return err
}
}
}
return nil
}
func copyFile(src, dest string, options CpOptions) error {
//println("copy "+src+" to "+dest)
srcFile, err := os.Open(src)
defer srcFile.Close()
if err != nil {
return err
}
sinf, err := srcFile.Stat()
if err != nil {
return err
}
if sinf.IsDir() && !options.Recursive {
return errors.New("Omitting directory " + src)
}
//check if destination given is full filename or its (existing) parent dir
var destFull string
dinf, err := os.Stat(dest)
if err != nil {
if !os.IsNotExist(err) {
return err
} else {
//doesnt exist
destFull = dest
}
} else {
if dinf.IsDir() {
//copy file name
destFull = filepath.Join(dest, sinf.Name())
} else {
destFull = dest
}
}
//println("copy "+src+" to "+destFull)
var destExists bool
dinf, err = os.Stat(destFull)
if err != nil {
if !os.IsNotExist(err) {
return err
} else {
//doesnt exist
destExists = false
}
} else {
destExists = true
if sinf.IsDir() && !dinf.IsDir() {
return errors.New("destination is an existing non-directory")
}
}
if sinf.IsDir() {
//println("copying dir")
if !destExists {
//println("mkdir")
err = os.Mkdir(destFull, sinf.Mode())
if err != nil {
return err
}
} else {
//continue
}
contents, err := srcFile.Readdir(0)
if err != nil {
return err
}
err = srcFile.Close()
if err != nil {
return err
}
for _, fi := range contents {
copyFile(filepath.Join(src, fi.Name()), destFull, options)
}
} else {
destFile, err := os.OpenFile(destFull, os.O_CREATE, sinf.Mode())
defer destFile.Close()
if err != nil {
return err
}
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
err = destFile.Close()
if err != nil {
return err
}
err = srcFile.Close()
if err != nil {
return err
}
}
return nil
}