filer_util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package s3api
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/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. IgnoreRecursiveError: true,
  45. }
  46. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  47. if resp, err := client.DeleteEntry(context.Background(), request); err != nil {
  48. glog.V(0).Infof("delete entry %v: %v", request, err)
  49. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  50. } else {
  51. if resp.Error != "" {
  52. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, resp.Error)
  53. }
  54. }
  55. return nil
  56. }
  57. func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  58. return filer_pb.Exists(s3a, parentDirectoryPath, entryName, isDirectory)
  59. }
  60. func (s3a *S3ApiServer) touch(parentDirectoryPath string, entryName string, entry *filer_pb.Entry) (err error) {
  61. return filer_pb.Touch(s3a, parentDirectoryPath, entryName, entry)
  62. }
  63. func (s3a *S3ApiServer) getEntry(parentDirectoryPath, entryName string) (entry *filer_pb.Entry, err error) {
  64. fullPath := util.NewFullPath(parentDirectoryPath, entryName)
  65. return filer_pb.GetEntry(s3a, fullPath)
  66. }
  67. func (s3a *S3ApiServer) updateEntry(parentDirectoryPath string, newEntry *filer_pb.Entry) error {
  68. updateEntryRequest := &filer_pb.UpdateEntryRequest{
  69. Directory: parentDirectoryPath,
  70. Entry: newEntry,
  71. }
  72. err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  73. err := filer_pb.UpdateEntry(client, updateEntryRequest)
  74. if err != nil {
  75. return err
  76. }
  77. return nil
  78. })
  79. return err
  80. }
  81. func (s3a *S3ApiServer) getCollectionName(bucket string) string {
  82. if s3a.option.FilerGroup != "" {
  83. return fmt.Sprintf("%s_%s", s3a.option.FilerGroup, bucket)
  84. }
  85. return bucket
  86. }
  87. func objectKey(key *string) *string {
  88. if strings.HasPrefix(*key, "/") {
  89. t := (*key)[1:]
  90. return &t
  91. }
  92. return key
  93. }