filer_util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, fn func(entry *filer_pb.Entry)) error {
  14. return filer_pb.MkFile(s3a, parentDirectoryPath, fileName, chunks, fn)
  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. if len(entries) == 0 {
  25. isLast = true
  26. }
  27. return
  28. }
  29. func (s3a *S3ApiServer) rm(parentDirectoryPath, entryName string, isDeleteData, isRecursive bool) error {
  30. return s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  31. err := doDeleteEntry(client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
  32. if err != nil {
  33. return err
  34. }
  35. return nil
  36. })
  37. }
  38. func doDeleteEntry(client filer_pb.SeaweedFilerClient, parentDirectoryPath string, entryName string, isDeleteData bool, isRecursive bool) error {
  39. request := &filer_pb.DeleteEntryRequest{
  40. Directory: parentDirectoryPath,
  41. Name: entryName,
  42. IsDeleteData: isDeleteData,
  43. IsRecursive: isRecursive,
  44. }
  45. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  46. if resp, err := client.DeleteEntry(context.Background(), request); err != nil {
  47. glog.V(0).Infof("delete entry %v: %v", request, err)
  48. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  49. } else {
  50. if resp.Error != "" {
  51. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, resp.Error)
  52. }
  53. }
  54. return nil
  55. }
  56. func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  57. return filer_pb.Exists(s3a, parentDirectoryPath, entryName, isDirectory)
  58. }
  59. func (s3a *S3ApiServer) touch(parentDirectoryPath string, entryName string, entry *filer_pb.Entry) (err error) {
  60. return filer_pb.Touch(s3a, parentDirectoryPath, entryName, entry)
  61. }
  62. func (s3a *S3ApiServer) getEntry(parentDirectoryPath, entryName string) (entry *filer_pb.Entry, err error) {
  63. fullPath := util.NewFullPath(parentDirectoryPath, entryName)
  64. return filer_pb.GetEntry(s3a, fullPath)
  65. }
  66. func objectKey(key *string) *string {
  67. if strings.HasPrefix(*key, "/") {
  68. t := (*key)[1:]
  69. return &t
  70. }
  71. return key
  72. }