-
Notifications
You must be signed in to change notification settings - Fork 239
/
jobargument.go
190 lines (161 loc) · 3.86 KB
/
jobargument.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 core
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/peak/s5cmd/opt"
"github.com/peak/s5cmd/stats"
"github.com/peak/s5cmd/url"
)
var (
// ErrObjectExistsButOk is used when a destination object already exists and opt.IfNotExists is set.
ErrObjectExistsButOk = NewAcceptableError("Object already exists")
// ErrObjectIsNewerButOk is used when a destination object is newer than the source and opt.IfSourceNewer is set.
ErrObjectIsNewerButOk = NewAcceptableError("Object is newer or same age")
// ErrObjectSizesMatchButOk is used when a destination object size matches the source and opt.IfSizeDiffers is set.
ErrObjectSizesMatchButOk = NewAcceptableError("Object size matches")
// ErrDisplayedHelp is used when a command is invoked with "-h"
ErrDisplayedHelp = NewAcceptableError("Displayed help for command")
)
// JobArgument is an argument of the job. Can be a file/directory, an s3 url ("s3" is set in this case) or an arbitrary string.
type JobArgument struct {
arg string
s3 *url.S3Url
filled bool
exists bool
size int64
modTime time.Time
}
func NewJobArgument(arg string, s3 *url.S3Url) *JobArgument {
return &JobArgument{arg: arg, s3: s3}
}
// Clone duplicates a JobArgument and returns a pointer to a new one
func (a *JobArgument) Clone() *JobArgument {
var s url.S3Url
if a.s3 != nil {
s = a.s3.Clone()
}
return NewJobArgument(a.arg, &s)
}
// StripS3 strips the S3 data from JobArgument and returns a new one
func (a *JobArgument) StripS3() *JobArgument {
return NewJobArgument(a.arg, nil)
}
// Append appends a string to a JobArgument and returns itself
func (a *JobArgument) Append(s string, isS3path bool) *JobArgument {
if a.s3 != nil && !isS3path {
// a is an S3 object but s is not
s = strings.Replace(s, string(filepath.Separator), "/", -1)
}
if a.s3 == nil && isS3path {
// a is a not an S3 object but s is
s = strings.Replace(s, "/", string(filepath.Separator), -1)
}
if a.s3 != nil {
if a.s3.Key == "" {
a.arg += "/" + s
} else {
a.arg += s
}
a.s3.Key += s
} else {
a.arg += s
}
return a
}
func (a *JobArgument) CheckConditionals(wp *WorkerParams, src *JobArgument, opts opt.OptionList) (ret error) {
if opts.Has(opt.IfNotExists) {
ex, err := a.Exists(wp)
if err != nil {
return err
}
if ex {
ret = ErrObjectExistsButOk
} else {
ret = nil
}
}
if opts.Has(opt.IfSizeDiffers) {
sDest, err := a.Size(wp)
if err != nil {
return err
}
sSrc, err := src.Size(wp)
if err != nil {
return err
}
if sDest == sSrc {
ret = ErrObjectSizesMatchButOk
} else {
ret = nil
}
}
if opts.Has(opt.IfSourceNewer) {
tDest, err := a.ModTime(wp)
if err != nil {
return err
}
tSrc, err := src.ModTime(wp)
if err != nil {
return err
}
if !tSrc.After(tDest) {
ret = ErrObjectIsNewerButOk
} else {
ret = nil
}
}
return ret
}
func (a *JobArgument) fillData(wp *WorkerParams) error {
if a.filled {
return nil
}
if a.s3 == nil {
st, err := os.Stat(a.arg)
if err != nil {
if os.IsNotExist(err) {
a.filled = true
a.exists = false
return nil
}
// error
return err
} else {
a.filled = true
a.exists = true
a.size = st.Size()
a.modTime = st.ModTime()
return nil
}
}
h, err := s3head(wp.s3svc, a.s3)
wp.st.IncrementIfSuccess(stats.S3Op, err)
if err != nil {
a.filled = true
a.exists = false
return nil
}
a.filled = true
a.exists = true
if h.LastModified != nil {
a.modTime = *(h.LastModified)
}
if h.ContentLength != nil {
a.size = *(h.ContentLength)
}
return nil
}
func (a *JobArgument) Size(wp *WorkerParams) (int64, error) {
err := a.fillData(wp)
return a.size, err
}
func (a *JobArgument) Exists(wp *WorkerParams) (bool, error) {
err := a.fillData(wp)
return a.exists, err
}
func (a *JobArgument) ModTime(wp *WorkerParams) (time.Time, error) {
err := a.fillData(wp)
return a.modTime, err
}