github.com/artpar/rclone@v1.67.3/backend/cache/directory.go (about)

     1  //go:build !plan9 && !js
     2  
     3  package cache
     4  
     5  import (
     6  	"context"
     7  	"path"
     8  	"time"
     9  
    10  	"github.com/artpar/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  // ModTime returns the cached ModTime
   105  func (d *Directory) ModTime(ctx context.Context) time.Time {
   106  	return time.Unix(0, d.CacheModTime)
   107  }
   108  
   109  // Size returns the cached Size
   110  func (d *Directory) Size() int64 {
   111  	return d.CacheSize
   112  }
   113  
   114  // Items returns the cached Items
   115  func (d *Directory) Items() int64 {
   116  	return d.CacheItems
   117  }
   118  
   119  // ID returns the ID of the cached directory if known
   120  func (d *Directory) ID() string {
   121  	if d.Directory == nil {
   122  		return ""
   123  	}
   124  	return d.Directory.ID()
   125  }
   126  
   127  var (
   128  	_ fs.Directory = (*Directory)(nil)
   129  )