2019-12-29 04:28:58 +08:00
|
|
|
package pb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
_ "github.com/chrislusf/seaweedfs/weed/storage/backend/s3_backend"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaybeLoadVolumeInfo load the file data as *volume_server_pb.VolumeInfo, the returned volumeInfo will not be nil
|
2020-02-03 07:37:23 +08:00
|
|
|
func MaybeLoadVolumeInfo(fileName string) (*volume_server_pb.VolumeInfo, bool, error) {
|
2019-12-29 04:28:58 +08:00
|
|
|
|
|
|
|
volumeInfo := &volume_server_pb.VolumeInfo{}
|
|
|
|
|
|
|
|
glog.V(1).Infof("maybeLoadVolumeInfo checks %s", fileName)
|
|
|
|
if exists, canRead, _, _, _ := util.CheckFile(fileName); !exists || !canRead {
|
|
|
|
if !exists {
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, false, nil
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
if !canRead {
|
|
|
|
glog.Warningf("can not read %s", fileName)
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, false, fmt.Errorf("can not read %s", fileName)
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, false, nil
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("maybeLoadVolumeInfo reads %s", fileName)
|
|
|
|
tierData, readErr := ioutil.ReadFile(fileName)
|
|
|
|
if readErr != nil {
|
|
|
|
glog.Warningf("fail to read %s : %v", fileName, readErr)
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, false, fmt.Errorf("fail to read %s : %v", fileName, readErr)
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("maybeLoadVolumeInfo Unmarshal volume info %v", fileName)
|
|
|
|
if err := jsonpb.Unmarshal(bytes.NewReader(tierData), volumeInfo); err != nil {
|
|
|
|
glog.Warningf("unmarshal error: %v", err)
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, false, fmt.Errorf("unmarshal error: %v", err)
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(volumeInfo.GetFiles()) == 0 {
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, false, nil
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:37:23 +08:00
|
|
|
return volumeInfo, true, nil
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func SaveVolumeInfo(fileName string, volumeInfo *volume_server_pb.VolumeInfo) error {
|
|
|
|
|
2019-12-29 13:40:12 +08:00
|
|
|
if exists, _, canWrite, _, _ := util.CheckFile(fileName); exists && !canWrite {
|
|
|
|
return fmt.Errorf("%s not writable", fileName)
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m := jsonpb.Marshaler{
|
|
|
|
EmitDefaults: true,
|
|
|
|
Indent: " ",
|
|
|
|
}
|
|
|
|
|
|
|
|
text, marshalErr := m.MarshalToString(volumeInfo)
|
|
|
|
if marshalErr != nil {
|
|
|
|
return fmt.Errorf("marshal to %s: %v", fileName, marshalErr)
|
|
|
|
}
|
|
|
|
|
2019-12-29 13:40:12 +08:00
|
|
|
writeErr := ioutil.WriteFile(fileName, []byte(text), 0755)
|
2019-12-29 04:28:58 +08:00
|
|
|
if writeErr != nil {
|
2019-12-29 13:40:12 +08:00
|
|
|
return fmt.Errorf("fail to write %s : %v", fileName, writeErr)
|
2019-12-29 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|