@@ -254,29 +254,6 @@ BOOL is_asset_alive(NSMapTable<NSString *, ETCoreMLAsset *> *assets_in_use_map,
254
254
255
255
return assets;
256
256
}
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
-
280
257
} // namespace
281
258
282
259
@interface ETCoreMLAssetManager () <NSFileManagerDelegate > {
@@ -322,17 +299,12 @@ - (nullable instancetype)initWithDatabase:(const std::shared_ptr<Database>&)data
322
299
if (!managedAssetsDirectoryURL) {
323
300
return nil ;
324
301
}
325
-
302
+
326
303
NSURL *managedTrashDirectoryURL = ::create_directory_if_needed (trashDirectoryURL, @" models" , fileManager, error);
327
304
if (!managedTrashDirectoryURL) {
328
305
return nil ;
329
306
}
330
-
331
- NSURL *managedStagingDirectoryURL = ::create_directory_if_needed (assetsDirectoryURL, @" staging" , fileManager, error);
332
- if (!managedStagingDirectoryURL) {
333
- return nil ;
334
- }
335
-
307
+
336
308
// If directory is empty then purge the stores
337
309
if (::is_directory_empty (managedAssetsDirectoryURL, fileManager, nil )) {
338
310
assetsMetaStore.impl ()->purge (ec);
@@ -343,7 +315,6 @@ - (nullable instancetype)initWithDatabase:(const std::shared_ptr<Database>&)data
343
315
_assetsStore = std::move (assetsStore);
344
316
_assetsMetaStore = std::move (assetsMetaStore);
345
317
_assetsDirectoryURL = managedAssetsDirectoryURL;
346
- _stagingDirectoryURL = managedStagingDirectoryURL;
347
318
_trashDirectoryURL = managedTrashDirectoryURL;
348
319
_estimatedSizeInBytes = sizeInBytes.value ();
349
320
_maxAssetsSizeInBytes = maxAssetsSizeInBytes;
@@ -375,15 +346,15 @@ - (nullable instancetype)initWithDatabaseURL:(NSURL *)databaseURL
375
346
error: error];
376
347
}
377
348
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 ;
383
355
}
384
-
385
- move_to_directory (dstURL, self.trashDirectoryURL , self.fileManager , nil );
386
- [self cleanupTrashDirectory ];
356
+
357
+ return dstURL;
387
358
}
388
359
389
360
- (void )cleanupAssetIfNeeded : (ETCoreMLAsset *)asset {
@@ -436,8 +407,9 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
436
407
return false ;
437
408
}
438
409
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
+
441
413
// Move the asset to assets directory.
442
414
if (![self .fileManager moveItemAtURL: srcURL toURL: dstURL error: error]) {
443
415
return false ;
@@ -461,25 +433,16 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
461
433
}
462
434
463
435
- (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 ;
472
438
}
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
+
480
440
__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
+ }
483
446
});
484
447
}
485
448
@@ -585,7 +548,7 @@ - (BOOL)_removeAssetWithIdentifier:(NSString *)identifier
585
548
586
549
NSURL *assetURL = ::get_asset_url (assetValue);
587
550
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] ) {
589
552
return false ;
590
553
}
591
554
@@ -686,7 +649,13 @@ - (NSUInteger)_compact:(NSUInteger)sizeInBytes error:(NSError * __autoreleasing
686
649
identifier);
687
650
}
688
651
}
689
-
652
+
653
+ // Trigger cleanup.
654
+ __weak __typeof (self) weakSelf = self;
655
+ dispatch_async (self.trashQueue , ^{
656
+ [weakSelf removeFilesInTrashDirectory ];
657
+ });
658
+
690
659
return _estimatedSizeInBytes;
691
660
}
692
661
@@ -695,10 +664,7 @@ - (NSUInteger)compact:(NSUInteger)sizeInBytes error:(NSError * __autoreleasing *
695
664
dispatch_sync (self.syncQueue , ^{
696
665
result = [self _compact: sizeInBytes error: error];
697
666
});
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
+
702
668
return result;
703
669
}
704
670
@@ -742,7 +708,7 @@ - (BOOL)_purge:(NSError * __autoreleasing *)error {
742
708
}
743
709
744
710
// 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] ) {
746
712
return false ;
747
713
}
748
714
@@ -758,7 +724,13 @@ - (BOOL)_purge:(NSError * __autoreleasing *)error {
758
724
759
725
::set_error_from_error_code (ec, error);
760
726
// 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
+
762
734
return static_cast <BOOL >(status);
763
735
}
764
736
0 commit comments