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.

84 lines
2.2 KiB

  1. package volume_info
  2. import (
  3. "fmt"
  4. "os"
  5. jsonpb "google.golang.org/protobuf/encoding/protojson"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  8. _ "github.com/seaweedfs/seaweedfs/weed/storage/backend/s3_backend"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. )
  11. // MaybeLoadVolumeInfo load the file data as *volume_server_pb.VolumeInfo, the returned volumeInfo will not be nil
  12. func MaybeLoadVolumeInfo(fileName string) (volumeInfo *volume_server_pb.VolumeInfo, hasRemoteFile bool, hasVolumeInfoFile bool, err error) {
  13. volumeInfo = &volume_server_pb.VolumeInfo{}
  14. glog.V(1).Infof("maybeLoadVolumeInfo checks %s", fileName)
  15. if exists, canRead, _, _, _ := util.CheckFile(fileName); !exists || !canRead {
  16. if !exists {
  17. return
  18. }
  19. hasVolumeInfoFile = true
  20. if !canRead {
  21. glog.Warningf("can not read %s", fileName)
  22. err = fmt.Errorf("can not read %s", fileName)
  23. return
  24. }
  25. return
  26. }
  27. hasVolumeInfoFile = true
  28. glog.V(1).Infof("maybeLoadVolumeInfo reads %s", fileName)
  29. tierData, readErr := os.ReadFile(fileName)
  30. if readErr != nil {
  31. glog.Warningf("fail to read %s : %v", fileName, readErr)
  32. err = fmt.Errorf("fail to read %s : %v", fileName, readErr)
  33. return
  34. }
  35. glog.V(1).Infof("maybeLoadVolumeInfo Unmarshal volume info %v", fileName)
  36. if err = jsonpb.Unmarshal(tierData, volumeInfo); err != nil {
  37. glog.Warningf("unmarshal error: %v", err)
  38. err = fmt.Errorf("unmarshal error: %v", err)
  39. return
  40. }
  41. if len(volumeInfo.GetFiles()) == 0 {
  42. return
  43. }
  44. hasRemoteFile = true
  45. return
  46. }
  47. func SaveVolumeInfo(fileName string, volumeInfo *volume_server_pb.VolumeInfo) error {
  48. if exists, _, canWrite, _, _ := util.CheckFile(fileName); exists && !canWrite {
  49. return fmt.Errorf("failed to check %s not writable", fileName)
  50. }
  51. m := jsonpb.MarshalOptions{
  52. AllowPartial: true,
  53. EmitUnpopulated: true,
  54. Indent: " ",
  55. }
  56. text, marshalErr := m.Marshal(volumeInfo)
  57. if marshalErr != nil {
  58. return fmt.Errorf("failed to marshal %s: %v", fileName, marshalErr)
  59. }
  60. writeErr := util.WriteFile(fileName, text, 0755)
  61. if writeErr != nil {
  62. return fmt.Errorf("failed to write %s: %v", fileName, writeErr)
  63. }
  64. return nil
  65. }