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.

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