2018-05-08 08:59:43 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-05-10 06:18:02 +00:00
|
|
|
"strconv"
|
|
|
|
|
2018-05-08 08:59:43 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-05-10 06:18:02 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-05-08 08:59:43 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
|
2018-05-08 08:59:43 +00:00
|
|
|
|
|
|
|
found, fileId, err := fs.filer.LookupDirectoryEntry(req.Directory, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
return &filer_pb.LookupDirectoryEntryResponse{
|
|
|
|
Entry: &filer_pb.Entry{
|
2018-05-08 08:59:43 +00:00
|
|
|
Name: req.Name,
|
|
|
|
IsDirectory: fileId == "",
|
|
|
|
FileId: fileId,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
|
2018-05-08 08:59:43 +00:00
|
|
|
|
|
|
|
directoryNames, err := fs.filer.ListDirectories(req.Directory)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
files, err := fs.filer.ListFiles(req.Directory, "", 1000)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
resp := &filer_pb.ListEntriesResponse{}
|
2018-05-08 08:59:43 +00:00
|
|
|
for _, dir := range directoryNames {
|
2018-05-10 06:18:02 +00:00
|
|
|
resp.Entries = append(resp.Entries, &filer_pb.Entry{
|
2018-05-08 08:59:43 +00:00
|
|
|
Name: string(dir),
|
|
|
|
IsDirectory: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, fileEntry := range files {
|
2018-05-10 06:18:02 +00:00
|
|
|
resp.Entries = append(resp.Entries, &filer_pb.Entry{
|
2018-05-08 08:59:43 +00:00
|
|
|
Name: fileEntry.Name,
|
|
|
|
IsDirectory: false,
|
|
|
|
FileId: string(fileEntry.Id),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
func (fs *FilerServer) GetFileAttributes(ctx context.Context, req *filer_pb.GetFileAttributesRequest) (*filer_pb.GetFileAttributesResponse, error) {
|
2018-05-08 08:59:43 +00:00
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
attributes := &filer_pb.FuseAttributes{}
|
2018-05-08 08:59:43 +00:00
|
|
|
|
|
|
|
server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
head, err := util.Head(server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
attributes.FileSize, err = strconv.ParseUint(head.Get("Content-Length"), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
return &filer_pb.GetFileAttributesResponse{
|
2018-05-08 08:59:43 +00:00
|
|
|
Attributes: attributes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
func (fs *FilerServer) GetFileContent(ctx context.Context, req *filer_pb.GetFileContentRequest) (*filer_pb.GetFileContentResponse, error) {
|
2018-05-08 08:59:43 +00:00
|
|
|
|
|
|
|
server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
content, err := util.Get(server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
return &filer_pb.GetFileContentResponse{
|
2018-05-08 08:59:43 +00:00
|
|
|
Content: content,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
|
2018-05-08 08:59:43 +00:00
|
|
|
if req.IsDirectory {
|
|
|
|
err = fs.filer.DeleteDirectory(req.Directory+req.Name, false)
|
|
|
|
} else {
|
|
|
|
fid, err := fs.filer.DeleteFile(req.Directory + req.Name)
|
|
|
|
if err == nil && fid != "" {
|
|
|
|
err = operation.DeleteFile(fs.getMasterNode(), fid, fs.jwt(fid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|