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.

85 lines
2.1 KiB

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