github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/backend/cache/directory.go (about)

     1  // +build !plan9
     2  
     3  package cache
     4  
     5  import (
     6  	"context"
     7  	"path"
     8  	"time"
     9  
    10  	"github.com/rclone/rclone/fs"
    11  )
    12  
    13  // Directory is a generic dir that stores basic information about it
    14  type Directory struct {
    15  	Directory fs.Directory `json:"-"` // can be nil
    16  
    17  	CacheFs      *Fs    `json:"-"`       // cache fs
    18  	Name         string `json:"name"`    // name of the directory
    19  	Dir          string `json:"dir"`     // abs path of the directory
    20  	CacheModTime int64  `json:"modTime"` // modification or creation time - IsZero for unknown
    21  	CacheSize    int64  `json:"size"`    // size of directory and contents or -1 if unknown
    22  
    23  	CacheItems int64      `json:"items"`     // number of objects or -1 for unknown
    24  	CacheType  string     `json:"cacheType"` // object type
    25  	CacheTs    *time.Time `json:",omitempty"`
    26  }
    27  
    28  // NewDirectory builds an empty dir which will be used to unmarshal data in it
    29  func NewDirectory(f *Fs, remote string) *Directory {
    30  	cd := ShallowDirectory(f, remote)
    31  	t := time.Now()
    32  	cd.CacheTs = &t
    33  
    34  	return cd
    35  }
    36  
    37  // ShallowDirectory builds an empty dir which will be used to unmarshal data in it
    38  func ShallowDirectory(f *Fs, remote string) *Directory {
    39  	var cd *Directory
    40  	fullRemote := cleanPath(path.Join(f.Root(), remote))
    41  
    42  	// build a new one
    43  	dir := cleanPath(path.Dir(fullRemote))
    44  	name := cleanPath(path.Base(fullRemote))
    45  	cd = &Directory{
    46  		CacheFs:      f,
    47  		Name:         name,
    48  		Dir:          dir,
    49  		CacheModTime: time.Now().UnixNano(),
    50  		CacheSize:    0,
    51  		CacheItems:   0,
    52  		CacheType:    "Directory",
    53  	}
    54  
    55  	return cd
    56  }
    57  
    58  // DirectoryFromOriginal builds one from a generic fs.Directory
    59  func DirectoryFromOriginal(ctx context.Context, f *Fs, d fs.Directory) *Directory {
    60  	var cd *Directory
    61  	fullRemote := path.Join(f.Root(), d.Remote())
    62  
    63  	dir := cleanPath(path.Dir(fullRemote))
    64  	name := cleanPath(path.Base(fullRemote))
    65  	t := time.Now()
    66  	cd = &Directory{
    67  		Directory:    d,
    68  		CacheFs:      f,
    69  		Name:         name,
    70  		Dir:          dir,
    71  		CacheModTime: d.ModTime(ctx).UnixNano(),
    72  		CacheSize:    d.Size(),
    73  		CacheItems:   d.Items(),
    74  		CacheType:    "Directory",
    75  		CacheTs:      &t,
    76  	}
    77  
    78  	return cd
    79  }
    80  
    81  // Fs returns its FS info
    82  func (d *Directory) Fs() fs.Info {
    83  	return d.CacheFs
    84  }
    85  
    86  // String returns a human friendly name for this object
    87  func (d *Directory) String() string {
    88  	if d == nil {
    89  		return "<nil>"
    90  	}
    91  	return d.Remote()
    92  }
    93  
    94  // Remote returns the remote path
    95  func (d *Directory) Remote() string {
    96  	return d.CacheFs.cleanRootFromPath(d.abs())
    97  }
    98  
    99  // abs returns the absolute path to the dir
   100  func (d *Directory) abs() string {
   101  	return cleanPath(path.Join(d.Dir, d.Name))
   102  }
   103  
   104  // parentRemote returns the absolute path parent remote
   105  func (d *Directory) parentRemote() string {
   106  	absPath := d.abs()
   107  	if absPath == "" {
   108  		return ""
   109  	}
   110  	return cleanPath(path.Dir(absPath))
   111  }
   112  
   113  // ModTime returns the cached ModTime
   114  func (d *Directory) ModTime(ctx context.Context) time.Time {
   115  	return time.Unix(0, d.CacheModTime)
   116  }
   117  
   118  // Size returns the cached Size
   119  func (d *Directory) Size() int64 {
   120  	return d.CacheSize
   121  }
   122  
   123  // Items returns the cached Items
   124  func (d *Directory) Items() int64 {
   125  	return d.CacheItems
   126  }
   127  
   128  // ID returns the ID of the cached directory if known
   129  func (d *Directory) ID() string {
   130  	if d.Directory == nil {
   131  		return ""
   132  	}
   133  	return d.Directory.ID()
   134  }
   135  
   136  var (
   137  	_ fs.Directory = (*Directory)(nil)
   138  )