aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchrislu <chris.lu@gmail.com>2022-02-04 11:14:04 -0800
committerchrislu <chris.lu@gmail.com>2022-02-04 11:14:04 -0800
commit76e297d64f76f5054a2aafe752d406c81ab3fada (patch)
tree468d7e6fba48d59d61c6032ec303855ef4fc4e60
parentd8e6bd0a942e2d2429061341c42dcfd243718ff8 (diff)
downloadseaweedfs-76e297d64f76f5054a2aafe752d406c81ab3fada.tar.xz
seaweedfs-76e297d64f76f5054a2aafe752d406c81ab3fada.zip
sync call to write file, avoid vif loading error
fix https://github.com/chrislusf/seaweedfs/issues/2633
-rw-r--r--weed/storage/volume_info/volume_info.go2
-rw-r--r--weed/util/file_util.go17
2 files changed, 18 insertions, 1 deletions
diff --git a/weed/storage/volume_info/volume_info.go b/weed/storage/volume_info/volume_info.go
index fa906e1d4..aa815970c 100644
--- a/weed/storage/volume_info/volume_info.go
+++ b/weed/storage/volume_info/volume_info.go
@@ -75,7 +75,7 @@ func SaveVolumeInfo(fileName string, volumeInfo *volume_server_pb.VolumeInfo) er
return fmt.Errorf("marshal to %s: %v", fileName, marshalErr)
}
- writeErr := os.WriteFile(fileName, []byte(text), 0755)
+ writeErr := util.WriteFile(fileName, []byte(text), 0755)
if writeErr != nil {
return fmt.Errorf("fail to write %s : %v", fileName, writeErr)
}
diff --git a/weed/util/file_util.go b/weed/util/file_util.go
index f9cc4f70b..6155d18e1 100644
--- a/weed/util/file_util.go
+++ b/weed/util/file_util.go
@@ -95,3 +95,20 @@ func FileNameBase(filename string) string {
}
return filename[:lastDotIndex]
}
+
+// Copied from os.WriteFile(), adding file sync.
+// see https://github.com/golang/go/issues/20599
+func WriteFile(name string, data []byte, perm os.FileMode) error {
+ f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
+ if err != nil {
+ return err
+ }
+ _, err = f.Write(data)
+ if err1 := f.Sync(); err1 != nil && err == nil {
+ err = err1
+ }
+ if err1 := f.Close(); err1 != nil && err == nil {
+ err = err1
+ }
+ return err
+}