@@ -254,29 +254,6 @@ BOOL is_asset_alive(NSMapTable<NSString *, ETCoreMLAsset *> *assets_in_use_map,
254254
255255 return assets;
256256}
257-
258- NSURL * _Nullable move_to_directory (NSURL *url,
259- NSURL *directoryURL,
260- NSFileManager *fileManager,
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-
272- NSURL *dstURL = [directoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
273- if (![fileManager moveItemAtURL: url toURL: dstURL error: error]) {
274- return nil ;
275- }
276-
277- return dstURL;
278- }
279-
280257} // namespace
281258
282259@interface ETCoreMLAssetManager () <NSFileManagerDelegate > {
@@ -322,17 +299,12 @@ - (nullable instancetype)initWithDatabase:(const std::shared_ptr<Database>&)data
322299 if (!managedAssetsDirectoryURL) {
323300 return nil ;
324301 }
325-
302+
326303 NSURL *managedTrashDirectoryURL = ::create_directory_if_needed (trashDirectoryURL, @" models" , fileManager, error);
327304 if (!managedTrashDirectoryURL) {
328305 return nil ;
329306 }
330-
331- NSURL *managedStagingDirectoryURL = ::create_directory_if_needed (assetsDirectoryURL, @" staging" , fileManager, error);
332- if (!managedStagingDirectoryURL) {
333- return nil ;
334- }
335-
307+
336308 // If directory is empty then purge the stores
337309 if (::is_directory_empty (managedAssetsDirectoryURL, fileManager, nil )) {
338310 assetsMetaStore.impl ()->purge (ec);
@@ -343,7 +315,6 @@ - (nullable instancetype)initWithDatabase:(const std::shared_ptr<Database>&)data
343315 _assetsStore = std::move (assetsStore);
344316 _assetsMetaStore = std::move (assetsMetaStore);
345317 _assetsDirectoryURL = managedAssetsDirectoryURL;
346- _stagingDirectoryURL = managedStagingDirectoryURL;
347318 _trashDirectoryURL = managedTrashDirectoryURL;
348319 _estimatedSizeInBytes = sizeInBytes.value ();
349320 _maxAssetsSizeInBytes = maxAssetsSizeInBytes;
@@ -375,15 +346,15 @@ - (nullable instancetype)initWithDatabaseURL:(NSURL *)databaseURL
375346 error: error];
376347}
377348
378- - (void )withTemporaryDirectory : (void (^)(NSURL *directoryURL))block {
379- NSURL *dstURL = [self .stagingDirectoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
380- block (dstURL);
381- if (![self .fileManager fileExistsAtPath: dstURL.path]) {
382- return ;
349+ - (nullable NSURL *)moveURL : (NSURL *)url
350+ toUniqueURLInDirectory : (NSURL *)directoryURL
351+ error : (NSError * __autoreleasing *)error {
352+ NSURL *dstURL = [directoryURL URLByAppendingPathComponent: [NSUUID UUID ].UUIDString];
353+ if (![self .fileManager moveItemAtURL: url toURL: dstURL error: error]) {
354+ return nil ;
383355 }
384-
385- move_to_directory (dstURL, self.trashDirectoryURL , self.fileManager , nil );
386- [self cleanupTrashDirectory ];
356+
357+ return dstURL;
387358}
388359
389360- (void )cleanupAssetIfNeeded : (ETCoreMLAsset *)asset {
@@ -436,8 +407,9 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
436407 return false ;
437408 }
438409
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 );
410+ // If an asset exists move it
411+ [self moveURL: dstURL toUniqueURLInDirectory: self .trashDirectoryURL error: nil ];
412+
441413 // Move the asset to assets directory.
442414 if (![self .fileManager moveItemAtURL: srcURL toURL: dstURL error: error]) {
443415 return false ;
@@ -461,25 +433,16 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
461433}
462434
463435- (void )triggerCompaction {
464- if (self.estimatedSizeInBytes >= self.maxAssetsSizeInBytes ) {
465- __weak __typeof (self) weakSelf = self;
466- dispatch_async (self.syncQueue , ^{
467- NSError *localError = nil ;
468- if (![weakSelf _compact: self .maxAssetsSizeInBytes error: &localError]) {
469- ETCoreMLLogError (localError, " Failed to compact asset store." );
470- }
471- });
436+ if (self.estimatedSizeInBytes < self.maxAssetsSizeInBytes ) {
437+ return ;
472438 }
473-
474- // Always clean the trash directory to ensure a minimal footprint.
475- // The `trashQueue` is serialized, so only one cleanup will run at a time.
476- [self cleanupTrashDirectory ];
477- }
478-
479- - (void )cleanupTrashDirectory {
439+
480440 __weak __typeof (self) weakSelf = self;
481- dispatch_async (self.trashQueue , ^{
482- [weakSelf removeFilesInTrashDirectory ];
441+ dispatch_async (self.syncQueue , ^{
442+ NSError *localError = nil ;
443+ if (![weakSelf _compact: self .maxAssetsSizeInBytes error: &localError]) {
444+ ETCoreMLLogError (localError, " Failed to compact asset store." );
445+ }
483446 });
484447}
485448
@@ -585,7 +548,7 @@ - (BOOL)_removeAssetWithIdentifier:(NSString *)identifier
585548
586549 NSURL *assetURL = ::get_asset_url (assetValue);
587550 if ([self .fileManager fileExistsAtPath: assetURL.path] &&
588- !move_to_directory ( assetURL, self.trashDirectoryURL , self. fileManager , error) ) {
551+ ![ self moveURL: assetURL toUniqueURLInDirectory: self .trashDirectoryURL error: error] ) {
589552 return false ;
590553 }
591554
@@ -686,7 +649,13 @@ - (NSUInteger)_compact:(NSUInteger)sizeInBytes error:(NSError * __autoreleasing
686649 identifier);
687650 }
688651 }
689-
652+
653+ // Trigger cleanup.
654+ __weak __typeof (self) weakSelf = self;
655+ dispatch_async (self.trashQueue , ^{
656+ [weakSelf removeFilesInTrashDirectory ];
657+ });
658+
690659 return _estimatedSizeInBytes;
691660}
692661
@@ -695,10 +664,7 @@ - (NSUInteger)compact:(NSUInteger)sizeInBytes error:(NSError * __autoreleasing *
695664 dispatch_sync (self.syncQueue , ^{
696665 result = [self _compact: sizeInBytes error: error];
697666 });
698-
699- // Always clean the trash directory to ensure a minimal footprint.
700- // The `trashQueue` is serialized, so only one cleanup will run at a time.
701- [self cleanupTrashDirectory ];
667+
702668 return result;
703669}
704670
@@ -742,7 +708,7 @@ - (BOOL)_purge:(NSError * __autoreleasing *)error {
742708 }
743709
744710 // Move the the whole assets directory to the temp directory.
745- if (!move_to_directory ( self.assetsDirectoryURL , self.trashDirectoryURL , self. fileManager , error) ) {
711+ if (![ self moveURL: self .assetsDirectoryURL toUniqueURLInDirectory: self .trashDirectoryURL error: error] ) {
746712 return false ;
747713 }
748714
@@ -758,7 +724,13 @@ - (BOOL)_purge:(NSError * __autoreleasing *)error {
758724
759725 ::set_error_from_error_code (ec, error);
760726 // Trigger cleanup
761- [self cleanupTrashDirectory ];
727+ if (status) {
728+ __weak __typeof (self) weakSelf = self;
729+ dispatch_async (self.trashQueue , ^{
730+ [weakSelf removeFilesInTrashDirectory ];
731+ });
732+ }
733+
762734 return static_cast <BOOL >(status);
763735}
764736
0 commit comments