2015-09-22 17:47:16 +00:00
|
|
|
// Package dircache provides a simple cache for caching directory to path lookups
|
2015-09-03 20:25:55 +00:00
|
|
|
package dircache
|
|
|
|
|
2015-09-06 09:26:41 +00:00
|
|
|
// _methods are called without the lock
|
|
|
|
|
2015-09-03 20:25:55 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirCache caches paths to directory IDs and vice versa
|
|
|
|
type DirCache struct {
|
2015-09-06 09:26:41 +00:00
|
|
|
mu sync.RWMutex
|
2015-09-03 20:25:55 +00:00
|
|
|
cache map[string]string
|
|
|
|
invCache map[string]string
|
2015-09-06 09:26:41 +00: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 20:25:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// DirCacher describes an interface for doing the low level directory work
|
2015-09-03 20:25:55 +00:00
|
|
|
type DirCacher interface {
|
|
|
|
FindLeaf(pathID, leaf string) (pathIDOut string, found bool, err error)
|
|
|
|
CreateDir(pathID, leaf string) (newID string, err error)
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// New makes a DirCache
|
2015-09-03 20:25:55 +00: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
|
|
|
|
}
|
|
|
|
|
2015-09-06 09:26:41 +00:00
|
|
|
// _get an ID given a path - without lock
|
|
|
|
func (dc *DirCache) _get(path string) (id string, ok bool) {
|
|
|
|
id, ok = dc.cache[path]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Get an ID given a path
|
2015-09-03 20:25:55 +00:00
|
|
|
func (dc *DirCache) Get(path string) (id string, ok bool) {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.RLock()
|
|
|
|
id, ok = dc._get(path)
|
|
|
|
dc.mu.RUnlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInv gets a path given an ID
|
|
|
|
func (dc *DirCache) GetInv(path string) (id string, ok bool) {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.RLock()
|
2015-09-03 20:25:55 +00:00
|
|
|
id, ok = dc.invCache[path]
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.RUnlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-06 09:26:41 +00:00
|
|
|
// _put a path, id into the map without lock
|
|
|
|
func (dc *DirCache) _put(path, id string) {
|
2015-09-03 20:25:55 +00:00
|
|
|
dc.cache[path] = id
|
|
|
|
dc.invCache[id] = path
|
|
|
|
}
|
|
|
|
|
2015-09-06 09:26:41 +00:00
|
|
|
// Put a path, id into the map
|
|
|
|
func (dc *DirCache) Put(path, id string) {
|
|
|
|
dc.mu.Lock()
|
|
|
|
dc._put(path, id)
|
|
|
|
dc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// _flush the map of all data without lock
|
|
|
|
func (dc *DirCache) _flush() {
|
2015-09-03 20:25:55 +00:00
|
|
|
dc.cache = make(map[string]string)
|
|
|
|
dc.invCache = make(map[string]string)
|
2015-09-06 09:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the map of all data
|
|
|
|
func (dc *DirCache) Flush() {
|
|
|
|
dc.mu.Lock()
|
|
|
|
dc._flush()
|
|
|
|
dc.mu.Unlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// SplitPath splits a path into directory, leaf
|
2015-09-03 20:25:55 +00: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 17:47:16 +00:00
|
|
|
// FindDir finds the directory passed in returning the directory ID
|
|
|
|
// starting from pathID
|
2015-09-03 20:25:55 +00: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
|
|
|
|
func (dc *DirCache) FindDir(path string, create bool) (pathID string, err error) {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.RLock()
|
|
|
|
defer dc.mu.RUnlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
return dc._findDir(path, create)
|
|
|
|
}
|
|
|
|
|
2015-09-06 09:26:41 +00:00
|
|
|
// Look for the root and in the cache - safe to call without the mu
|
2015-09-03 20:25:55 +00: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-09-06 09:26:41 +00:00
|
|
|
pathID, ok := dc._get(path)
|
2015-09-03 20:25:55 +00:00
|
|
|
if ok {
|
|
|
|
// fmt.Println("Cache hit on", path)
|
|
|
|
return pathID
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-09-06 09:26:41 +00:00
|
|
|
// Unlocked findDir - must have mu
|
2015-09-03 20:25:55 +00:00
|
|
|
func (dc *DirCache) _findDir(path string, create bool) (pathID string, err error) {
|
2015-09-06 09:26:41 +00:00
|
|
|
// if !dc.foundRoot {
|
|
|
|
// return "", fmt.Errorf("FindDir called before FindRoot")
|
|
|
|
// }
|
|
|
|
|
2015-09-03 20:25:55 +00: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
|
|
|
|
parentPathID, err := dc._findDir(directory, create)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the leaf in parentPathID
|
|
|
|
pathID, found, err := dc.fs.FindLeaf(parentPathID, leaf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not found create the directory if required or return an error
|
|
|
|
if !found {
|
|
|
|
if create {
|
|
|
|
pathID, err = dc.fs.CreateDir(parentPathID, leaf)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to make directory: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("Couldn't find directory: %q", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the leaf directory in the cache
|
2015-09-06 09:26:41 +00:00
|
|
|
dc._put(path, pathID)
|
2015-09-03 20:25:55 +00:00
|
|
|
|
|
|
|
// fmt.Println("Dir", path, "is", pathID)
|
|
|
|
return pathID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPath finds the leaf and directoryID from a path
|
|
|
|
//
|
|
|
|
// If create is set parent directories will be created if they don't exist
|
|
|
|
func (dc *DirCache) FindPath(path string, create bool) (leaf, directoryID string, err error) {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
directory, leaf := SplitPath(path)
|
2015-09-06 09:26:41 +00:00
|
|
|
directoryID, err = dc._findDir(directory, create)
|
2015-09-03 20:25:55 +00:00
|
|
|
if err != nil {
|
|
|
|
if create {
|
|
|
|
err = fmt.Errorf("Couldn't find or make directory %q: %s", directory, err)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Couldn't find directory %q: %s", directory, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// FindRoot finds the root directory if not already found
|
2015-09-03 20:25:55 +00:00
|
|
|
//
|
|
|
|
// Resets the root directory
|
|
|
|
//
|
|
|
|
// If create is set it will make the directory if not found
|
|
|
|
func (dc *DirCache) FindRoot(create bool) error {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
if dc.foundRoot {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.foundRoot = true
|
|
|
|
rootID, err := dc._findDir(dc.root, create)
|
2015-09-03 20:25:55 +00:00
|
|
|
if err != nil {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.foundRoot = false
|
2015-09-03 20:25:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dc.rootID = rootID
|
2015-09-06 09:26:41 +00:00
|
|
|
|
|
|
|
// Find the parent of the root while we still have the root
|
|
|
|
// directory tree cached
|
|
|
|
rootParentPath, _ := SplitPath(dc.root)
|
|
|
|
dc.rootParentID, _ = dc._get(rootParentPath)
|
|
|
|
|
|
|
|
// Reset the tree based on dc.root
|
|
|
|
dc._flush()
|
2015-09-03 20:25:55 +00:00
|
|
|
// Put the root directory in
|
2015-09-06 09:26:41 +00:00
|
|
|
dc._put("", dc.rootID)
|
2015-09-03 20:25:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootID returns the ID of the root directory
|
|
|
|
//
|
|
|
|
// This should be called after FindRoot
|
|
|
|
func (dc *DirCache) RootID() string {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
|
|
|
if !dc.foundRoot {
|
2015-09-03 20:25:55 +00:00
|
|
|
log.Fatalf("Internal Error: RootID() called before FindRoot")
|
|
|
|
}
|
|
|
|
return dc.rootID
|
|
|
|
}
|
|
|
|
|
2015-09-06 09:26:41 +00: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 {
|
|
|
|
return "", fmt.Errorf("Internal Error: RootID() called before FindRoot")
|
|
|
|
}
|
|
|
|
if dc.rootParentID == "" {
|
|
|
|
return "", fmt.Errorf("Internal Error: Didn't find rootParentID")
|
|
|
|
}
|
|
|
|
if dc.rootID == dc.trueRootID {
|
|
|
|
return "", fmt.Errorf("Is root directory")
|
|
|
|
}
|
|
|
|
return dc.rootParentID, nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ResetRoot resets the root directory to the absolute root and clears
|
|
|
|
// the DirCache
|
2015-09-03 20:25:55 +00:00
|
|
|
func (dc *DirCache) ResetRoot() {
|
2015-09-06 09:26:41 +00:00
|
|
|
dc.mu.Lock()
|
|
|
|
defer dc.mu.Unlock()
|
2015-09-03 20:25:55 +00:00
|
|
|
dc.foundRoot = false
|
2015-09-06 09:26:41 +00:00
|
|
|
dc._flush()
|
2015-09-03 20:25:55 +00:00
|
|
|
|
|
|
|
// Put the true root in
|
|
|
|
dc.rootID = dc.trueRootID
|
|
|
|
|
|
|
|
// Put the root directory in
|
2015-09-06 09:26:41 +00:00
|
|
|
dc._put("", dc.rootID)
|
2015-09-03 20:25:55 +00:00
|
|
|
}
|