2015-05-24 02:56:48 +08:00
|
|
|
package dropbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-09-22 14:31:12 +08:00
|
|
|
"strings"
|
|
|
|
|
2015-05-24 02:56:48 +08:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/stacktic/dropbox"
|
|
|
|
)
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
type nameTreeNode struct {
|
2015-05-24 02:56:48 +08:00
|
|
|
// Map from lowercase directory name to tree node
|
2015-09-23 01:47:16 +08:00
|
|
|
Directories map[string]*nameTreeNode
|
2015-05-24 02:56:48 +08:00
|
|
|
|
|
|
|
// Map from file name (case sensitive) to dropbox entry
|
|
|
|
Files map[string]*dropbox.Entry
|
|
|
|
|
|
|
|
// Empty string if exact case is unknown or root node
|
|
|
|
CaseCorrectName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func newNameTreeNode(caseCorrectName string) *nameTreeNode {
|
|
|
|
return &nameTreeNode{
|
2015-05-24 02:56:48 +08:00
|
|
|
CaseCorrectName: caseCorrectName,
|
2015-09-23 01:47:16 +08:00
|
|
|
Directories: make(map[string]*nameTreeNode),
|
2015-05-24 02:56:48 +08:00
|
|
|
Files: make(map[string]*dropbox.Entry),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func newNameTree() *nameTreeNode {
|
2015-05-24 02:56:48 +08:00
|
|
|
return newNameTreeNode("")
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func (tree *nameTreeNode) String() string {
|
2015-05-24 02:56:48 +08:00
|
|
|
if len(tree.CaseCorrectName) == 0 {
|
2015-09-23 01:47:16 +08:00
|
|
|
return "nameTreeNode/<root>"
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
2015-09-23 01:47:16 +08:00
|
|
|
return fmt.Sprintf("nameTreeNode/%q", tree.CaseCorrectName)
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func (tree *nameTreeNode) getTreeNode(path string) *nameTreeNode {
|
2015-05-24 02:56:48 +08:00
|
|
|
if len(path) == 0 {
|
|
|
|
// no lookup required, just return root
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
current := tree
|
|
|
|
for _, component := range strings.Split(path, "/") {
|
|
|
|
if len(component) == 0 {
|
|
|
|
fs.Stats.Error()
|
2015-08-09 03:10:31 +08:00
|
|
|
fs.ErrorLog(tree, "getTreeNode: path component is empty (full path %q)", path)
|
2015-05-24 02:56:48 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lowercase := strings.ToLower(component)
|
|
|
|
|
|
|
|
lookup := current.Directories[lowercase]
|
|
|
|
if lookup == nil {
|
|
|
|
lookup = newNameTreeNode("")
|
|
|
|
current.Directories[lowercase] = lookup
|
|
|
|
}
|
|
|
|
|
|
|
|
current = lookup
|
|
|
|
}
|
|
|
|
|
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:46:52 +08:00
|
|
|
// PutCaseCorrectPath puts a known good path into the nameTree
|
|
|
|
func (tree *nameTreeNode) PutCaseCorrectPath(caseCorrectPath string) {
|
|
|
|
if len(caseCorrectPath) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
current := tree
|
|
|
|
for _, component := range strings.Split(caseCorrectPath, "/") {
|
|
|
|
if len(component) == 0 {
|
|
|
|
fs.Stats.Error()
|
|
|
|
fs.ErrorLog(tree, "PutCaseCorrectPath: path component is empty (full path %q)", caseCorrectPath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lowercase := strings.ToLower(component)
|
|
|
|
lookup := current.Directories[lowercase]
|
|
|
|
if lookup == nil {
|
|
|
|
lookup = newNameTreeNode(component)
|
|
|
|
current.Directories[lowercase] = lookup
|
|
|
|
}
|
|
|
|
current = lookup
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func (tree *nameTreeNode) PutCaseCorrectDirectoryName(parentPath string, caseCorrectDirectoryName string) {
|
2015-05-24 02:56:48 +08:00
|
|
|
if len(caseCorrectDirectoryName) == 0 {
|
|
|
|
fs.Stats.Error()
|
2015-08-09 03:10:31 +08:00
|
|
|
fs.ErrorLog(tree, "PutCaseCorrectDirectoryName: empty caseCorrectDirectoryName is not allowed (parentPath: %q)", parentPath)
|
2015-05-24 02:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node := tree.getTreeNode(parentPath)
|
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lowerCaseDirectoryName := strings.ToLower(caseCorrectDirectoryName)
|
|
|
|
directory := node.Directories[lowerCaseDirectoryName]
|
|
|
|
if directory == nil {
|
|
|
|
directory = newNameTreeNode(caseCorrectDirectoryName)
|
|
|
|
node.Directories[lowerCaseDirectoryName] = directory
|
|
|
|
} else {
|
|
|
|
if len(directory.CaseCorrectName) > 0 {
|
|
|
|
fs.Stats.Error()
|
2015-08-09 03:10:31 +08:00
|
|
|
fs.ErrorLog(tree, "PutCaseCorrectDirectoryName: directory %q is already exists under parent path %q", caseCorrectDirectoryName, parentPath)
|
2015-05-24 02:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
directory.CaseCorrectName = caseCorrectDirectoryName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func (tree *nameTreeNode) PutFile(parentPath string, caseCorrectFileName string, dropboxEntry *dropbox.Entry) {
|
2015-05-24 02:56:48 +08:00
|
|
|
node := tree.getTreeNode(parentPath)
|
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Files[caseCorrectFileName] != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-09 03:10:31 +08:00
|
|
|
fs.ErrorLog(tree, "PutFile: file %q is already exists at %q", caseCorrectFileName, parentPath)
|
2015-05-24 02:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Files[caseCorrectFileName] = dropboxEntry
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
func (tree *nameTreeNode) GetPathWithCorrectCase(path string) *string {
|
2015-05-24 02:56:48 +08:00
|
|
|
if path == "" {
|
|
|
|
empty := ""
|
|
|
|
return &empty
|
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
|
|
|
|
|
|
|
current := tree
|
|
|
|
for _, component := range strings.Split(path, "/") {
|
|
|
|
if component == "" {
|
|
|
|
fs.Stats.Error()
|
2015-08-09 03:10:31 +08:00
|
|
|
fs.ErrorLog(tree, "GetPathWithCorrectCase: path component is empty (full path %q)", path)
|
2015-05-24 02:56:48 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lowercase := strings.ToLower(component)
|
|
|
|
|
|
|
|
current = current.Directories[lowercase]
|
|
|
|
if current == nil || current.CaseCorrectName == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 14:31:12 +08:00
|
|
|
_, _ = result.WriteString("/")
|
|
|
|
_, _ = result.WriteString(current.CaseCorrectName)
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resultString := result.String()
|
|
|
|
return &resultString
|
|
|
|
}
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
type nameTreeFileWalkFunc func(caseCorrectFilePath string, entry *dropbox.Entry) error
|
2015-05-24 02:56:48 +08:00
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
func (tree *nameTreeNode) walkFilesRec(currentPath string, walkFunc nameTreeFileWalkFunc) error {
|
2015-05-24 02:56:48 +08:00
|
|
|
var prefix string
|
|
|
|
if currentPath == "" {
|
|
|
|
prefix = ""
|
|
|
|
} else {
|
|
|
|
prefix = currentPath + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, entry := range tree.Files {
|
2016-04-22 03:06:21 +08:00
|
|
|
err := walkFunc(prefix+name, entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for lowerCaseName, directory := range tree.Directories {
|
|
|
|
caseCorrectName := directory.CaseCorrectName
|
|
|
|
if caseCorrectName == "" {
|
|
|
|
fs.Stats.Error()
|
2015-08-09 03:10:31 +08:00
|
|
|
fs.ErrorLog(tree, "WalkFiles: exact name of the directory %q is unknown (parent path: %q)", lowerCaseName, currentPath)
|
2015-05-24 02:56:48 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
err := directory.walkFilesRec(prefix+caseCorrectName, walkFunc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
|
|
|
|
return nil
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
func (tree *nameTreeNode) WalkFiles(rootPath string, walkFunc nameTreeFileWalkFunc) error {
|
2015-05-24 02:56:48 +08:00
|
|
|
node := tree.getTreeNode(rootPath)
|
|
|
|
if node == nil {
|
2016-04-22 03:06:21 +08:00
|
|
|
return nil
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
return node.walkFilesRec(rootPath, walkFunc)
|
2015-05-24 02:56:48 +08:00
|
|
|
}
|