seaweedfs/weed/s3api/filer_util.go

189 lines
4.7 KiB
Go
Raw Normal View History

2018-09-03 20:03:16 +00:00
package s3api
import (
"context"
2018-09-03 23:47:00 +00:00
"fmt"
2019-12-13 08:22:37 +00:00
"io"
2018-09-03 23:47:00 +00:00
"os"
2019-07-08 19:37:20 +00:00
"strings"
2018-09-03 23:47:00 +00:00
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2018-09-03 20:03:16 +00:00
)
2020-02-26 06:23:59 +00:00
func (s3a *S3ApiServer) mkdir(parentDirectoryPath string, dirName string, fn func(entry *filer_pb.Entry)) error {
return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-09-03 20:03:16 +00:00
entry := &filer_pb.Entry{
Name: dirName,
IsDirectory: true,
Attributes: &filer_pb.FuseAttributes{
Mtime: time.Now().Unix(),
Crtime: time.Now().Unix(),
FileMode: uint32(0777 | os.ModeDir),
Uid: OS_UID,
Gid: OS_GID,
},
}
if fn != nil {
fn(entry)
}
2018-09-03 20:03:16 +00:00
request := &filer_pb.CreateEntryRequest{
Directory: parentDirectoryPath,
Entry: entry,
2018-09-03 20:03:16 +00:00
}
2018-09-09 23:25:43 +00:00
glog.V(1).Infof("mkdir: %v", request)
if err := filer_pb.CreateEntry(client, request); err != nil {
2019-09-16 16:48:30 +00:00
glog.V(0).Infof("mkdir %v: %v", request, err)
2018-09-03 20:03:16 +00:00
return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
}
return nil
})
}
2020-02-26 06:23:59 +00:00
func (s3a *S3ApiServer) mkFile(parentDirectoryPath string, fileName string, chunks []*filer_pb.FileChunk) error {
return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-09-09 23:25:43 +00:00
entry := &filer_pb.Entry{
Name: fileName,
IsDirectory: false,
Attributes: &filer_pb.FuseAttributes{
Mtime: time.Now().Unix(),
Crtime: time.Now().Unix(),
FileMode: uint32(0770),
Uid: OS_UID,
Gid: OS_GID,
},
Chunks: chunks,
}
request := &filer_pb.CreateEntryRequest{
Directory: parentDirectoryPath,
Entry: entry,
}
glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
if err := filer_pb.CreateEntry(client, request); err != nil {
2019-09-16 16:48:30 +00:00
glog.V(0).Infof("create file %v:%v", request, err)
2018-09-09 23:25:43 +00:00
return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
}
return nil
})
}
2020-02-26 06:23:59 +00:00
func (s3a *S3ApiServer) list(parentDirectoryPath, prefix, startFrom string, inclusive bool, limit int) (entries []*filer_pb.Entry, err error) {
2018-09-03 20:03:16 +00:00
err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-09-03 20:03:16 +00:00
request := &filer_pb.ListEntriesRequest{
2018-09-12 07:46:12 +00:00
Directory: parentDirectoryPath,
2018-09-09 23:25:43 +00:00
Prefix: prefix,
StartFromFileName: startFrom,
InclusiveStartFrom: inclusive,
Limit: uint32(limit),
2018-09-03 20:03:16 +00:00
}
glog.V(4).Infof("read directory: %v", request)
2020-02-26 06:23:59 +00:00
stream, err := client.ListEntries(context.Background(), request)
2018-09-03 20:03:16 +00:00
if err != nil {
glog.V(0).Infof("read directory %v: %v", request, err)
2018-09-03 20:03:16 +00:00
return fmt.Errorf("list dir %v: %v", parentDirectoryPath, err)
}
2019-12-13 08:22:37 +00:00
for {
resp, recvErr := stream.Recv()
if recvErr != nil {
if recvErr == io.EOF {
break
} else {
return recvErr
}
}
entries = append(entries, resp.Entry)
}
2018-09-03 20:03:16 +00:00
return nil
})
return
}
2018-09-03 20:16:26 +00:00
func (s3a *S3ApiServer) rm(parentDirectoryPath, entryName string, isDeleteData, isRecursive bool) error {
2018-09-03 20:16:26 +00:00
return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-09-03 20:16:26 +00:00
err := doDeleteEntry(client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
if err != nil {
return err
2018-09-03 20:16:26 +00:00
}
return nil
})
}
func doDeleteEntry(client filer_pb.SeaweedFilerClient, parentDirectoryPath string, entryName string, isDeleteData bool, isRecursive bool) error {
request := &filer_pb.DeleteEntryRequest{
Directory: parentDirectoryPath,
Name: entryName,
IsDeleteData: isDeleteData,
IsRecursive: isRecursive,
}
2020-02-25 22:38:36 +00:00
glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
if resp, err := client.DeleteEntry(context.Background(), request); err != nil {
glog.V(0).Infof("delete entry %v: %v", request, err)
return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
} else {
if resp.Error != "" {
return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, resp.Error)
2020-02-25 22:38:36 +00:00
}
}
return nil
2020-02-25 22:38:36 +00:00
}
2020-02-26 06:23:59 +00:00
func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
request := &filer_pb.LookupDirectoryEntryRequest{
Directory: parentDirectoryPath,
Name: entryName,
}
2018-09-12 07:46:12 +00:00
glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
resp, err := filer_pb.LookupEntry(client, request)
if err != nil {
2020-03-08 01:01:39 +00:00
if err == filer_pb.ErrNotFound {
exists = false
return nil
}
2019-09-16 16:48:30 +00:00
glog.V(0).Infof("exists entry %v: %v", request, err)
2018-09-09 23:25:43 +00:00
return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
}
exists = resp.Entry.IsDirectory == isDirectory
return nil
})
return
}
2019-07-08 19:37:20 +00:00
func objectKey(key *string) *string {
if strings.HasPrefix(*key, "/") {
t := (*key)[1:]
return &t
}
return key
2019-07-22 04:51:38 +00:00
}