Overview
Comment: | OFURLHandler: Add methods for copy and move
Also refactor OFFileManager to make use of them. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
c8defc67ad7963eed83fe30acb4da0b1 |
User & Date: | js on 2017-12-29 21:33:41 |
Other Links: | manifest | tags |
Context
2017-12-30
| ||
19:12 | OFFileManager: Don't use OF_KINDOF check-in: f71d8c57e2 user: js tags: trunk | |
2017-12-29
| ||
21:33 | OFURLHandler: Add methods for copy and move check-in: c8defc67ad user: js tags: trunk | |
16:00 | OFFileManager: Refactor -[copyItemAtURL:toURL:] check-in: b4835ea9b7 user: js tags: trunk | |
Changes
Modified src/OFFileManager.h from [819d7d3e09] to [72331c3b2f].
︙ | ︙ | |||
437 438 439 440 441 442 443 | /*! * @brief Moves an item. * * The destination URL must have a full path, which means it must include the * name of the item. * | | | | | 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | /*! * @brief Moves an item. * * The destination URL must have a full path, which means it must include the * name of the item. * * If the destination is on a different logical device or uses a different * scheme, the source will be copied to the destination using * @ref copyItemAtURL:toURL: and the source removed using @ref removeItemAtURL:. * * @param source The item to rename * @param destination The new name for the item */ - (void)moveItemAtURL: (OFURL *)source toURL: (OFURL *)destination; |
︙ | ︙ |
Modified src/OFFileManager.m from [d50176e0c3] to [d68d8af1f6].
︙ | ︙ | |||
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | objc_autoreleasePoolPop(pool); } - (void)copyItemAtURL: (OFURL *)source toURL: (OFURL *)destination { void *pool; of_file_attributes_t attributes; of_file_type_t type; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); if ([self fileExistsAtURL: destination]) @throw [OFCopyItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: EEXIST]; | > > > > > > > > > | 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | objc_autoreleasePoolPop(pool); } - (void)copyItemAtURL: (OFURL *)source toURL: (OFURL *)destination { void *pool; OF_KINDOF(OFURLHandler *) URLHandler; of_file_attributes_t attributes; of_file_type_t type; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); if ((URLHandler = [OFURLHandler handlerForURL: source]) == nil) @throw [OFUnsupportedProtocolException exceptionWithURL: source]; if ([URLHandler copyItemAtURL: source toURL: destination]) return; if ([self fileExistsAtURL: destination]) @throw [OFCopyItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: EEXIST]; |
︙ | ︙ | |||
638 639 640 641 642 643 644 | objc_autoreleasePoolPop(pool); } - (void)moveItemAtPath: (OFString *)source toPath: (OFString *)destination { | < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < > > > > > > | > > > | > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > | 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | objc_autoreleasePoolPop(pool); } - (void)moveItemAtPath: (OFString *)source toPath: (OFString *)destination { void *pool = objc_autoreleasePoolPush(); [self moveItemAtURL: [OFURL fileURLWithPath: source] toURL: [OFURL fileURLWithPath: destination]]; objc_autoreleasePoolPop(pool); } - (void)moveItemAtURL: (OFURL *)source toURL: (OFURL *)destination { void *pool; OF_KINDOF(OFURLHandler *) URLHandler; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); if ((URLHandler = [OFURLHandler handlerForURL: source]) == nil) @throw [OFUnsupportedProtocolException exceptionWithURL: source]; @try { if ([URLHandler moveItemAtURL: source toURL: destination]) return; } @catch (OFMoveItemFailedException *e) { if ([e errNo] != EXDEV) @throw e; } if ([self fileExistsAtURL: destination]) @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: EEXIST]; @try { [self copyItemAtURL: source toURL: destination]; } @catch (OFCopyItemFailedException *e) { [self removeItemAtURL: destination]; @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: [e errNo]]; } @try { [self removeItemAtURL: source]; } @catch (OFRemoveItemFailedException *e) { @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: [e errNo]]; } objc_autoreleasePoolPop(pool); } - (void)removeItemAtURL: (OFURL *)URL { OF_KINDOF(OFURLHandler *) URLHandler; |
︙ | ︙ |
Modified src/OFURLHandler.h from [310f8dcadd] to [a57f482d01].
︙ | ︙ | |||
180 181 182 183 184 185 186 187 188 189 | * privileges! * * @param URL The URL to the item which should symbolically link to the target * @param target The target of the symbolic link */ - (void)createSymbolicLinkAtURL: (OFURL *)URL withDestinationPath: (OFString *)target; @end OF_ASSUME_NONNULL_END | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | * privileges! * * @param URL The URL to the item which should symbolically link to the target * @param target The target of the symbolic link */ - (void)createSymbolicLinkAtURL: (OFURL *)URL withDestinationPath: (OFString *)target; /*! * @brief Tries to efficiently copy an item. If a copy would only be possible * by reading the entire item and then writing it, it returns false. * * The destination URL must have a full path, which means it must include the * name of the item. * * If an item already exists, the copy operation fails. This is also the case * if a directory is copied and an item already exists in the destination * directory. * * @param source The file, directory or symbolic link to copy * @param destination The destination URL * @return True if an efficient copy was performed, false if an efficient copy * was not possible. Note that errors while performing a copy are * reported via exceptions and not by returning false! */ - (bool)copyItemAtURL: (OFURL *)source toURL: (OFURL *)destination; /*! * @brief Tries to efficiently move an item. If a move would only be possible * by copying the source and deleting it, it returns false. * * The destination URL must have a full path, which means it must include the * name of the item. * * If the destination is on a different logical device or uses a different * scheme, an efficient move is not possible and false is returned. * * @param source The item to rename * @param destination The new name for the item * @return True if an efficient move was performed, false if an efficient move * was not possible. Note that errors while performing a move are * reported via exceptions and not by returning false! */ - (bool)moveItemAtURL: (OFURL *)source toURL: (OFURL *)destination; @end OF_ASSUME_NONNULL_END |
Modified src/OFURLHandler.m from [a812aa8773] to [d27034ecd7].
︙ | ︙ | |||
168 169 170 171 172 173 174 175 | } - (void)createSymbolicLinkAtURL: (OFURL *)destination withDestinationPath: (OFString *)source { OF_UNRECOGNIZED_SELECTOR } @end | > > > > > > > > > > > > | 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | } - (void)createSymbolicLinkAtURL: (OFURL *)destination withDestinationPath: (OFString *)source { OF_UNRECOGNIZED_SELECTOR } - (bool)copyItemAtURL: (OFURL *)source toURL: (OFURL *)destination { return false; } - (bool)moveItemAtURL: (OFURL *)source toURL: (OFURL *)destination { return false; } @end |
Modified src/OFURLHandler_file.m from [a088ce861b] to [e4f29d35cd].
︙ | ︙ | |||
46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # import "OFMutex.h" #endif #import "OFCreateDirectoryFailedException.h" #import "OFCreateSymbolicLinkFailedException.h" #import "OFInvalidArgumentException.h" #import "OFLinkFailedException.h" #import "OFNotImplementedException.h" #import "OFOpenItemFailedException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFRemoveItemFailedException.h" #import "OFRetrieveItemAttributesFailedException.h" #import "OFSetItemAttributesFailedException.h" | > | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # import "OFMutex.h" #endif #import "OFCreateDirectoryFailedException.h" #import "OFCreateSymbolicLinkFailedException.h" #import "OFInvalidArgumentException.h" #import "OFLinkFailedException.h" #import "OFMoveItemFailedException.h" #import "OFNotImplementedException.h" #import "OFOpenItemFailedException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFRemoveItemFailedException.h" #import "OFRetrieveItemAttributesFailedException.h" #import "OFSetItemAttributesFailedException.h" |
︙ | ︙ | |||
1070 1071 1072 1073 1074 1075 1076 1077 | target: target errNo: 0]; # endif objc_autoreleasePoolPop(pool); } #endif @end | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 | target: target errNo: 0]; # endif objc_autoreleasePoolPop(pool); } #endif - (bool)moveItemAtURL: (OFURL *)source toURL: (OFURL *)destination { void *pool; if (![[source scheme] isEqual: _scheme] || ![[destination scheme] isEqual: _scheme]) return false; if ([self fileExistsAtURL: destination]) @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: EEXIST]; pool = objc_autoreleasePoolPush(); #if defined(OF_WINDOWS) if (_wrename([[source fileSystemRepresentation] UTF16String], [[destination fileSystemRepresentation] UTF16String]) != 0) @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: errno]; #elif defined(OF_MORPHOS) of_string_encoding_t encoding = [OFLocalization encoding]; if (!Rename([[source fileSystemRepresentation] cStringWithEncoding: encoding], [[destination fileSystemRepresentation] cStringWithEncoding: encoding]) != 0) { int errNo; switch (IoErr()) { case ERROR_RENAME_ACROSS_DEVICES: errNo = EXDEV; break; case ERROR_OBJECT_IN_USE: case ERROR_DISK_NOT_VALIDATED: errNo = EBUSY; break; case ERROR_OBJECT_EXISTS: errNo = EEXIST; break; case ERROR_OBJECT_NOT_FOUND: errNo = ENOENT; break; case ERROR_DISK_WRITE_PROTECTED: errNo = EROFS; break; default: errNo = 0; break; } @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: errNo]; } #else of_string_encoding_t encoding = [OFLocalization encoding]; if (rename([[source fileSystemRepresentation] cStringWithEncoding: encoding], [[destination fileSystemRepresentation] cStringWithEncoding: encoding]) != 0) @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination errNo: errno]; #endif objc_autoreleasePoolPop(pool); return true; } @end |
Modified src/exceptions/OFCopyItemFailedException.m from [d0facaaa0d] to [785894e8e1].
︙ | ︙ | |||
14 15 16 17 18 19 20 21 22 23 24 25 26 27 | * file. */ #include "config.h" #import "OFCopyItemFailedException.h" #import "OFString.h" @implementation OFCopyItemFailedException @synthesize sourceURL = _sourceURL, destinationURL = _destinationURL; @synthesize errNo = _errNo; + (instancetype)exception { | > | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | * file. */ #include "config.h" #import "OFCopyItemFailedException.h" #import "OFString.h" #import "OFURL.h" @implementation OFCopyItemFailedException @synthesize sourceURL = _sourceURL, destinationURL = _destinationURL; @synthesize errNo = _errNo; + (instancetype)exception { |
︙ | ︙ |
Modified src/exceptions/OFMoveItemFailedException.h from [b9a783ccba] to [e79452cdc8].
︙ | ︙ | |||
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | * file. */ #import "OFException.h" OF_ASSUME_NONNULL_BEGIN /*! * @class OFMoveItemFailedException \ * OFMoveItemFailedException.h ObjFW/OFMoveItemFailedException.h * * @brief An exception indicating that moving an item failed. */ @interface OFMoveItemFailedException: OFException { | > > | | | | | | | | | | | | | | | | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | * file. */ #import "OFException.h" OF_ASSUME_NONNULL_BEGIN @class OFURL; /*! * @class OFMoveItemFailedException \ * OFMoveItemFailedException.h ObjFW/OFMoveItemFailedException.h * * @brief An exception indicating that moving an item failed. */ @interface OFMoveItemFailedException: OFException { OFURL *_sourceURL, *_destinationURL; int _errNo; } /*! * @brief The original URL. */ @property (readonly, nonatomic) OFURL *sourceURL; /*! * @brief The new URL. */ @property (readonly, nonatomic) OFURL *destinationURL; /*! * @brief The errno of the error that occurred. */ @property (readonly, nonatomic) int errNo; + (instancetype)exception OF_UNAVAILABLE; /*! * @brief Creates a new, autoreleased move item failed exception. * * @param sourceURL The original URL * @param destinationURL The new URL * @param errNo The errno of the error that occurred * @return A new, autoreleased move item failed exception */ + (instancetype)exceptionWithSourceURL: (OFURL *)sourceURL destinationURL: (OFURL *)destinationURL errNo: (int)errNo; - (instancetype)init OF_UNAVAILABLE; /*! * @brief Initializes an already allocated move item failed exception. * * @param sourceURL The original URL * @param destinationURL The new URL * @param errNo The errno of the error that occurred * @return An initialized move item failed exception */ - (instancetype)initWithSourceURL: (OFURL *)sourceURL destinationURL: (OFURL *)destinationURL errNo: (int)errNo OF_DESIGNATED_INITIALIZER; @end OF_ASSUME_NONNULL_END |
Modified src/exceptions/OFMoveItemFailedException.m from [265677a4a5] to [b4c69e61e3].
︙ | ︙ | |||
14 15 16 17 18 19 20 21 22 | * file. */ #include "config.h" #import "OFMoveItemFailedException.h" #import "OFString.h" @implementation OFMoveItemFailedException | > | | | | | | | | | | | | | | | | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | * file. */ #include "config.h" #import "OFMoveItemFailedException.h" #import "OFString.h" #import "OFURL.h" @implementation OFMoveItemFailedException @synthesize sourceURL = _sourceURL, destinationURL = _destinationURL; @synthesize errNo = _errNo; + (instancetype)exception { OF_UNRECOGNIZED_SELECTOR } + (instancetype)exceptionWithSourceURL: (OFURL *)sourceURL destinationURL: (OFURL *)destinationURL errNo: (int)errNo { return [[[self alloc] initWithSourceURL: sourceURL destinationURL: destinationURL errNo: errNo] autorelease]; } - (instancetype)init { OF_INVALID_INIT_METHOD } - (instancetype)initWithSourceURL: (OFURL *)sourceURL destinationURL: (OFURL *)destinationURL errNo: (int)errNo { self = [super init]; @try { _sourceURL = [sourceURL copy]; _destinationURL = [destinationURL copy]; _errNo = errNo; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_sourceURL release]; [_destinationURL release]; [super dealloc]; } - (OFString *)description { return [OFString stringWithFormat: @"Failed to move item at %@ to %@: %@", _sourceURL, _destinationURL, of_strerror(_errNo)]; } @end |