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.

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