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.

116 lines
2.8 KiB

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("create bucket: %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) list(parentDirectoryPath string) (entries []*filer_pb.Entry, err error) {
  38. err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  39. request := &filer_pb.ListEntriesRequest{
  40. Directory: s3a.option.BucketsPath,
  41. }
  42. glog.V(4).Infof("read directory: %v", request)
  43. resp, err := client.ListEntries(context.Background(), request)
  44. if err != nil {
  45. return fmt.Errorf("list dir %v: %v", parentDirectoryPath, err)
  46. }
  47. entries = resp.Entries
  48. return nil
  49. })
  50. return
  51. }
  52. func (s3a *S3ApiServer) rm(parentDirectoryPath string, entryName string, isDirectory, isDeleteData, isRecursive bool) error {
  53. return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  54. ctx := context.Background()
  55. request := &filer_pb.DeleteEntryRequest{
  56. Directory: parentDirectoryPath,
  57. Name: entryName,
  58. IsDirectory: isDirectory,
  59. IsDeleteData: isDeleteData,
  60. IsRecursive: isRecursive,
  61. }
  62. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  63. if _, err := client.DeleteEntry(ctx, request); err != nil {
  64. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  65. }
  66. return nil
  67. })
  68. }
  69. func (s3a *S3ApiServer) exists(parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  70. err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  71. ctx := context.Background()
  72. request := &filer_pb.LookupDirectoryEntryRequest{
  73. Directory: parentDirectoryPath,
  74. Name: entryName,
  75. }
  76. glog.V(1).Infof("delete entry %v/%v: %v", parentDirectoryPath, entryName, request)
  77. resp, err := client.LookupDirectoryEntry(ctx, request)
  78. if err != nil {
  79. return fmt.Errorf("delete entry %s/%s: %v", parentDirectoryPath, entryName, err)
  80. }
  81. exists = resp.Entry.IsDirectory == isDirectory
  82. return nil
  83. })
  84. return
  85. }