/
copy.go
83 lines (73 loc) · 2.1 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
76
77
78
79
80
81
82
83
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
func retryCopyDir(sourceInfo os.FileInfo, source, destination string) error {
if stat, err := os.Stat(destination); err == nil {
if !stat.IsDir() {
return errors.New("target exists but is not a directory: " + destination)
}
fmt.Println("Using existing destination:", destination)
} else {
if err := os.Mkdir(destination, sourceInfo.Mode()&os.ModePerm); err != nil {
return err
}
}
listing := retryListDir(source)
for _, info := range listing {
newSource := filepath.Join(source, info.Name())
newDest := filepath.Join(destination, info.Name())
if info.IsDir() {
if err := retryCopyDir(info, newSource, newDest); err != nil {
return err
}
} else {
if err := retryCopyFile(info, newSource, newDest); err != nil {
return err
}
}
}
// Change the times after copying the contents to avoid updating the modification time.
if err := os.Chtimes(destination, sourceInfo.ModTime(), sourceInfo.ModTime()); err != nil {
return err
}
return nil
}
func retryCopyFile(sourceInfo os.FileInfo, source, destination string) error {
if destInfo, err := os.Lstat(destination); err == nil {
if destInfo.IsDir() {
return errors.New("target exists but is a directory: " + destination)
}
if (destInfo.Mode() & os.ModeSymlink) != (sourceInfo.Mode() & os.ModeSymlink) {
return errors.New("target exists but symlink-ness differs: " + destination)
}
if destInfo.Size() == sourceInfo.Size() {
fmt.Println("Skipping file:", destination)
return nil
}
fmt.Println("Overwriting file:", destination)
os.Remove(destination)
}
if (sourceInfo.Mode() & os.ModeSymlink) != 0 {
fmt.Println("Copying link:", source)
linkDest := retryReadlink(source)
return os.Symlink(linkDest, destination)
}
fmt.Println("Copying file:", source)
output, err := os.Create(destination)
if err != nil {
return err
}
reader := newRetryFileReader(source)
_, err = io.Copy(output, reader)
reader.Close()
output.Close()
if err != nil {
return err
}
return os.Chtimes(destination, sourceInfo.ModTime(), sourceInfo.ModTime())
}