2016-06-02 15:18:56 -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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-06-02 15:18:56 -04:00
|
|
|
|
2016-11-22 18:29:39 -05:00
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
)
|
2016-06-02 15:18:56 -04:00
|
|
|
|
|
|
|
// Returns if the prefix is a multipart upload.
|
|
|
|
func (fs fsObjects) isMultipartUpload(bucket, prefix string) bool {
|
|
|
|
_, err := fs.storage.StatFile(bucket, pathJoin(prefix, uploadsJSONFile))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isUploadIDExists - verify if a given uploadID exists and is valid.
|
|
|
|
func (fs fsObjects) isUploadIDExists(bucket, object, uploadID string) bool {
|
2016-11-22 16:15:06 -05:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
|
|
|
_, err := fs.storage.StatFile(minioMetaMultipartBucket, path.Join(uploadIDPath, fsMetaJSONFile))
|
2016-06-02 15:18:56 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return false
|
|
|
|
}
|
2016-11-22 16:15:06 -05:00
|
|
|
errorIf(err, "Unable to access upload id "+pathJoin(minioMetaMultipartBucket, uploadIDPath))
|
2016-06-02 15:18:56 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2016-06-29 05:10:40 -04:00
|
|
|
|
2016-11-22 18:29:39 -05:00
|
|
|
// updateUploadJSON - add or remove upload ID info in all `uploads.json`.
|
|
|
|
func (fs fsObjects) updateUploadJSON(bucket, object, uploadID string, initiated time.Time, isRemove bool) error {
|
2016-11-22 16:15:06 -05:00
|
|
|
uploadsPath := path.Join(bucket, object, uploadsJSONFile)
|
2016-11-22 19:52:37 -05:00
|
|
|
tmpUploadsPath := mustGetUUID()
|
2016-06-29 05:10:40 -04:00
|
|
|
|
2016-10-30 12:27:29 -04:00
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, fs.storage)
|
|
|
|
if errorCause(err) == errFileNotFound {
|
|
|
|
// If file is not found, we assume a default (empty)
|
|
|
|
// upload info.
|
|
|
|
uploadsJSON, err = newUploadsV1("fs"), nil
|
2016-06-29 05:10:40 -04:00
|
|
|
}
|
2016-10-30 12:27:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-29 05:10:40 -04:00
|
|
|
}
|
|
|
|
|
2016-10-30 12:27:29 -04:00
|
|
|
// update the uploadsJSON struct
|
2016-11-22 18:29:39 -05:00
|
|
|
if !isRemove {
|
2016-10-30 12:27:29 -04:00
|
|
|
// Add the uploadID
|
2016-11-22 18:29:39 -05:00
|
|
|
uploadsJSON.AddUploadID(uploadID, initiated)
|
2016-10-30 12:27:29 -04:00
|
|
|
} else {
|
|
|
|
// Remove the upload ID
|
2016-11-22 18:29:39 -05:00
|
|
|
uploadsJSON.RemoveUploadID(uploadID)
|
2016-06-29 05:10:40 -04:00
|
|
|
}
|
2016-10-30 12:27:29 -04:00
|
|
|
|
|
|
|
// update the file or delete it?
|
|
|
|
if len(uploadsJSON.Uploads) > 0 {
|
|
|
|
err = writeUploadJSON(&uploadsJSON, uploadsPath, tmpUploadsPath, fs.storage)
|
|
|
|
} else {
|
|
|
|
// no uploads, so we delete the file.
|
2016-11-22 16:15:06 -05:00
|
|
|
if err = fs.storage.DeleteFile(minioMetaMultipartBucket, uploadsPath); err != nil {
|
|
|
|
return toObjectErr(traceError(err), minioMetaMultipartBucket, uploadsPath)
|
2016-10-30 12:27:29 -04:00
|
|
|
}
|
2016-06-29 05:10:40 -04:00
|
|
|
}
|
2016-10-30 12:27:29 -04:00
|
|
|
return err
|
2016-06-29 05:10:40 -04:00
|
|
|
}
|
2016-11-22 18:29:39 -05:00
|
|
|
|
|
|
|
// addUploadID - add upload ID and its initiated time to 'uploads.json'.
|
|
|
|
func (fs fsObjects) addUploadID(bucket, object string, uploadID string, initiated time.Time) error {
|
|
|
|
return fs.updateUploadJSON(bucket, object, uploadID, initiated, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeUploadID - remove upload ID in 'uploads.json'.
|
|
|
|
func (fs fsObjects) removeUploadID(bucket, object string, uploadID string) error {
|
|
|
|
return fs.updateUploadJSON(bucket, object, uploadID, time.Time{}, true)
|
|
|
|
}
|