-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcp.go
190 lines (156 loc) · 4.64 KB
/
cp.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
package cp
import (
"io"
"os"
"path/filepath"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/go-errors/errors"
"github.com/itchio/butler/cmd/dl"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
"github.com/itchio/httpkit/httpfile"
"github.com/itchio/httpkit/retrycontext"
"github.com/itchio/wharf/counter"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/state"
)
type OnCopyStart func(initialProgress float64, totalBytes int64)
type OnCopyStop func()
type CopyParams struct {
OnStart OnCopyStart
OnStop OnCopyStop
Consumer *state.Consumer
}
var args = struct {
src *string
dest *string
resume *bool
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("cp", "Copy src to dest").Hidden()
args.src = cmd.Arg("src", "File to read from").Required().String()
args.dest = cmd.Arg("dest", "File to write to").Required().String()
args.resume = cmd.Flag("resume", "Try to resume if dest is partially written (doesn't check existing data)").Bool()
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
params := &CopyParams{
OnStart: func(initialProgress float64, totalBytes int64) {
comm.Progress(initialProgress)
comm.StartProgressWithTotalBytes(totalBytes)
},
OnStop: func() {
comm.EndProgress()
},
Consumer: comm.NewStateConsumer(),
}
ctx.Must(Do(ctx, params, *args.src, *args.dest, *args.resume))
}
func Do(ctx *mansion.Context, params *CopyParams, srcPath string, destPath string, resume bool) error {
retryCtx := retrycontext.NewDefault()
retryCtx.Settings.Consumer = comm.NewStateConsumer()
for retryCtx.ShouldTry() {
err := Try(ctx, params, srcPath, destPath, resume)
if err != nil {
if dl.IsIntegrityError(err) {
retryCtx.Retry(err.Error())
continue
}
// if it's not an integrity error, just bubble it up
return err
}
return nil
}
return errors.New("cp: too many errors, giving up")
}
func Try(ctx *mansion.Context, params *CopyParams, srcPath string, destPath string, resume bool) error {
consumer := params.Consumer
src, err := eos.Open(srcPath)
if err != nil {
return err
}
defer src.Close()
dir := filepath.Dir(destPath)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
flags := os.O_CREATE | os.O_WRONLY
dest, err := os.OpenFile(destPath, flags, 0644)
if err != nil {
return err
}
defer dest.Close()
stats, err := src.Stat()
if err != nil {
return err
}
totalBytes := int64(stats.Size())
var startOffset int64
var copiedBytes int64
start := time.Now()
err = func() error {
if resume {
startOffset, err = dest.Seek(0, io.SeekEnd)
if err != nil {
return err
}
if startOffset == 0 {
consumer.Infof("Downloading %s", humanize.IBytes(uint64(totalBytes)))
} else if startOffset > totalBytes {
consumer.Warnf("Existing data too big (%s > %s), starting over", humanize.IBytes(uint64(startOffset)), humanize.IBytes(uint64(totalBytes)))
startOffset, err = dest.Seek(0, io.SeekStart)
if err != nil {
return err
}
} else if startOffset == totalBytes {
consumer.Infof("All %s already there", humanize.IBytes(uint64(totalBytes)))
return nil
}
consumer.Infof("Resuming at %s / %s", humanize.IBytes(uint64(startOffset)), humanize.IBytes(uint64(totalBytes)))
_, err = src.Seek(startOffset, io.SeekStart)
if err != nil {
return err
}
} else {
consumer.Infof("Downloading %s", humanize.IBytes(uint64(totalBytes)))
}
initialProgress := float64(startOffset) / float64(totalBytes)
params.OnStart(initialProgress, totalBytes)
cw := counter.NewWriterCallback(func(count int64) {
alpha := float64(startOffset+count) / float64(totalBytes)
consumer.Progress(alpha)
}, dest)
copiedBytes, err = io.Copy(cw, src)
if err != nil {
return err
}
params.OnStop()
return os.Truncate(destPath, totalBytes)
}()
if err != nil {
return err
}
if hf, ok := src.(*httpfile.HTTPFile); ok {
header := hf.GetHeader()
if header != nil {
err = dl.CheckIntegrity(comm.NewStateConsumer(), header, totalBytes, destPath)
if err != nil {
comm.Log("Integrity checks failed, truncating")
os.Truncate(destPath, 0)
return errors.Wrap(err, 1)
}
} else {
comm.Debugf("Not performing integrity checks (no header)")
}
} else {
comm.Debugf("Not performing integrity checks (not an HTTP resource)")
}
totalDuration := time.Since(start)
prettyStartOffset := humanize.IBytes(uint64(startOffset))
prettySize := humanize.IBytes(uint64(copiedBytes))
perSecond := humanize.IBytes(uint64(float64(totalBytes-startOffset) / totalDuration.Seconds()))
comm.Statf("%s + %s copied @ %s/s\n", prettyStartOffset, prettySize, perSecond)
return nil
}