-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcp.go
329 lines (303 loc) · 9.48 KB
/
pcp.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
// A progress-enhanced version of cp that shows progress while copying.
// TODO:
// - add estimated time to completion
// - add -R switch
// - tune progress_freq and resolution for input file size
import (
"os"
"io"
"io/ioutil"
"fmt"
"math"
"path"
"path/filepath"
mlib "github.com/msoulier/mlib"
"time"
"github.com/op/go-logging"
"flag"
)
var (
copysize int64 = 16384
progress_freq = 1000
rate_freq = 50
log *logging.Logger = nil
debug bool = false
)
func init() {
flag.Int64Var(©size, "copysize", 16384, "Internal copy buffer size")
flag.BoolVar(&debug, "debug", false, "Debug logging")
}
// Copied from Roland Singer [[email protected]].
// copyFile copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file. The file mode will be copied from the source and
// the copied data is synced/flushed to stable storage.
func copyFile(src, dst string, name chan string, progress chan int64) (err error) {
var bytes_copied int64 = 0
in, err := os.Open(src)
if err != nil {
return err
}
// Need the source file size
var source_size int64 = 0
if stat, err := os.Stat(src); err != nil {
panic(err)
} else {
source_size = stat.Size()
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
// Error handling
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
// Report the file name
name <- src
// Report the file size
progress <- source_size
i := 0
for {
var bytes int64 = 0
bytes, err = io.CopyN(out, in, copysize)
if err != nil {
if err == io.EOF {
if bytes_copied > 0 {
progress <- bytes_copied
}
progress <- 0
break
} else {
return err
}
}
bytes_copied += bytes
// Report progress at regular intervals.
i++
if i % progress_freq == 0 {
progress <- bytes_copied
bytes_copied = 0
}
}
// FIXME: make conditional on a command-line option
//err = out.Sync()
return nil
}
// Copied from Roland Singer [[email protected]].
// copyDir recursively copies a directory tree, attempting to preserve
// permissions.
// Source directory must exist, destination directory must *not* exist.
// Symlinks are ignored and skipped.
func copyDir(src string, dst string, name chan string, progress chan int64) (err error) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}
_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
return fmt.Errorf("destination already exists")
}
err = os.MkdirAll(dst, si.Mode())
if err != nil {
return err
}
entries, err := ioutil.ReadDir(src)
if err != nil {
return err
}
log.Debugf("directory entries for %s: %v", src, entries)
for i, entry := range entries {
log.Debugf("looping on direntry %d", i)
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
log.Debugf("src = %s, dst = %s", srcPath, dstPath)
if entry.IsDir() {
log.Debugf("entry is a dir, recursion!")
err = copyDir(srcPath, dstPath, name, progress)
log.Debugf("copyDir returned %v", err)
if err != nil {
log.Errorf("copyDir returned an error: %s", err)
return err
}
} else {
// Skip symlinks.
// FIXME
if entry.Mode() & os.ModeSymlink != 0 {
log.Debugf("skipping symlink")
continue
}
log.Debugf("calling copyFile on %s, %s", srcPath, dstPath)
err = copyFile(srcPath, dstPath, name, progress)
log.Debugf("copyFile returned %v", err)
if err != nil {
log.Errorf("copyFile returned an error: %s", err)
return err
}
}
}
log.Debug("returning nil")
return nil
}
func config_logging() {
format := logging.MustStringFormatter(
`%{time:2006-01-02 15:04:05.000-0700} %{level} [%{shortfile}] %{message}`,
)
stderrBackend := logging.NewLogBackend(os.Stderr, "", 0)
stderrFormatter := logging.NewBackendFormatter(stderrBackend, format)
stderrBackendLevelled := logging.AddModuleLevel(stderrFormatter)
logging.SetBackend(stderrBackendLevelled)
if debug {
stderrBackendLevelled.SetLevel(logging.DEBUG, "pcp")
} else {
stderrBackendLevelled.SetLevel(logging.INFO, "pcp")
}
log = logging.MustGetLogger("pcp")
}
func parse_args() (string, string) {
usage := "Usage: pcp [options] <source> <destination>\n"
flag.Parse()
args := flag.Args()
if debug {
fmt.Fprintf(os.Stderr, "DEBUG logging enabled\n")
fmt.Fprintf(os.Stderr, "args is %v\n", args)
}
if len(args) < 2 {
os.Stderr.WriteString(usage)
flag.PrintDefaults()
os.Exit(1)
}
source := args[0]
dest := args[1]
return source, dest
}
func main() {
source, dest := parse_args()
config_logging()
var bytes_copied int64 = 0
var source_size int64 = 0
var dircopy bool = false
// Start and end time for the overall completion of the operation.
start_time := time.Now()
// If dest is a directory, add the name of the file to it.
if stat, err := os.Stat(dest); err == nil && stat.IsDir() {
log.Debugf("destination %s is a directory", dest)
source_name := path.Base(source)
dest = path.Join(dest, source_name)
log.Debugf("new destination is %s", dest)
}
// If it doesn't exist, we'll create it as a file. This is standard cp
// behaviour.
// FIXME: get confirmation if we're overwriting something
// stat the source file to get its size
// If there is one source and one dest, check if the source is a
// directory.
// FIXME: allow multiple source files
if stat, err := os.Stat(source); err != nil {
panic(err)
} else {
if stat.IsDir() {
dircopy = true
}
}
// A channel for copying progress.
progress := make(chan int64)
// A channel for the name to be communicated.
name := make(chan string)
go func() {
var err error = nil
if dircopy {
log.Debugf("copy goroutine: calling copyDir")
err = copyDir(source, dest, name, progress)
} else {
log.Debugf("copy goroutine: calling copyFile")
err = copyFile(source, dest, name, progress)
}
log.Debugf("and we're back: err = %v", err)
if err != nil {
panic(err)
}
// And we're done
log.Debugf("sending empty string to name channel")
name <- ""
log.Debugf("sending -1 to progress channel")
progress <- -1
}()
oldTime := time.Now()
i := 0
rate := int64(0)
time_remaining := time.Duration(0)
filename := <-name
fmt.Printf("copying file name %s\n", filename)
var percent float64 = 0
var remaining_bytes int64 = 0
for {
// if bytes_copied is zero, the first number is the source_size
log.Debug("blocking on progress channel")
copied := <-progress
log.Debugf("copied is %d", copied)
if copied < 0 {
break
}
if source_size == 0 {
source_size = copied
copied = 0
continue
}
if copied == 0 {
percent = 100
remaining_bytes = 0
time_remaining = time.Duration(0)
} else if copied > 0 {
bytes_copied += copied
percent = (float64(bytes_copied) / float64(source_size)) * 100
remaining_bytes = source_size - bytes_copied
timeDiff := time.Since(oldTime)
oldTime = oldTime.Add(timeDiff)
// Only recompute the rate every rate_freq iterations, just to
// buffer the updates to something readable.
if i++; i % rate_freq == 0 || rate == 0 {
rate = int64(float64(copied) / timeDiff.Seconds())
if rate != 0 {
time_remaining = time.Duration( float64(remaining_bytes) / float64(rate) ) * time.Second
}
}
}
fmt.Printf("\r \r")
// FIXME: leave rate and time remaining blank until they're non-zero
fmt.Printf(" %8s copied: %3d%% - %10s/s - %s remaining ",
mlib.Bytes2human(bytes_copied),
int64(math.Floor(percent)),
mlib.Bytes2human(rate),
time_remaining)
if copied == 0 {
log.Debugf("copied is %d", copied)
bytes_copied = 0
percent = 0
source_size = 0
oldTime = time.Now()
operation_duration := time.Since(start_time)
operation_duration = operation_duration.Round(time.Millisecond)
fmt.Printf("%s\n", operation_duration)
log.Debug("blocking on name channel")
filename = <-name
log.Debugf("===> new name '%s'", filename)
fmt.Printf("%s:\n", filename)
}
}
os.Exit(0)
}