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.
277 lines
7.2 KiB
277 lines
7.2 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package file
|
||
10 years ago
|
|
||
|
import (
|
||
10 years ago
|
"bytes"
|
||
10 years ago
|
"io"
|
||
|
"os"
|
||
|
"path"
|
||
|
"strings"
|
||
|
|
||
|
"crypto/md5"
|
||
10 years ago
|
"encoding/base64"
|
||
10 years ago
|
"encoding/gob"
|
||
|
"encoding/hex"
|
||
|
|
||
10 years ago
|
"github.com/minio-io/minio/pkg/drivers"
|
||
10 years ago
|
)
|
||
|
|
||
|
/// Object Operations
|
||
|
|
||
10 years ago
|
// GetPartialObject - GET object from range
|
||
10 years ago
|
func (file *fileDriver) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) {
|
||
10 years ago
|
// validate bucket
|
||
10 years ago
|
if drivers.IsValidBucket(bucket) == false {
|
||
|
return 0, drivers.BucketNameInvalid{Bucket: bucket}
|
||
10 years ago
|
}
|
||
|
|
||
|
// validate object
|
||
10 years ago
|
if drivers.IsValidObject(object) == false {
|
||
|
return 0, drivers.ObjectNameInvalid{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
objectPath := path.Join(file.root, bucket, object)
|
||
10 years ago
|
filestat, err := os.Stat(objectPath)
|
||
|
switch err := err.(type) {
|
||
|
case nil:
|
||
|
{
|
||
|
if filestat.IsDir() {
|
||
10 years ago
|
return 0, drivers.ObjectNotFound{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
if os.IsNotExist(err) {
|
||
10 years ago
|
return 0, drivers.ObjectNotFound{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
10 years ago
|
return 0, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
f, err := os.Open(objectPath)
|
||
|
defer f.Close()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return 0, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
_, err = f.Seek(start, os.SEEK_SET)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return 0, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
count, err := io.CopyN(w, f, length)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return count, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
|
||
|
return count, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// GetObject - GET object from key
|
||
10 years ago
|
func (file *fileDriver) GetObject(w io.Writer, bucket string, object string) (int64, error) {
|
||
10 years ago
|
// validate bucket
|
||
10 years ago
|
if drivers.IsValidBucket(bucket) == false {
|
||
|
return 0, drivers.BucketNameInvalid{Bucket: bucket}
|
||
10 years ago
|
}
|
||
|
|
||
|
// validate object
|
||
10 years ago
|
if drivers.IsValidObject(object) == false {
|
||
|
return 0, drivers.ObjectNameInvalid{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
objectPath := path.Join(file.root, bucket, object)
|
||
10 years ago
|
|
||
|
filestat, err := os.Stat(objectPath)
|
||
|
switch err := err.(type) {
|
||
|
case nil:
|
||
|
{
|
||
|
if filestat.IsDir() {
|
||
10 years ago
|
return 0, drivers.ObjectNotFound{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
if os.IsNotExist(err) {
|
||
10 years ago
|
return 0, drivers.ObjectNotFound{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
10 years ago
|
return 0, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
f, err := os.Open(objectPath)
|
||
|
defer f.Close()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return 0, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
count, err := io.Copy(w, f)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return count, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
return count, nil
|
||
|
}
|
||
|
|
||
|
// GetObjectMetadata - HEAD object
|
||
10 years ago
|
func (file *fileDriver) GetObjectMetadata(bucket, object, prefix string) (drivers.ObjectMetadata, error) {
|
||
|
if drivers.IsValidBucket(bucket) == false {
|
||
|
return drivers.ObjectMetadata{}, drivers.BucketNameInvalid{Bucket: bucket}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if drivers.IsValidObject(object) == false {
|
||
|
return drivers.ObjectMetadata{}, drivers.ObjectNameInvalid{Bucket: bucket, Object: bucket}
|
||
10 years ago
|
}
|
||
|
|
||
|
// Do not use path.Join() since path.Join strips off any object names with '/', use them as is
|
||
|
// in a static manner so that we can send a proper 'ObjectNotFound' reply back upon os.Stat()
|
||
10 years ago
|
objectPath := file.root + "/" + bucket + "/" + object
|
||
10 years ago
|
stat, err := os.Stat(objectPath)
|
||
|
if os.IsNotExist(err) {
|
||
10 years ago
|
return drivers.ObjectMetadata{}, drivers.ObjectNotFound{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
|
|
||
|
_, err = os.Stat(objectPath + "$metadata")
|
||
|
if os.IsNotExist(err) {
|
||
10 years ago
|
return drivers.ObjectMetadata{}, drivers.ObjectNotFound{Bucket: bucket, Object: object}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
f, err := os.Open(objectPath + "$metadata")
|
||
|
defer f.Close()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return drivers.ObjectMetadata{}, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
var deserializedMetadata fileMetadata
|
||
|
decoder := gob.NewDecoder(f)
|
||
10 years ago
|
err = decoder.Decode(&deserializedMetadata)
|
||
|
if err != nil {
|
||
10 years ago
|
return drivers.ObjectMetadata{}, drivers.EmbedError(bucket, object, err)
|
||
10 years ago
|
}
|
||
|
|
||
|
contentType := "application/octet-stream"
|
||
|
if deserializedMetadata.ContentType != "" {
|
||
|
contentType = deserializedMetadata.ContentType
|
||
|
}
|
||
|
contentType = strings.TrimSpace(contentType)
|
||
|
|
||
|
etag := bucket + "#" + path.Base(object)
|
||
|
if len(deserializedMetadata.Md5sum) != 0 {
|
||
|
etag = hex.EncodeToString(deserializedMetadata.Md5sum)
|
||
|
}
|
||
|
trimmedObject := strings.TrimPrefix(object, prefix)
|
||
10 years ago
|
metadata := drivers.ObjectMetadata{
|
||
10 years ago
|
Bucket: bucket,
|
||
|
Key: trimmedObject,
|
||
|
Created: stat.ModTime(),
|
||
|
Size: stat.Size(),
|
||
10 years ago
|
Md5: etag,
|
||
10 years ago
|
ContentType: contentType,
|
||
|
}
|
||
|
|
||
|
return metadata, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// CreateObject - PUT object
|
||
10 years ago
|
func (file *fileDriver) CreateObject(bucket, key, contentType, md5sum string, data io.Reader) error {
|
||
10 years ago
|
// TODO Commits should stage then move instead of writing directly
|
||
10 years ago
|
file.lock.Lock()
|
||
|
defer file.lock.Unlock()
|
||
10 years ago
|
|
||
|
// check bucket name valid
|
||
10 years ago
|
if drivers.IsValidBucket(bucket) == false {
|
||
|
return drivers.BucketNameInvalid{Bucket: bucket}
|
||
10 years ago
|
}
|
||
|
|
||
|
// check bucket exists
|
||
10 years ago
|
if _, err := os.Stat(path.Join(file.root, bucket)); os.IsNotExist(err) {
|
||
|
return drivers.BucketNotFound{Bucket: bucket}
|
||
10 years ago
|
}
|
||
|
|
||
|
// verify object path legal
|
||
10 years ago
|
if drivers.IsValidObject(key) == false {
|
||
|
return drivers.ObjectNameInvalid{Bucket: bucket, Object: key}
|
||
10 years ago
|
}
|
||
|
|
||
|
// verify content type
|
||
|
if contentType == "" {
|
||
|
contentType = "application/octet-stream"
|
||
|
}
|
||
|
contentType = strings.TrimSpace(contentType)
|
||
|
|
||
|
// get object path
|
||
10 years ago
|
objectPath := path.Join(file.root, bucket, key)
|
||
10 years ago
|
objectDir := path.Dir(objectPath)
|
||
|
if _, err := os.Stat(objectDir); os.IsNotExist(err) {
|
||
|
err = os.MkdirAll(objectDir, 0700)
|
||
|
if err != nil {
|
||
10 years ago
|
return drivers.EmbedError(bucket, key, err)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// check if object exists
|
||
|
if _, err := os.Stat(objectPath); !os.IsNotExist(err) {
|
||
10 years ago
|
return drivers.ObjectExists{
|
||
10 years ago
|
Bucket: bucket,
|
||
|
Object: key,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// write object
|
||
10 years ago
|
f, err := os.OpenFile(objectPath, os.O_WRONLY|os.O_CREATE, 0600)
|
||
|
defer f.Close()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return drivers.EmbedError(bucket, key, err)
|
||
10 years ago
|
}
|
||
|
|
||
|
h := md5.New()
|
||
10 years ago
|
mw := io.MultiWriter(f, h)
|
||
10 years ago
|
|
||
|
_, err = io.Copy(mw, data)
|
||
|
if err != nil {
|
||
10 years ago
|
return drivers.EmbedError(bucket, key, err)
|
||
10 years ago
|
}
|
||
|
|
||
|
//
|
||
10 years ago
|
f, err = os.OpenFile(objectPath+"$metadata", os.O_WRONLY|os.O_CREATE, 0600)
|
||
|
defer f.Close()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return drivers.EmbedError(bucket, key, err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
metadata := &fileMetadata{
|
||
10 years ago
|
ContentType: contentType,
|
||
|
Md5sum: h.Sum(nil),
|
||
10 years ago
|
}
|
||
|
// serialize metadata to gob
|
||
10 years ago
|
encoder := gob.NewEncoder(f)
|
||
10 years ago
|
err = encoder.Encode(metadata)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return drivers.EmbedError(bucket, key, err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Verify data received to be correct, Content-MD5 received
|
||
|
if md5sum != "" {
|
||
|
var data []byte
|
||
|
data, err = base64.StdEncoding.DecodeString(md5sum)
|
||
|
if err != nil {
|
||
10 years ago
|
return drivers.InvalidDigest{Bucket: bucket, Key: key, Md5: md5sum}
|
||
10 years ago
|
}
|
||
|
if !bytes.Equal(metadata.Md5sum, data) {
|
||
10 years ago
|
return drivers.BadDigest{Bucket: bucket, Key: key, Md5: md5sum}
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
return nil
|
||
|
}
|