| 
									
										
										
										
											2016-06-03 03:18:56 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-06-03 03:18:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2016-06-03 03:18:56 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-06-03 03:18:56 +08: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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Checks whether bucket exists.
 | 
					
						
							|  |  |  | func (fs fsObjects) isBucketExist(bucket string) bool { | 
					
						
							|  |  |  | 	// Check whether bucket exists.
 | 
					
						
							|  |  |  | 	_, err := fs.storage.StatVol(bucket) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if err == errVolumeNotFound { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		errorIf(err, "Stat failed on bucket "+bucket+".") | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // isUploadIDExists - verify if a given uploadID exists and is valid.
 | 
					
						
							|  |  |  | func (fs fsObjects) isUploadIDExists(bucket, object, uploadID string) bool { | 
					
						
							|  |  |  | 	uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID) | 
					
						
							|  |  |  | 	_, err := fs.storage.StatFile(minioMetaBucket, path.Join(uploadIDPath, fsMetaJSONFile)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if err == errFileNotFound { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		errorIf(err, "Unable to access upload id"+uploadIDPath) | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // writeUploadJSON - create `uploads.json` or update it with new uploadID.
 | 
					
						
							|  |  |  | func (fs fsObjects) writeUploadJSON(bucket, object, uploadID string, initiated time.Time) (err error) { | 
					
						
							|  |  |  | 	uploadsPath := path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile) | 
					
						
							|  |  |  | 	uniqueID := getUUID() | 
					
						
							|  |  |  | 	tmpUploadsPath := path.Join(tmpMetaPrefix, uniqueID) | 
					
						
							|  |  |  | 	var uploadsJSON uploadsV1 | 
					
						
							|  |  |  | 	uploadsJSON, err = readUploadsJSON(bucket, object, fs.storage) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		// uploads.json might not exist hence ignore errFileNotFound.
 | 
					
						
							|  |  |  | 		if errorCause(err) != errFileNotFound { | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// Set uploads format to `fs`.
 | 
					
						
							|  |  |  | 		uploadsJSON = newUploadsV1("fs") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Add a new upload id.
 | 
					
						
							|  |  |  | 	uploadsJSON.AddUploadID(uploadID, initiated) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update `uploads.json` on all disks.
 | 
					
						
							|  |  |  | 	uploadsJSONBytes, wErr := json.Marshal(&uploadsJSON) | 
					
						
							|  |  |  | 	if wErr != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		return traceError(wErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// Write `uploads.json` to disk.
 | 
					
						
							|  |  |  | 	if wErr = fs.storage.AppendFile(minioMetaBucket, tmpUploadsPath, uploadsJSONBytes); wErr != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		return traceError(wErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	wErr = fs.storage.RenameFile(minioMetaBucket, tmpUploadsPath, minioMetaBucket, uploadsPath) | 
					
						
							|  |  |  | 	if wErr != nil { | 
					
						
							|  |  |  | 		if dErr := fs.storage.DeleteFile(minioMetaBucket, tmpUploadsPath); dErr != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 			return traceError(dErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		return traceError(wErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // updateUploadsJSON - update `uploads.json` with new uploadsJSON for all disks.
 | 
					
						
							|  |  |  | func (fs fsObjects) updateUploadsJSON(bucket, object string, uploadsJSON uploadsV1) (err error) { | 
					
						
							|  |  |  | 	uploadsPath := path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile) | 
					
						
							|  |  |  | 	uniqueID := getUUID() | 
					
						
							|  |  |  | 	tmpUploadsPath := path.Join(tmpMetaPrefix, uniqueID) | 
					
						
							|  |  |  | 	uploadsBytes, wErr := json.Marshal(uploadsJSON) | 
					
						
							|  |  |  | 	if wErr != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		return traceError(wErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if wErr = fs.storage.AppendFile(minioMetaBucket, tmpUploadsPath, uploadsBytes); wErr != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		return traceError(wErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if wErr = fs.storage.RenameFile(minioMetaBucket, tmpUploadsPath, minioMetaBucket, uploadsPath); wErr != nil { | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 		return traceError(wErr) | 
					
						
							| 
									
										
										
										
											2016-06-29 17:10:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |