@ -20,8 +20,8 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"path"
"path/filepath"
"strings"
@ -29,7 +29,8 @@ import (
)
const (
multipartMetaFile = "multipart.json"
multipartSuffix = ".minio.multipart"
multipartMetaFile = "00000" + multipartSuffix
)
// xlObjects - Implements fs object layer.
@ -72,25 +73,6 @@ func (xl xlObjects) DeleteBucket(bucket string) error {
// GetObject - get an object.
func ( xl xlObjects ) GetObject ( bucket , object string , startOffset int64 ) ( io . ReadCloser , error ) {
findPartOffset := func ( parts completedParts ) ( partIndex int , partOffset int64 , err error ) {
partOffset = startOffset
for i , part := range parts {
partIndex = i
var fileInfo FileInfo
fileInfo , err = xl . storage . StatFile ( bucket , pathJoin ( object , fmt . Sprint ( part . PartNumber ) ) )
if err != nil {
return
}
if partOffset < fileInfo . Size {
return
}
partOffset -= fileInfo . Size
}
// Offset beyond the size of the object
err = errUnexpected
return
}
// Verify if bucket is valid.
if ! IsValidBucketName ( bucket ) {
return nil , BucketNameInvalid { Bucket : bucket }
@ -111,18 +93,18 @@ func (xl xlObjects) GetObject(bucket, object string, startOffset int64) (io.Read
return nil , toObjectErr ( err , bucket , object )
}
fileReader , fileWriter := io . Pipe ( )
parts , err := xl . getParts ( bucket , object )
info , err := xl . getMultipartObjectInfo ( bucket , object )
if err != nil {
return nil , toObjectErr ( err , bucket , object )
}
partIndex , offset , err := findPartOffset ( parts )
partIndex , offset , err := info . GetPartNumberOffset ( startOffset )
if err != nil {
return nil , toObjectErr ( err , bucket , object )
}
go func ( ) {
for ; partIndex < len ( parts ) ; partIndex ++ {
part := parts [ partIndex ]
r , err := xl . storage . ReadFile ( bucket , pathJoin ( object , fmt . Sprint ( part . PartNumber ) ) , offset )
for ; partIndex < len ( info ) ; partIndex ++ {
part := info [ partIndex ]
r , err := xl . storage . ReadFile ( bucket , pathJoin ( object , partNumToPartFileName ( part . PartNumber ) ) , offset )
if err != nil {
fileWriter . CloseWithError ( err )
return
@ -138,38 +120,19 @@ func (xl xlObjects) GetObject(bucket, object string, startOffset int64) (io.Read
}
// Return the parts of a multipart upload.
func ( xl xlObjects ) getParts ( bucket , object string ) ( parts completedParts , err error ) {
func ( xl xlObjects ) getMultipartObjectInfo ( bucket , object string ) ( info MultipartObjectInfo , err error ) {
offset := int64 ( 0 )
r , err := xl . storage . ReadFile ( bucket , pathJoin ( object , multipartMetaFile ) , offset )
if err != nil {
return
}
// FIXME: what if multipart.json is > 4MB
b := make ( [ ] byte , 4 * 1024 * 1024 )
n , err := io . ReadFull ( r , b )
if err != nil && err != io . ErrUnexpectedEOF {
return
}
b = b [ : n ]
err = json . Unmarshal ( b , & parts )
if err != nil {
return
}
decoder := json . NewDecoder ( r )
err = decoder . Decode ( & info )
return
}
// GetObjectInfo - get object info.
func ( xl xlObjects ) GetObjectInfo ( bucket , object string ) ( ObjectInfo , error ) {
getMultpartFileSize := func ( parts completedParts ) ( size int64 ) {
for _ , part := range parts {
fi , err := xl . storage . StatFile ( bucket , pathJoin ( object , fmt . Sprint ( part . PartNumber ) ) )
if err != nil {
continue
}
size += fi . Size
}
return size
}
// Verify if bucket is valid.
if ! IsValidBucketName ( bucket ) {
return ObjectInfo { } , BucketNameInvalid { Bucket : bucket }
@ -180,11 +143,11 @@ func (xl xlObjects) GetObjectInfo(bucket, object string) (ObjectInfo, error) {
}
fi , err := xl . storage . StatFile ( bucket , object )
if err != nil {
parts , err := xl . getParts ( bucket , object )
info , err := xl . getMultipartObjectInfo ( bucket , object )
if err != nil {
return ObjectInfo { } , toObjectErr ( err , bucket , object )
}
fi . Size = getMultpartFileSize ( parts )
fi . Size = info . GetSize ( )
}
contentType := "application/octet-stream"
if objectExt := filepath . Ext ( object ) ; objectExt != "" {
@ -287,6 +250,8 @@ func (xl xlObjects) DeleteObject(bucket, object string) error {
return nil
}
// TODO - support non-recursive case, figure out file size for files uploaded using multipart.
func ( xl xlObjects ) ListObjects ( bucket , prefix , marker , delimiter string , maxKeys int ) ( ListObjectsInfo , error ) {
// Verify if bucket is valid.
if ! IsValidBucketName ( bucket ) {
@ -311,21 +276,71 @@ func (xl xlObjects) ListObjects(bucket, prefix, marker, delimiter string, maxKey
}
}
if maxKeys == 0 {
return ListObjectsInfo { } , nil
}
// Default is recursive, if delimiter is set then list non recursive.
recursive := true
if delimiter == slashSeparator {
recursive = false
}
fileInfos , eof , err := xl . storage . ListFiles ( bucket , prefix , marker , recursive , maxKeys )
if err != nil {
return ListObjectsInfo { } , toObjectErr ( err , bucket )
}
if maxKeys == 0 {
return ListObjectsInfo { } , nil
var allFileInfos , fileInfos [ ] FileInfo
var eof bool
var err error
for {
fileInfos , eof , err = xl . storage . ListFiles ( bucket , prefix , marker , recursive , maxKeys )
if err != nil {
return ListObjectsInfo { } , toObjectErr ( err , bucket )
}
for _ , fileInfo := range fileInfos {
// FIXME: use fileInfo.Mode.IsDir() instead after fixing the bug in
// XL listing which is not reseting the Mode to 0 for leaf dirs.
if strings . HasSuffix ( fileInfo . Name , slashSeparator ) {
if isLeafDirectory ( xl . storage , bucket , fileInfo . Name ) {
fileInfo . Name = strings . TrimSuffix ( fileInfo . Name , slashSeparator )
// Set the Mode to a "regular" file.
fileInfo . Mode = 0
var info MultipartObjectInfo
info , err = xl . getMultipartObjectInfo ( bucket , fileInfo . Name )
if err != nil {
return ListObjectsInfo { } , toObjectErr ( err , bucket )
}
fileInfo . Size = info . GetSize ( )
allFileInfos = append ( allFileInfos , fileInfo )
maxKeys --
continue
}
}
if strings . HasSuffix ( fileInfo . Name , multipartMetaFile ) {
fileInfo . Name = path . Dir ( fileInfo . Name )
var info MultipartObjectInfo
info , err = xl . getMultipartObjectInfo ( bucket , fileInfo . Name )
if err != nil {
return ListObjectsInfo { } , toObjectErr ( err , bucket )
}
fileInfo . Size = info . GetSize ( )
allFileInfos = append ( allFileInfos , fileInfo )
maxKeys --
continue
}
if strings . HasSuffix ( fileInfo . Name , multipartSuffix ) {
continue
}
allFileInfos = append ( allFileInfos , fileInfo )
maxKeys --
}
if maxKeys == 0 {
break
}
if eof {
break
}
}
result := ListObjectsInfo { IsTruncated : ! eof }
for _ , fileInfo := range fileInfos {
for _ , fileInfo := range allFileInfos {
// With delimiter set we fill in NextMarker and Prefixes.
if delimiter == slashSeparator {
result . NextMarker = fileInfo . Name