You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. )
  11. func (s3a *S3ApiServer) mkdir(ctx context.Context, parentDirectoryPath string, dirName string, fn func(entry *filer_pb.Entry)) error {
  12. return s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  13. entry := &filer_pb.Entry{
  14. Name: dirName,
  15. IsDirectory: true,
  16. Attributes: &filer_pb.FuseAttributes{
  17. Mtime: time.Now().Unix(),
  18. Crtime: time.Now().Unix(),
  19. FileMode: uint32(0777 | os.ModeDir),
  20. Uid: OS_UID,
  21. Gid: OS_GID,
  22. },
  23. }
  24. if fn != nil {
  25. fn(entry)
  26. }
  27. request := &filer_pb.CreateEntryRequest{
  28. Directory: parentDirectoryPath,
  29. Entry: entry,
  30. }
  31. glog.V(1).Infof("mkdir: %v", request)
  32. if _, err := client.CreateEntry(ctx, request); err != nil {
  33. glog.V(0).Infof("mkdir %v: %v", request, err)
  34. return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
  35. }
  36. return nil
  37. })
  38. }
  39. func (s3a *S3ApiServer) mkFile(ctx context.Context, parentDirectoryPath string, fileName string, chunks []*filer_pb.FileChunk) error {
  40. return s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  41. entry := &filer_pb.Entry{
  42. Name: fileName,
  43. IsDirectory: false,
  44. Attributes: &filer_pb.FuseAttributes{
  45. Mtime: time.Now().Unix(),
  46. Crtime: time.Now().Unix(),
  47. FileMode: uint32(0770),
  48. Uid: OS_UID,
  49. Gid: OS_GID,
  50. },
  51. Chunks: chunks,
  52. }
  53. request := &filer_pb.CreateEntryRequest{
  54. Directory: parentDirectoryPath,
  55. Entry: entry,
  56. }
  57. glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
  58. if _, err := client.CreateEntry(ctx, request); err != nil {
  59. glog.V(0).Infof("create file %v:%v", request, err)
  60. return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
  61. }
  62. return nil
  63. })
  64. }
  65. func (s3a *S3ApiServer) list(ctx context.Context, parentDirectoryPath, prefix, startFrom string, inclusive bool, limit int) (entries []*filer_pb.Entry, err error) {
  66. err = s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  67. request := &filer_pb.ListEntriesRequest{
  68. Directory: parentDirectoryPath,
  69. Prefix: prefix,
  70. StartFromFileName: startFrom,
  71. InclusiveStartFrom: inclusive,
  72. Limit: uint32(limit),
  73. }
  74. glog.V(4).Infof("read directory: %v", request)
  75. resp, err := client.ListEntries(ctx, request)
  76. if err != nil {
  77. glog.V(0).Infof("read directory %v: %v", request, err)
  78. return fmt.Errorf("list dir %v: %v", parentDirectoryPath, err)
  79. }
  80. entries = resp.Entries
  81. return nil
  82. })
  83. return
  84. }
  85. func (s3a *S3ApiServer) rm(ctx context.Context, parentDirectoryPath string, entryName string, isDirectory, isDeleteData, isRecursive bool) error {
  86. return s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  87. request := &filer_pb.DeleteEntryRequest{
  88. Directory: parentDirectoryPath,
  89. Name: entryName,
  90. IsDeleteData: isDeleteData,
  91. IsRecursive: isRecursive,
  92. }
  93. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  94. if _, err := client.DeleteEntry(ctx, request); err != nil {
  95. glog.V(0).Infof("delete entry %v: %v", request, err)
  96. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  97. }
  98. return nil
  99. })
  100. }
  101. func (s3a *S3ApiServer) exists(ctx context.Context, parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  102. err = s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  103. request := &filer_pb.LookupDirectoryEntryRequest{
  104. Directory: parentDirectoryPath,
  105. Name: entryName,
  106. }
  107. glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  108. resp, err := client.LookupDirectoryEntry(ctx, request)
  109. if err != nil {
  110. glog.V(0).Infof("exists entry %v: %v", request, err)
  111. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  112. }
  113. exists = resp.Entry.IsDirectory == isDirectory
  114. return nil
  115. })
  116. return
  117. }
  118. func objectKey(key *string) *string {
  119. if strings.HasPrefix(*key, "/") {
  120. t := (*key)[1:]
  121. return &t
  122. }
  123. return key
  124. }