2015-09-22 19:47:16 +02:00
|
|
|
// Package dircache provides a simple cache for caching directory to path lookups
|
2015-09-03 22:25:55 +02:00
|
|
|
package dircache
|
|
|
|
|
2015-09-06 11:26:41 +02:00
|
|
|
// _methods are called without the lock
|
|
|
|
|
2015-09-03 22:25:55 +02:00
|
|
|
import (
|
2019-08-22 12:47:50 +02:00
|
|
|
"bytes"
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2019-08-22 12:47:50 +02:00
|
|
|
"fmt"
|
2015-09-03 22:25:55 +02:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-04-23 22:46:52 +02:00
|
|
|
|
2016-06-12 16:06:02 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2015-09-03 22:25:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// DirCache caches paths to directory IDs and vice versa
|
|
|
|
type DirCache struct {
|
2015-10-04 23:05:45 +02:00
|
|
|
cacheMu sync.RWMutex
|
2015-09-03 22:25:55 +02:00
|
|
|
cache map[string]string
|
|
|
|
invCache map[string]string
|
2015-10-04 23:05:45 +02:00
|
|
|
mu sync.Mutex
|
2015-09-06 11:26:41 +02:00
|
|
|
fs DirCacher // Interface to find and make stuff
|
|
|
|
trueRootID string // ID of the absolute root
|
|
|
|
root string // the path we are working on
|
|
|
|
rootID string // ID of the root directory
|
|
|
|
rootParentID string // ID of the root's parent directory
|
|
|
|
foundRoot bool // Whether we have found the root or not
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// DirCacher describes an interface for doing the low level directory work
|
2015-09-03 22:25:55 +02:00
|
|
|
type DirCacher interface {
|
2019-06-17 10:34:30 +02:00
|
|
|
FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut string, found bool, err error)
|
|
|
|
CreateDir(ctx context.Context, pathID, leaf string) (newID string, err error)
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// New makes a DirCache
|
2015-09-03 22:25:55 +02:00
|
|
|
//
|
|
|
|
// The cache is safe for concurrent use
|
|
|
|
func New(root string, trueRootID string, fs DirCacher) *DirCache {
|
|
|
|
d := &DirCache{
|
|
|
|
trueRootID: trueRootID,
|
|
|
|
root: root,
|
|
|
|
fs: fs,
|
|
|
|
}
|
|
|
|
d.Flush()
|
|
|
|
d.ResetRoot()
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2019-08-22 12:47:50 +02:00
|
|
|
// String returns the directory cache in string form for debugging
|
|
|
|
func (dc *DirCache) String() string {
|
|
|
|
dc.cacheMu.RLock()
|
|
|
|
defer dc.cacheMu.RUnlock()
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, _ = buf.WriteString("DirCache{\n")
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\ttrueRootID: %q,\n", dc.trueRootID)
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\troot: %q,\n", dc.root)
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\trootID: %q,\n", dc.rootID)
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\trootParentID: %q,\n", dc.rootParentID)
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\tfoundRoot: %v,\n", dc.foundRoot)
|
|
|
|
_, _ = buf.WriteString("\tcache: {\n")
|
|
|
|
for k, v := range dc.cache {
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\t\t%q: %q,\n", k, v)
|
|
|
|
}
|
|
|
|
_, _ = buf.WriteString("\t},\n")
|
|
|
|
_, _ = buf.WriteString("\tinvCache: {\n")
|
|
|
|
for k, v := range dc.invCache {
|
|
|
|
_, _ = fmt.Fprintf(&buf, "\t\t%q: %q,\n", k, v)
|
|
|
|
}
|
|
|
|
_, _ = buf.WriteString("\t},\n")
|
|
|
|
_, _ = buf.WriteString("}\n")
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Get an ID given a path
|
2015-09-03 22:25:55 +02:00
|
|
|
func (dc *DirCache) Get(path string) (id string, ok bool) {
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.cacheMu.RLock()
|
|
|
|
id, ok = dc.cache[path]
|
|
|
|
dc.cacheMu.RUnlock()
|
2015-09-03 22:25:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInv gets a path given an ID
|
2015-10-04 23:05:45 +02:00
|
|
|
func (dc *DirCache) GetInv(id string) (path string, ok bool) {
|
|
|
|
dc.cacheMu.RLock()
|
|
|
|
path, ok = dc.invCache[id]
|
|
|
|
dc.cacheMu.RUnlock()
|
2015-09-03 22:25:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-06 11:26:41 +02:00
|
|
|
// Put a path, id into the map
|
|
|
|
func (dc *DirCache) Put(path, id string) {
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.cacheMu.Lock()
|
|
|
|
dc.cache[path] = id
|
|
|
|
dc.invCache[id] = path
|
|
|
|
dc.cacheMu.Unlock()
|
2015-09-06 11:26:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the map of all data
|
|
|
|
func (dc *DirCache) Flush() {
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.cacheMu.Lock()
|
|
|
|
dc.cache = make(map[string]string)
|
|
|
|
dc.invCache = make(map[string]string)
|
|
|
|
dc.cacheMu.Unlock()
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 22:52:43 +01:00
|
|
|
// FlushDir flushes the map of all data starting with dir
|
|
|
|
//
|
|
|
|
// If dir is empty then this is equivalent to calling ResetRoot
|
|
|
|
func (dc *DirCache) FlushDir(dir string) {
|
|
|
|
if dir == "" {
|
|
|
|
dc.ResetRoot()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dc.cacheMu.Lock()
|
|
|
|
|
|
|
|
// Delete the root dir
|
|
|
|
ID, ok := dc.cache[dir]
|
|
|
|
if ok {
|
|
|
|
delete(dc.cache, dir)
|
|
|
|
delete(dc.invCache, ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And any sub directories
|
|
|
|
dir += "/"
|
|
|
|
for key, ID := range dc.cache {
|
|
|
|
if strings.HasPrefix(key, dir) {
|
|
|
|
delete(dc.cache, key)
|
|
|
|
delete(dc.invCache, ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.cacheMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// SplitPath splits a path into directory, leaf
|
2015-09-03 22:25:55 +02:00
|
|
|
//
|
|
|
|
// Path shouldn't start or end with a /
|
|
|
|
//
|
|
|
|
// If there are no slashes then directory will be "" and leaf = path
|
|
|
|
func SplitPath(path string) (directory, leaf string) {
|
|
|
|
lastSlash := strings.LastIndex(path, "/")
|
|
|
|
if lastSlash >= 0 {
|
|
|
|
directory = path[:lastSlash]
|
|
|
|
leaf = path[lastSlash+1:]
|
|
|
|
} else {
|
|
|
|
directory = ""
|
|
|
|
leaf = path
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// FindDir finds the directory passed in returning the directory ID
|
|
|
|
// starting from pathID
|
2015-09-03 22:25:55 +02:00
|
|
|
//
|
|
|
|
// Path shouldn't start or end with a /
|
|
|
|
//
|
|
|
|
// If create is set it will make the directory if not found
|
|
|
|
//
|
|
|
|
// Algorithm:
|
|
|
|
// Look in the cache for the path, if found return the pathID
|
|
|
|
// If not found strip the last path off the path and recurse
|
|
|
|
// Now have a parent directory id, so look in the parent for self and return it
|
2019-06-17 10:34:30 +02:00
|
|
|
func (dc *DirCache) FindDir(ctx context.Context, path string, create bool) (pathID string, err error) {
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2019-06-17 10:34:30 +02:00
|
|
|
return dc._findDir(ctx, path, create)
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
2015-09-06 11:26:41 +02:00
|
|
|
// Look for the root and in the cache - safe to call without the mu
|
2015-09-03 22:25:55 +02:00
|
|
|
func (dc *DirCache) _findDirInCache(path string) string {
|
|
|
|
// fmt.Println("Finding",path,"create",create,"cache",cache)
|
|
|
|
// If it is the root, then return it
|
|
|
|
if path == "" {
|
|
|
|
// fmt.Println("Root")
|
|
|
|
return dc.rootID
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is in the cache then return it
|
2015-10-04 23:05:45 +02:00
|
|
|
pathID, ok := dc.Get(path)
|
2015-09-03 22:25:55 +02:00
|
|
|
if ok {
|
|
|
|
// fmt.Println("Cache hit on", path)
|
|
|
|
return pathID
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-09-06 11:26:41 +02:00
|
|
|
// Unlocked findDir - must have mu
|
2019-06-17 10:34:30 +02:00
|
|
|
func (dc *DirCache) _findDir(ctx context.Context, path string, create bool) (pathID string, err error) {
|
2015-09-03 22:25:55 +02:00
|
|
|
pathID = dc._findDirInCache(path)
|
|
|
|
if pathID != "" {
|
|
|
|
return pathID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the path into directory, leaf
|
|
|
|
directory, leaf := SplitPath(path)
|
|
|
|
|
|
|
|
// Recurse and find pathID for parent directory
|
2019-06-17 10:34:30 +02:00
|
|
|
parentPathID, err := dc._findDir(ctx, directory, create)
|
2015-09-03 22:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the leaf in parentPathID
|
2019-06-17 10:34:30 +02:00
|
|
|
pathID, found, err := dc.fs.FindLeaf(ctx, parentPathID, leaf)
|
2015-09-03 22:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not found create the directory if required or return an error
|
|
|
|
if !found {
|
|
|
|
if create {
|
2019-06-17 10:34:30 +02:00
|
|
|
pathID, err = dc.fs.CreateDir(ctx, parentPathID, leaf)
|
2015-09-03 22:25:55 +02:00
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.Wrap(err, "failed to make directory")
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-23 22:46:52 +02:00
|
|
|
return "", fs.ErrorDirNotFound
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the leaf directory in the cache
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.Put(path, pathID)
|
2015-09-03 22:25:55 +02:00
|
|
|
|
|
|
|
// fmt.Println("Dir", path, "is", pathID)
|
|
|
|
return pathID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPath finds the leaf and directoryID from a path
|
|
|
|
//
|
2018-04-14 18:15:00 +02:00
|
|
|
// Do not call FindPath with the root directory - it will return an error
|
|
|
|
//
|
2015-09-03 22:25:55 +02:00
|
|
|
// If create is set parent directories will be created if they don't exist
|
2019-06-17 10:34:30 +02:00
|
|
|
func (dc *DirCache) FindPath(ctx context.Context, path string, create bool) (leaf, directoryID string, err error) {
|
2018-04-14 18:15:00 +02:00
|
|
|
if path == "" {
|
|
|
|
err = errors.New("internal error: can't call FindPath with root directory")
|
|
|
|
return
|
|
|
|
}
|
2015-09-06 11:26:41 +02:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2015-09-03 22:25:55 +02:00
|
|
|
directory, leaf := SplitPath(path)
|
2019-06-17 10:34:30 +02:00
|
|
|
directoryID, err = dc._findDir(ctx, directory, create)
|
2015-09-03 22:25:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// FindRoot finds the root directory if not already found
|
2015-09-03 22:25:55 +02:00
|
|
|
//
|
|
|
|
// Resets the root directory
|
|
|
|
//
|
|
|
|
// If create is set it will make the directory if not found
|
2019-06-17 10:34:30 +02:00
|
|
|
func (dc *DirCache) FindRoot(ctx context.Context, create bool) error {
|
2015-09-06 11:26:41 +02:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2015-09-03 22:25:55 +02:00
|
|
|
if dc.foundRoot {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
rootID, err := dc._findDir(ctx, dc.root, create)
|
2015-09-03 22:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-10 16:33:46 +02:00
|
|
|
dc.foundRoot = true
|
2015-09-03 22:25:55 +02:00
|
|
|
dc.rootID = rootID
|
2015-09-06 11:26:41 +02:00
|
|
|
|
|
|
|
// Find the parent of the root while we still have the root
|
|
|
|
// directory tree cached
|
|
|
|
rootParentPath, _ := SplitPath(dc.root)
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.rootParentID, _ = dc.Get(rootParentPath)
|
2015-09-06 11:26:41 +02:00
|
|
|
|
|
|
|
// Reset the tree based on dc.root
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.Flush()
|
2015-09-03 22:25:55 +02:00
|
|
|
// Put the root directory in
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.Put("", dc.rootID)
|
2015-09-03 22:25:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:55:05 +01:00
|
|
|
// FindRootAndPath finds the root first if not found then finds leaf and directoryID from a path
|
|
|
|
//
|
|
|
|
// If create is set parent directories will be created if they don't exist
|
2019-06-17 10:34:30 +02:00
|
|
|
func (dc *DirCache) FindRootAndPath(ctx context.Context, path string, create bool) (leaf, directoryID string, err error) {
|
|
|
|
err = dc.FindRoot(ctx, create)
|
2017-03-15 21:55:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
return dc.FindPath(ctx, path, create)
|
2017-03-15 21:55:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-10 16:33:46 +02:00
|
|
|
// FoundRoot returns whether the root directory has been found yet
|
|
|
|
//
|
|
|
|
// Call this from FindLeaf or CreateDir only
|
|
|
|
func (dc *DirCache) FoundRoot() bool {
|
|
|
|
return dc.foundRoot
|
|
|
|
}
|
|
|
|
|
2015-09-03 22:25:55 +02:00
|
|
|
// RootID returns the ID of the root directory
|
|
|
|
//
|
|
|
|
// This should be called after FindRoot
|
|
|
|
func (dc *DirCache) RootID() string {
|
2015-09-06 11:26:41 +02:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
|
|
|
if !dc.foundRoot {
|
2015-09-03 22:25:55 +02:00
|
|
|
log.Fatalf("Internal Error: RootID() called before FindRoot")
|
|
|
|
}
|
|
|
|
return dc.rootID
|
|
|
|
}
|
|
|
|
|
2015-09-06 11:26:41 +02:00
|
|
|
// RootParentID returns the ID of the parent of the root directory
|
|
|
|
//
|
|
|
|
// This should be called after FindRoot
|
|
|
|
func (dc *DirCache) RootParentID() (string, error) {
|
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
|
|
|
if !dc.foundRoot {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.New("internal error: RootID() called before FindRoot")
|
2015-09-06 11:26:41 +02:00
|
|
|
}
|
|
|
|
if dc.rootParentID == "" {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.New("internal error: didn't find rootParentID")
|
2015-09-06 11:26:41 +02:00
|
|
|
}
|
|
|
|
if dc.rootID == dc.trueRootID {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.New("is root directory")
|
2015-09-06 11:26:41 +02:00
|
|
|
}
|
|
|
|
return dc.rootParentID, nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// ResetRoot resets the root directory to the absolute root and clears
|
|
|
|
// the DirCache
|
2015-09-03 22:25:55 +02:00
|
|
|
func (dc *DirCache) ResetRoot() {
|
2015-09-06 11:26:41 +02:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2015-09-03 22:25:55 +02:00
|
|
|
dc.foundRoot = false
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.Flush()
|
2015-09-03 22:25:55 +02:00
|
|
|
|
|
|
|
// Put the true root in
|
|
|
|
dc.rootID = dc.trueRootID
|
|
|
|
|
|
|
|
// Put the root directory in
|
2015-10-04 23:05:45 +02:00
|
|
|
dc.Put("", dc.rootID)
|
2015-09-03 22:25:55 +02:00
|
|
|
}
|