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.

150 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
  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(parentDirectoryPath string, dirName string, fn func(entry *filer_pb.Entry)) error {
  11. return s3a.withFilerClient(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(context.Background(), 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(parentDirectoryPath string, fileName string, chunks []*filer_pb.FileChunk) error {
  38. return s3a.withFilerClient(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(context.Background(), 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(parentDirectoryPath, prefix, startFrom string, inclusive bool, limit int) (entries []*filer_pb.Entry, err error) {
  63. err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  64. request := &filer_pb.ListEntriesRequest{
  65. Directory: s3a.option.BucketsPath,
  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(context.Background(), 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(parentDirectoryPath string, entryName string, isDirectory, isDeleteData, isRecursive bool) error {
  82. return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  83. ctx := context.Background()
  84. request := &filer_pb.DeleteEntryRequest{
  85. Directory: parentDirectoryPath,
  86. Name: entryName,
  87. IsDirectory: isDirectory,
  88. IsDeleteData: isDeleteData,
  89. IsRecursive: isRecursive,
  90. }
  91. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  92. if _, err := client.DeleteEntry(ctx, request); err != nil {
  93. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  94. }
  95. return nil
  96. })
  97. }
  98. func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  99. err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  100. ctx := context.Background()
  101. request := &filer_pb.LookupDirectoryEntryRequest{
  102. Directory: parentDirectoryPath,
  103. Name: entryName,
  104. }
  105. glog.V(1).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  106. resp, err := client.LookupDirectoryEntry(ctx, request)
  107. if err != nil {
  108. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  109. }
  110. exists = resp.Entry.IsDirectory == isDirectory
  111. return nil
  112. })
  113. return
  114. }