@@ -259,10 +259,21 @@ BOOL is_asset_alive(NSMapTable<NSString *, ETCoreMLAsset *> *assets_in_use_map,
259259 NSURL *directoryURL,
260260 NSFileManager *fileManager,
261261 NSError * __autoreleasing *error) {
262+ if (!url) {
263+ ETCoreMLLogErrorAndSetNSError (error, ETCoreMLErrorInternalError, " Move operation failed: source URL is nil." );
264+ return nil ;
265+ }
266+
267+ if (!directoryURL) {
268+ ETCoreMLLogErrorAndSetNSError (error, ETCoreMLErrorInternalError, " Move operation failed: destination URL is nil." );
269+ return nil ;
270+ }
271+
262272 NSURL *dstURL = [directoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
263273 if (![fileManager moveItemAtURL: url toURL: dstURL error: error]) {
264274 return nil ;
265275 }
276+
266277 return dstURL;
267278}
268279
@@ -317,9 +328,6 @@ - (nullable instancetype)initWithDatabase:(const std::shared_ptr<Database>&)data
317328 return nil ;
318329 }
319330
320- // Remove any existing contents in the staging directory by moving them to the trash directory,
321- // then recreate a clean staging directory for new use.
322- move_to_directory ([assetsDirectoryURL URLByAppendingPathComponent: @" staging" ], managedTrashDirectoryURL, fileManager, nil );
323331 NSURL *managedStagingDirectoryURL = ::create_directory_if_needed (assetsDirectoryURL, @" staging" , fileManager, error);
324332 if (!managedStagingDirectoryURL) {
325333 return nil ;
@@ -367,20 +375,14 @@ - (nullable instancetype)initWithDatabaseURL:(NSURL *)databaseURL
367375 error: error];
368376}
369377
370- - (nullable NSURL *)moveURL : (NSURL *)url
371- toUniqueURLInDirectory : (NSURL *)directoryURL
372- error : (NSError * __autoreleasing *)error {
373- return move_to_directory (url, directoryURL, self.fileManager , error);
374- }
375-
376378- (void )withTemporaryDirectory : (void (^)(NSURL *directoryURL))block {
377379 NSURL *dstURL = [self .stagingDirectoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
378380 block (dstURL);
379381 if (![self .fileManager fileExistsAtPath: dstURL.path]) {
380382 return ;
381383 }
382384
383- [ self moveURL: dstURL toUniqueURLInDirectory: self .trashDirectoryURL error: nil ] ;
385+ move_to_directory ( dstURL, self.trashDirectoryURL , self. fileManager , nil ) ;
384386 [self cleanupTrashDirectory ];
385387}
386388
@@ -434,9 +436,8 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
434436 return false ;
435437 }
436438
437- // If an asset exists move it,
438- [self moveURL: dstURL toUniqueURLInDirectory: self .trashDirectoryURL error: nil ];
439-
439+ // If a file already exists at `dstURL`, move it to the trash for removal.
440+ move_to_directory (dstURL, self.trashDirectoryURL , self.fileManager , nil );
440441 // Move the asset to assets directory.
441442 if (![self .fileManager moveItemAtURL: srcURL toURL: dstURL error: error]) {
442443 return false ;
@@ -584,7 +585,7 @@ - (BOOL)_removeAssetWithIdentifier:(NSString *)identifier
584585
585586 NSURL *assetURL = ::get_asset_url (assetValue);
586587 if ([self .fileManager fileExistsAtPath: assetURL.path] &&
587- ![ self moveURL: assetURL toUniqueURLInDirectory: self .trashDirectoryURL error: error] ) {
588+ !move_to_directory ( assetURL, self.trashDirectoryURL , self. fileManager , error) ) {
588589 return false ;
589590 }
590591
@@ -741,7 +742,7 @@ - (BOOL)_purge:(NSError * __autoreleasing *)error {
741742 }
742743
743744 // Move the the whole assets directory to the temp directory.
744- if (![ self moveURL: self .assetsDirectoryURL toUniqueURLInDirectory: self .trashDirectoryURL error: error] ) {
745+ if (!move_to_directory ( self.assetsDirectoryURL , self.trashDirectoryURL , self. fileManager , error) ) {
745746 return false ;
746747 }
747748
0 commit comments