-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathlist.go
executable file
·335 lines (287 loc) · 11.8 KB
/
list.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright © 2017 Microsoft <wastore@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"context"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/Azure/azure-storage-azcopy/v10/common"
"github.com/Azure/azure-storage-azcopy/v10/ste"
)
type rawListCmdArgs struct {
// obtained from argument
sourcePath string
Properties string
MachineReadable bool
RunningTally bool
MegaUnits bool
trailingDot string
}
type validProperty string
const (
lastModifiedTime validProperty = "LastModifiedTime"
versionId validProperty = "VersionId"
blobType validProperty = "BlobType"
blobAccessTier validProperty = "BlobAccessTier"
contentType validProperty = "ContentType"
contentEncoding validProperty = "ContentEncoding"
contentMD5 validProperty = "ContentMD5"
leaseState validProperty = "LeaseState"
leaseDuration validProperty = "LeaseDuration"
leaseStatus validProperty = "LeaseStatus"
archiveStatus validProperty = "ArchiveStatus"
)
// validProperties returns an array of possible values for the validProperty const type.
func validProperties() []validProperty {
return []validProperty{lastModifiedTime, versionId, blobType, blobAccessTier,
contentType, contentEncoding, contentMD5, leaseState, leaseDuration, leaseStatus, archiveStatus}
}
func (raw *rawListCmdArgs) parseProperties(rawProperties string) []validProperty {
parsedProperties := make([]validProperty, 0)
listProperties := strings.Split(rawProperties, ";")
for _, p := range listProperties {
for _, vp := range validProperties() {
// check for empty string and also ignore the case
if len(p) != 0 && strings.EqualFold(string(vp), p) {
parsedProperties = append(parsedProperties, vp)
break
}
}
}
return parsedProperties
}
func (raw rawListCmdArgs) cook() (cookedListCmdArgs, error) {
cooked = cookedListCmdArgs{}
// the expected argument in input is the container sas / or path of virtual directory in the container.
// verifying the location type
location := InferArgumentLocation(raw.sourcePath)
// Only support listing for Azure locations
if location != location.Blob() && location != location.File() && location != location.BlobFS() {
return cooked, errors.New("invalid path passed for listing. given source is of type " + location.String() + " while expect is container / container path ")
}
cooked.sourcePath = raw.sourcePath
cooked.MachineReadable = raw.MachineReadable
cooked.RunningTally = raw.RunningTally
cooked.MegaUnits = raw.MegaUnits
cooked.location = location
err := cooked.trailingDot.Parse(raw.trailingDot)
if err != nil {
return cooked, err
}
if raw.Properties != "" {
cooked.properties = raw.parseProperties(raw.Properties)
}
return cooked, nil
}
type cookedListCmdArgs struct {
sourcePath string
location common.Location
properties []validProperty
MachineReadable bool
RunningTally bool
MegaUnits bool
trailingDot common.TrailingDotOption
}
var raw rawListCmdArgs
var cooked cookedListCmdArgs
func init() {
raw = rawListCmdArgs{}
// listContainerCmd represents the list container command
// listContainer list the blobs inside the container or virtual directory inside the container
listContainerCmd := &cobra.Command{
Use: "list [containerURL]",
Aliases: []string{"ls"},
Short: listCmdShortDescription,
Long: listCmdLongDescription,
Example: listCmdExample,
Args: func(cmd *cobra.Command, args []string) error {
// the listContainer command requires necessarily to have an argument
// If no argument is passed then it is not valid
// lsc expects the container path / virtual directory
if len(args) == 0 || len(args) > 2 {
return errors.New("this command only requires container destination")
}
raw.sourcePath = args[0]
return nil
},
Run: func(cmd *cobra.Command, args []string) {
cooked, err := raw.cook()
if err != nil {
glcm.Error("failed to parse user input due to error: " + err.Error())
return
}
err = cooked.HandleListContainerCommand()
if err == nil {
glcm.Exit(nil, common.EExitCode.Success())
} else {
glcm.Error(err.Error())
}
},
}
listContainerCmd.PersistentFlags().BoolVar(&raw.MachineReadable, "machine-readable", false, "Lists file sizes in bytes.")
listContainerCmd.PersistentFlags().BoolVar(&raw.RunningTally, "running-tally", false, "Counts the total number of files and their sizes.")
listContainerCmd.PersistentFlags().BoolVar(&raw.MegaUnits, "mega-units", false, "Displays units in orders of 1000, not 1024.")
listContainerCmd.PersistentFlags().StringVar(&raw.Properties, "properties", "", "delimiter (;) separated values of properties required in list output.")
listContainerCmd.PersistentFlags().StringVar(&raw.trailingDot, "trailing-dot", "", "'Enable' by default to treat file share related operations in a safe manner. Available options: Enable, Disable. "+
"Choose 'Disable' to go back to legacy (potentially unsafe) treatment of trailing dot files where the file service will trim any trailing dots in paths. This can result in potential data corruption if the transfer contains two paths that differ only by a trailing dot (ex: mypath and mypath.). If this flag is set to 'Disable' and AzCopy encounters a trailing dot file, it will warn customers in the scanning log but will not attempt to abort the operation."+
"If the destination does not support trailing dot files (Windows or Blob Storage), AzCopy will fail if the trailing dot file is the root of the transfer and skip any trailing dot paths encountered during enumeration.")
rootCmd.AddCommand(listContainerCmd)
}
func (cooked cookedListCmdArgs) processProperties(object StoredObject) string {
builder := strings.Builder{}
for _, property := range cooked.properties {
propertyStr := string(property)
switch property {
case lastModifiedTime:
builder.WriteString(propertyStr + ": " + object.lastModifiedTime.String() + "; ")
case versionId:
builder.WriteString(propertyStr + ": " + object.blobVersionID + "; ")
case blobType:
builder.WriteString(propertyStr + ": " + string(object.blobType) + "; ")
case blobAccessTier:
builder.WriteString(propertyStr + ": " + string(object.blobAccessTier) + "; ")
case contentType:
builder.WriteString(propertyStr + ": " + object.contentType + "; ")
case contentEncoding:
builder.WriteString(propertyStr + ": " + object.contentEncoding + "; ")
case contentMD5:
builder.WriteString(propertyStr + ": " + base64.StdEncoding.EncodeToString(object.md5) + "; ")
case leaseState:
builder.WriteString(propertyStr + ": " + string(object.leaseState) + "; ")
case leaseStatus:
builder.WriteString(propertyStr + ": " + string(object.leaseStatus) + "; ")
case leaseDuration:
builder.WriteString(propertyStr + ": " + string(object.leaseDuration) + "; ")
case archiveStatus:
builder.WriteString(propertyStr + ": " + string(object.archiveStatus) + "; ")
}
}
return builder.String()
}
// HandleListContainerCommand handles the list container command
func (cooked cookedListCmdArgs) HandleListContainerCommand() (err error) {
// TODO: Temporarily use context.TODO(), this should be replaced with a root context from main.
ctx := context.WithValue(context.TODO(), ste.ServiceAPIVersionOverride, ste.DefaultServiceApiVersion)
credentialInfo := common.CredentialInfo{}
source, err := SplitResourceString(cooked.sourcePath, cooked.location)
if err != nil {
return err
}
if err := common.VerifyIsURLResolvable(raw.sourcePath); cooked.location.IsRemote() && err != nil {
return fmt.Errorf("failed to resolve target: %w", err)
}
level, err := DetermineLocationLevel(source.Value, cooked.location, true)
if err != nil {
return err
}
// isSource is rather misnomer for canBePublic. We can list public containers, and hence isSource=true
if credentialInfo, _, err = GetCredentialInfoForLocation(ctx, cooked.location, source.Value, source.SAS, true, common.CpkOptions{}); err != nil {
return fmt.Errorf("failed to obtain credential info: %s", err.Error())
} else if cooked.location == cooked.location.File() && source.SAS == "" {
return errors.New("azure files requires a SAS token for authentication")
} else if credentialInfo.CredentialType.IsAzureOAuth() {
uotm := GetUserOAuthTokenManagerInstance()
if tokenInfo, err := uotm.GetTokenInfo(ctx); err != nil {
return err
} else {
credentialInfo.OAuthTokenInfo = *tokenInfo
}
}
traverser, err := InitResourceTraverser(source, cooked.location, &ctx, &credentialInfo, common.ESymlinkHandlingType.Skip(), nil, true, true, false, common.EPermanentDeleteOption.None(), func(common.EntityType) {}, nil, false, common.ESyncHashType.None(), common.EPreservePermissionsOption.None(), common.LogNone, common.CpkOptions{}, nil, false, cooked.trailingDot, nil, nil)
if err != nil {
return fmt.Errorf("failed to initialize traverser: %s", err.Error())
}
var fileCount int64 = 0
var sizeCount int64 = 0
processor := func(object StoredObject) error {
path := object.relativePath
if object.entityType == common.EEntityType.Folder() {
path += "/" // TODO: reviewer: same questions as for jobs status: OK to hard code direction of slash? OK to use trailing slash to distinguish dirs from files?
}
properties := "; " + cooked.processProperties(object)
objectSummary := path + properties + " Content Length: "
if level == level.Service() {
objectSummary = object.ContainerName + "/" + objectSummary
}
if cooked.MachineReadable {
objectSummary += strconv.Itoa(int(object.size))
} else {
objectSummary += byteSizeToString(object.size)
}
if cooked.RunningTally {
fileCount++
sizeCount += object.size
}
glcm.Info(objectSummary)
// No need to strip away from the name as the traverser has already done so.
return nil
}
err = traverser.Traverse(nil, processor, nil)
if err != nil {
return fmt.Errorf("failed to traverse container: %s", err.Error())
}
if cooked.RunningTally {
glcm.Info("")
glcm.Info("File count: " + strconv.Itoa(int(fileCount)))
if cooked.MachineReadable {
glcm.Info("Total file size: " + strconv.Itoa(int(sizeCount)))
} else {
glcm.Info("Total file size: " + byteSizeToString(sizeCount))
}
}
return nil
}
var megaSize = []string{
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
}
func byteSizeToString(size int64) string {
units := []string{
"B",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB", // Let's face it, a file, account, or container probably won't be more than 1000 exabytes in YEARS.
// (and int64 literally isn't large enough to handle too many exbibytes. 128 bit processors when)
}
unit := 0
floatSize := float64(size)
gigSize := 1024
if cooked.MegaUnits {
gigSize = 1000
units = megaSize
}
for floatSize/float64(gigSize) >= 1 {
unit++
floatSize /= float64(gigSize)
}
return strconv.FormatFloat(floatSize, 'f', 2, 64) + " " + units[unit]
}