From 5f4dc1140974d0481921337cd53c692a15acabe6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 9 Apr 2014 21:01:48 -0700 Subject: [PATCH] 1. root dir has id of 0 2. only delete empty folders 3. correct listing files under a folder --- go/filer/directory_in_map.go | 2 +- go/filer/filer_embedded.go | 8 ++++++++ go/filer/filer_in_leveldb.go | 17 ++++++++++------- go/weed/weed_server/filer_server.go | 2 +- go/weed/weed_server/filer_server_handlers.go | 10 ++++++---- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/go/filer/directory_in_map.go b/go/filer/directory_in_map.go index 606abe20d..4a900d4ed 100644 --- a/go/filer/directory_in_map.go +++ b/go/filer/directory_in_map.go @@ -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) diff --git a/go/filer/filer_embedded.go b/go/filer/filer_embedded.go index b1ef38be4..12783bc37 100644 --- a/go/filer/filer_embedded.go +++ b/go/filer/filer_embedded.go @@ -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) { diff --git a/go/filer/filer_in_leveldb.go b/go/filer/filer_in_leveldb.go index 4d0af7073..aef43a7c3 100644 --- a/go/filer/filer_in_leveldb.go +++ b/go/filer/filer_in_leveldb.go @@ -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 diff --git a/go/weed/weed_server/filer_server.go b/go/weed/weed_server/filer_server.go index 60116f425..c91839ac0 100644 --- a/go/weed/weed_server/filer_server.go +++ b/go/weed/weed_server/filer_server.go @@ -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 { - glog.Fatal("Can not start filer in dir:", dir) + glog.Fatal("Can not start filer in dir", dir, ": ", err.Error()) return } diff --git a/go/weed/weed_server/filer_server_handlers.go b/go/weed/weed_server/filer_server_handlers.go index 3de4b2d0b..31e9d0c85 100644 --- a/go/weed/weed_server/filer_server_handlers.go +++ b/go/weed/weed_server/filer_server_handlers.go @@ -43,12 +43,12 @@ func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Reque m := make(map[string]interface{}) m["Directory"] = r.URL.Path m["Subdirectories"] = dirlist - lastFile := r.FormValue("lastFile") + lastFileName := r.FormValue("lastFileName") limit, limit_err := strconv.Atoi(r.FormValue("limit")) if limit_err != nil { 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) } 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) } } - if err != nil { - glog.V(1).Infoln("deleting", r.URL.Path, ":", err.Error()) + if err == nil { w.WriteHeader(http.StatusAccepted) + writeJsonQuiet(w, r, map[string]string{"error": ""}) } else { + glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error()) w.WriteHeader(http.StatusInternalServerError) + writeJsonError(w, r, err) } }