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.

154 lines
3.9 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
  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. return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
  34. }
  35. return nil
  36. })
  37. }
  38. func (s3a *S3ApiServer) mkFile(ctx context.Context, parentDirectoryPath string, fileName string, chunks []*filer_pb.FileChunk) error {
  39. return s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  40. entry := &filer_pb.Entry{
  41. Name: fileName,
  42. IsDirectory: false,
  43. Attributes: &filer_pb.FuseAttributes{
  44. Mtime: time.Now().Unix(),
  45. Crtime: time.Now().Unix(),
  46. FileMode: uint32(0770),
  47. Uid: OS_UID,
  48. Gid: OS_GID,
  49. },
  50. Chunks: chunks,
  51. }
  52. request := &filer_pb.CreateEntryRequest{
  53. Directory: parentDirectoryPath,
  54. Entry: entry,
  55. }
  56. glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
  57. if _, err := client.CreateEntry(ctx, request); err != nil {
  58. return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
  59. }
  60. return nil
  61. })
  62. }
  63. func (s3a *S3ApiServer) list(ctx context.Context, parentDirectoryPath, prefix, startFrom string, inclusive bool, limit int) (entries []*filer_pb.Entry, err error) {
  64. err = s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  65. request := &filer_pb.ListEntriesRequest{
  66. Directory: parentDirectoryPath,
  67. Prefix: prefix,
  68. StartFromFileName: startFrom,
  69. InclusiveStartFrom: inclusive,
  70. Limit: uint32(limit),
  71. }
  72. glog.V(4).Infof("read directory: %v", request)
  73. resp, err := client.ListEntries(ctx, request)
  74. if err != nil {
  75. return fmt.Errorf("list dir %v: %v", parentDirectoryPath, err)
  76. }
  77. entries = resp.Entries
  78. return nil
  79. })
  80. return
  81. }
  82. func (s3a *S3ApiServer) rm(ctx context.Context, parentDirectoryPath string, entryName string, isDirectory, isDeleteData, isRecursive bool) error {
  83. return s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  84. request := &filer_pb.DeleteEntryRequest{
  85. Directory: parentDirectoryPath,
  86. Name: entryName,
  87. IsDeleteData: isDeleteData,
  88. IsRecursive: isRecursive,
  89. }
  90. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  91. if _, err := client.DeleteEntry(ctx, request); err != nil {
  92. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  93. }
  94. return nil
  95. })
  96. }
  97. func (s3a *S3ApiServer) exists(ctx context.Context, parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  98. err = s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  99. request := &filer_pb.LookupDirectoryEntryRequest{
  100. Directory: parentDirectoryPath,
  101. Name: entryName,
  102. }
  103. glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  104. resp, err := client.LookupDirectoryEntry(ctx, request)
  105. if err != nil {
  106. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  107. }
  108. exists = resp.Entry.IsDirectory == isDirectory
  109. return nil
  110. })
  111. return
  112. }
  113. func objectKey(key *string) *string {
  114. if strings.HasPrefix(*key, "/") {
  115. t := (*key)[1:]
  116. return &t
  117. }
  118. return key
  119. }