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.

102 lines
2.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  6. "github.com/golang/protobuf/proto"
  7. )
  8. func InsertMountMapping(filerClient filer_pb.FilerClient, dir string, remoteStorageLocation *remote_pb.RemoteStorageLocation) (err error) {
  9. // read current mapping
  10. var oldContent, newContent []byte
  11. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  12. oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  13. return err
  14. })
  15. if err != nil {
  16. if err != filer_pb.ErrNotFound {
  17. return fmt.Errorf("read existing mapping: %v", err)
  18. }
  19. }
  20. // add new mapping
  21. newContent, err = addRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  22. if err != nil {
  23. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  24. }
  25. // save back
  26. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  27. return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
  28. })
  29. if err != nil {
  30. return fmt.Errorf("save mapping: %v", err)
  31. }
  32. return nil
  33. }
  34. func DeleteMountMapping(filerClient filer_pb.FilerClient, dir string) (err error) {
  35. // read current mapping
  36. var oldContent, newContent []byte
  37. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  38. oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  39. return err
  40. })
  41. if err != nil {
  42. if err != filer_pb.ErrNotFound {
  43. return fmt.Errorf("read existing mapping: %v", err)
  44. }
  45. }
  46. // add new mapping
  47. newContent, err = removeRemoteStorageMapping(oldContent, dir)
  48. if err != nil {
  49. return fmt.Errorf("delete mount %s: %v", dir, err)
  50. }
  51. // save back
  52. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  53. return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
  54. })
  55. if err != nil {
  56. return fmt.Errorf("save mapping: %v", err)
  57. }
  58. return nil
  59. }
  60. func addRemoteStorageMapping(oldContent []byte, dir string, storageLocation *remote_pb.RemoteStorageLocation) (newContent []byte, err error) {
  61. mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
  62. if unmarshalErr != nil {
  63. // skip
  64. }
  65. // set the new mapping
  66. mappings.Mappings[dir] = storageLocation
  67. if newContent, err = proto.Marshal(mappings); err != nil {
  68. return oldContent, fmt.Errorf("marshal mappings: %v", err)
  69. }
  70. return
  71. }
  72. func removeRemoteStorageMapping(oldContent []byte, dir string) (newContent []byte, err error) {
  73. mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
  74. if unmarshalErr != nil {
  75. return nil, unmarshalErr
  76. }
  77. // set the new mapping
  78. delete(mappings.Mappings, dir)
  79. if newContent, err = proto.Marshal(mappings); err != nil {
  80. return oldContent, fmt.Errorf("marshal mappings: %v", err)
  81. }
  82. return
  83. }