2022-08-28 13:21:57 +02:00
|
|
|
// Package tree provides the tree command.
|
2017-07-24 13:39:39 +02:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2017-07-24 13:39:39 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-08-01 15:46:21 +02:00
|
|
|
"path/filepath"
|
2017-07-24 13:39:39 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/a8m/tree"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 17:55:04 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/dirtree"
|
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2023-02-08 18:17:55 +01:00
|
|
|
"github.com/rclone/rclone/lib/encoder"
|
2023-03-01 23:27:41 +01:00
|
|
|
"github.com/rclone/rclone/lib/terminal"
|
2017-07-24 13:39:39 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
opts tree.Options
|
|
|
|
outFileName string
|
|
|
|
noReport bool
|
|
|
|
sort string
|
2023-02-08 18:17:55 +01:00
|
|
|
enc = encoder.OS
|
2017-07-24 13:39:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 17:58:11 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 17:55:04 +02:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2017-07-24 13:39:39 +02:00
|
|
|
// List
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.BoolVarP(cmdFlags, &opts.All, "all", "a", false, "All files are listed (list . files too)", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.DirsOnly, "dirs-only", "d", false, "List directories only", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.FullPath, "full-path", "", false, "Print the full path prefix for each file", "")
|
|
|
|
//flags.BoolVarP(cmdFlags, &opts.IgnoreCase, "ignore-case", "", false, "Ignore case when pattern matching", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &noReport, "noreport", "", false, "Turn off file/directory count at end of tree listing", "")
|
|
|
|
// flags.BoolVarP(cmdFlags, &opts.FollowLink, "follow", "l", false, "Follow symbolic links like directories","")
|
|
|
|
flags.IntVarP(cmdFlags, &opts.DeepLevel, "level", "", 0, "Descend only level directories deep", "")
|
2021-08-16 11:30:01 +02:00
|
|
|
// flags.StringVarP(cmdFlags, &opts.Pattern, "pattern", "P", "", "List only those files that match the pattern given")
|
|
|
|
// flags.StringVarP(cmdFlags, &opts.IPattern, "exclude", "", "", "Do not list files that match the given pattern")
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.StringVarP(cmdFlags, &outFileName, "output", "o", "", "Output to file instead of stdout", "")
|
2017-07-24 13:39:39 +02:00
|
|
|
// Files
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.BoolVarP(cmdFlags, &opts.ByteSize, "size", "s", false, "Print the size in bytes of each file.", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.FileMode, "protections", "p", false, "Print the protections for each file.", "")
|
2019-10-11 17:55:04 +02:00
|
|
|
// flags.BoolVarP(cmdFlags, &opts.ShowUid, "uid", "", false, "Displays file owner or UID number.")
|
|
|
|
// flags.BoolVarP(cmdFlags, &opts.ShowGid, "gid", "", false, "Displays file group owner or GID number.")
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.BoolVarP(cmdFlags, &opts.Quotes, "quote", "Q", false, "Quote filenames with double quotes.", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.LastMod, "modtime", "D", false, "Print the date of last modification.", "")
|
2019-10-11 17:55:04 +02:00
|
|
|
// flags.BoolVarP(cmdFlags, &opts.Inodes, "inodes", "", false, "Print inode number of each file.")
|
|
|
|
// flags.BoolVarP(cmdFlags, &opts.Device, "device", "", false, "Print device ID number to which each file belongs.")
|
2017-07-24 13:39:39 +02:00
|
|
|
// Sort
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.BoolVarP(cmdFlags, &opts.NoSort, "unsorted", "U", false, "Leave files unsorted", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.VerSort, "version", "", false, "Sort files alphanumerically by version", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.ModSort, "sort-modtime", "t", false, "Sort files by last modification time", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.CTimeSort, "sort-ctime", "", false, "Sort files by last status change time", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.ReverSort, "sort-reverse", "r", false, "Reverse the order of the sort", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &opts.DirSort, "dirsfirst", "", false, "List directories before files (-U disables)", "")
|
|
|
|
flags.StringVarP(cmdFlags, &sort, "sort", "", "", "Select sort: name,version,size,mtime,ctime", "")
|
2017-07-24 13:39:39 +02:00
|
|
|
// Graphics
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.BoolVarP(cmdFlags, &opts.NoIndent, "noindent", "", false, "Don't print indentation lines", "")
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:58:11 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-07-24 13:39:39 +02:00
|
|
|
Use: "tree remote:path",
|
|
|
|
Short: `List the contents of the remote in a tree like fashion.`,
|
2024-08-12 18:17:46 +02:00
|
|
|
Long: `Lists the contents of a remote in a similar way to the unix tree command.
|
2017-07-24 13:39:39 +02:00
|
|
|
|
|
|
|
For example
|
|
|
|
|
|
|
|
$ rclone tree remote:path
|
|
|
|
/
|
|
|
|
├── file1
|
|
|
|
├── file2
|
|
|
|
├── file3
|
|
|
|
└── subdir
|
|
|
|
├── file4
|
|
|
|
└── file5
|
2021-11-04 12:50:43 +01:00
|
|
|
|
2017-07-24 13:39:39 +02:00
|
|
|
1 directories, 5 files
|
|
|
|
|
2020-10-13 23:49:58 +02:00
|
|
|
You can use any of the filtering options with the tree command (e.g.
|
2022-06-19 15:51:37 +02:00
|
|
|
` + "`--include` and `--exclude`" + `. You can also use ` + "`--fast-list`" + `.
|
2017-07-24 13:39:39 +02:00
|
|
|
|
|
|
|
The tree command has many options for controlling the listing which
|
2022-06-19 15:51:37 +02:00
|
|
|
are compatible with the tree command, for example you can include file
|
|
|
|
sizes with ` + "`--size`" + `. Note that not all of them have
|
2017-07-24 13:39:39 +02:00
|
|
|
short options as they conflict with rclone's short options.
|
2022-06-19 15:51:37 +02:00
|
|
|
|
|
|
|
For a more interactive navigation of the remote see the
|
|
|
|
[ncdu](/commands/rclone_ncdu/) command.
|
2017-07-24 13:39:39 +02:00
|
|
|
`,
|
2022-11-26 23:40:49 +01:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.38",
|
2023-07-10 19:34:10 +02:00
|
|
|
"groups": "Filter,Listing",
|
2022-11-26 23:40:49 +01:00
|
|
|
},
|
2018-01-12 17:30:54 +01:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2017-07-24 13:39:39 +02:00
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2023-03-01 23:27:41 +01:00
|
|
|
ci := fs.GetConfig(context.Background())
|
|
|
|
var outFile io.Writer
|
2017-07-24 13:39:39 +02:00
|
|
|
if outFileName != "" {
|
|
|
|
var err error
|
|
|
|
outFile, err = os.Create(outFileName)
|
|
|
|
if err != nil {
|
2022-06-08 22:54:39 +02:00
|
|
|
return fmt.Errorf("failed to create output file: %w", err)
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
2023-03-01 23:27:41 +01:00
|
|
|
opts.Colorize = false
|
|
|
|
} else {
|
|
|
|
terminal.Start()
|
|
|
|
outFile = terminal.Out
|
|
|
|
opts.Colorize = true
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
opts.VerSort = opts.VerSort || sort == "version"
|
|
|
|
opts.ModSort = opts.ModSort || sort == "mtime"
|
|
|
|
opts.CTimeSort = opts.CTimeSort || sort == "ctime"
|
|
|
|
opts.NameSort = sort == "name"
|
|
|
|
opts.SizeSort = sort == "size"
|
2021-04-03 13:56:15 +02:00
|
|
|
opts.UnitSize = ci.HumanReadable
|
2017-07-24 13:39:39 +02:00
|
|
|
if opts.DeepLevel == 0 {
|
2020-11-05 12:33:32 +01:00
|
|
|
opts.DeepLevel = ci.MaxDepth
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
return Tree(fsrc, outFile, &opts)
|
|
|
|
})
|
2018-01-12 17:30:54 +01:00
|
|
|
return nil
|
2017-07-24 13:39:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tree lists fsrc to outFile using the Options passed in
|
|
|
|
func Tree(fsrc fs.Fs, outFile io.Writer, opts *tree.Options) error {
|
2019-06-17 10:34:30 +02:00
|
|
|
dirs, err := walk.NewDirTree(context.Background(), fsrc, "", false, opts.DeepLevel)
|
2017-07-24 13:39:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
opts.Fs = NewFs(dirs)
|
|
|
|
opts.OutFile = outFile
|
|
|
|
inf := tree.New("/")
|
|
|
|
var nd, nf int
|
|
|
|
if d, f := inf.Visit(opts); f != 0 {
|
|
|
|
nd, nf = nd+d, nf+f
|
|
|
|
}
|
|
|
|
inf.Print(opts)
|
|
|
|
// Print footer report
|
|
|
|
if !noReport {
|
|
|
|
footer := fmt.Sprintf("\n%d directories", nd)
|
|
|
|
if !opts.DirsOnly {
|
|
|
|
footer += fmt.Sprintf(", %d files", nf)
|
|
|
|
}
|
2018-05-22 10:41:13 +02:00
|
|
|
_, _ = fmt.Fprintln(outFile, footer)
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// FileInfo maps an fs.DirEntry into an os.FileInfo
|
2017-07-24 13:39:39 +02:00
|
|
|
type FileInfo struct {
|
|
|
|
entry fs.DirEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name is base name of the file
|
|
|
|
func (to *FileInfo) Name() string {
|
2023-02-08 18:17:55 +01:00
|
|
|
return enc.FromStandardName(path.Base(to.entry.Remote()))
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size in bytes for regular files; system-dependent for others
|
|
|
|
func (to *FileInfo) Size() int64 {
|
|
|
|
return to.entry.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode is file mode bits
|
|
|
|
func (to *FileInfo) Mode() os.FileMode {
|
|
|
|
if to.IsDir() {
|
|
|
|
return os.FileMode(0777)
|
|
|
|
}
|
|
|
|
return os.FileMode(0666)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime is modification time
|
|
|
|
func (to *FileInfo) ModTime() time.Time {
|
2019-06-17 10:34:30 +02:00
|
|
|
return to.entry.ModTime(context.Background())
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDir is abbreviation for Mode().IsDir()
|
|
|
|
func (to *FileInfo) IsDir() bool {
|
|
|
|
_, ok := to.entry.(fs.Directory)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sys is underlying data source (can return nil)
|
|
|
|
func (to *FileInfo) Sys() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the full path
|
|
|
|
func (to *FileInfo) String() string {
|
2023-02-08 18:17:55 +01:00
|
|
|
return filepath.FromSlash(enc.FromStandardPath(to.entry.Remote()))
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fs maps an fs.Fs into a tree.Fs
|
2019-06-27 16:54:43 +02:00
|
|
|
type Fs dirtree.DirTree
|
2017-07-24 13:39:39 +02:00
|
|
|
|
|
|
|
// NewFs creates a new tree
|
2019-06-27 16:54:43 +02:00
|
|
|
func NewFs(dirs dirtree.DirTree) Fs {
|
2017-07-24 13:39:39 +02:00
|
|
|
return Fs(dirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns info about the file
|
|
|
|
func (dirs Fs) Stat(filePath string) (fi os.FileInfo, err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(nil, "filePath=%q", filePath)("fi=%+v, err=%v", &fi, &err)
|
2017-08-01 15:46:21 +02:00
|
|
|
filePath = filepath.ToSlash(filePath)
|
2023-02-08 18:17:55 +01:00
|
|
|
filePath = enc.ToStandardPath(filePath)
|
2017-07-24 13:39:39 +02:00
|
|
|
filePath = strings.TrimLeft(filePath, "/")
|
|
|
|
if filePath == "" {
|
|
|
|
return &FileInfo{fs.NewDir("", time.Now())}, nil
|
|
|
|
}
|
2019-06-27 16:54:43 +02:00
|
|
|
_, entry := dirtree.DirTree(dirs).Find(filePath)
|
2017-07-24 13:39:39 +02:00
|
|
|
if entry == nil {
|
2022-06-08 22:54:39 +02:00
|
|
|
return nil, fmt.Errorf("couldn't find %q in directory cache", filePath)
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
return &FileInfo{entry}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDir returns info about the directory and fills up the directory cache
|
|
|
|
func (dirs Fs) ReadDir(dir string) (names []string, err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(nil, "dir=%s", dir)("names=%+v, err=%v", &names, &err)
|
2017-08-01 15:46:21 +02:00
|
|
|
dir = filepath.ToSlash(dir)
|
2023-02-08 18:17:55 +01:00
|
|
|
dir = enc.ToStandardPath(dir)
|
2017-07-24 13:39:39 +02:00
|
|
|
dir = strings.TrimLeft(dir, "/")
|
|
|
|
entries, ok := dirs[dir]
|
|
|
|
if !ok {
|
2022-06-08 22:54:39 +02:00
|
|
|
return nil, fmt.Errorf("couldn't find directory %q", dir)
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
2023-02-08 18:17:55 +01:00
|
|
|
names = append(names, enc.FromStandardName(path.Base(entry.Remote())))
|
2017-07-24 13:39:39 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check interfaces
|
|
|
|
var (
|
|
|
|
_ tree.Fs = (*Fs)(nil)
|
|
|
|
_ os.FileInfo = (*FileInfo)(nil)
|
|
|
|
)
|