filer_util.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package s3api
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "strings"
  9. )
  10. func (s3a *S3ApiServer) mkdir(parentDirectoryPath string, dirName string, fn func(entry *filer_pb.Entry)) error {
  11. return filer_pb.Mkdir(s3a, parentDirectoryPath, dirName, fn)
  12. }
  13. func (s3a *S3ApiServer) mkFile(parentDirectoryPath string, fileName string, chunks []*filer_pb.FileChunk) error {
  14. return filer_pb.MkFile(s3a, parentDirectoryPath, fileName, chunks)
  15. }
  16. func (s3a *S3ApiServer) list(parentDirectoryPath, prefix, startFrom string, inclusive bool, limit uint32) (entries []*filer_pb.Entry, isLast bool, err error) {
  17. err = filer_pb.List(s3a, parentDirectoryPath, prefix, func(entry *filer_pb.Entry, isLastEntry bool) error {
  18. entries = append(entries, entry)
  19. if isLastEntry {
  20. isLast = true
  21. }
  22. return nil
  23. }, startFrom, inclusive, limit)
  24. return
  25. }
  26. func (s3a *S3ApiServer) rm(parentDirectoryPath, entryName string, isDeleteData, isRecursive bool) error {
  27. return s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  28. err := doDeleteEntry(client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
  29. if err != nil {
  30. return err
  31. }
  32. return nil
  33. })
  34. }
  35. func doDeleteEntry(client filer_pb.SeaweedFilerClient, parentDirectoryPath string, entryName string, isDeleteData bool, isRecursive bool) error {
  36. request := &filer_pb.DeleteEntryRequest{
  37. Directory: parentDirectoryPath,
  38. Name: entryName,
  39. IsDeleteData: isDeleteData,
  40. IsRecursive: isRecursive,
  41. }
  42. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  43. if resp, err := client.DeleteEntry(context.Background(), request); err != nil {
  44. glog.V(0).Infof("delete entry %v: %v", request, err)
  45. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  46. } else {
  47. if resp.Error != "" {
  48. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, resp.Error)
  49. }
  50. }
  51. return nil
  52. }
  53. func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  54. return filer_pb.Exists(s3a, parentDirectoryPath, entryName, isDirectory)
  55. }
  56. func (s3a *S3ApiServer) getEntry(parentDirectoryPath, entryName string) (entry *filer_pb.Entry, err error) {
  57. fullPath := util.NewFullPath(parentDirectoryPath, entryName)
  58. return filer_pb.GetEntry(s3a, fullPath)
  59. }
  60. func objectKey(key *string) *string {
  61. if strings.HasPrefix(*key, "/") {
  62. t := (*key)[1:]
  63. return &t
  64. }
  65. return key
  66. }