-
Notifications
You must be signed in to change notification settings - Fork 3
/
copy.go
75 lines (65 loc) · 1.57 KB
/
copy.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
// Copyright 2018 Daniel Theophanes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fsop has common file system operations.
package fsop
import (
"io"
"os"
"path/filepath"
)
// Only takes a path and returns true to include the file or folder.
type Only func(p string) bool
// Copy the the oldpath to the newpath. If only is not nil, only copy the
// files and folders where only returns true.
func Copy(oldpath, newpath string, only Only) error {
if only != nil && !only(oldpath) {
return nil
}
fi, err := os.Stat(oldpath)
if err != nil {
return err
}
if fi.IsDir() {
return copyFolder(fi, oldpath, newpath, only)
}
return copyFile(fi, oldpath, newpath)
}
func copyFile(fi os.FileInfo, oldpath, newpath string) error {
old, err := os.Open(oldpath)
if err != nil {
return err
}
defer old.Close()
err = os.MkdirAll(filepath.Dir(newpath), fi.Mode()|0700)
if err != nil {
return err
}
new, err := os.OpenFile(newpath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fi.Mode())
if err != nil {
return err
}
_, err = io.Copy(new, old)
cerr := new.Close()
if cerr != nil {
return cerr
}
return err
}
func copyFolder(fi os.FileInfo, oldpath, newpath string, only Only) error {
err := os.MkdirAll(newpath, fi.Mode())
if err != nil {
return err
}
list, err := os.ReadDir(oldpath)
if err != nil {
return err
}
for _, item := range list {
err = Copy(filepath.Join(oldpath, item.Name()), filepath.Join(newpath, item.Name()), only)
if err != nil {
return err
}
}
return nil
}