2016-05-04 15:18:20 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2016-05-07 03:59:43 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
2016-05-04 15:18:20 -04:00
|
|
|
|
|
|
|
type fsFormat struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type xlFormat struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Disks []string `json:"disks"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type formatConfigV1 struct {
|
2016-05-07 03:59:43 -04:00
|
|
|
Version string `json:"version"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
FS *fsFormat `json:"fs,omitempty"`
|
|
|
|
XL *xlFormat `json:"xl,omitempty"`
|
2016-05-04 15:18:20 -04:00
|
|
|
}
|
|
|
|
|
2016-05-07 03:59:43 -04:00
|
|
|
// FIXME: currently we don't check single exportPath which uses FS layer.
|
2016-05-04 15:18:20 -04:00
|
|
|
|
2016-05-07 03:59:43 -04:00
|
|
|
// loadFormatXL - load XL format.json.
|
|
|
|
func loadFormatXL(storage StorageAPI) (xl *xlFormat, err error) {
|
|
|
|
offset := int64(0)
|
|
|
|
r, err := storage.ReadFile(minioMetaBucket, formatConfigFile, offset)
|
2016-05-04 15:18:20 -04:00
|
|
|
if err != nil {
|
2016-05-07 03:59:43 -04:00
|
|
|
return nil, err
|
2016-05-04 15:18:20 -04:00
|
|
|
}
|
2016-05-07 03:59:43 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
formatXL := formatConfigV1{}
|
|
|
|
err = decoder.Decode(&formatXL)
|
2016-05-04 15:18:20 -04:00
|
|
|
if err != nil {
|
2016-05-07 03:59:43 -04:00
|
|
|
return nil, err
|
2016-05-04 15:18:20 -04:00
|
|
|
}
|
2016-05-07 03:59:43 -04:00
|
|
|
if err = r.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if formatXL.Version != "1" {
|
|
|
|
return nil, fmt.Errorf("Unsupported version of backend format [%s] found.", formatXL.Version)
|
|
|
|
}
|
|
|
|
if formatXL.Format != "xl" {
|
|
|
|
return nil, fmt.Errorf("Unsupported backend format [%s] found.", formatXL.Format)
|
|
|
|
}
|
|
|
|
return formatXL.XL, nil
|
|
|
|
}
|
2016-05-04 15:18:20 -04:00
|
|
|
|
2016-05-07 03:59:43 -04:00
|
|
|
// checkFormat - validates if format.json file exists.
|
|
|
|
func checkFormat(storage StorageAPI) error {
|
|
|
|
_, err := storage.StatFile(minioMetaBucket, formatConfigFile)
|
2016-05-04 15:18:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// saveFormatXL - save XL format configuration
|
2016-05-07 03:59:43 -04:00
|
|
|
func saveFormatXL(storage StorageAPI, xl *xlFormat) error {
|
|
|
|
w, err := storage.CreateFile(minioMetaBucket, formatConfigFile)
|
2016-05-04 15:18:20 -04:00
|
|
|
if err != nil {
|
2016-05-07 03:59:43 -04:00
|
|
|
return err
|
2016-05-04 15:18:20 -04:00
|
|
|
}
|
2016-05-07 03:59:43 -04:00
|
|
|
formatXL := formatConfigV1{
|
|
|
|
Version: "1",
|
|
|
|
Format: "xl",
|
|
|
|
XL: xl,
|
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
err = encoder.Encode(&formatXL)
|
2016-05-04 15:18:20 -04:00
|
|
|
if err != nil {
|
2016-05-07 03:59:43 -04:00
|
|
|
if clErr := safeCloseAndRemove(w); clErr != nil {
|
|
|
|
return clErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(w); clErr != nil {
|
|
|
|
return clErr
|
|
|
|
}
|
|
|
|
return err
|
2016-05-04 15:18:20 -04:00
|
|
|
}
|
2016-05-07 03:59:43 -04:00
|
|
|
return nil
|
2016-05-04 15:18:20 -04:00
|
|
|
}
|