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.

229 lines
5.6 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, isDirectory, isDeleteData, isRecursive bool) error {
  97. return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  98. request := &filer_pb.DeleteEntryRequest{
  99. Directory: parentDirectoryPath,
  100. Name: entryName,
  101. IsDeleteData: isDeleteData,
  102. IsRecursive: isRecursive,
  103. }
  104. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  105. if _, err := client.DeleteEntry(context.Background(), request); err != nil {
  106. glog.V(0).Infof("delete entry %v: %v", request, err)
  107. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  108. }
  109. return nil
  110. })
  111. }
  112. func (s3a *S3ApiServer) streamRemove(quiet bool, fn func() (finished bool, parentDirectoryPath string, entryName string, isDeleteData, isRecursive bool), respFn func(err string)) error {
  113. return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  114. stream, err := client.StreamDeleteEntries(context.Background())
  115. if err != nil {
  116. glog.V(0).Infof("stream delete entry: %v", err)
  117. return fmt.Errorf("stream delete entry: %v", err)
  118. }
  119. waitc := make(chan struct{})
  120. go func() {
  121. for {
  122. resp, err := stream.Recv()
  123. if err == io.EOF {
  124. // read done.
  125. close(waitc)
  126. return
  127. }
  128. if err != nil {
  129. glog.V(0).Infof("streamRemove: %v", err)
  130. return
  131. }
  132. respFn(resp.Error)
  133. }
  134. }()
  135. for {
  136. finished, parentDirectoryPath, entryName, isDeleteData, isRecursive := fn()
  137. if finished {
  138. break
  139. }
  140. err = stream.Send(&filer_pb.DeleteEntryRequest{
  141. Directory: parentDirectoryPath,
  142. Name: entryName,
  143. IsDeleteData: isDeleteData,
  144. IsRecursive: isRecursive,
  145. IgnoreRecursiveError: quiet,
  146. })
  147. if err != nil {
  148. glog.V(0).Infof("streamRemove: %v", err)
  149. break
  150. }
  151. }
  152. stream.CloseSend()
  153. <-waitc
  154. return err
  155. })
  156. }
  157. func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  158. err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  159. request := &filer_pb.LookupDirectoryEntryRequest{
  160. Directory: parentDirectoryPath,
  161. Name: entryName,
  162. }
  163. glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  164. resp, err := filer_pb.LookupEntry(client, request)
  165. if err != nil {
  166. if err == filer_pb.ErrNotFound {
  167. exists = false
  168. return nil
  169. }
  170. glog.V(0).Infof("exists entry %v: %v", request, err)
  171. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  172. }
  173. exists = resp.Entry.IsDirectory == isDirectory
  174. return nil
  175. })
  176. return
  177. }
  178. func objectKey(key *string) *string {
  179. if strings.HasPrefix(*key, "/") {
  180. t := (*key)[1:]
  181. return &t
  182. }
  183. return key
  184. }