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.
175 lines
4.4 KiB
175 lines
4.4 KiB
9 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
8 years ago
|
"os"
|
||
9 years ago
|
"path"
|
||
|
"sort"
|
||
9 years ago
|
"strings"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
const (
|
||
9 years ago
|
fsMetaJSONFile = "fs.json"
|
||
|
fsFormatJSONFile = "format.json"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
// A fsMetaV1 represents a metadata header mapping keys to sets of values.
|
||
|
type fsMetaV1 struct {
|
||
|
Version string `json:"version"`
|
||
|
Format string `json:"format"`
|
||
|
Minio struct {
|
||
|
Release string `json:"release"`
|
||
|
} `json:"minio"`
|
||
9 years ago
|
// Metadata map for current object `fs.json`.
|
||
|
Meta map[string]string `json:"meta,omitempty"`
|
||
|
Parts []objectPartInfo `json:"parts,omitempty"`
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// ObjectPartIndex - returns the index of matching object part number.
|
||
|
func (m fsMetaV1) ObjectPartIndex(partNumber int) (partIndex int) {
|
||
9 years ago
|
for i, part := range m.Parts {
|
||
9 years ago
|
if partNumber == part.Number {
|
||
|
partIndex = i
|
||
|
return partIndex
|
||
9 years ago
|
}
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
// AddObjectPart - add a new object part in order.
|
||
9 years ago
|
func (m *fsMetaV1) AddObjectPart(partNumber int, partName string, partETag string, partSize int64) {
|
||
|
partInfo := objectPartInfo{
|
||
|
Number: partNumber,
|
||
|
Name: partName,
|
||
|
ETag: partETag,
|
||
|
Size: partSize,
|
||
|
}
|
||
|
|
||
|
// Update part info if it already exists.
|
||
|
for i, part := range m.Parts {
|
||
|
if partNumber == part.Number {
|
||
|
m.Parts[i] = partInfo
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Proceed to include new part info.
|
||
|
m.Parts = append(m.Parts, partInfo)
|
||
|
|
||
|
// Parts in fsMeta should be in sorted order by part number.
|
||
9 years ago
|
sort.Sort(byObjectPartNumber(m.Parts))
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// readFSMetadata - returns the object metadata `fs.json` content.
|
||
9 years ago
|
func readFSMetadata(disk StorageAPI, bucket, object string) (fsMeta fsMetaV1, err error) {
|
||
9 years ago
|
// Read all `fs.json`.
|
||
|
buf, err := disk.ReadAll(bucket, path.Join(object, fsMetaJSONFile))
|
||
|
if err != nil {
|
||
9 years ago
|
return fsMetaV1{}, err
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Decode `fs.json` into fsMeta structure.
|
||
|
if err = json.Unmarshal(buf, &fsMeta); err != nil {
|
||
9 years ago
|
return fsMetaV1{}, err
|
||
|
}
|
||
9 years ago
|
|
||
|
// Success.
|
||
9 years ago
|
return fsMeta, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// newFSMetaV1 - initializes new fsMetaV1.
|
||
|
func newFSMetaV1() (fsMeta fsMetaV1) {
|
||
|
fsMeta = fsMetaV1{}
|
||
9 years ago
|
fsMeta.Version = "1.0.0"
|
||
9 years ago
|
fsMeta.Format = "fs"
|
||
8 years ago
|
fsMeta.Minio.Release = ReleaseTag
|
||
9 years ago
|
return fsMeta
|
||
|
}
|
||
|
|
||
9 years ago
|
// newFSFormatV1 - initializes new formatConfigV1 with FS format info.
|
||
|
func newFSFormatV1() (format formatConfigV1) {
|
||
|
return formatConfigV1{
|
||
|
Version: "1",
|
||
|
Format: "fs",
|
||
|
FS: &fsFormat{
|
||
|
Version: "1",
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// isFSFormat - returns whether given formatConfigV1 is FS type or not.
|
||
|
func isFSFormat(format formatConfigV1) bool {
|
||
|
return format.Format == "fs"
|
||
|
}
|
||
|
|
||
9 years ago
|
// writes FS format (format.json) into minioMetaBucket.
|
||
|
func writeFSFormatData(storage StorageAPI, fsFormat formatConfigV1) error {
|
||
|
metadataBytes, err := json.Marshal(fsFormat)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
// fsFormatJSONFile - format.json file stored in minioMetaBucket(.minio) directory.
|
||
9 years ago
|
if err = storage.AppendFile(minioMetaBucket, fsFormatJSONFile, metadataBytes); err != nil {
|
||
9 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// writeFSMetadata - writes `fs.json` metadata, marshals fsMeta object into json
|
||
|
// and saves it to disk.
|
||
|
func writeFSMetadata(storage StorageAPI, bucket, path string, fsMeta fsMetaV1) error {
|
||
9 years ago
|
metadataBytes, err := json.Marshal(fsMeta)
|
||
9 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
9 years ago
|
if err = storage.AppendFile(bucket, path, metadataBytes); err != nil {
|
||
9 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
9 years ago
|
|
||
|
var extendedHeaders = []string{
|
||
|
"X-Amz-Meta-",
|
||
|
"X-Minio-Meta-",
|
||
|
// Add new extended headers.
|
||
|
}
|
||
|
|
||
|
// isExtendedHeader validates if input string matches extended headers.
|
||
|
func isExtendedHeader(header string) bool {
|
||
|
for _, extendedHeader := range extendedHeaders {
|
||
|
if strings.HasPrefix(header, extendedHeader) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Return true if extended HTTP headers are set, false otherwise.
|
||
|
func hasExtendedHeader(metadata map[string]string) bool {
|
||
8 years ago
|
if os.Getenv("MINIO_ENABLE_FSMETA") == "1" {
|
||
|
return true
|
||
|
}
|
||
9 years ago
|
for k := range metadata {
|
||
|
if isExtendedHeader(k) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|