-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
SyncManager.m
415 lines (299 loc) · 15.7 KB
/
SyncManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// SyncManager.m
// Strongbox
//
// Created by Strongbox on 20/06/2020.
// Copyright © 2014-2021 Mark McGuill. All rights reserved.
//
#import "SyncManager.h"
#import "DatabasePreferences.h"
#import "SafeStorageProviderFactory.h"
#import "AppPreferences.h"
#import "OfflineDetector.h"
#import "LocalDeviceStorageProvider.h"
#import "FilesAppUrlBookmarkProvider.h"
#import "StrongboxiOSFilesManager.h"
#import "LocalDatabaseIdentifier.h"
#import "DatabaseModel.h"
#import "Constants.h"
#import "Utils.h"
#import "BackupsManager.h"
#import "ConcurrentMutableDictionary.h"
#import "SyncStatus.h"
#import "AppPreferences.h"
#import "Serializator.h"
#import "WorkingCopyManager.h"
@interface SyncManager ()
@property (nonatomic, strong) dispatch_queue_t dispatchQueue;
@property (nonatomic, strong) dispatch_source_t source;
@property (nonatomic, strong) dispatch_queue_t wcDispatchQueue;
@property (nonatomic, strong) dispatch_source_t wcSource;
@end
@implementation SyncManager
+ (instancetype)sharedInstance {
static SyncManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[SyncManager alloc] init];
});
return sharedInstance;
}
- (void)backgroundSyncOutstandingUpdates {
for (DatabasePreferences* database in DatabasePreferences.allDatabases) {
if (database.outstandingUpdateId) {
[self backgroundSyncDatabase:database];
}
}
}
- (void)backgroundSyncAll {
for (DatabasePreferences* database in DatabasePreferences.allDatabases) {
[self backgroundSyncDatabase:database];
}
}
- (void)backgroundSyncLocalDeviceDatabasesOnly {
for (DatabasePreferences* database in DatabasePreferences.allDatabases) {
if (database.storageProvider == kLocalDevice) {
[self backgroundSyncDatabase:database];
}
}
}
- (void)backgroundSyncDatabase:(DatabasePreferences*)database {
[self backgroundSyncDatabase:database join:YES];
}
- (void)backgroundSyncDatabase:(DatabasePreferences*)database join:(BOOL)join {
[self backgroundSyncDatabase:database join:join key:nil completion:^(SyncAndMergeResult result, BOOL localWasChanged, NSError * error) { }];
}
- (void)backgroundSyncDatabase:(DatabasePreferences*)database join:(BOOL)join key:(CompositeKeyFactors*_Nullable)key completion:(SyncAndMergeCompletionBlock)completion {
SyncParameters* params = [[SyncParameters alloc] init];
params.inProgressBehaviour = join ? kInProgressBehaviourJoin : kInProgressBehaviourEnqueueAnotherSync;
params.syncForcePushDoNotCheckForConflicts = AppPreferences.sharedInstance.syncForcePushDoNotCheckForConflicts;
params.syncPullEvenIfModifiedDateSame = AppPreferences.sharedInstance.syncPullEvenIfModifiedDateSame;
params.key = key;
slog(@"🟢 BACKGROUND SYNC Start: [%@]", database.nickName);
[SyncAndMergeSequenceManager.sharedInstance enqueueSyncForDatabaseId:database.uuid
parameters:params
completion:^(SyncAndMergeResult result, BOOL localWasChanged, NSError * _Nullable error) {
slog(@"🟢 BACKGROUND SYNC DONE: [%@] - [%@] - workingCacheWasChanged = %hhd - [%@]", database.nickName, syncResultToString(result), localWasChanged, error);
completion(result, localWasChanged, error);
}];
}
- (void)sync:(DatabasePreferences *)database
interactiveVC:(UIViewController *)interactiveVC
key:(CompositeKeyFactors*)key join:(BOOL)join completion:(SyncAndMergeCompletionBlock)completion {
SyncParameters* params = [[SyncParameters alloc] init];
params.interactiveVC = interactiveVC;
params.key = key;
params.inProgressBehaviour = join ? kInProgressBehaviourJoin : kInProgressBehaviourEnqueueAnotherSync;
params.syncForcePushDoNotCheckForConflicts = AppPreferences.sharedInstance.syncForcePushDoNotCheckForConflicts;
params.syncPullEvenIfModifiedDateSame = AppPreferences.sharedInstance.syncPullEvenIfModifiedDateSame;
[SyncAndMergeSequenceManager.sharedInstance enqueueSyncForDatabaseId:database.uuid
parameters:params
completion:^(SyncAndMergeResult result, BOOL localWasChanged, NSError * _Nullable error) {
slog(@"INTERACTIVE SYNC DONE: [%@] - [%@][%@]", database.nickName, syncResultToString(result), error);
completion(result, localWasChanged, error);
}];
}
- (BOOL)updateLocalCopyMarkAsRequiringSync:(DatabasePreferences *)database file:(NSString *)file error:(NSError **)error {
return [self updateLocalCopyMarkAsRequiringSync:database data:nil file:file error:error];
}
- (BOOL)updateLocalCopyMarkAsRequiringSync:(DatabasePreferences *)database data:(NSData *)data error:(NSError**)error {
return [self updateLocalCopyMarkAsRequiringSync:database data:data file:nil error:error];
}
- (BOOL)updateLocalCopyMarkAsRequiringSync:(DatabasePreferences *)database data:(NSData *)data file:(NSString *)file error:(NSError**)error {
NSURL* localWorkingCache = [WorkingCopyManager.sharedInstance getLocalWorkingCache:database.uuid];
if (localWorkingCache) {
if(![BackupsManager.sharedInstance writeBackup:localWorkingCache metadata:database]) {
slog(@"WARNWARN: Local Working Cache unavailable or could not write backup: [%@]", localWorkingCache);
NSString* em = NSLocalizedString(@"model_error_cannot_write_backup", @"Could not write backup, will not proceed with write of database!");
if(error) {
*error = [Utils createNSError:em errorCode:-1];
}
return NO;
}
}
NSUUID* updateId = NSUUID.UUID;
database.outstandingUpdateId = updateId;
NSURL* url;
if ( file ) {
url = [WorkingCopyManager.sharedInstance setWorkingCacheWithFile:file
dateModified:NSDate.date
database:database.uuid
error:error];
}
else {
url = [WorkingCopyManager.sharedInstance setWorkingCacheWithData:data
dateModified:NSDate.date
database:database.uuid
error:error];
}
return url != nil;
}
- (SyncStatus*)getSyncStatus:(DatabasePreferences *)database {
return [SyncAndMergeSequenceManager.sharedInstance getSyncStatusForDatabaseId:database.uuid];
}
- (BOOL)syncInProgressForDatabase:(NSString*)databaseId {
SyncStatus *status = [SyncAndMergeSequenceManager.sharedInstance getSyncStatusForDatabaseId:databaseId];
return status.state == kSyncOperationStateInProgress;
}
- (NSString *)getPrimaryStorageDisplayName:(DatabasePreferences *)database {
return [SafeStorageProviderFactory getStorageDisplayName:database];
}
- (void)startMonitoringWorkingCacheDirectory {
NSString * homeDirectory = StrongboxFilesManager.sharedInstance.syncManagerLocalWorkingCachesDirectory.path;
int filedes = open([homeDirectory cStringUsingEncoding:NSASCIIStringEncoding], O_EVTONLY);
self.wcDispatchQueue = dispatch_queue_create("WorkingCacheMonitorQueue", 0);
self.wcSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, filedes, DISPATCH_VNODE_WRITE, _dispatchQueue);
dispatch_source_set_event_handler(self.wcSource, ^(){
dispatch_async(dispatch_get_main_queue(), ^(void) {
slog(@"🚀 Working Cache File Change Detected!");
});
});
dispatch_source_set_cancel_handler(self.wcSource, ^() {
close(filedes);
});
dispatch_resume(self.wcSource);
}
- (void)startMonitoringDocumentsDirectory {
NSString * homeDirectory = StrongboxFilesManager.sharedInstance.documentsDirectory.path;
int filedes = open([homeDirectory cStringUsingEncoding:NSASCIIStringEncoding], O_EVTONLY);
_dispatchQueue = dispatch_queue_create("FileMonitorQueue", 0);
_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, filedes, DISPATCH_VNODE_WRITE, _dispatchQueue);
dispatch_source_set_event_handler(_source, ^(){
dispatch_async(dispatch_get_main_queue(), ^(void) {
slog(@"File Change Detected! Scanning for New Safes");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self syncLocalSafesWithFileSystem];
});
});
});
dispatch_source_set_cancel_handler(_source, ^() {
close(filedes);
});
dispatch_resume(_source);
[self syncLocalSafesWithFileSystem];
}
- (void)stopMonitoringDocumentsDirectory {
dispatch_source_cancel(_source);
}
- (NSArray<StorageBrowserItem *>*)scanForNewDatabases {
NSArray<DatabasePreferences*> * localSafes = [DatabasePreferences forAllDatabasesOfProvider:kLocalDevice];
NSMutableSet *existing = [NSMutableSet set];
for (DatabasePreferences* safe in localSafes) {
LocalDatabaseIdentifier* identifier = [self getLegacyLocalDatabaseStorageIdentifier:safe];
if(identifier && !identifier.sharedStorage) {
[existing addObject:identifier.filename];
}
}
NSError* error;
NSArray<StorageBrowserItem *> *items = [self getDocumentFiles:&error];
NSMutableArray<StorageBrowserItem *> *newSafes = [NSMutableArray array];
if(items) {
for (StorageBrowserItem *item in items) {
if(!item.folder && ![existing containsObject:item.name]) {
NSURL *url = [StrongboxFilesManager.sharedInstance.documentsDirectory URLByAppendingPathComponent:item.name];
if([Serializator isValidDatabase:url error:&error]) {
slog(@"New File: [%@] is a valid database", item.name);
[newSafes addObject:item];
}
else {
}
}
}
}
else {
slog(@"Error Scanning for New Files. List Root: %@", error);
}
return newSafes;
}
- (NSArray<StorageBrowserItem*>*)getDocumentFiles:(NSError**)ppError {
NSError *error;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:StrongboxFilesManager.sharedInstance.documentsDirectory.path
error:&error];
if (error) {
if ( ppError ) {
*ppError = error;
}
return nil;
}
NSMutableArray<StorageBrowserItem*>* items = [NSMutableArray array];
for (NSString* file in directoryContent) {
BOOL isDirectory;
NSString *fullPath = [StrongboxFilesManager.sharedInstance.documentsDirectory.path stringByAppendingPathComponent:file];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
if(exists) {
LocalDatabaseIdentifier *identifier = [[LocalDatabaseIdentifier alloc] init];
identifier.sharedStorage = NO;
identifier.filename = file;
StorageBrowserItem *browserItem = [StorageBrowserItem itemWithName:file identifier:nil folder:isDirectory != 0 providerData:identifier];
[items addObject:browserItem];
}
}
return items;
}
- (void)syncLocalSafesWithFileSystem {
NSArray<StorageBrowserItem*> *items = [self scanForNewDatabases];
for(StorageBrowserItem* item in items) {
NSString* name = [DatabasePreferences trimDatabaseNickName:[item.name stringByDeletingPathExtension]];
DatabasePreferences *safe = [LocalDeviceStorageProvider.sharedInstance getDatabasePreferences:name
providerData:item.providerData];
NSURL *url = [StrongboxFilesManager.sharedInstance.documentsDirectory URLByAppendingPathComponent:item.name];
NSData* snapshot = [NSData dataWithContentsOfURL:url];
NSError* error;
NSDictionary *att = [NSFileManager.defaultManager attributesOfItemAtPath:url.path error:&error];
NSError* addError;
if ( ![safe addWithDuplicateCheck:snapshot initialCacheModDate:att.fileModificationDate error:&addError] ) {
slog(@"Error adding database - error = [%@]", error);
}
}
NSArray<DatabasePreferences*> *localSafes = [DatabasePreferences forAllDatabasesOfProvider:kLocalDevice];
for (DatabasePreferences* localSafe in localSafes) {
NSURL* url = [self getLegacyLocalDatabaseFileUrl:localSafe];
if(![NSFileManager.defaultManager fileExistsAtPath:url.path]) {
slog(@"WARNWARN: Database [%@] underlying file [%@] no longer exists in Documents Directory.", localSafe.nickName, localSafe.fileName);
}
}
[SyncManager.sharedInstance backgroundSyncLocalDeviceDatabasesOnly];
}
- (BOOL)toggleLocalDatabaseFilesVisibility:(DatabasePreferences*)databaseMetadata error:(NSError**)error {
LocalDatabaseIdentifier* identifier = [self getLegacyLocalDatabaseStorageIdentifier:databaseMetadata];
NSURL* src = [self getLegacyLocalDatabaseFileUrl:identifier.sharedStorage filename:identifier.filename];
NSURL* dest = [self getLegacyLocalDatabaseFileUrl:!identifier.sharedStorage filename:identifier.filename];
int i=0;
NSString* extension = [identifier.filename pathExtension];
NSString* baseFileName = [identifier.filename stringByDeletingPathExtension];
[self stopMonitoringDocumentsDirectory];
while ([[NSFileManager defaultManager] fileExistsAtPath:dest.path]) {
identifier.filename = [[baseFileName stringByAppendingFormat:@"-%d", i] stringByAppendingPathExtension:extension];
dest = [self getLegacyLocalDatabaseFileUrl:!identifier.sharedStorage filename:identifier.filename];
slog(@"File exists at destination... Trying: [%@]", dest);
}
if(![NSFileManager.defaultManager moveItemAtURL:src toURL:dest error:error]) {
slog(@"Error moving local file: [%@]", *error);
[self startMonitoringDocumentsDirectory];
return NO;
}
else {
slog(@"OK - Moved local file: [%@] -> [%@]", src, dest);
}
identifier.sharedStorage = !identifier.sharedStorage;
databaseMetadata.fileIdentifier = [identifier toJson];
databaseMetadata.fileName = identifier.filename;
[self startMonitoringDocumentsDirectory];
return YES;
}
- (LocalDatabaseIdentifier*)getLegacyLocalDatabaseStorageIdentifier:(DatabasePreferences*)metaData {
NSString* json = metaData.fileIdentifier;
return [LocalDatabaseIdentifier fromJson:json];
}
- (NSURL*)getLegacyLocalDatabaseDirectory:(BOOL)shared {
return shared ? StrongboxFilesManager.sharedInstance.sharedAppGroupDirectory : StrongboxFilesManager.sharedInstance.documentsDirectory;
}
- (NSURL*)getLegacyLocalDatabaseFileUrl:(DatabasePreferences*)safeMetaData {
LocalDatabaseIdentifier* identifier = [self getLegacyLocalDatabaseStorageIdentifier:safeMetaData];
return identifier ? [self getLegacyLocalDatabaseFileUrl:identifier.sharedStorage filename:identifier.filename] : nil;
}
- (NSURL*)getLegacyLocalDatabaseFileUrl:(BOOL)sharedStorage filename:(NSString*)filename {
NSURL* folder = [self getLegacyLocalDatabaseDirectory:sharedStorage];
return [folder URLByAppendingPathComponent:filename];
}
@end