2019-04-12 19:27:00 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2018 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-24 17:50:15 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORFlatFileStorage.h"
|
2019-04-12 19:27:00 -07:00
|
|
|
|
2020-10-01 20:56:26 -04:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
|
2020-10-05 14:00:49 -04:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORLifecycle.h"
|
|
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORPlatform.h"
|
|
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageEventSelector.h"
|
2020-07-31 08:12:23 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
|
|
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
|
2019-04-12 19:27:00 -07:00
|
|
|
|
2020-06-24 17:50:15 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCOREvent_Private.h"
|
|
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
|
|
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
|
2019-04-12 19:27:00 -07:00
|
|
|
|
2020-09-11 10:42:30 -04:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORDirectorySizeTracker.h"
|
|
|
|
|
|
2020-07-09 17:12:54 -04:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
/** A library data key this class uses to track batchIDs. */
|
|
|
|
|
static NSString *const gBatchIDCounterKey = @"GDTCORFlatFileStorageBatchIDCounter";
|
|
|
|
|
|
|
|
|
|
/** The separator used between metadata elements in filenames. */
|
|
|
|
|
static NSString *const kMetadataSeparator = @"-";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCOREventComponentsEventIDKey = @"GDTCOREventComponentsEventIDKey";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCOREventComponentsQoSTierKey = @"GDTCOREventComponentsQoSTierKey";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCOREventComponentsMappingIDKey = @"GDTCOREventComponentsMappingIDKey";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCOREventComponentsExpirationKey = @"GDTCOREventComponentsExpirationKey";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCORBatchComponentsTargetKey = @"GDTCORBatchComponentsTargetKey";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCORBatchComponentsBatchIDKey = @"GDTCORBatchComponentsBatchIDKey";
|
|
|
|
|
|
|
|
|
|
NSString *const kGDTCORBatchComponentsExpirationKey = @"GDTCORBatchComponentsExpirationKey";
|
|
|
|
|
|
2020-09-11 10:42:30 -04:00
|
|
|
NSString *const GDTCORFlatFileStorageErrorDomain = @"GDTCORFlatFileStorage";
|
|
|
|
|
|
|
|
|
|
const uint64_t kGDTCORFlatFileStorageSizeLimit = 20 * 1000 * 1000; // 20 MB.
|
|
|
|
|
|
|
|
|
|
@interface GDTCORFlatFileStorage ()
|
|
|
|
|
|
|
|
|
|
/** An instance of the size tracker to keep track of the disk space consumed by the storage. */
|
|
|
|
|
@property(nonatomic, readonly) GDTCORDirectorySizeTracker *sizeTracker;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2020-04-09 13:14:15 -07:00
|
|
|
@implementation GDTCORFlatFileStorage
|
|
|
|
|
|
2020-09-11 10:42:30 -04:00
|
|
|
@synthesize sizeTracker = _sizeTracker;
|
|
|
|
|
|
2020-04-09 13:14:15 -07:00
|
|
|
+ (void)load {
|
2020-06-23 16:04:18 -07:00
|
|
|
#if !NDEBUG
|
|
|
|
|
[[GDTCORRegistrar sharedInstance] registerStorage:[self sharedInstance] target:kGDTCORTargetTest];
|
|
|
|
|
#endif // !NDEBUG
|
2020-04-09 13:14:15 -07:00
|
|
|
[[GDTCORRegistrar sharedInstance] registerStorage:[self sharedInstance] target:kGDTCORTargetCCT];
|
|
|
|
|
[[GDTCORRegistrar sharedInstance] registerStorage:[self sharedInstance] target:kGDTCORTargetFLL];
|
|
|
|
|
[[GDTCORRegistrar sharedInstance] registerStorage:[self sharedInstance] target:kGDTCORTargetCSH];
|
2020-08-12 18:20:21 -04:00
|
|
|
[[GDTCORRegistrar sharedInstance] registerStorage:[self sharedInstance] target:kGDTCORTargetINT];
|
2020-04-09 13:14:15 -07:00
|
|
|
}
|
2019-04-12 19:27:00 -07:00
|
|
|
|
|
|
|
|
+ (instancetype)sharedInstance {
|
2020-04-09 13:14:15 -07:00
|
|
|
static GDTCORFlatFileStorage *sharedStorage;
|
2019-04-12 19:27:00 -07:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
2020-04-09 13:14:15 -07:00
|
|
|
sharedStorage = [[GDTCORFlatFileStorage alloc] init];
|
2019-04-12 19:27:00 -07:00
|
|
|
});
|
|
|
|
|
return sharedStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
2020-04-09 13:14:15 -07:00
|
|
|
_storageQueue =
|
|
|
|
|
dispatch_queue_create("com.google.GDTCORFlatFileStorage", DISPATCH_QUEUE_SERIAL);
|
2019-09-09 15:21:29 -07:00
|
|
|
_uploadCoordinator = [GDTCORUploadCoordinator sharedInstance];
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 10:42:30 -04:00
|
|
|
- (GDTCORDirectorySizeTracker *)sizeTracker {
|
|
|
|
|
if (_sizeTracker == nil) {
|
|
|
|
|
_sizeTracker =
|
|
|
|
|
[[GDTCORDirectorySizeTracker alloc] initWithDirectoryPath:GDTCORRootDirectory().path];
|
|
|
|
|
}
|
|
|
|
|
return _sizeTracker;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
#pragma mark - GDTCORStorageProtocol
|
|
|
|
|
|
2020-02-03 08:15:31 -08:00
|
|
|
- (void)storeEvent:(GDTCOREvent *)event
|
2020-04-09 13:14:15 -07:00
|
|
|
onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
2020-04-14 12:30:29 -07:00
|
|
|
GDTCORLogDebug(@"Saving event: %@", event);
|
2020-06-23 16:04:18 -07:00
|
|
|
if (event == nil || event.serializedDataObjectBytes == nil) {
|
2020-04-14 12:30:29 -07:00
|
|
|
GDTCORLogDebug(@"%@", @"The event was nil, so it was not saved.");
|
2020-06-23 16:04:18 -07:00
|
|
|
if (completion) {
|
|
|
|
|
completion(NO, [NSError errorWithDomain:NSInternalInconsistencyException
|
|
|
|
|
code:-1
|
|
|
|
|
userInfo:nil]);
|
|
|
|
|
}
|
2019-09-03 20:37:16 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2020-02-03 08:15:31 -08:00
|
|
|
if (!completion) {
|
2020-04-09 13:14:15 -07:00
|
|
|
completion = ^(BOOL wasWritten, NSError *_Nullable error) {
|
2020-03-18 15:00:37 -07:00
|
|
|
GDTCORLogDebug(@"event %@ stored. success:%@ error:%@", event, wasWritten ? @"YES" : @"NO",
|
|
|
|
|
error);
|
2020-02-03 08:15:31 -08:00
|
|
|
};
|
|
|
|
|
}
|
2019-09-03 20:37:16 -07:00
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
__block GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
|
2019-10-02 10:40:45 -04:00
|
|
|
bgID = [[GDTCORApplication sharedApplication]
|
|
|
|
|
beginBackgroundTaskWithName:@"GDTStorage"
|
|
|
|
|
expirationHandler:^{
|
|
|
|
|
// End the background task if it's still valid.
|
|
|
|
|
[[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
|
|
|
|
|
bgID = GDTCORBackgroundIdentifierInvalid;
|
|
|
|
|
}];
|
2019-04-12 19:27:00 -07:00
|
|
|
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
|
|
|
|
// Check that a backend implementation is available for this target.
|
2020-06-23 16:04:18 -07:00
|
|
|
GDTCORTarget target = event.target;
|
|
|
|
|
NSString *filePath = [GDTCORFlatFileStorage pathForTarget:target
|
|
|
|
|
eventID:event.eventID
|
|
|
|
|
qosTier:@(event.qosTier)
|
|
|
|
|
expirationDate:event.expirationDate
|
|
|
|
|
mappingID:event.mappingID];
|
|
|
|
|
NSError *error;
|
2020-09-11 10:42:30 -04:00
|
|
|
NSData *encodedEvent = GDTCOREncodeArchive(event, nil, &error);
|
2020-06-23 16:04:18 -07:00
|
|
|
if (error) {
|
2020-04-14 12:30:29 -07:00
|
|
|
completion(NO, error);
|
|
|
|
|
return;
|
2020-09-11 10:42:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check storage size limit before storing the event.
|
|
|
|
|
uint64_t resultingStorageSize = self.sizeTracker.directoryContentSize + encodedEvent.length;
|
|
|
|
|
if (resultingStorageSize > kGDTCORFlatFileStorageSizeLimit) {
|
|
|
|
|
NSError *error = [NSError
|
|
|
|
|
errorWithDomain:GDTCORFlatFileStorageErrorDomain
|
|
|
|
|
code:GDTCORFlatFileStorageErrorSizeLimitReached
|
|
|
|
|
userInfo:@{
|
|
|
|
|
NSLocalizedFailureReasonErrorKey : @"Storage size limit has been reached."
|
|
|
|
|
}];
|
|
|
|
|
completion(NO, error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the encoded event to the file.
|
|
|
|
|
BOOL writeResult = GDTCORWriteDataToFile(encodedEvent, filePath, &error);
|
|
|
|
|
if (writeResult == NO || error) {
|
|
|
|
|
GDTCORLogDebug(@"Attempt to write archive failed: path:%@ error:%@", filePath, error);
|
|
|
|
|
completion(NO, error);
|
|
|
|
|
return;
|
2020-04-09 13:14:15 -07:00
|
|
|
} else {
|
2020-09-11 10:42:30 -04:00
|
|
|
GDTCORLogDebug(@"Writing archive succeeded: %@", filePath);
|
2020-06-23 16:04:18 -07:00
|
|
|
completion(YES, nil);
|
2020-03-18 15:00:37 -07:00
|
|
|
}
|
2019-04-12 19:27:00 -07:00
|
|
|
|
2020-09-11 10:42:30 -04:00
|
|
|
// Notify size tracker.
|
|
|
|
|
[self.sizeTracker fileWasAddedAtPath:filePath withSize:encodedEvent.length];
|
|
|
|
|
|
2019-04-12 19:27:00 -07:00
|
|
|
// Check the QoS, if it's high priority, notify the target that it has a high priority event.
|
2019-09-09 15:21:29 -07:00
|
|
|
if (event.qosTier == GDTCOREventQoSFast) {
|
2020-07-07 09:10:22 -04:00
|
|
|
// TODO: Remove a direct dependency on the upload coordinator.
|
2019-05-16 12:58:42 -07:00
|
|
|
[self.uploadCoordinator forceUploadForTarget:target];
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
2019-10-02 10:40:45 -04:00
|
|
|
// Cancel or end the associated background task if it's still valid.
|
|
|
|
|
[[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
|
|
|
|
|
bgID = GDTCORBackgroundIdentifierInvalid;
|
2019-04-12 19:27:00 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
- (void)batchWithEventSelector:(nonnull GDTCORStorageEventSelector *)eventSelector
|
|
|
|
|
batchExpiration:(nonnull NSDate *)expiration
|
|
|
|
|
onComplete:
|
|
|
|
|
(nonnull void (^)(NSNumber *_Nullable batchID,
|
|
|
|
|
NSSet<GDTCOREvent *> *_Nullable events))onComplete {
|
|
|
|
|
dispatch_queue_t queue = _storageQueue;
|
|
|
|
|
void (^onPathsForTargetComplete)(NSNumber *, NSSet<NSString *> *_Nonnull) = ^(
|
|
|
|
|
NSNumber *batchID, NSSet<NSString *> *_Nonnull paths) {
|
|
|
|
|
dispatch_async(queue, ^{
|
|
|
|
|
NSMutableSet<GDTCOREvent *> *events = [[NSMutableSet alloc] init];
|
|
|
|
|
for (NSString *eventPath in paths) {
|
2020-04-09 13:14:15 -07:00
|
|
|
NSError *error;
|
2020-06-23 16:04:18 -07:00
|
|
|
GDTCOREvent *event =
|
|
|
|
|
(GDTCOREvent *)GDTCORDecodeArchive([GDTCOREvent class], eventPath, nil, &error);
|
|
|
|
|
if (event == nil || error) {
|
|
|
|
|
GDTCORLogDebug(@"Error deserializing event: %@", error);
|
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:eventPath error:nil];
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
NSString *fileName = [eventPath lastPathComponent];
|
|
|
|
|
NSString *batchPath =
|
|
|
|
|
[GDTCORFlatFileStorage batchPathForTarget:eventSelector.selectedTarget
|
|
|
|
|
batchID:batchID
|
|
|
|
|
expirationDate:expiration];
|
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:batchPath
|
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
|
attributes:nil
|
|
|
|
|
error:nil];
|
|
|
|
|
NSString *destinationPath = [batchPath stringByAppendingPathComponent:fileName];
|
|
|
|
|
error = nil;
|
|
|
|
|
[[NSFileManager defaultManager] moveItemAtPath:eventPath
|
|
|
|
|
toPath:destinationPath
|
|
|
|
|
error:&error];
|
|
|
|
|
if (error) {
|
|
|
|
|
GDTCORLogDebug(@"An event file wasn't moveable into the batch directory: %@", error);
|
|
|
|
|
}
|
|
|
|
|
[events addObject:event];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (onComplete) {
|
2020-07-07 09:10:22 -04:00
|
|
|
if (events.count == 0) {
|
|
|
|
|
onComplete(nil, nil);
|
|
|
|
|
} else {
|
|
|
|
|
onComplete(batchID, events);
|
|
|
|
|
}
|
2020-06-23 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void (^onBatchIDFetchComplete)(NSNumber *) = ^(NSNumber *batchID) {
|
|
|
|
|
dispatch_async(queue, ^{
|
|
|
|
|
if (batchID == nil) {
|
|
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(nil, nil);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[self pathsForTarget:eventSelector.selectedTarget
|
|
|
|
|
eventIDs:eventSelector.selectedEventIDs
|
|
|
|
|
qosTiers:eventSelector.selectedQosTiers
|
|
|
|
|
mappingIDs:eventSelector.selectedMappingIDs
|
|
|
|
|
onComplete:^(NSSet<NSString *> *_Nonnull paths) {
|
|
|
|
|
onPathsForTargetComplete(batchID, paths);
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[self nextBatchID:^(NSNumber *_Nullable batchID) {
|
|
|
|
|
if (batchID == nil) {
|
|
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(nil, nil);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
onBatchIDFetchComplete(batchID);
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)removeBatchWithID:(nonnull NSNumber *)batchID
|
|
|
|
|
deleteEvents:(BOOL)deleteEvents
|
|
|
|
|
onComplete:(void (^_Nullable)(void))onComplete {
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
2020-07-09 17:12:54 -04:00
|
|
|
[self syncThreadUnsafeRemoveBatchWithID:batchID deleteEvents:deleteEvents];
|
2020-07-07 09:10:22 -04:00
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete();
|
|
|
|
|
}
|
2020-05-19 15:29:05 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
- (void)batchIDsForTarget:(GDTCORTarget)target
|
|
|
|
|
onComplete:(nonnull void (^)(NSSet<NSNumber *> *_Nullable))onComplete {
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
NSError *error;
|
|
|
|
|
NSArray<NSString *> *batchPaths =
|
|
|
|
|
[fileManager contentsOfDirectoryAtPath:[GDTCORFlatFileStorage batchDataStoragePath]
|
|
|
|
|
error:&error];
|
|
|
|
|
if (error || batchPaths.count == 0) {
|
|
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(nil);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSMutableSet<NSNumber *> *batchIDs = [[NSMutableSet alloc] init];
|
|
|
|
|
for (NSString *path in batchPaths) {
|
|
|
|
|
NSDictionary<NSString *, id> *components = [self batchComponentsFromFilename:path];
|
|
|
|
|
NSNumber *targetNumber = components[kGDTCORBatchComponentsTargetKey];
|
|
|
|
|
NSNumber *batchID = components[kGDTCORBatchComponentsBatchIDKey];
|
|
|
|
|
if (targetNumber.intValue == target) {
|
|
|
|
|
[batchIDs addObject:batchID];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(batchIDs);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-05-19 15:29:05 -07:00
|
|
|
|
|
|
|
|
- (void)libraryDataForKey:(nonnull NSString *)key
|
2020-06-23 16:04:18 -07:00
|
|
|
onFetchComplete:(nonnull void (^)(NSData *_Nullable, NSError *_Nullable))onFetchComplete
|
|
|
|
|
setNewValue:(NSData *_Nullable (^_Nullable)(void))setValueBlock {
|
2020-05-19 15:29:05 -07:00
|
|
|
dispatch_async(_storageQueue, ^{
|
2020-06-23 16:04:18 -07:00
|
|
|
NSString *dataPath = [[[self class] libraryDataStoragePath] stringByAppendingPathComponent:key];
|
2020-05-19 15:29:05 -07:00
|
|
|
NSError *error;
|
|
|
|
|
NSData *data = [NSData dataWithContentsOfFile:dataPath options:0 error:&error];
|
2020-06-23 16:04:18 -07:00
|
|
|
if (onFetchComplete) {
|
|
|
|
|
onFetchComplete(data, error);
|
|
|
|
|
}
|
|
|
|
|
if (setValueBlock) {
|
|
|
|
|
NSData *newValue = setValueBlock();
|
|
|
|
|
// The -isKindOfClass check is necessary because without an explicit 'return nil' in the block
|
|
|
|
|
// the implicit return value will be the block itself. The compiler doesn't detect this.
|
|
|
|
|
if (newValue != nil && [newValue isKindOfClass:[NSData class]] && newValue.length) {
|
|
|
|
|
NSError *newValueError;
|
2020-09-11 10:42:30 -04:00
|
|
|
if ([newValue writeToFile:dataPath options:NSDataWritingAtomic error:&newValueError]) {
|
|
|
|
|
// Update storage size.
|
|
|
|
|
[self.sizeTracker fileWasRemovedAtPath:dataPath withSize:data.length];
|
|
|
|
|
[self.sizeTracker fileWasAddedAtPath:dataPath withSize:newValue.length];
|
|
|
|
|
} else {
|
2020-06-23 16:04:18 -07:00
|
|
|
GDTCORLogDebug(@"Error writing new value in libraryDataForKey: %@", newValueError);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-19 15:29:05 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)storeLibraryData:(NSData *)data
|
|
|
|
|
forKey:(nonnull NSString *)key
|
2020-06-23 16:04:18 -07:00
|
|
|
onComplete:(nullable void (^)(NSError *_Nullable error))onComplete {
|
2020-05-19 15:29:05 -07:00
|
|
|
if (!data || data.length <= 0) {
|
|
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete([NSError errorWithDomain:NSInternalInconsistencyException code:-1 userInfo:nil]);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
|
|
|
|
NSError *error;
|
2020-06-23 16:04:18 -07:00
|
|
|
NSString *dataPath = [[[self class] libraryDataStoragePath] stringByAppendingPathComponent:key];
|
2020-09-11 10:42:30 -04:00
|
|
|
if ([data writeToFile:dataPath options:NSDataWritingAtomic error:&error]) {
|
|
|
|
|
[self.sizeTracker fileWasAddedAtPath:dataPath withSize:data.length];
|
|
|
|
|
}
|
2020-05-19 15:29:05 -07:00
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)removeLibraryDataForKey:(nonnull NSString *)key
|
|
|
|
|
onComplete:(nonnull void (^)(NSError *_Nullable error))onComplete {
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
|
|
|
|
NSError *error;
|
2020-06-23 16:04:18 -07:00
|
|
|
NSString *dataPath = [[[self class] libraryDataStoragePath] stringByAppendingPathComponent:key];
|
2020-09-11 10:42:30 -04:00
|
|
|
GDTCORStorageSizeBytes fileSize =
|
|
|
|
|
[self.sizeTracker fileSizeAtURL:[NSURL fileURLWithPath:dataPath]];
|
|
|
|
|
|
2020-05-19 15:29:05 -07:00
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
|
2020-09-11 10:42:30 -04:00
|
|
|
if ([[NSFileManager defaultManager] removeItemAtPath:dataPath error:&error]) {
|
|
|
|
|
[self.sizeTracker fileWasRemovedAtPath:dataPath withSize:fileSize];
|
|
|
|
|
}
|
2020-05-19 15:29:05 -07:00
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(error);
|
|
|
|
|
}
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
- (void)hasEventsForTarget:(GDTCORTarget)target onComplete:(void (^)(BOOL hasEvents))onComplete {
|
2020-04-09 13:14:15 -07:00
|
|
|
dispatch_async(_storageQueue, ^{
|
2020-06-23 16:04:18 -07:00
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
NSString *targetPath = [NSString
|
|
|
|
|
stringWithFormat:@"%@/%ld", [GDTCORFlatFileStorage eventDataStoragePath], (long)target];
|
|
|
|
|
[fileManager createDirectoryAtPath:targetPath
|
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
|
attributes:nil
|
|
|
|
|
error:nil];
|
|
|
|
|
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:targetPath];
|
|
|
|
|
BOOL hasEventAtLeastOneEvent = [enumerator nextObject] != nil;
|
|
|
|
|
if (onComplete) {
|
|
|
|
|
onComplete(hasEventAtLeastOneEvent);
|
2020-04-09 13:14:15 -07:00
|
|
|
}
|
|
|
|
|
});
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
- (void)checkForExpirations {
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
|
|
|
|
GDTCORLogDebug(@"%@", @"Checking for expired events and batches");
|
|
|
|
|
NSTimeInterval now = [NSDate date].timeIntervalSince1970;
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
2020-07-09 17:12:54 -04:00
|
|
|
|
|
|
|
|
// TODO: Storage may not have enough context to remove batches because a batch may be being
|
|
|
|
|
// uploaded but the storage has not context of it.
|
|
|
|
|
|
|
|
|
|
// Find expired batches and move their events back to the main storage.
|
|
|
|
|
// If a batch contains expired events they are expected to be removed further in the method
|
|
|
|
|
// together with other expired events in the main storage.
|
|
|
|
|
NSString *batchDataPath = [GDTCORFlatFileStorage batchDataStoragePath];
|
|
|
|
|
NSArray<NSString *> *batchDataPaths = [fileManager contentsOfDirectoryAtPath:batchDataPath
|
|
|
|
|
error:nil];
|
|
|
|
|
for (NSString *path in batchDataPaths) {
|
2020-11-24 12:46:50 -05:00
|
|
|
@autoreleasepool {
|
|
|
|
|
NSString *fileName = [path lastPathComponent];
|
|
|
|
|
NSDictionary<NSString *, id> *batchComponents = [self batchComponentsFromFilename:fileName];
|
|
|
|
|
NSDate *expirationDate = batchComponents[kGDTCORBatchComponentsExpirationKey];
|
2020-07-09 17:12:54 -04:00
|
|
|
NSNumber *batchID = batchComponents[kGDTCORBatchComponentsBatchIDKey];
|
2020-11-24 12:46:50 -05:00
|
|
|
if (expirationDate != nil && expirationDate.timeIntervalSince1970 < now && batchID != nil) {
|
|
|
|
|
NSNumber *batchID = batchComponents[kGDTCORBatchComponentsBatchIDKey];
|
|
|
|
|
// Move all events from the expired batch back to the main storage.
|
|
|
|
|
[self syncThreadUnsafeRemoveBatchWithID:batchID deleteEvents:NO];
|
|
|
|
|
}
|
2020-07-09 17:12:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find expired events and remove them from the storage.
|
2020-06-23 16:04:18 -07:00
|
|
|
NSString *eventDataPath = [GDTCORFlatFileStorage eventDataStoragePath];
|
|
|
|
|
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:eventDataPath];
|
|
|
|
|
NSString *path;
|
2020-11-24 12:46:50 -05:00
|
|
|
|
|
|
|
|
while (YES) {
|
|
|
|
|
@autoreleasepool {
|
|
|
|
|
// Call `[enumerator nextObject]` under autorelease pool to make sure all autoreleased
|
|
|
|
|
// objects created under the hood are released on each iteration end to avoid unnecessary
|
|
|
|
|
// memory growth.
|
|
|
|
|
path = [enumerator nextObject];
|
|
|
|
|
if (path == nil) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSString *fileName = [path lastPathComponent];
|
|
|
|
|
NSDictionary<NSString *, id> *eventComponents = [self eventComponentsFromFilename:fileName];
|
|
|
|
|
NSDate *expirationDate = eventComponents[kGDTCOREventComponentsExpirationKey];
|
|
|
|
|
if (expirationDate != nil && expirationDate.timeIntervalSince1970 < now) {
|
|
|
|
|
NSString *pathToDelete = [eventDataPath stringByAppendingPathComponent:path];
|
|
|
|
|
NSError *error;
|
|
|
|
|
[fileManager removeItemAtPath:pathToDelete error:&error];
|
|
|
|
|
if (error != nil) {
|
|
|
|
|
GDTCORLogDebug(@"There was an error deleting an expired item: %@", error);
|
|
|
|
|
} else {
|
|
|
|
|
GDTCORLogDebug(@"Item deleted because it expired: %@", pathToDelete);
|
|
|
|
|
}
|
2020-07-09 17:12:54 -04:00
|
|
|
}
|
2020-06-23 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-11 10:42:30 -04:00
|
|
|
|
|
|
|
|
[self.sizeTracker resetCachedSize];
|
2020-06-23 16:04:18 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)storageSizeWithCallback:(void (^)(uint64_t storageSize))onComplete {
|
2020-09-11 10:42:30 -04:00
|
|
|
if (!onComplete) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
dispatch_async(_storageQueue, ^{
|
2020-09-11 10:42:30 -04:00
|
|
|
onComplete([self.sizeTracker directoryContentSize]);
|
2020-06-23 16:04:18 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 09:10:22 -04:00
|
|
|
#pragma mark - Private not thread safe methods
|
|
|
|
|
/** Looks for directory paths containing events for a batch with the specified ID.
|
|
|
|
|
* @param batchID A batch ID.
|
|
|
|
|
* @param outError A pointer to `NSError *` to assign as possible error to.
|
|
|
|
|
* @return An array of an array of paths to directories for event batches with a specified batch ID
|
|
|
|
|
* or `nil` in the case of an error. Usually returns a single path but potentially return more in
|
|
|
|
|
* cases when the app is terminated while uploading a batch.
|
|
|
|
|
*/
|
|
|
|
|
- (nullable NSArray<NSString *> *)batchDirPathsForBatchID:(NSNumber *)batchID
|
|
|
|
|
error:(NSError **)outError {
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
NSError *error;
|
|
|
|
|
NSArray<NSString *> *batches =
|
|
|
|
|
[fileManager contentsOfDirectoryAtPath:[GDTCORFlatFileStorage batchDataStoragePath]
|
|
|
|
|
error:&error];
|
|
|
|
|
if (batches == nil) {
|
|
|
|
|
*outError = error;
|
|
|
|
|
GDTCORLogDebug(@"Failed to find event file paths for batchID: %@, error: %@", batchID, error);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSMutableArray<NSString *> *batchDirPaths = [NSMutableArray array];
|
|
|
|
|
for (NSString *path in batches) {
|
|
|
|
|
NSDictionary<NSString *, id> *components = [self batchComponentsFromFilename:path];
|
|
|
|
|
NSNumber *pathBatchID = components[kGDTCORBatchComponentsBatchIDKey];
|
|
|
|
|
if ([pathBatchID isEqual:batchID]) {
|
|
|
|
|
NSString *batchDirPath =
|
|
|
|
|
[[GDTCORFlatFileStorage batchDataStoragePath] stringByAppendingPathComponent:path];
|
|
|
|
|
[batchDirPaths addObject:batchDirPath];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [batchDirPaths copy];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Makes a copy of the contents of a directory to a directory at the specified path.*/
|
|
|
|
|
- (BOOL)moveContentsOfDirectoryAtPath:(NSString *)sourcePath
|
|
|
|
|
to:(NSString *)destinationPath
|
|
|
|
|
error:(NSError **)outError {
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
|
|
NSError *error;
|
|
|
|
|
NSArray<NSString *> *contentsPaths = [fileManager contentsOfDirectoryAtPath:sourcePath
|
|
|
|
|
error:&error];
|
|
|
|
|
if (contentsPaths == nil) {
|
|
|
|
|
*outError = error;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSMutableArray<NSError *> *errors = [NSMutableArray array];
|
|
|
|
|
for (NSString *path in contentsPaths) {
|
|
|
|
|
NSString *contentDestinationPath = [destinationPath stringByAppendingPathComponent:path];
|
|
|
|
|
NSString *contentSourcePath = [sourcePath stringByAppendingPathComponent:path];
|
|
|
|
|
|
|
|
|
|
NSError *moveError;
|
|
|
|
|
if (![fileManager moveItemAtPath:contentSourcePath
|
|
|
|
|
toPath:contentDestinationPath
|
|
|
|
|
error:&moveError] &&
|
|
|
|
|
moveError) {
|
|
|
|
|
[errors addObject:moveError];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errors.count == 0) {
|
|
|
|
|
return YES;
|
|
|
|
|
} else {
|
|
|
|
|
NSError *combinedError = [NSError errorWithDomain:@"GDTCORFlatFileStorage"
|
|
|
|
|
code:-1
|
|
|
|
|
userInfo:@{NSUnderlyingErrorKey : errors}];
|
|
|
|
|
*outError = combinedError;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 17:12:54 -04:00
|
|
|
- (void)syncThreadUnsafeRemoveBatchWithID:(nonnull NSNumber *)batchID
|
|
|
|
|
deleteEvents:(BOOL)deleteEvents {
|
|
|
|
|
NSError *error;
|
|
|
|
|
NSArray<NSString *> *batchDirPaths = [self batchDirPathsForBatchID:batchID error:&error];
|
|
|
|
|
|
|
|
|
|
if (batchDirPaths == nil) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
|
|
void (^removeBatchDir)(NSString *batchDirPath) = ^(NSString *batchDirPath) {
|
|
|
|
|
NSError *error;
|
|
|
|
|
if ([fileManager removeItemAtPath:batchDirPath error:&error]) {
|
|
|
|
|
GDTCORLogDebug(@"Batch removed at path: %@", batchDirPath);
|
|
|
|
|
} else {
|
|
|
|
|
GDTCORLogDebug(@"Failed to remove batch at path: %@", batchDirPath);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (NSString *batchDirPath in batchDirPaths) {
|
2020-11-24 12:46:50 -05:00
|
|
|
@autoreleasepool {
|
|
|
|
|
if (deleteEvents) {
|
|
|
|
|
removeBatchDir(batchDirPath);
|
2020-07-09 17:12:54 -04:00
|
|
|
} else {
|
2020-11-24 12:46:50 -05:00
|
|
|
NSString *batchDirName = [batchDirPath lastPathComponent];
|
|
|
|
|
NSDictionary<NSString *, id> *components = [self batchComponentsFromFilename:batchDirName];
|
|
|
|
|
NSNumber *target = components[kGDTCORBatchComponentsTargetKey];
|
|
|
|
|
NSString *destinationPath = [[GDTCORFlatFileStorage eventDataStoragePath]
|
|
|
|
|
stringByAppendingPathComponent:target.stringValue];
|
2020-07-09 17:12:54 -04:00
|
|
|
|
2020-11-24 12:46:50 -05:00
|
|
|
// `- [NSFileManager moveItemAtPath:toPath:error:]` method fails if an item by the
|
|
|
|
|
// destination path already exists (which usually is the case for the current method). Move
|
|
|
|
|
// the events one by one instead.
|
|
|
|
|
if ([self moveContentsOfDirectoryAtPath:batchDirPath to:destinationPath error:&error]) {
|
|
|
|
|
GDTCORLogDebug(@"Batched events at path: %@ moved back to the storage: %@", batchDirPath,
|
|
|
|
|
destinationPath);
|
|
|
|
|
} else {
|
|
|
|
|
GDTCORLogDebug(@"Error encountered whilst moving events back: %@", error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Even if not all events where moved back to the storage, there is not much can be done at
|
|
|
|
|
// this point, so cleanup batch directory now to avoid clattering.
|
|
|
|
|
removeBatchDir(batchDirPath);
|
|
|
|
|
}
|
2020-07-09 17:12:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-11 10:42:30 -04:00
|
|
|
|
|
|
|
|
[self.sizeTracker resetCachedSize];
|
2020-07-09 17:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-23 16:04:18 -07:00
|
|
|
#pragma mark - Private helper methods
|
|
|
|
|
|
|
|
|
|
+ (NSString *)eventDataStoragePath {
|
|
|
|
|
static NSString *eventDataPath;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
eventDataPath = [NSString stringWithFormat:@"%@/%@/gdt_event_data", GDTCORRootDirectory().path,
|
|
|
|
|
NSStringFromClass([self class])];
|
|
|
|
|
});
|
|
|
|
|
NSError *error;
|
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:eventDataPath
|
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
|
attributes:0
|
|
|
|
|
error:&error];
|
|
|
|
|
GDTCORAssert(error == nil, @"Creating the library data path failed: %@", error);
|
|
|
|
|
return eventDataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString *)batchDataStoragePath {
|
|
|
|
|
static NSString *batchDataPath;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
batchDataPath = [NSString stringWithFormat:@"%@/%@/gdt_batch_data", GDTCORRootDirectory().path,
|
|
|
|
|
NSStringFromClass([self class])];
|
|
|
|
|
});
|
|
|
|
|
NSError *error;
|
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:batchDataPath
|
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
|
attributes:0
|
|
|
|
|
error:&error];
|
|
|
|
|
GDTCORAssert(error == nil, @"Creating the batch data path failed: %@", error);
|
|
|
|
|
return batchDataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString *)libraryDataStoragePath {
|
|
|
|
|
static NSString *libraryDataPath;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
libraryDataPath =
|
|
|
|
|
[NSString stringWithFormat:@"%@/%@/gdt_library_data", GDTCORRootDirectory().path,
|
|
|
|
|
NSStringFromClass([self class])];
|
|
|
|
|
});
|
|
|
|
|
NSError *error;
|
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:libraryDataPath
|
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
|
attributes:0
|
|
|
|
|
error:&error];
|
|
|
|
|
GDTCORAssert(error == nil, @"Creating the library data path failed: %@", error);
|
|
|
|
|
return libraryDataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString *)batchPathForTarget:(GDTCORTarget)target
|
|
|
|
|
batchID:(NSNumber *)batchID
|
|
|
|
|
expirationDate:(NSDate *)expirationDate {
|
|
|
|
|
return
|
|
|
|
|
[NSString stringWithFormat:@"%@/%ld%@%@%@%llu", [GDTCORFlatFileStorage batchDataStoragePath],
|
|
|
|
|
(long)target, kMetadataSeparator, batchID, kMetadataSeparator,
|
|
|
|
|
((uint64_t)expirationDate.timeIntervalSince1970)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString *)pathForTarget:(GDTCORTarget)target
|
2020-06-25 09:46:01 -04:00
|
|
|
eventID:(NSString *)eventID
|
2020-06-23 16:04:18 -07:00
|
|
|
qosTier:(NSNumber *)qosTier
|
|
|
|
|
expirationDate:(NSDate *)expirationDate
|
|
|
|
|
mappingID:(NSString *)mappingID {
|
|
|
|
|
NSMutableCharacterSet *allowedChars = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
|
2020-06-25 09:46:01 -04:00
|
|
|
[allowedChars addCharactersInString:kMetadataSeparator];
|
2020-06-23 16:04:18 -07:00
|
|
|
mappingID = [mappingID stringByAddingPercentEncodingWithAllowedCharacters:allowedChars];
|
|
|
|
|
return [NSString stringWithFormat:@"%@/%ld/%@%@%@%@%llu%@%@",
|
|
|
|
|
[GDTCORFlatFileStorage eventDataStoragePath], (long)target,
|
|
|
|
|
eventID, kMetadataSeparator, qosTier, kMetadataSeparator,
|
|
|
|
|
((uint64_t)expirationDate.timeIntervalSince1970),
|
|
|
|
|
kMetadataSeparator, mappingID];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)pathsForTarget:(GDTCORTarget)target
|
2020-06-25 09:46:01 -04:00
|
|
|
eventIDs:(nullable NSSet<NSString *> *)eventIDs
|
2020-06-23 16:04:18 -07:00
|
|
|
qosTiers:(nullable NSSet<NSNumber *> *)qosTiers
|
|
|
|
|
mappingIDs:(nullable NSSet<NSString *> *)mappingIDs
|
|
|
|
|
onComplete:(void (^)(NSSet<NSString *> *paths))onComplete {
|
|
|
|
|
void (^completion)(NSSet<NSString *> *) = onComplete == nil ? ^(NSSet<NSString *> *paths){} : onComplete;
|
|
|
|
|
dispatch_async(_storageQueue, ^{
|
|
|
|
|
NSMutableSet<NSString *> *paths = [[NSMutableSet alloc] init];
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
NSString *targetPath = [NSString
|
|
|
|
|
stringWithFormat:@"%@/%ld", [GDTCORFlatFileStorage eventDataStoragePath], (long)target];
|
|
|
|
|
[fileManager createDirectoryAtPath:targetPath
|
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
|
attributes:nil
|
|
|
|
|
error:nil];
|
|
|
|
|
NSError *error;
|
|
|
|
|
NSArray<NSString *> *dirPaths = [fileManager contentsOfDirectoryAtPath:targetPath error:&error];
|
|
|
|
|
if (error) {
|
|
|
|
|
GDTCORLogDebug(@"There was an error reading the contents of the target path: %@", error);
|
|
|
|
|
completion(paths);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
BOOL checkingIDs = eventIDs.count > 0;
|
|
|
|
|
BOOL checkingQosTiers = qosTiers.count > 0;
|
|
|
|
|
BOOL checkingMappingIDs = mappingIDs.count > 0;
|
|
|
|
|
BOOL checkingAnything = checkingIDs == NO && checkingQosTiers == NO && checkingMappingIDs == NO;
|
|
|
|
|
for (NSString *path in dirPaths) {
|
|
|
|
|
// Skip hidden files that are created as part of atomic file creation.
|
|
|
|
|
if ([path hasPrefix:@"."]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
NSString *filePath = [targetPath stringByAppendingPathComponent:path];
|
|
|
|
|
if (checkingAnything) {
|
|
|
|
|
[paths addObject:filePath];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
NSString *filename = [path lastPathComponent];
|
|
|
|
|
NSDictionary<NSString *, id> *eventComponents = [self eventComponentsFromFilename:filename];
|
|
|
|
|
if (!eventComponents) {
|
|
|
|
|
GDTCORLogDebug(@"There was an error reading the filename components: %@", eventComponents);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-06-25 09:46:01 -04:00
|
|
|
NSString *eventID = eventComponents[kGDTCOREventComponentsEventIDKey];
|
2020-06-23 16:04:18 -07:00
|
|
|
NSNumber *qosTier = eventComponents[kGDTCOREventComponentsQoSTierKey];
|
|
|
|
|
NSString *mappingID = eventComponents[kGDTCOREventComponentsMappingIDKey];
|
|
|
|
|
|
|
|
|
|
NSNumber *eventIDMatch = checkingIDs ? @([eventIDs containsObject:eventID]) : nil;
|
|
|
|
|
NSNumber *qosTierMatch = checkingQosTiers ? @([qosTiers containsObject:qosTier]) : nil;
|
|
|
|
|
NSNumber *mappingIDMatch =
|
|
|
|
|
checkingMappingIDs
|
|
|
|
|
? @([mappingIDs containsObject:[mappingID stringByRemovingPercentEncoding]])
|
|
|
|
|
: nil;
|
|
|
|
|
if ((eventIDMatch == nil || eventIDMatch.boolValue) &&
|
|
|
|
|
(qosTierMatch == nil || qosTierMatch.boolValue) &&
|
|
|
|
|
(mappingIDMatch == nil || mappingIDMatch.boolValue)) {
|
|
|
|
|
[paths addObject:filePath];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
completion(paths);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)nextBatchID:(void (^)(NSNumber *_Nullable batchID))nextBatchID {
|
|
|
|
|
__block int32_t lastBatchID = -1;
|
|
|
|
|
[self libraryDataForKey:gBatchIDCounterKey
|
|
|
|
|
onFetchComplete:^(NSData *_Nullable data, NSError *_Nullable getValueError) {
|
|
|
|
|
if (!getValueError) {
|
|
|
|
|
[data getBytes:(void *)&lastBatchID length:sizeof(int32_t)];
|
|
|
|
|
}
|
|
|
|
|
if (data == nil) {
|
|
|
|
|
lastBatchID = 0;
|
|
|
|
|
}
|
|
|
|
|
if (nextBatchID) {
|
|
|
|
|
nextBatchID(@(lastBatchID));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setNewValue:^NSData *_Nullable(void) {
|
|
|
|
|
if (lastBatchID != -1) {
|
|
|
|
|
int32_t incrementedValue = lastBatchID + 1;
|
|
|
|
|
return [NSData dataWithBytes:&incrementedValue length:sizeof(int32_t)];
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable NSDictionary<NSString *, id> *)eventComponentsFromFilename:(NSString *)fileName {
|
|
|
|
|
NSArray<NSString *> *components = [fileName componentsSeparatedByString:kMetadataSeparator];
|
|
|
|
|
if (components.count >= 4) {
|
2020-06-25 09:46:01 -04:00
|
|
|
NSString *eventID = components[0];
|
2020-06-23 16:04:18 -07:00
|
|
|
NSNumber *qosTier = @(components[1].integerValue);
|
|
|
|
|
NSDate *expirationDate = [NSDate dateWithTimeIntervalSince1970:components[2].longLongValue];
|
|
|
|
|
NSString *mappingID = [[components subarrayWithRange:NSMakeRange(3, components.count - 3)]
|
|
|
|
|
componentsJoinedByString:kMetadataSeparator];
|
|
|
|
|
if (eventID == nil || qosTier == nil || mappingID == nil || expirationDate == nil) {
|
|
|
|
|
GDTCORLogDebug(@"There was an error parsing the event filename components: %@", components);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
return @{
|
|
|
|
|
kGDTCOREventComponentsEventIDKey : eventID,
|
|
|
|
|
kGDTCOREventComponentsQoSTierKey : qosTier,
|
|
|
|
|
kGDTCOREventComponentsExpirationKey : expirationDate,
|
|
|
|
|
kGDTCOREventComponentsMappingIDKey : mappingID
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
GDTCORLogDebug(@"The event filename could not be split: %@", fileName);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable NSDictionary<NSString *, id> *)batchComponentsFromFilename:(NSString *)fileName {
|
|
|
|
|
NSArray<NSString *> *components = [fileName componentsSeparatedByString:kMetadataSeparator];
|
|
|
|
|
if (components.count == 3) {
|
|
|
|
|
NSNumber *target = @(components[0].integerValue);
|
|
|
|
|
NSNumber *batchID = @(components[1].integerValue);
|
|
|
|
|
NSDate *expirationDate = [NSDate dateWithTimeIntervalSince1970:components[2].doubleValue];
|
|
|
|
|
if (target == nil || batchID == nil || expirationDate == nil) {
|
|
|
|
|
GDTCORLogDebug(@"There was an error parsing the batch filename components: %@", components);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
return @{
|
|
|
|
|
kGDTCORBatchComponentsTargetKey : target,
|
|
|
|
|
kGDTCORBatchComponentsBatchIDKey : batchID,
|
|
|
|
|
kGDTCORBatchComponentsExpirationKey : expirationDate
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
GDTCORLogDebug(@"The batch filename could not be split: %@", fileName);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - GDTCORLifecycleProtocol
|
|
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
- (void)appWillBackground:(GDTCORApplication *)app {
|
2019-04-12 19:27:00 -07:00
|
|
|
dispatch_async(_storageQueue, ^{
|
2020-06-23 16:04:18 -07:00
|
|
|
// Immediately request a background task to run until the end of the current queue of work,
|
|
|
|
|
// and cancel it once the work is done.
|
2019-10-02 10:40:45 -04:00
|
|
|
__block GDTCORBackgroundIdentifier bgID =
|
|
|
|
|
[app beginBackgroundTaskWithName:@"GDTStorage"
|
|
|
|
|
expirationHandler:^{
|
|
|
|
|
[app endBackgroundTask:bgID];
|
|
|
|
|
bgID = GDTCORBackgroundIdentifierInvalid;
|
|
|
|
|
}];
|
|
|
|
|
// End the background task if it's still valid.
|
|
|
|
|
[app endBackgroundTask:bgID];
|
|
|
|
|
bgID = GDTCORBackgroundIdentifierInvalid;
|
2019-04-12 19:27:00 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
- (void)appWillTerminate:(GDTCORApplication *)application {
|
2020-02-10 13:28:27 -08:00
|
|
|
dispatch_sync(_storageQueue, ^{
|
2020-06-23 16:04:18 -07:00
|
|
|
});
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
2020-07-09 17:12:54 -04:00
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|