Browse Source

Merge pull request #472 from sparklxb/master

complement weed mount: add read and delete
pull/455/merge
Chris Lu 8 years ago
committed by GitHub
parent
commit
e936fe9001
  1. 39
      weed/command/mount_std.go
  2. 68
      weed/filer/client_operations.go
  3. 37
      weed/server/filer_server_handlers_api.go
  4. 2
      weed/server/filer_server_handlers_write.go
  5. 11
      weed/util/http_util.go

39
weed/command/mount_std.go

@ -56,6 +56,8 @@ func (WFS) Root() (fs.Node, error) {
return &Dir{Path: "/"}, nil return &Dir{Path: "/"}, nil
} }
var fileIdMap = make(map[uint64]filer.FileId)
type Dir struct { type Dir struct {
Id uint64 Id uint64
Path string Path string
@ -71,7 +73,7 @@ func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) { func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
if dirent, ok := dir.DirentMap[name]; ok { if dirent, ok := dir.DirentMap[name]; ok {
if dirent.Type == fuse.DT_File { if dirent.Type == fuse.DT_File {
return &File{dirent.Inode, dirent.Name}, nil
return &File{Id: dirent.Inode, FileId: fileIdMap[dirent.Inode], Name: dirent.Name}, nil
} }
return &Dir{ return &Dir{
Id: dirent.Inode, Id: dirent.Inode,
@ -101,20 +103,45 @@ func (dir *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dirent := fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File} dirent := fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File}
ret = append(ret, dirent) ret = append(ret, dirent)
dir.DirentMap[f.Name] = &dirent dir.DirentMap[f.Name] = &dirent
fileIdMap[fileInode] = f.Id
} }
} }
} }
return ret, nil return ret, nil
} }
func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
name := path.Join(dir.Path, req.Name)
err := filer.DeleteDirectoryOrFile(*mountOptions.filer, name, req.Dir)
if err != nil {
fmt.Printf("Delete file %s [ERROR] %s\n", name, err)
}
return err
}
type File struct { type File struct {
Id uint64
// FileId filer.FileId
Name string
Id uint64
FileId filer.FileId
Name string
} }
func (file *File) Attr(context context.Context, attr *fuse.Attr) error { func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
attr.Inode = file.Id attr.Inode = file.Id
attr.Mode = 0000
return nil
attr.Mode = 0444
ret, err := filer.GetFileSize(*mountOptions.filer, string(file.FileId))
if err == nil {
attr.Size = ret.Size
} else {
fmt.Printf("Get file %s attr [ERROR] %s\n", file.Name, err)
}
return err
}
func (file *File) ReadAll(ctx context.Context) ([]byte, error) {
ret, err := filer.GetFileContent(*mountOptions.filer, string(file.FileId))
if err == nil {
return ret.Content, nil
}
fmt.Printf("Get file %s content [ERROR] %s\n", file.Name, err)
return nil, err
} }

68
weed/filer/client_operations.go

@ -4,16 +4,16 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/url"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"net/url"
) )
type ApiRequest struct { type ApiRequest struct {
Command string //"listFiles", "listDirectories"
Command string //"listFiles", "listDirectories", "getFileSize"
Directory string Directory string
FileName string FileName string
FileId string
} }
type ListFilesResult struct { type ListFilesResult struct {
@ -21,16 +21,47 @@ type ListFilesResult struct {
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
func ListFiles(server string, directory string, fileName string) (*ListFilesResult, error) {
var ret ListFilesResult
if err := call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, &ret); err == nil {
func ListFiles(server string, directory string, fileName string) (ret *ListFilesResult, err error) {
ret = new(ListFilesResult)
if err = call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, ret); err == nil {
if ret.Error != "" {
return nil, errors.New(ret.Error)
}
return ret, nil
}
return nil, err
}
type GetFileSizeResult struct {
Size uint64
Error string `json:"error,omitempty"`
}
func GetFileSize(server string, fileId string) (ret *GetFileSizeResult, err error) {
ret = new(GetFileSizeResult)
if err = call(server, ApiRequest{Command: "getFileSize", FileId: fileId}, ret); err == nil {
if ret.Error != "" { if ret.Error != "" {
return nil, errors.New(ret.Error) return nil, errors.New(ret.Error)
} }
return &ret, nil
} else {
return nil, err
return ret, nil
} }
return nil, err
}
type GetFileContentResult struct {
Content []byte
Error string `json:"error,omitempty"`
}
func GetFileContent(server string, fileId string) (ret *GetFileContentResult, err error) {
ret = new(GetFileContentResult)
if err = call(server, ApiRequest{Command: "getFileContent", FileId: fileId}, ret); err == nil {
if ret.Error != "" {
return nil, errors.New(ret.Error)
}
return ret, nil
}
return nil, err
} }
type ListDirectoriesResult struct { type ListDirectoriesResult struct {
@ -38,16 +69,23 @@ type ListDirectoriesResult struct {
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
func ListDirectories(server string, directory string) (*ListDirectoriesResult, error) {
var ret ListDirectoriesResult
if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, &ret); err == nil {
func ListDirectories(server string, directory string) (ret *ListDirectoriesResult, err error) {
ret = new(ListDirectoriesResult)
if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, ret); err == nil {
if ret.Error != "" { if ret.Error != "" {
return nil, errors.New(ret.Error) return nil, errors.New(ret.Error)
} }
return &ret, nil
} else {
return nil, err
return ret, nil
}
return nil, err
}
func DeleteDirectoryOrFile(server string, path string, isDir bool) error {
destUrl := fmt.Sprintf("http://%s%s", server, path)
if isDir {
destUrl += "/?recursive=true"
} }
return util.Delete(destUrl, "")
} }
func call(server string, request ApiRequest, ret interface{}) error { func call(server string, request ApiRequest, ret interface{}) error {

37
weed/server/filer_server_handlers_api.go

@ -3,9 +3,12 @@ package weed_server
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strconv"
"github.com/chrislusf/seaweedfs/weed/filer" "github.com/chrislusf/seaweedfs/weed/filer"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/util"
) )
func (fs *FilerServer) apiHandler(w http.ResponseWriter, r *http.Request) { func (fs *FilerServer) apiHandler(w http.ResponseWriter, r *http.Request) {
@ -32,5 +35,39 @@ func (fs *FilerServer) apiHandler(w http.ResponseWriter, r *http.Request) {
res.Error = err.Error() res.Error = err.Error()
} }
writeJsonQuiet(w, r, http.StatusOK, res) writeJsonQuiet(w, r, http.StatusOK, res)
case "getFileSize":
res := filer.GetFileSizeResult{}
server, err := operation.LookupFileId(fs.getMasterNode(), apiRequest.FileId)
if err != nil {
res.Error = err.Error()
writeJsonQuiet(w, r, http.StatusOK, res)
return
}
head, err := util.Head(server)
if err != nil {
res.Error = err.Error()
writeJsonQuiet(w, r, http.StatusOK, res)
return
}
res.Size, err = strconv.ParseUint(head.Get("Content-Length"), 10, 0)
if err != nil {
res.Error = err.Error()
}
writeJsonQuiet(w, r, http.StatusOK, res)
case "getFileContent":
res := filer.GetFileContentResult{}
server, err := operation.LookupFileId(fs.getMasterNode(), apiRequest.FileId)
if err != nil {
res.Error = err.Error()
writeJsonQuiet(w, r, http.StatusOK, res)
return
}
res.Content, err = util.Get(server)
if err != nil {
res.Error = err.Error()
writeJsonQuiet(w, r, http.StatusOK, res)
return
}
writeJsonQuiet(w, r, http.StatusOK, res)
} }
} }

2
weed/server/filer_server_handlers_write.go

@ -532,7 +532,7 @@ func (fs *FilerServer) doUpload(urlLocation string, w http.ResponseWriter, r *ht
} }
// curl -X DELETE http://localhost:8888/path/to // curl -X DELETE http://localhost:8888/path/to
// curl -X DELETE http://localhost:8888/path/to?recursive=true
// curl -X DELETE http://localhost:8888/path/to/?recursive=true
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) { func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
var err error var err error
var fid string var fid string

11
weed/util/http_util.go

@ -74,6 +74,17 @@ func Get(url string) ([]byte, error) {
return b, nil return b, nil
} }
func Head(url string) (http.Header, error) {
r, err := client.Head(url)
if err != nil {
return nil, err
}
if r.StatusCode >= 400 {
return nil, fmt.Errorf("%s: %s", url, r.Status)
}
return r.Header, nil
}
func Delete(url string, jwt security.EncodedJwt) error { func Delete(url string, jwt security.EncodedJwt) error {
req, err := http.NewRequest("DELETE", url, nil) req, err := http.NewRequest("DELETE", url, nil)
if jwt != "" { if jwt != "" {

Loading…
Cancel
Save