-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopier.go
110 lines (90 loc) · 1.92 KB
/
copier.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
package main
import (
"fmt"
"github.com/schollz/progressbar/v3"
"io"
"os"
"path/filepath"
"sync"
)
func copierProgressBar(ch <-chan CounterType, len int) {
fileBar := progressbar.Default(int64(len))
for cType := range ch {
switch cType {
case File:
fileBar.Add(1)
}
}
fileBar.Finish()
}
type ErrFileExists struct {
src string
dst string
}
func NewErrFileExists(src, dst string) ErrFileExists {
return ErrFileExists{src, dst}
}
func (e ErrFileExists) Error() string {
return fmt.Sprintf("%s already exists at %s", e.src, e.dst)
}
type Copy struct {
src string
dst string
}
func NewCopy(src, dst string) *Copy {
return &Copy{src, dst}
}
type Copier struct {
// directories that are known to exist
dirs sync.Map
}
func (c *Copier) Copy(cp *Copy) error {
err := c.copy(cp.src, cp.dst)
if err != nil {
return err
}
return nil
}
func (c *Copier) copy(src, dst string) error {
dstDir := filepath.Dir(dst)
if _, ok := c.dirs.Load(dstDir); ok {
return c.unsafeCopy(src, dst)
} else {
c.dirs.Store(dstDir, true)
err := os.MkdirAll(dstDir, os.ModePerm)
if err != nil {
return FormatError("Copier-MkdirAll", err)
}
return c.unsafeCopy(src, dst)
}
}
func (c *Copier) unsafeCopy(src, dst string) error {
fDst, destErr := os.Stat(dst)
if destErr == nil {
fSrc, err := os.Stat(src)
if err != nil {
return FormatError("Copier-Stat-Nil", err)
}
if fSrc.Size() == fDst.Size() {
return NewErrFileExists(src, dst)
}
if DebugLevel == Debug {
LogInfo("Copier-Overwrite", fmt.Sprintf("%s already exists at %s, overwriting", src, dst))
}
}
source, err := os.Open(src)
if err != nil {
return FormatError("Copier-Open", err)
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return FormatError("Copier-Create", err)
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
return FormatError("Copier-Copy", err)
}
return nil
}