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) { func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryInMap, name string) (d *DirectoryEntryInMap) {
d = &DirectoryEntryInMap{Name: name, Parent: parent} d = &DirectoryEntryInMap{Name: name, Parent: parent}
d.SubDirectories = make(map[string]*DirectoryEntryInMap) d.SubDirectories = make(map[string]*DirectoryEntryInMap)
d.Id = dm.max
dm.max++ dm.max++
d.Id = dm.max
parts := make([]string, 0) parts := make([]string, 0)
for p := d; p != nil && p.Name != ""; p = p.Parent { for p := d; p != nil && p.Name != ""; p = p.Parent {
parts = append(parts, p.Name) parts = append(parts, p.Name)

View file

@ -1,6 +1,7 @@
package filer package filer
import ( import (
"fmt"
"path/filepath" "path/filepath"
) )
@ -52,6 +53,13 @@ func (filer *FilerEmbedded) ListFiles(dirPath string, lastFileName string, limit
return filer.files.ListFiles(dirId, lastFileName, limit), nil return filer.files.ListFiles(dirId, lastFileName, limit), nil
} }
func (filer *FilerEmbedded) DeleteDirectory(dirPath string) (err error) { 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) return filer.directories.DeleteDirectory(dirPath)
} }
func (filer *FilerEmbedded) DeleteFile(filePath string) (fid string, err error) { 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) glog.V(4).Infoln("directory", dirId, "lastFileName", lastFileName, "limit", limit)
dirKey := genKey(dirId, "") dirKey := genKey(dirId, "")
iter := fl.db.NewIterator(&util.Range{Start: genKey(dirId, lastFileName)}, nil) iter := fl.db.NewIterator(&util.Range{Start: genKey(dirId, lastFileName)}, nil)
limitCounter := -1 limitCounter := 0
for iter.Next() { for iter.Next() {
key := iter.Key()
if !bytes.HasPrefix(key, dirKey) {
break
}
fileName := string(key[len(dirKey):])
if fileName == lastFileName {
continue
}
limitCounter++ limitCounter++
if limit > 0 { if limit > 0 {
if limitCounter > limit { if limitCounter > limit {
break break
} }
} }
key := iter.Key() files = append(files, FileEntry{Name: fileName, Id: FileId(string(iter.Value()))})
if !bytes.HasPrefix(key, dirKey) {
break
}
fileName := key[len(dirKey):]
files = append(files, FileEntry{Name: string(fileName), Id: FileId(string(iter.Value()))})
} }
iter.Release() iter.Release()
return return

View file

@ -22,7 +22,7 @@ func NewFilerServer(r *http.ServeMux, port int, master string, dir string, colle
} }
if fs.filer, err = filer.NewFilerEmbedded(dir); err != nil { if fs.filer, err = filer.NewFilerEmbedded(dir); err != nil {
glog.Fatal("Can not start filer in dir:", dir) glog.Fatal("Can not start filer in dir", dir, ": ", err.Error())
return return
} }

View file

@ -43,12 +43,12 @@ func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Reque
m := make(map[string]interface{}) m := make(map[string]interface{})
m["Directory"] = r.URL.Path m["Directory"] = r.URL.Path
m["Subdirectories"] = dirlist m["Subdirectories"] = dirlist
lastFile := r.FormValue("lastFile") lastFileName := r.FormValue("lastFileName")
limit, limit_err := strconv.Atoi(r.FormValue("limit")) limit, limit_err := strconv.Atoi(r.FormValue("limit"))
if limit_err != nil { if limit_err != nil {
limit = 100 limit = 100
} }
m["Files"], _ = fs.filer.ListFiles(r.URL.Path, lastFile, limit) m["Files"], _ = fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
writeJsonQuiet(w, r, m) writeJsonQuiet(w, r, m)
} }
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) { func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
@ -190,10 +190,12 @@ func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
err = operation.DeleteFile(fs.master, fid) err = operation.DeleteFile(fs.master, fid)
} }
} }
if err != nil { if err == nil {
glog.V(1).Infoln("deleting", r.URL.Path, ":", err.Error())
w.WriteHeader(http.StatusAccepted) w.WriteHeader(http.StatusAccepted)
writeJsonQuiet(w, r, map[string]string{"error": ""})
} else { } else {
glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
writeJsonError(w, r, err)
} }
} }