-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathditto.go
178 lines (144 loc) · 3.48 KB
/
ditto.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
package ditto
import (
"io"
"os"
"path/filepath"
"github.com/go-errors/errors"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
"github.com/itchio/wharf/archiver"
)
var args = struct {
src *string
dst *string
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("ditto", "Create a mirror (incl. symlinks) of a directory into another dir (rsync -az)").Hidden()
args.src = cmd.Arg("src", "Directory to mirror").Required().String()
args.dst = cmd.Arg("dst", "Path where to create a mirror").Required().String()
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
ctx.Must(Do(*args.src, *args.dst))
}
// Does not preserve users, nor permission, except the executable bit
func Do(src string, dst string) error {
comm.Debugf("rsync -a %s %s", src, dst)
totalSize := int64(0)
doneSize := int64(0)
oldProgress := 0.0
inc := func(_ string, f os.FileInfo, err error) error {
if err != nil {
return nil
}
totalSize += f.Size()
return nil
}
onFile := func(path string, f os.FileInfo, err error) error {
if err != nil {
comm.Logf("ignoring error %s", err.Error())
return nil
}
rel, err := filepath.Rel(src, path)
if err != nil {
return errors.Wrap(err, 0)
}
comm.Result(&mansion.FileMirroredResult{
Type: "entry",
Path: rel,
})
dstpath := filepath.Join(dst, rel)
mode := f.Mode()
switch {
case mode.IsDir():
err := dittoMkdir(dstpath)
if err != nil {
return errors.Wrap(err, 0)
}
case mode.IsRegular():
err := dittoReg(path, dstpath, os.FileMode(f.Mode()&archiver.LuckyMode|archiver.ModeMask))
if err != nil {
return errors.Wrap(err, 0)
}
case (mode&os.ModeSymlink > 0):
err := dittoSymlink(path, dstpath, f)
if err != nil {
return errors.Wrap(err, 0)
}
}
comm.Debug(rel)
doneSize += f.Size()
progress := float64(doneSize) / float64(totalSize)
if progress-oldProgress > 0.01 {
oldProgress = progress
comm.Progress(progress)
}
return nil
}
rootinfo, err := os.Lstat(src)
if err != nil {
return errors.Wrap(err, 0)
}
if rootinfo.IsDir() {
totalSize = 0
comm.Logf("Counting files in %s...", src)
filepath.Walk(src, inc)
comm.Logf("Mirroring...")
filepath.Walk(src, onFile)
} else {
totalSize = rootinfo.Size()
onFile(src, rootinfo, nil)
}
comm.EndProgress()
return nil
}
func dittoMkdir(dstpath string) error {
comm.Debugf("mkdir %s", dstpath)
err := archiver.Mkdir(dstpath)
if err != nil {
return errors.Wrap(err, 0)
}
return nil
}
func dittoReg(srcpath string, dstpath string, mode os.FileMode) error {
comm.Debugf("cp -f %s %s", srcpath, dstpath)
err := os.RemoveAll(dstpath)
if err != nil {
return errors.Wrap(err, 0)
}
writer, err := os.OpenFile(dstpath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
if err != nil {
return errors.Wrap(err, 0)
}
defer writer.Close()
reader, err := os.Open(srcpath)
if err != nil {
return errors.Wrap(err, 0)
}
defer reader.Close()
_, err = io.Copy(writer, reader)
if err != nil {
return errors.Wrap(err, 0)
}
err = os.Chmod(dstpath, mode)
if err != nil {
return errors.Wrap(err, 0)
}
return nil
}
func dittoSymlink(srcpath string, dstpath string, f os.FileInfo) error {
err := os.RemoveAll(dstpath)
if err != nil {
return errors.Wrap(err, 0)
}
linkname, err := os.Readlink(srcpath)
if err != nil {
return errors.Wrap(err, 0)
}
comm.Debugf("ln -s %s %s", linkname, dstpath)
err = os.Symlink(linkname, dstpath)
if err != nil {
return errors.Wrap(err, 0)
}
return nil
}