1. root dir has id of 0

2. only delete empty folders
3. correct listing files under a folder
This commit is contained in:
Chris Lu
2014-04-09 21:01:48 -07:00
parent abde40377c
commit 5f4dc11409
5 changed files with 26 additions and 13 deletions

View File

@@ -28,8 +28,8 @@ type DirectoryManagerInMap struct {
func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryInMap, name string) (d *DirectoryEntryInMap) {
d = &DirectoryEntryInMap{Name: name, Parent: parent}
d.SubDirectories = make(map[string]*DirectoryEntryInMap)
d.Id = dm.max
dm.max++
d.Id = dm.max
parts := make([]string, 0)
for p := d; p != nil && p.Name != ""; p = p.Parent {
parts = append(parts, p.Name)

View File

@@ -1,6 +1,7 @@
package filer
import (
"fmt"
"path/filepath"
)
@@ -52,6 +53,13 @@ func (filer *FilerEmbedded) ListFiles(dirPath string, lastFileName string, limit
return filer.files.ListFiles(dirId, lastFileName, limit), nil
}
func (filer *FilerEmbedded) DeleteDirectory(dirPath string) (err error) {
dirId, e := filer.directories.FindDirectory(dirPath)
if e != nil {
return e
}
if len(filer.files.ListFiles(dirId, "", 1)) > 0 {
return fmt.Errorf("Fail to delete non-empty directory %s!", dirPath)
}
return filer.directories.DeleteDirectory(dirPath)
}
func (filer *FilerEmbedded) DeleteFile(filePath string) (fid string, err error) {

View File

@@ -50,20 +50,23 @@ func (fl *FileListInLevelDb) ListFiles(dirId DirectoryId, lastFileName string, l
glog.V(4).Infoln("directory", dirId, "lastFileName", lastFileName, "limit", limit)
dirKey := genKey(dirId, "")
iter := fl.db.NewIterator(&util.Range{Start: genKey(dirId, lastFileName)}, nil)
limitCounter := -1
limitCounter := 0
for iter.Next() {
key := iter.Key()
if !bytes.HasPrefix(key, dirKey) {
break
}
fileName := string(key[len(dirKey):])
if fileName == lastFileName {
continue
}
limitCounter++
if limit > 0 {
if limitCounter > limit {
break
}
}
key := iter.Key()
if !bytes.HasPrefix(key, dirKey) {
break
}
fileName := key[len(dirKey):]
files = append(files, FileEntry{Name: string(fileName), Id: FileId(string(iter.Value()))})
files = append(files, FileEntry{Name: fileName, Id: FileId(string(iter.Value()))})
}
iter.Release()
return