forked from rclone/rclone
/
lsjson.go
345 lines (330 loc) · 9.41 KB
/
lsjson.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
336
337
338
339
340
341
342
343
344
345
package operations
import (
"context"
"errors"
"fmt"
"path"
"strings"
"time"
"github.com/artpar/rclone/backend/crypt"
"github.com/artpar/rclone/fs"
"github.com/artpar/rclone/fs/hash"
"github.com/artpar/rclone/fs/walk"
)
// ListJSONItem in the struct which gets marshalled for each line
type ListJSONItem struct {
Path string
Name string
EncryptedPath string `json:",omitempty"`
Encrypted string `json:",omitempty"`
Size int64
MimeType string `json:",omitempty"`
ModTime Timestamp //`json:",omitempty"`
IsDir bool
Hashes map[string]string `json:",omitempty"`
ID string `json:",omitempty"`
OrigID string `json:",omitempty"`
Tier string `json:",omitempty"`
IsBucket bool `json:",omitempty"`
Metadata fs.Metadata `json:",omitempty"`
}
// Timestamp a time in the provided format
type Timestamp struct {
When time.Time
Format string
}
// MarshalJSON turns a Timestamp into JSON
func (t Timestamp) MarshalJSON() (out []byte, err error) {
if t.When.IsZero() {
return []byte(`""`), nil
}
return []byte(`"` + t.When.Format(t.Format) + `"`), nil
}
// Returns a time format for the given precision
func formatForPrecision(precision time.Duration) string {
switch {
case precision <= time.Nanosecond:
return "2006-01-02T15:04:05.000000000Z07:00"
case precision <= 10*time.Nanosecond:
return "2006-01-02T15:04:05.00000000Z07:00"
case precision <= 100*time.Nanosecond:
return "2006-01-02T15:04:05.0000000Z07:00"
case precision <= time.Microsecond:
return "2006-01-02T15:04:05.000000Z07:00"
case precision <= 10*time.Microsecond:
return "2006-01-02T15:04:05.00000Z07:00"
case precision <= 100*time.Microsecond:
return "2006-01-02T15:04:05.0000Z07:00"
case precision <= time.Millisecond:
return "2006-01-02T15:04:05.000Z07:00"
case precision <= 10*time.Millisecond:
return "2006-01-02T15:04:05.00Z07:00"
case precision <= 100*time.Millisecond:
return "2006-01-02T15:04:05.0Z07:00"
}
return time.RFC3339
}
// ListJSONOpt describes the options for ListJSON
type ListJSONOpt struct {
Recurse bool `json:"recurse"`
NoModTime bool `json:"noModTime"`
NoMimeType bool `json:"noMimeType"`
ShowEncrypted bool `json:"showEncrypted"`
ShowOrigIDs bool `json:"showOrigIDs"`
ShowHash bool `json:"showHash"`
DirsOnly bool `json:"dirsOnly"`
FilesOnly bool `json:"filesOnly"`
Metadata bool `json:"metadata"`
HashTypes []string `json:"hashTypes"` // hash types to show if ShowHash is set, e.g. "MD5", "SHA-1"
}
// state for ListJson
type listJSON struct {
fsrc fs.Fs
remote string
format string
opt *ListJSONOpt
cipher *crypt.Cipher
hashTypes []hash.Type
dirs bool
files bool
canGetTier bool
isBucket bool
showHash bool
}
func newListJSON(ctx context.Context, fsrc fs.Fs, remote string, opt *ListJSONOpt) (*listJSON, error) {
lj := &listJSON{
fsrc: fsrc,
remote: remote,
opt: opt,
dirs: true,
files: true,
}
// Dirs Files
// !FilesOnly,!DirsOnly true true
// !FilesOnly,DirsOnly true false
// FilesOnly,!DirsOnly false true
// FilesOnly,DirsOnly true true
if !opt.FilesOnly && opt.DirsOnly {
lj.files = false
} else if opt.FilesOnly && !opt.DirsOnly {
lj.dirs = false
}
if opt.ShowEncrypted {
fsInfo, _, _, config, err := fs.ConfigFs(fsrc.Name() + ":" + fsrc.Root())
if err != nil {
return nil, fmt.Errorf("ListJSON failed to load config for crypt remote: %w", err)
}
if fsInfo.Name != "crypt" {
return nil, errors.New("the remote needs to be of type \"crypt\"")
}
lj.cipher, err = crypt.NewCipher(config)
if err != nil {
return nil, fmt.Errorf("ListJSON failed to make new crypt remote: %w", err)
}
}
features := fsrc.Features()
lj.canGetTier = features.GetTier
lj.format = formatForPrecision(fsrc.Precision())
lj.isBucket = features.BucketBased && remote == "" && fsrc.Root() == "" // if bucket-based remote listing the root mark directories as buckets
lj.showHash = opt.ShowHash
lj.hashTypes = fsrc.Hashes().Array()
if len(opt.HashTypes) != 0 {
lj.showHash = true
lj.hashTypes = []hash.Type{}
for _, hashType := range opt.HashTypes {
var ht hash.Type
err := ht.Set(hashType)
if err != nil {
return nil, err
}
lj.hashTypes = append(lj.hashTypes, ht)
}
}
return lj, nil
}
// Convert a single entry to JSON
//
// It may return nil if there is no entry to return
func (lj *listJSON) entry(ctx context.Context, entry fs.DirEntry) (*ListJSONItem, error) {
switch entry.(type) {
case fs.Directory:
if lj.opt.FilesOnly {
return nil, nil
}
case fs.Object:
if lj.opt.DirsOnly {
return nil, nil
}
default:
fs.Errorf(nil, "Unknown type %T in listing", entry)
}
item := &ListJSONItem{
Path: entry.Remote(),
Name: path.Base(entry.Remote()),
Size: entry.Size(),
}
if entry.Remote() == "" {
item.Name = ""
}
if !lj.opt.NoModTime {
item.ModTime = Timestamp{When: entry.ModTime(ctx), Format: lj.format}
}
if !lj.opt.NoMimeType {
item.MimeType = fs.MimeTypeDirEntry(ctx, entry)
}
if lj.cipher != nil {
switch entry.(type) {
case fs.Directory:
item.EncryptedPath = lj.cipher.EncryptDirName(entry.Remote())
case fs.Object:
item.EncryptedPath = lj.cipher.EncryptFileName(entry.Remote())
default:
fs.Errorf(nil, "Unknown type %T in listing", entry)
}
item.Encrypted = path.Base(item.EncryptedPath)
}
if lj.opt.Metadata {
metadata, err := fs.GetMetadata(ctx, entry)
if err != nil {
fs.Errorf(entry, "Failed to read metadata: %v", err)
} else if metadata != nil {
item.Metadata = metadata
}
}
if do, ok := entry.(fs.IDer); ok {
item.ID = do.ID()
}
if o, ok := entry.(fs.Object); lj.opt.ShowOrigIDs && ok {
if do, ok := fs.UnWrapObject(o).(fs.IDer); ok {
item.OrigID = do.ID()
}
}
switch x := entry.(type) {
case fs.Directory:
item.IsDir = true
item.IsBucket = lj.isBucket
case fs.Object:
item.IsDir = false
if lj.showHash {
item.Hashes = make(map[string]string)
for _, hashType := range lj.hashTypes {
hash, err := x.Hash(ctx, hashType)
if err != nil {
fs.Errorf(x, "Failed to read hash: %v", err)
} else if hash != "" {
item.Hashes[hashType.String()] = hash
}
}
}
if lj.canGetTier {
if do, ok := x.(fs.GetTierer); ok {
item.Tier = do.GetTier()
}
}
default:
fs.Errorf(nil, "Unknown type %T in listing in ListJSON", entry)
}
return item, nil
}
// ListJSON lists fsrc using the options in opt calling callback for each item
func ListJSON(ctx context.Context, fsrc fs.Fs, remote string, opt *ListJSONOpt, callback func(*ListJSONItem) error) error {
lj, err := newListJSON(ctx, fsrc, remote, opt)
if err != nil {
return err
}
err = walk.ListR(ctx, fsrc, remote, false, ConfigMaxDepth(ctx, lj.opt.Recurse), walk.ListAll, func(entries fs.DirEntries) (err error) {
for _, entry := range entries {
item, err := lj.entry(ctx, entry)
if err != nil {
return fmt.Errorf("creating entry failed in ListJSON: %w", err)
}
if item != nil {
err = callback(item)
if err != nil {
return fmt.Errorf("callback failed in ListJSON: %w", err)
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("error in ListJSON: %w", err)
}
return nil
}
// StatJSON returns a single JSON stat entry for the fsrc, remote path
//
// The item returned may be nil if it is not found or excluded with DirsOnly/FilesOnly
func StatJSON(ctx context.Context, fsrc fs.Fs, remote string, opt *ListJSONOpt) (item *ListJSONItem, err error) {
// FIXME this could me more efficient we had a new primitive
// NewDirEntry() which returned an Object or a Directory
lj, err := newListJSON(ctx, fsrc, remote, opt)
if err != nil {
return nil, err
}
// Root is always a directory. When we have a NewDirEntry
// primitive we need to call it, but for now this will do.
if remote == "" {
if !lj.dirs {
return nil, nil
}
// Check the root directory exists
_, err := fsrc.List(ctx, "")
if err != nil {
return nil, err
}
return lj.entry(ctx, fs.NewDir("", time.Now()))
}
// Could be a file or a directory here
if lj.files && !strings.HasSuffix(remote, "/") {
// NewObject can return the sentinel errors ErrorObjectNotFound or ErrorIsDir
// ErrorObjectNotFound can mean the source is a directory or not found
obj, err := fsrc.NewObject(ctx, remote)
if err == fs.ErrorObjectNotFound {
if !lj.dirs {
return nil, nil
}
} else if err == fs.ErrorIsDir {
if !lj.dirs {
return nil, nil
}
// This could return a made up ListJSONItem here
// but that wouldn't have the IDs etc in
} else if err != nil {
if !lj.dirs {
return nil, err
}
} else {
return lj.entry(ctx, obj)
}
}
// Must be a directory here
//
// Remove trailing / as rclone listings won't have them
remote = strings.TrimRight(remote, "/")
parent := path.Dir(remote)
if parent == "." || parent == "/" {
parent = ""
}
entries, err := fsrc.List(ctx, parent)
if err == fs.ErrorDirNotFound {
return nil, nil
} else if err != nil {
return nil, err
}
equal := func(a, b string) bool { return a == b }
if fsrc.Features().CaseInsensitive {
equal = strings.EqualFold
}
var foundEntry fs.DirEntry
for _, entry := range entries {
if equal(entry.Remote(), remote) {
foundEntry = entry
break
}
}
if foundEntry == nil {
return nil, nil
}
return lj.entry(ctx, foundEntry)
}