2016-07-21 20:31:14 -04:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017, 2017 Minio, Inc.
|
2016-07-21 20:31:14 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
import (
|
2017-01-16 20:05:00 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
pathutil "path"
|
|
|
|
"strings"
|
|
|
|
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2017-01-16 20:05:00 -05:00
|
|
|
"github.com/minio/minio/pkg/lock"
|
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
2017-04-04 12:14:03 -04:00
|
|
|
"github.com/tidwall/gjson"
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// FS format, and object metadata.
|
2016-05-25 00:24:20 -04:00
|
|
|
const (
|
2017-05-14 15:05:51 -04:00
|
|
|
// fs.json object metadata.
|
|
|
|
fsMetaJSONFile = "fs.json"
|
2016-05-25 00:24:20 -04:00
|
|
|
)
|
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// FS metadata constants.
|
|
|
|
const (
|
|
|
|
// FS backend meta 1.0.0 version.
|
|
|
|
fsMetaVersion100 = "1.0.0"
|
|
|
|
|
|
|
|
// FS backend meta 1.0.1 version.
|
|
|
|
fsMetaVersion = "1.0.1"
|
|
|
|
|
|
|
|
// FS backend meta format.
|
|
|
|
fsMetaFormat = "fs"
|
|
|
|
|
|
|
|
// Add more constants here.
|
|
|
|
)
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// 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"`
|
2016-07-21 20:31:14 -04:00
|
|
|
// Metadata map for current object `fs.json`.
|
|
|
|
Meta map[string]string `json:"meta,omitempty"`
|
|
|
|
Parts []objectPartInfo `json:"parts,omitempty"`
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// IsValid - tells if the format is sane by validating the version
|
|
|
|
// string and format style.
|
|
|
|
func (m fsMetaV1) IsValid() bool {
|
|
|
|
return isFSMetaValid(m.Version, m.Format)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifies if the backend format metadata is sane by validating
|
|
|
|
// the version string and format style.
|
|
|
|
func isFSMetaValid(version, format string) bool {
|
|
|
|
return ((version == fsMetaVersion || version == fsMetaVersion100) &&
|
|
|
|
format == fsMetaFormat)
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Converts metadata to object info.
|
|
|
|
func (m fsMetaV1) ToObjectInfo(bucket, object string, fi os.FileInfo) ObjectInfo {
|
|
|
|
if len(m.Meta) == 0 {
|
|
|
|
m.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess content-type from the extension if possible.
|
|
|
|
if m.Meta["content-type"] == "" {
|
|
|
|
if objectExt := pathutil.Ext(object); objectExt != "" {
|
|
|
|
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
|
|
|
m.Meta["content-type"] = content.ContentType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 20:20:54 -04:00
|
|
|
if hasSuffix(object, slashSeparator) {
|
|
|
|
m.Meta["etag"] = emptyETag // For directories etag is d41d8cd98f00b204e9800998ecf8427e
|
|
|
|
m.Meta["content-type"] = "application/octet-stream"
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
objInfo := ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
|
|
|
}
|
|
|
|
|
2017-04-04 12:14:03 -04:00
|
|
|
// We set file info only if its valid.
|
2017-01-16 20:05:00 -05:00
|
|
|
objInfo.ModTime = timeSentinel
|
|
|
|
if fi != nil {
|
|
|
|
objInfo.ModTime = fi.ModTime()
|
|
|
|
objInfo.Size = fi.Size()
|
2017-10-16 20:20:54 -04:00
|
|
|
if fi.IsDir() {
|
|
|
|
// Directory is always 0 bytes in S3 API, treat it as such.
|
|
|
|
objInfo.Size = 0
|
|
|
|
objInfo.IsDir = fi.IsDir()
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// Extract etag from metadata.
|
|
|
|
objInfo.ETag = extractETag(m.Meta)
|
2017-01-16 20:05:00 -05:00
|
|
|
objInfo.ContentType = m.Meta["content-type"]
|
|
|
|
objInfo.ContentEncoding = m.Meta["content-encoding"]
|
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// etag/md5Sum has already been extracted. We need to
|
|
|
|
// remove to avoid it from appearing as part of
|
|
|
|
// response headers. e.g, X-Minio-* or X-Amz-*.
|
2018-01-04 01:14:45 -05:00
|
|
|
objInfo.UserDefined = cleanMetadata(m.Meta)
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
// Success..
|
|
|
|
return objInfo
|
|
|
|
}
|
|
|
|
|
2017-01-25 15:29:06 -05:00
|
|
|
func (m *fsMetaV1) WriteTo(lk *lock.LockedFile) (n int64, err error) {
|
2018-02-06 18:37:48 -05:00
|
|
|
if err = jsonSave(lk, m); err != nil {
|
|
|
|
return 0, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2018-02-06 18:37:48 -05:00
|
|
|
fi, err := lk.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2018-02-06 18:37:48 -05:00
|
|
|
return fi.Size(), nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2017-04-04 12:14:03 -04:00
|
|
|
func parseFSVersion(fsMetaBuf []byte) string {
|
|
|
|
return gjson.GetBytes(fsMetaBuf, "version").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFSFormat(fsMetaBuf []byte) string {
|
|
|
|
return gjson.GetBytes(fsMetaBuf, "format").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFSRelease(fsMetaBuf []byte) string {
|
|
|
|
return gjson.GetBytes(fsMetaBuf, "minio.release").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFSMetaMap(fsMetaBuf []byte) map[string]string {
|
|
|
|
// Get xlMetaV1.Meta map.
|
|
|
|
metaMapResult := gjson.GetBytes(fsMetaBuf, "meta").Map()
|
|
|
|
metaMap := make(map[string]string)
|
|
|
|
for key, valResult := range metaMapResult {
|
|
|
|
metaMap[key] = valResult.String()
|
|
|
|
}
|
|
|
|
return metaMap
|
|
|
|
}
|
|
|
|
|
2017-01-25 15:29:06 -05:00
|
|
|
func (m *fsMetaV1) ReadFrom(lk *lock.LockedFile) (n int64, err error) {
|
2017-04-04 12:14:03 -04:00
|
|
|
var fsMetaBuf []byte
|
2017-01-25 15:29:06 -05:00
|
|
|
fi, err := lk.Stat()
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return 0, errors.Trace(err)
|
2017-01-25 15:29:06 -05:00
|
|
|
}
|
|
|
|
|
2017-04-04 12:14:03 -04:00
|
|
|
fsMetaBuf, err = ioutil.ReadAll(io.NewSectionReader(lk, 0, fi.Size()))
|
2016-08-31 16:42:57 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return 0, errors.Trace(err)
|
2016-08-31 16:42:57 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2017-04-04 12:14:03 -04:00
|
|
|
if len(fsMetaBuf) == 0 {
|
2017-11-25 14:58:29 -05:00
|
|
|
return 0, errors.Trace(io.EOF)
|
2016-08-25 12:39:01 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2017-04-04 12:14:03 -04:00
|
|
|
// obtain version.
|
|
|
|
m.Version = parseFSVersion(fsMetaBuf)
|
|
|
|
|
|
|
|
// obtain format.
|
|
|
|
m.Format = parseFSFormat(fsMetaBuf)
|
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// Verify if the format is valid, return corrupted format
|
|
|
|
// for unrecognized formats.
|
|
|
|
if !isFSMetaValid(m.Version, m.Format) {
|
2017-11-25 14:58:29 -05:00
|
|
|
return 0, errors.Trace(errCorruptedFormat)
|
2017-05-14 15:05:51 -04:00
|
|
|
}
|
|
|
|
|
2017-04-04 12:14:03 -04:00
|
|
|
// obtain metadata.
|
|
|
|
m.Meta = parseFSMetaMap(fsMetaBuf)
|
|
|
|
|
|
|
|
// obtain minio release date.
|
|
|
|
m.Minio.Release = parseFSRelease(fsMetaBuf)
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
// Success.
|
2017-04-04 12:14:03 -04:00
|
|
|
return int64(len(fsMetaBuf)), nil
|
2016-08-31 16:42:57 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// newFSMetaV1 - initializes new fsMetaV1.
|
|
|
|
func newFSMetaV1() (fsMeta fsMetaV1) {
|
|
|
|
fsMeta = fsMetaV1{}
|
2017-01-18 15:24:34 -05:00
|
|
|
fsMeta.Version = fsMetaVersion
|
|
|
|
fsMeta.Format = fsMetaFormat
|
2016-08-18 19:23:42 -04:00
|
|
|
fsMeta.Minio.Release = ReleaseTag
|
2016-05-26 06:15:01 -04:00
|
|
|
return fsMeta
|
|
|
|
}
|