2015-01-06 06:58:30 +08:00
|
|
|
package embedded_filer
|
2014-04-10 00:44:58 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-09-05 10:26:31 +08:00
|
|
|
"sync"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2014-04-10 00:44:58 +08:00
|
|
|
)
|
|
|
|
|
2014-09-05 10:26:31 +08:00
|
|
|
var writeLock sync.Mutex //serialize changes to dir.log
|
|
|
|
|
2014-04-10 00:44:58 +08:00
|
|
|
type DirectoryEntryInMap struct {
|
2016-03-31 03:34:11 +08:00
|
|
|
sync.Mutex
|
2014-04-10 00:44:58 +08:00
|
|
|
Name string
|
|
|
|
Parent *DirectoryEntryInMap
|
2016-03-31 03:34:11 +08:00
|
|
|
subDirectories map[string]*DirectoryEntryInMap
|
2015-01-06 06:58:30 +08:00
|
|
|
Id filer.DirectoryId
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 03:34:11 +08:00
|
|
|
func (de *DirectoryEntryInMap) getChild(dirName string) (*DirectoryEntryInMap, bool) {
|
|
|
|
de.Lock()
|
|
|
|
defer de.Unlock()
|
|
|
|
child, ok := de.subDirectories[dirName]
|
|
|
|
return child, ok
|
|
|
|
}
|
|
|
|
func (de *DirectoryEntryInMap) addChild(dirName string, child *DirectoryEntryInMap) {
|
|
|
|
de.Lock()
|
|
|
|
defer de.Unlock()
|
|
|
|
de.subDirectories[dirName] = child
|
|
|
|
}
|
|
|
|
func (de *DirectoryEntryInMap) removeChild(dirName string) {
|
|
|
|
de.Lock()
|
|
|
|
defer de.Unlock()
|
|
|
|
delete(de.subDirectories, dirName)
|
|
|
|
}
|
|
|
|
func (de *DirectoryEntryInMap) hasChildren() bool {
|
|
|
|
de.Lock()
|
|
|
|
defer de.Unlock()
|
|
|
|
return len(de.subDirectories) > 0
|
|
|
|
}
|
|
|
|
func (de *DirectoryEntryInMap) children() (dirNames []filer.DirectoryEntry) {
|
|
|
|
de.Lock()
|
|
|
|
defer de.Unlock()
|
|
|
|
for k, v := range de.subDirectories {
|
|
|
|
dirNames = append(dirNames, filer.DirectoryEntry{Name: k, Id: v.Id})
|
|
|
|
}
|
|
|
|
return dirNames
|
|
|
|
}
|
|
|
|
|
2014-04-10 00:44:58 +08:00
|
|
|
type DirectoryManagerInMap struct {
|
|
|
|
Root *DirectoryEntryInMap
|
2015-01-06 06:58:30 +08:00
|
|
|
max filer.DirectoryId
|
2014-04-10 00:44:58 +08:00
|
|
|
logFile *os.File
|
|
|
|
isLoading bool
|
|
|
|
}
|
|
|
|
|
2016-04-05 07:32:16 +08:00
|
|
|
func (dm *DirectoryManagerInMap) newDirectoryEntryInMap(parent *DirectoryEntryInMap, name string) (d *DirectoryEntryInMap, err error) {
|
2016-03-31 03:34:11 +08:00
|
|
|
d = &DirectoryEntryInMap{Name: name, Parent: parent, subDirectories: make(map[string]*DirectoryEntryInMap)}
|
2015-03-10 15:20:31 +08:00
|
|
|
var parts []string
|
2014-04-10 00:44:58 +08:00
|
|
|
for p := d; p != nil && p.Name != ""; p = p.Parent {
|
|
|
|
parts = append(parts, p.Name)
|
|
|
|
}
|
|
|
|
n := len(parts)
|
|
|
|
if n <= 0 {
|
2014-09-05 10:34:43 +08:00
|
|
|
return nil, fmt.Errorf("Failed to create folder %s/%s", parent.Name, name)
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < n/2; i++ {
|
|
|
|
parts[i], parts[n-1-i] = parts[n-1-i], parts[i]
|
|
|
|
}
|
2014-09-05 10:26:31 +08:00
|
|
|
dm.max++
|
|
|
|
d.Id = dm.max
|
2014-04-10 00:44:58 +08:00
|
|
|
dm.log("add", "/"+strings.Join(parts, "/"), strconv.Itoa(int(d.Id)))
|
2014-09-05 10:34:43 +08:00
|
|
|
return d, nil
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DirectoryManagerInMap) log(words ...string) {
|
|
|
|
if !dm.isLoading {
|
|
|
|
dm.logFile.WriteString(strings.Join(words, "\t") + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDirectoryManagerInMap(dirLogFile string) (dm *DirectoryManagerInMap, err error) {
|
|
|
|
dm = &DirectoryManagerInMap{}
|
2016-04-05 07:32:16 +08:00
|
|
|
//dm.Root do not use newDirectoryEntryInMap, since dm.max will be changed
|
2016-03-31 03:34:11 +08:00
|
|
|
dm.Root = &DirectoryEntryInMap{subDirectories: make(map[string]*DirectoryEntryInMap)}
|
2014-04-10 00:44:58 +08:00
|
|
|
if dm.logFile, err = os.OpenFile(dirLogFile, os.O_RDWR|os.O_CREATE, 0644); err != nil {
|
2015-07-08 16:43:26 +08:00
|
|
|
return nil, fmt.Errorf("cannot write directory log file %s: %v", dirLogFile, err)
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
return dm, dm.load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DirectoryManagerInMap) processEachLine(line string) error {
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if line == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(line, "\t")
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch parts[0] {
|
|
|
|
case "add":
|
|
|
|
v, pe := strconv.Atoi(parts[2])
|
|
|
|
if pe != nil {
|
|
|
|
return pe
|
|
|
|
}
|
2015-01-06 06:58:30 +08:00
|
|
|
if e := dm.loadDirectory(parts[1], filer.DirectoryId(v)); e != nil {
|
2014-04-10 00:44:58 +08:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
case "mov":
|
2014-07-21 14:12:49 +08:00
|
|
|
newName := ""
|
|
|
|
if len(parts) >= 4 {
|
|
|
|
newName = parts[3]
|
|
|
|
}
|
|
|
|
if e := dm.MoveUnderDirectory(parts[1], parts[2], newName); e != nil {
|
2014-04-10 00:44:58 +08:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
case "del":
|
|
|
|
if e := dm.DeleteDirectory(parts[1]); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("line %s has %s!\n", line, parts[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (dm *DirectoryManagerInMap) load() error {
|
|
|
|
dm.max = 0
|
|
|
|
lines := bufio.NewReader(dm.logFile)
|
|
|
|
dm.isLoading = true
|
|
|
|
defer func() { dm.isLoading = false }()
|
|
|
|
for {
|
|
|
|
line, err := util.Readln(lines)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pe := dm.processEachLine(string(line)); pe != nil {
|
|
|
|
return pe
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DirectoryManagerInMap) findDirectory(dirPath string) (*DirectoryEntryInMap, error) {
|
|
|
|
if dirPath == "" {
|
|
|
|
return dm.Root, nil
|
|
|
|
}
|
2015-07-08 16:43:26 +08:00
|
|
|
dirPath = CleanFilePath(dirPath)
|
2014-04-10 00:44:58 +08:00
|
|
|
if dirPath == "/" {
|
|
|
|
return dm.Root, nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(dirPath, "/")
|
|
|
|
dir := dm.Root
|
|
|
|
for i := 1; i < len(parts); i++ {
|
2016-03-31 03:34:11 +08:00
|
|
|
if sub, ok := dir.getChild(parts[i]); ok {
|
2014-04-10 00:44:58 +08:00
|
|
|
dir = sub
|
|
|
|
} else {
|
|
|
|
return dm.Root, fmt.Errorf("Directory %s Not Found", dirPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
2015-01-06 06:58:30 +08:00
|
|
|
func (dm *DirectoryManagerInMap) FindDirectory(dirPath string) (filer.DirectoryId, error) {
|
2014-04-10 00:44:58 +08:00
|
|
|
d, e := dm.findDirectory(dirPath)
|
|
|
|
if e == nil {
|
|
|
|
return d.Id, nil
|
|
|
|
}
|
|
|
|
return dm.Root.Id, e
|
|
|
|
}
|
|
|
|
|
2015-01-06 06:58:30 +08:00
|
|
|
func (dm *DirectoryManagerInMap) loadDirectory(dirPath string, dirId filer.DirectoryId) error {
|
2015-07-08 16:43:26 +08:00
|
|
|
dirPath = CleanFilePath(dirPath)
|
2014-04-10 00:44:58 +08:00
|
|
|
if dirPath == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(dirPath, "/")
|
|
|
|
dir := dm.Root
|
|
|
|
for i := 1; i < len(parts); i++ {
|
2016-03-31 03:34:11 +08:00
|
|
|
sub, ok := dir.getChild(parts[i])
|
2014-04-10 00:44:58 +08:00
|
|
|
if !ok {
|
2016-04-05 07:32:16 +08:00
|
|
|
writeLock.Lock()
|
|
|
|
if sub2, createdByOtherThread := dir.getChild(parts[i]); createdByOtherThread {
|
|
|
|
sub = sub2
|
|
|
|
} else {
|
|
|
|
if i != len(parts)-1 {
|
|
|
|
writeLock.Unlock()
|
|
|
|
return fmt.Errorf("%s should be created after parent %s", dirPath, parts[i])
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
sub, err = dm.newDirectoryEntryInMap(dir, parts[i])
|
|
|
|
if err != nil {
|
|
|
|
writeLock.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sub.Id != dirId {
|
|
|
|
writeLock.Unlock()
|
|
|
|
// the dir.log should be the same order as in-memory directory id
|
|
|
|
return fmt.Errorf("%s should be have id %v instead of %v", dirPath, sub.Id, dirId)
|
|
|
|
}
|
|
|
|
dir.addChild(parts[i], sub)
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
2016-04-05 07:32:16 +08:00
|
|
|
writeLock.Unlock()
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
dir = sub
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DirectoryManagerInMap) makeDirectory(dirPath string) (dir *DirectoryEntryInMap, created bool) {
|
2015-07-08 16:43:26 +08:00
|
|
|
dirPath = CleanFilePath(dirPath)
|
2014-04-10 00:44:58 +08:00
|
|
|
if dirPath == "/" {
|
|
|
|
return dm.Root, false
|
|
|
|
}
|
|
|
|
parts := strings.Split(dirPath, "/")
|
|
|
|
dir = dm.Root
|
|
|
|
for i := 1; i < len(parts); i++ {
|
2016-03-31 03:34:11 +08:00
|
|
|
sub, ok := dir.getChild(parts[i])
|
2014-04-10 00:44:58 +08:00
|
|
|
if !ok {
|
2016-04-05 07:32:16 +08:00
|
|
|
writeLock.Lock()
|
|
|
|
if sub2, createdByOtherThread := dir.getChild(parts[i]); createdByOtherThread {
|
|
|
|
sub = sub2
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
sub, err = dm.newDirectoryEntryInMap(dir, parts[i])
|
|
|
|
if err != nil {
|
|
|
|
writeLock.Unlock()
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
dir.addChild(parts[i], sub)
|
|
|
|
created = true
|
2014-09-05 10:34:43 +08:00
|
|
|
}
|
2016-04-05 07:32:16 +08:00
|
|
|
writeLock.Unlock()
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
dir = sub
|
|
|
|
}
|
|
|
|
return dir, created
|
|
|
|
}
|
|
|
|
|
2015-01-06 06:58:30 +08:00
|
|
|
func (dm *DirectoryManagerInMap) MakeDirectory(dirPath string) (filer.DirectoryId, error) {
|
2014-04-10 00:44:58 +08:00
|
|
|
dir, _ := dm.makeDirectory(dirPath)
|
|
|
|
return dir.Id, nil
|
|
|
|
}
|
|
|
|
|
2014-07-21 14:12:49 +08:00
|
|
|
func (dm *DirectoryManagerInMap) MoveUnderDirectory(oldDirPath string, newParentDirPath string, newName string) error {
|
2014-09-05 10:26:31 +08:00
|
|
|
writeLock.Lock()
|
|
|
|
defer writeLock.Unlock()
|
2014-04-10 00:44:58 +08:00
|
|
|
oldDir, oe := dm.findDirectory(oldDirPath)
|
|
|
|
if oe != nil {
|
|
|
|
return oe
|
|
|
|
}
|
|
|
|
parentDir, pe := dm.findDirectory(newParentDirPath)
|
|
|
|
if pe != nil {
|
|
|
|
return pe
|
|
|
|
}
|
2014-07-21 14:12:49 +08:00
|
|
|
dm.log("mov", oldDirPath, newParentDirPath, newName)
|
2016-03-31 03:34:11 +08:00
|
|
|
oldDir.Parent.removeChild(oldDir.Name)
|
2014-07-21 14:12:49 +08:00
|
|
|
if newName == "" {
|
|
|
|
newName = oldDir.Name
|
|
|
|
}
|
2016-03-31 03:34:11 +08:00
|
|
|
parentDir.addChild(newName, oldDir)
|
2014-07-21 14:12:49 +08:00
|
|
|
oldDir.Name = newName
|
2014-04-10 00:44:58 +08:00
|
|
|
oldDir.Parent = parentDir
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 06:58:30 +08:00
|
|
|
func (dm *DirectoryManagerInMap) ListDirectories(dirPath string) (dirNames []filer.DirectoryEntry, err error) {
|
2014-04-10 00:44:58 +08:00
|
|
|
d, e := dm.findDirectory(dirPath)
|
|
|
|
if e != nil {
|
|
|
|
return dirNames, e
|
|
|
|
}
|
2016-03-31 03:34:11 +08:00
|
|
|
return d.children(), nil
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
func (dm *DirectoryManagerInMap) DeleteDirectory(dirPath string) error {
|
2014-09-05 10:26:31 +08:00
|
|
|
writeLock.Lock()
|
|
|
|
defer writeLock.Unlock()
|
2014-04-10 00:44:58 +08:00
|
|
|
if dirPath == "/" {
|
|
|
|
return fmt.Errorf("Can not delete %s", dirPath)
|
|
|
|
}
|
|
|
|
d, e := dm.findDirectory(dirPath)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2016-03-31 03:34:11 +08:00
|
|
|
if d.hasChildren() {
|
2014-04-10 00:44:58 +08:00
|
|
|
return fmt.Errorf("dir %s still has sub directories", dirPath)
|
|
|
|
}
|
2016-03-31 03:34:11 +08:00
|
|
|
d.Parent.removeChild(d.Name)
|
2014-04-10 00:44:58 +08:00
|
|
|
d.Parent = nil
|
|
|
|
dm.log("del", dirPath)
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-08 16:43:26 +08:00
|
|
|
|
|
|
|
func CleanFilePath(fp string) string {
|
|
|
|
ret := filepath.Clean(fp)
|
|
|
|
if os.PathSeparator == '\\' {
|
|
|
|
return strings.Replace(ret, "\\", "/", -1)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|