2018-10-26 13:50:55 +00:00
|
|
|
package operations
|
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2018-10-26 13:50:55 +00:00
|
|
|
"path"
|
2020-09-23 16:20:28 +00:00
|
|
|
"strings"
|
2018-10-26 13:50:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/backend/crypt"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2020-04-28 17:44:04 +00:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2018-10-26 13:50:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListJSONItem in the struct which gets marshalled for each line
|
|
|
|
type ListJSONItem struct {
|
2019-04-15 17:12:09 +00:00
|
|
|
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"`
|
2019-03-20 12:45:06 +00:00
|
|
|
Tier string `json:",omitempty"`
|
2019-06-07 16:28:15 +00:00
|
|
|
IsBucket bool `json:",omitempty"`
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 11:01:52 +00:00
|
|
|
// Timestamp a time in the provided format
|
|
|
|
type Timestamp struct {
|
|
|
|
When time.Time
|
|
|
|
Format string
|
|
|
|
}
|
2018-10-26 13:50:55 +00:00
|
|
|
|
|
|
|
// MarshalJSON turns a Timestamp into JSON
|
|
|
|
func (t Timestamp) MarshalJSON() (out []byte, err error) {
|
2019-02-03 11:01:52 +00:00
|
|
|
if t.When.IsZero() {
|
2018-10-26 13:50:55 +00:00
|
|
|
return []byte(`""`), nil
|
|
|
|
}
|
2019-02-03 11:01:52 +00:00
|
|
|
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
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListJSONOpt describes the options for ListJSON
|
|
|
|
type ListJSONOpt struct {
|
2020-04-28 17:44:04 +00:00
|
|
|
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"`
|
2020-10-13 21:49:58 +00:00
|
|
|
HashTypes []string `json:"hashTypes"` // hash types to show if ShowHash is set, e.g. "MD5", "SHA-1"
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 16:20:28 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-10-26 13:50:55 +00:00
|
|
|
if opt.ShowEncrypted {
|
|
|
|
fsInfo, _, _, config, err := fs.ConfigFs(fsrc.Name() + ":" + fsrc.Root())
|
|
|
|
if err != nil {
|
2020-09-23 16:20:28 +00:00
|
|
|
return nil, errors.Wrap(err, "ListJSON failed to load config for crypt remote")
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
if fsInfo.Name != "crypt" {
|
2020-09-23 16:20:28 +00:00
|
|
|
return nil, errors.New("The remote needs to be of type \"crypt\"")
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
lj.cipher, err = crypt.NewCipher(config)
|
2018-10-26 13:50:55 +00:00
|
|
|
if err != nil {
|
2020-09-23 16:20:28 +00:00
|
|
|
return nil, errors.Wrap(err, "ListJSON failed to make new crypt remote")
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 16:28:15 +00:00
|
|
|
features := fsrc.Features()
|
2020-09-23 16:20:28 +00:00
|
|
|
lj.canGetTier = features.GetTier
|
|
|
|
lj.format = formatForPrecision(fsrc.Precision())
|
2021-11-04 11:50:43 +00:00
|
|
|
lj.isBucket = features.BucketBased && remote == "" && fsrc.Root() == "" // if bucket-based remote listing the root mark directories as buckets
|
2020-09-23 16:20:28 +00:00
|
|
|
lj.showHash = opt.ShowHash
|
|
|
|
lj.hashTypes = fsrc.Hashes().Array()
|
2020-04-28 17:44:04 +00:00
|
|
|
if len(opt.HashTypes) != 0 {
|
2020-09-23 16:20:28 +00:00
|
|
|
lj.showHash = true
|
|
|
|
lj.hashTypes = []hash.Type{}
|
2020-04-28 17:44:04 +00:00
|
|
|
for _, hashType := range opt.HashTypes {
|
|
|
|
var ht hash.Type
|
|
|
|
err := ht.Set(hashType)
|
|
|
|
if err != nil {
|
2020-09-23 16:20:28 +00:00
|
|
|
return nil, err
|
2020-04-28 17:44:04 +00:00
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
lj.hashTypes = append(lj.hashTypes, ht)
|
2020-04-28 17:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
return lj, nil
|
|
|
|
}
|
2019-04-05 20:24:09 +00:00
|
|
|
|
2020-09-23 16:20:28 +00:00
|
|
|
// 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 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
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
}
|
|
|
|
if lj.canGetTier {
|
|
|
|
if do, ok := x.(fs.GetTierer); ok {
|
|
|
|
item.Tier = do.GetTier()
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
}
|
|
|
|
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 errors.Wrap(err, "creating entry failed in ListJSON")
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
if item != nil {
|
|
|
|
err = callback(item)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "callback failed in ListJSON")
|
2018-10-26 13:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error in ListJSON")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-23 16:20:28 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-10-16 09:17:41 +00:00
|
|
|
|
|
|
|
// 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()))
|
|
|
|
}
|
|
|
|
|
2020-09-23 16:20:28 +00:00
|
|
|
// Could be a file or a directory here
|
|
|
|
if lj.files {
|
|
|
|
// 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
|
|
|
|
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)
|
|
|
|
}
|