| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-06 12:15:28 +08:00
										 |  |  | // Wrapper functions to os.RemoveAll, which calls reliableRemoveAll
 | 
					
						
							|  |  |  | // this is to ensure that if there is a racy parent directory
 | 
					
						
							|  |  |  | // create in between we can simply retry the operation.
 | 
					
						
							|  |  |  | func removeAll(dirPath string) (err error) { | 
					
						
							|  |  |  | 	if dirPath == "" { | 
					
						
							|  |  |  | 		return errInvalidArgument | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = checkPathLength(dirPath); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = reliableRemoveAll(dirPath); err != nil { | 
					
						
							|  |  |  | 		switch { | 
					
						
							|  |  |  | 		case isSysErrNotDir(err): | 
					
						
							|  |  |  | 			// File path cannot be verified since one of
 | 
					
						
							|  |  |  | 			// the parents is a file.
 | 
					
						
							|  |  |  | 			return errFileAccessDenied | 
					
						
							|  |  |  | 		case isSysErrPathNotFound(err): | 
					
						
							|  |  |  | 			// This is a special case should be handled only for
 | 
					
						
							|  |  |  | 			// windows, because windows API does not return "not a
 | 
					
						
							|  |  |  | 			// directory" error message. Handle this specifically
 | 
					
						
							|  |  |  | 			// here.
 | 
					
						
							|  |  |  | 			return errFileAccessDenied | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Reliably retries os.RemoveAll if for some reason os.RemoveAll returns
 | 
					
						
							|  |  |  | // syscall.ENOTEMPTY (children has files).
 | 
					
						
							|  |  |  | func reliableRemoveAll(dirPath string) (err error) { | 
					
						
							|  |  |  | 	i := 0 | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		// Removes all the directories and files.
 | 
					
						
							| 
									
										
										
										
											2021-03-24 05:51:27 +08:00
										 |  |  | 		if err = RemoveAll(dirPath); err != nil { | 
					
						
							| 
									
										
										
										
											2018-08-06 12:15:28 +08:00
										 |  |  | 			// Retry only for the first retryable error.
 | 
					
						
							|  |  |  | 			if isSysErrNotEmpty(err) && i == 0 { | 
					
						
							|  |  |  | 				i++ | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | // Wrapper functions to os.MkdirAll, which calls reliableMkdirAll
 | 
					
						
							|  |  |  | // this is to ensure that if there is a racy parent directory
 | 
					
						
							|  |  |  | // delete in between we can simply retry the operation.
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | func mkdirAll(dirPath string, mode os.FileMode, baseDir string) (err error) { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 	if dirPath == "" { | 
					
						
							|  |  |  | 		return errInvalidArgument | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = checkPathLength(dirPath); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | 	if err = reliableMkdirAll(dirPath, mode, baseDir); err != nil { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 		// File path cannot be verified since one of the parents is a file.
 | 
					
						
							|  |  |  | 		if isSysErrNotDir(err) { | 
					
						
							|  |  |  | 			return errFileAccessDenied | 
					
						
							|  |  |  | 		} else if isSysErrPathNotFound(err) { | 
					
						
							|  |  |  | 			// This is a special case should be handled only for
 | 
					
						
							|  |  |  | 			// windows, because windows API does not return "not a
 | 
					
						
							|  |  |  | 			// directory" error message. Handle this specifically here.
 | 
					
						
							|  |  |  | 			return errFileAccessDenied | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-16 03:56:58 +08:00
										 |  |  | 		return osErrToFileErr(err) | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-16 03:56:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Reliably retries os.MkdirAll if for some reason os.MkdirAll returns
 | 
					
						
							|  |  |  | // syscall.ENOENT (parent does not exist).
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | func reliableMkdirAll(dirPath string, mode os.FileMode, baseDir string) (err error) { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 	i := 0 | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		// Creates all the parent directories, with mode 0777 mkdir honors system umask.
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | 		if err = osMkdirAll(dirPath, mode, baseDir); err != nil { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 			// Retry only for the first retryable error.
 | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 			if osIsNotExist(err) && i == 0 { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 				i++ | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper function to os.Rename, which calls reliableMkdirAll
 | 
					
						
							|  |  |  | // and reliableRenameAll. This is to ensure that if there is a
 | 
					
						
							|  |  |  | // racy parent directory delete in between we can simply retry
 | 
					
						
							|  |  |  | // the operation.
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | func renameAll(srcFilePath, dstFilePath, baseDir string) (err error) { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 	if srcFilePath == "" || dstFilePath == "" { | 
					
						
							|  |  |  | 		return errInvalidArgument | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = checkPathLength(srcFilePath); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err = checkPathLength(dstFilePath); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | 	if err = reliableRename(srcFilePath, dstFilePath, baseDir); err != nil { | 
					
						
							| 
									
										
										
										
											2018-08-07 01:26:40 +08:00
										 |  |  | 		switch { | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 		case isSysErrNotDir(err) && !osIsNotExist(err): | 
					
						
							|  |  |  | 			// Windows can have both isSysErrNotDir(err) and osIsNotExist(err) returning
 | 
					
						
							| 
									
										
										
										
											2021-11-22 02:41:30 +08:00
										 |  |  | 			// true if the source file path contains an non-existent directory. In that case,
 | 
					
						
							| 
									
										
										
										
											2020-05-15 09:09:30 +08:00
										 |  |  | 			// we want to return errFileNotFound instead, which will honored in subsequent
 | 
					
						
							|  |  |  | 			// switch cases
 | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 			return errFileAccessDenied | 
					
						
							| 
									
										
										
										
											2018-08-07 01:26:40 +08:00
										 |  |  | 		case isSysErrPathNotFound(err): | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 			// This is a special case should be handled only for
 | 
					
						
							|  |  |  | 			// windows, because windows API does not return "not a
 | 
					
						
							|  |  |  | 			// directory" error message. Handle this specifically here.
 | 
					
						
							|  |  |  | 			return errFileAccessDenied | 
					
						
							| 
									
										
										
										
											2018-08-07 01:26:40 +08:00
										 |  |  | 		case isSysErrCrossDevice(err): | 
					
						
							| 
									
										
										
										
											2019-12-03 01:28:01 +08:00
										 |  |  | 			return fmt.Errorf("%w (%s)->(%s)", errCrossDeviceLink, srcFilePath, dstFilePath) | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 		case osIsNotExist(err): | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 			return errFileNotFound | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 		case osIsExist(err): | 
					
						
							| 
									
										
										
										
											2019-03-21 04:06:53 +08:00
										 |  |  | 			// This is returned only when destination is a directory and we
 | 
					
						
							|  |  |  | 			// are attempting a rename from file to directory.
 | 
					
						
							|  |  |  | 			return errIsNotRegular | 
					
						
							| 
									
										
										
										
											2018-08-07 01:26:40 +08:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-07 01:26:40 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Reliably retries os.RenameAll if for some reason os.RenameAll returns
 | 
					
						
							|  |  |  | // syscall.ENOENT (parent does not exist).
 | 
					
						
							| 
									
										
										
										
											2023-09-13 23:14:36 +08:00
										 |  |  | func reliableRename(srcFilePath, dstFilePath, baseDir string) (err error) { | 
					
						
							|  |  |  | 	if err = reliableMkdirAll(path.Dir(dstFilePath), 0o777, baseDir); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 13:36:15 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 	i := 0 | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		// After a successful parent directory create attempt a renameAll.
 | 
					
						
							| 
									
										
										
										
											2021-03-24 05:51:27 +08:00
										 |  |  | 		if err = Rename(srcFilePath, dstFilePath); err != nil { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 			// Retry only for the first retryable error.
 | 
					
						
							| 
									
										
										
										
											2020-11-24 00:36:49 +08:00
										 |  |  | 			if osIsNotExist(err) && i == 0 { | 
					
						
							| 
									
										
										
										
											2018-01-14 01:13:02 +08:00
										 |  |  | 				i++ | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } |