mirror of
https://github.com/minio/minio.git
synced 2025-11-29 21:33:31 -05:00
Fix writing 'format.json' and make it atomic (#8296)
- Choose a unique uuid such that under situations of duplicate mounts we do not append to an existing json entry. - Avoid AppendFile instead use WriteAll() to write the entire byte array atomically.
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -291,16 +292,6 @@ func formatXLMigrateV2ToV3(export string) error {
|
||||
return ioutil.WriteFile(formatPath, b, 0644)
|
||||
}
|
||||
|
||||
// Returns true, if one of the errors is non-nil and is Unformatted disk.
|
||||
func hasAnyErrorsUnformatted(errs []error) bool {
|
||||
for _, err := range errs {
|
||||
if err != nil && err == errUnformattedDisk {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// countErrs - count a specific error.
|
||||
func countErrs(errs []error, err error) int {
|
||||
var i = 0
|
||||
@@ -367,16 +358,18 @@ func saveFormatXL(disk StorageAPI, format interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
tmpFormatJSON := mustGetUUID() + ".json"
|
||||
|
||||
// Purge any existing temporary file, okay to ignore errors here.
|
||||
defer disk.DeleteFile(minioMetaBucket, formatConfigFileTmp)
|
||||
defer disk.DeleteFile(minioMetaBucket, tmpFormatJSON)
|
||||
|
||||
// Append file `format.json.tmp`.
|
||||
if err = disk.AppendFile(minioMetaBucket, formatConfigFileTmp, formatBytes); err != nil {
|
||||
if err = disk.WriteAll(minioMetaBucket, tmpFormatJSON, bytes.NewReader(formatBytes)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Rename file `format.json.tmp` --> `format.json`.
|
||||
return disk.RenameFile(minioMetaBucket, formatConfigFileTmp, minioMetaBucket, formatConfigFile)
|
||||
// Rename file `uuid.json` --> `format.json`.
|
||||
return disk.RenameFile(minioMetaBucket, tmpFormatJSON, minioMetaBucket, formatConfigFile)
|
||||
}
|
||||
|
||||
var ignoredHiddenDirectories = []string{
|
||||
|
||||
Reference in New Issue
Block a user