| 
									
										
										
										
											2016-04-30 08:52:17 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-04-30 08:52:17 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-04-30 08:52:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2017-01-21 08:33:01 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	humanize "github.com/dustin/go-humanize" | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-04-30 08:52:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// Block size used for all internal operations version 1.
 | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	blockSizeV1 = 10 * humanize.MiByte | 
					
						
							| 
									
										
										
										
											2016-06-25 18:03:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Staging buffer read size for all internal operations version 1.
 | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	readSizeV1 = 1 * humanize.MiByte | 
					
						
							| 
									
										
										
										
											2016-07-22 08:31:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Buckets meta prefix.
 | 
					
						
							|  |  |  | 	bucketMetaPrefix = "buckets" | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 14:03:10 +08:00
										 |  |  | // Global object layer mutex, used for safely updating object layer.
 | 
					
						
							|  |  |  | var globalObjLayerMutex *sync.Mutex | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Global object layer, only accessed by newObjectLayerFn().
 | 
					
						
							|  |  |  | var globalObjectAPI ObjectLayer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							|  |  |  | 	// Initialize this once per server initialization.
 | 
					
						
							|  |  |  | 	globalObjLayerMutex = &sync.Mutex{} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | // Check if the disk is remote.
 | 
					
						
							|  |  |  | func isRemoteDisk(disk StorageAPI) bool { | 
					
						
							|  |  |  | 	_, ok := disk.(*networkStorage) | 
					
						
							|  |  |  | 	return ok | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-21 08:33:01 +08:00
										 |  |  | // Checks if the object is a directory, this logic uses
 | 
					
						
							|  |  |  | // if size == 0 and object ends with slashSeparator then
 | 
					
						
							|  |  |  | // returns true.
 | 
					
						
							|  |  |  | func isObjectDir(object string, size int64) bool { | 
					
						
							|  |  |  | 	return strings.HasSuffix(object, slashSeparator) && size == 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Converts just bucket, object metadata into ObjectInfo datatype.
 | 
					
						
							|  |  |  | func dirObjectInfo(bucket, object string, size int64, metadata map[string]string) ObjectInfo { | 
					
						
							|  |  |  | 	// This is a special case with size as '0' and object ends with
 | 
					
						
							|  |  |  | 	// a slash separator, we treat it like a valid operation and
 | 
					
						
							|  |  |  | 	// return success.
 | 
					
						
							|  |  |  | 	md5Sum := metadata["md5Sum"] | 
					
						
							|  |  |  | 	delete(metadata, "md5Sum") | 
					
						
							|  |  |  | 	return ObjectInfo{ | 
					
						
							|  |  |  | 		Bucket:      bucket, | 
					
						
							|  |  |  | 		Name:        object, | 
					
						
							|  |  |  | 		ModTime:     time.Now().UTC(), | 
					
						
							|  |  |  | 		ContentType: "application/octet-stream", | 
					
						
							|  |  |  | 		IsDir:       true, | 
					
						
							|  |  |  | 		Size:        size, | 
					
						
							|  |  |  | 		MD5Sum:      md5Sum, | 
					
						
							|  |  |  | 		UserDefined: metadata, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-20 10:59:48 +08:00
										 |  |  | // House keeping code for FS/XL and distributed Minio setup.
 | 
					
						
							|  |  |  | func houseKeeping(storageDisks []StorageAPI) error { | 
					
						
							|  |  |  | 	var wg = &sync.WaitGroup{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize errs to collect errors inside go-routine.
 | 
					
						
							|  |  |  | 	var errs = make([]error, len(storageDisks)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize all disks in parallel.
 | 
					
						
							|  |  |  | 	for index, disk := range storageDisks { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		if disk == nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 		// Skip remote disks.
 | 
					
						
							|  |  |  | 		if isRemoteDisk(disk) { | 
					
						
							| 
									
										
										
										
											2016-10-20 10:59:48 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func(index int, disk StorageAPI) { | 
					
						
							|  |  |  | 			// Indicate this wait group is done.
 | 
					
						
							|  |  |  | 			defer wg.Done() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Cleanup all temp entries upon start.
 | 
					
						
							| 
									
										
										
										
											2016-11-21 06:25:43 +08:00
										 |  |  | 			err := cleanupDir(disk, minioMetaTmpBucket, "") | 
					
						
							| 
									
										
										
										
											2016-10-20 10:59:48 +08:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2016-11-24 12:05:04 +08:00
										 |  |  | 				if !isErrIgnored(errorCause(err), errDiskNotFound, errVolumeNotFound, errFileNotFound) { | 
					
						
							| 
									
										
										
										
											2016-10-20 10:59:48 +08:00
										 |  |  | 					errs[index] = err | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}(index, disk) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Wait for all cleanup to finish.
 | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return upon first error.
 | 
					
						
							|  |  |  | 	for _, err := range errs { | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-21 06:25:43 +08:00
										 |  |  | 		return toObjectErr(err, minioMetaTmpBucket, "*") | 
					
						
							| 
									
										
										
										
											2016-05-31 07:51:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-20 10:59:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Return success here.
 | 
					
						
							| 
									
										
										
										
											2016-05-31 07:51:59 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | // Check if a network path is local to this node.
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | func isLocalStorage(ep *url.URL) bool { | 
					
						
							|  |  |  | 	if ep.Host == "" { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	if globalMinioHost != "" && globalMinioPort != "" { | 
					
						
							|  |  |  | 		// if --address host:port was specified for distXL we short
 | 
					
						
							|  |  |  | 		// circuit only the endPoint that matches host:port
 | 
					
						
							|  |  |  | 		if net.JoinHostPort(globalMinioHost, globalMinioPort) == ep.Host { | 
					
						
							| 
									
										
										
										
											2016-08-16 05:33:48 +08:00
										 |  |  | 			return true | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	// Split host to extract host information.
 | 
					
						
							|  |  |  | 	host, _, err := net.SplitHostPort(ep.Host) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Cannot split host port") | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	// Resolve host to address to check if the IP is loopback.
 | 
					
						
							|  |  |  | 	// If address resolution fails, assume it's a non-local host.
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	addrs, err := net.LookupHost(host) | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Failed to lookup host") | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, addr := range addrs { | 
					
						
							|  |  |  | 		if ip := net.ParseIP(addr); ip.IsLoopback() { | 
					
						
							|  |  |  | 			return true | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	iaddrs, err := net.InterfaceAddrs() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Unable to list interface addresses") | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, addr := range addrs { | 
					
						
							|  |  |  | 		for _, iaddr := range iaddrs { | 
					
						
							|  |  |  | 			ip, _, err := net.ParseCIDR(iaddr.String()) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				errorIf(err, "Unable to parse CIDR") | 
					
						
							|  |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if ip.String() == addr { | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 				return true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	return false | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | // Fetch the path component from *url.URL*.
 | 
					
						
							|  |  |  | func getPath(ep *url.URL) string { | 
					
						
							|  |  |  | 	if ep == nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var diskPath string | 
					
						
							|  |  |  | 	// For windows ep.Path is usually empty
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	if runtime.GOOS == globalWindowsOSName { | 
					
						
							| 
									
										
										
										
											2016-10-29 04:08:56 +08:00
										 |  |  | 		switch ep.Scheme { | 
					
						
							|  |  |  | 		case "": | 
					
						
							|  |  |  | 			// Eg. "minio server .\export"
 | 
					
						
							|  |  |  | 			diskPath = ep.Path | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 		case httpScheme, httpsScheme: | 
					
						
							| 
									
										
										
										
											2016-10-29 04:08:56 +08:00
										 |  |  | 			// For full URLs windows drive is part of URL path.
 | 
					
						
							|  |  |  | 			// Eg: http://ip:port/C:\mydrive
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 			// For windows trim off the preceding "/".
 | 
					
						
							|  |  |  | 			diskPath = ep.Path[1:] | 
					
						
							| 
									
										
										
										
											2016-10-29 04:08:56 +08:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 			// For the rest url splits drive letter into
 | 
					
						
							|  |  |  | 			// Scheme contruct the disk path back.
 | 
					
						
							|  |  |  | 			diskPath = ep.Scheme + ":" + ep.Opaque | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// For other operating systems ep.Path is non empty.
 | 
					
						
							|  |  |  | 		diskPath = ep.Path | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return diskPath | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | // Depending on the disk type network or local, initialize storage API.
 | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | func newStorageAPI(ep *url.URL) (storage StorageAPI, err error) { | 
					
						
							| 
									
										
										
										
											2016-10-19 03:49:24 +08:00
										 |  |  | 	if isLocalStorage(ep) { | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 		return newPosix(getPath(ep)) | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	return newStorageRPC(ep) | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-24 12:05:04 +08:00
										 |  |  | var initMetaVolIgnoredErrs = append(baseIgnoredErrs, errVolumeExists) | 
					
						
							| 
									
										
										
										
											2016-11-21 17:46:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-02 16:59:28 +08:00
										 |  |  | // Initializes meta volume on all input storage disks.
 | 
					
						
							|  |  |  | func initMetaVolume(storageDisks []StorageAPI) error { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:00:03 +08:00
										 |  |  | 	// This happens for the first time, but keep this here since this
 | 
					
						
							|  |  |  | 	// is the only place where it can be made expensive optimizing all
 | 
					
						
							|  |  |  | 	// other calls. Create minio meta volume, if it doesn't exist yet.
 | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 	var wg = &sync.WaitGroup{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize errs to collect errors inside go-routine.
 | 
					
						
							|  |  |  | 	var errs = make([]error, len(storageDisks)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize all disks in parallel.
 | 
					
						
							|  |  |  | 	for index, disk := range storageDisks { | 
					
						
							| 
									
										
										
										
											2016-06-03 07:34:15 +08:00
										 |  |  | 		if disk == nil { | 
					
						
							| 
									
										
										
										
											2016-07-11 05:38:15 +08:00
										 |  |  | 			// Ignore create meta volume on disks which are not found.
 | 
					
						
							| 
									
										
										
										
											2016-06-03 07:34:15 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func(index int, disk StorageAPI) { | 
					
						
							|  |  |  | 			// Indicate this wait group is done.
 | 
					
						
							|  |  |  | 			defer wg.Done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:31:50 +08:00
										 |  |  | 			// Attempt to create `.minio.sys`.
 | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 			err := disk.MakeVol(minioMetaBucket) | 
					
						
							| 
									
										
										
										
											2016-07-02 16:59:28 +08:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2016-11-24 12:05:04 +08:00
										 |  |  | 				if !isErrIgnored(err, initMetaVolIgnoredErrs...) { | 
					
						
							| 
									
										
										
										
											2016-07-02 16:59:28 +08:00
										 |  |  | 					errs[index] = err | 
					
						
							| 
									
										
										
										
											2016-11-26 10:11:50 +08:00
										 |  |  | 					return | 
					
						
							| 
									
										
										
										
											2016-07-02 16:59:28 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-11-21 06:25:43 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			err = disk.MakeVol(minioMetaTmpBucket) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2016-11-24 12:05:04 +08:00
										 |  |  | 				if !isErrIgnored(err, initMetaVolIgnoredErrs...) { | 
					
						
							| 
									
										
										
										
											2016-11-21 06:25:43 +08:00
										 |  |  | 					errs[index] = err | 
					
						
							| 
									
										
										
										
											2016-11-26 10:11:50 +08:00
										 |  |  | 					return | 
					
						
							| 
									
										
										
										
											2016-11-21 06:25:43 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-05-20 17:22:22 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-11-23 05:15:06 +08:00
										 |  |  | 			err = disk.MakeVol(minioMetaMultipartBucket) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2016-11-24 12:05:04 +08:00
										 |  |  | 				if !isErrIgnored(err, initMetaVolIgnoredErrs...) { | 
					
						
							| 
									
										
										
										
											2016-11-23 05:15:06 +08:00
										 |  |  | 					errs[index] = err | 
					
						
							| 
									
										
										
										
											2016-11-26 10:11:50 +08:00
										 |  |  | 					return | 
					
						
							| 
									
										
										
										
											2016-11-23 05:15:06 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-07-02 16:59:28 +08:00
										 |  |  | 		}(index, disk) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Wait for all cleanup to finish.
 | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return upon first error.
 | 
					
						
							|  |  |  | 	for _, err := range errs { | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return toObjectErr(err, minioMetaBucket) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return success here.
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | // Cleanup a directory recursively.
 | 
					
						
							|  |  |  | func cleanupDir(storage StorageAPI, volume, dirPath string) error { | 
					
						
							|  |  |  | 	var delFunc func(string) error | 
					
						
							|  |  |  | 	// Function to delete entries recursively.
 | 
					
						
							|  |  |  | 	delFunc = func(entryPath string) error { | 
					
						
							|  |  |  | 		if !strings.HasSuffix(entryPath, slashSeparator) { | 
					
						
							| 
									
										
										
										
											2016-07-12 16:01:47 +08:00
										 |  |  | 			// Delete the file entry.
 | 
					
						
							| 
									
										
										
										
											2016-10-18 07:38:46 +08:00
										 |  |  | 			return traceError(storage.DeleteFile(volume, entryPath)) | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-07-12 16:01:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | 		// If it's a directory, list and call delFunc() for each entry.
 | 
					
						
							|  |  |  | 		entries, err := storage.ListDir(volume, entryPath) | 
					
						
							| 
									
										
										
										
											2016-07-12 16:01:47 +08:00
										 |  |  | 		// If entryPath prefix never existed, safe to ignore.
 | 
					
						
							|  |  |  | 		if err == errFileNotFound { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} else if err != nil { // For any other errors fail.
 | 
					
						
							| 
									
										
										
										
											2016-08-26 00:39:01 +08:00
										 |  |  | 			return traceError(err) | 
					
						
							| 
									
										
										
										
											2016-07-12 16:01:47 +08:00
										 |  |  | 		} // else on success..
 | 
					
						
							| 
									
										
										
										
											2016-06-14 16:39:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 16:01:47 +08:00
										 |  |  | 		// Recurse and delete all other entries.
 | 
					
						
							|  |  |  | 		for _, entry := range entries { | 
					
						
							|  |  |  | 			if err = delFunc(pathJoin(entryPath, entry)); err != nil { | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-03 07:34:15 +08:00
										 |  |  | 	err := delFunc(retainSlash(pathJoin(dirPath))) | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | } |