@@ -259,10 +259,21 @@ BOOL is_asset_alive(NSMapTable<NSString *, ETCoreMLAsset *> *assets_in_use_map,
259
259
NSURL *directoryURL,
260
260
NSFileManager *fileManager,
261
261
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
+
262
272
NSURL *dstURL = [directoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
263
273
if (![fileManager moveItemAtURL: url toURL: dstURL error: error]) {
264
274
return nil ;
265
275
}
276
+
266
277
return dstURL;
267
278
}
268
279
@@ -317,9 +328,6 @@ - (nullable instancetype)initWithDatabase:(const std::shared_ptr<Database>&)data
317
328
return nil ;
318
329
}
319
330
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 );
323
331
NSURL *managedStagingDirectoryURL = ::create_directory_if_needed (assetsDirectoryURL, @" staging" , fileManager, error);
324
332
if (!managedStagingDirectoryURL) {
325
333
return nil ;
@@ -367,20 +375,14 @@ - (nullable instancetype)initWithDatabaseURL:(NSURL *)databaseURL
367
375
error: error];
368
376
}
369
377
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
-
376
378
- (void )withTemporaryDirectory : (void (^)(NSURL *directoryURL))block {
377
379
NSURL *dstURL = [self .stagingDirectoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
378
380
block (dstURL);
379
381
if (![self .fileManager fileExistsAtPath: dstURL.path]) {
380
382
return ;
381
383
}
382
384
383
- [ self moveURL: dstURL toUniqueURLInDirectory: self .trashDirectoryURL error: nil ] ;
385
+ move_to_directory ( dstURL, self.trashDirectoryURL , self. fileManager , nil ) ;
384
386
[self cleanupTrashDirectory ];
385
387
}
386
388
@@ -434,9 +436,8 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
434
436
return false ;
435
437
}
436
438
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 );
440
441
// Move the asset to assets directory.
441
442
if (![self .fileManager moveItemAtURL: srcURL toURL: dstURL error: error]) {
442
443
return false ;
@@ -584,7 +585,7 @@ - (BOOL)_removeAssetWithIdentifier:(NSString *)identifier
584
585
585
586
NSURL *assetURL = ::get_asset_url (assetValue);
586
587
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) ) {
588
589
return false ;
589
590
}
590
591
@@ -741,7 +742,7 @@ - (BOOL)_purge:(NSError * __autoreleasing *)error {
741
742
}
742
743
743
744
// 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) ) {
745
746
return false ;
746
747
}
747
748
0 commit comments