From 9afb77a13b2387deec0fbf085bd50700531e868b Mon Sep 17 00:00:00 2001 From: "Christopher J. Brody" Date: Thu, 12 Mar 2020 12:02:44 -0400 Subject: [PATCH] avoid incorrect default directory on iOS/macOS - to be extra safe (see ) - ensure that default "nosync" directory *always* has resource value set for `NSURLIsExcludedFromBackupKey` - add more checks for missing database directory --- CHANGES.md | 4 +++- src/ios/SQLitePlugin.m | 50 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 192db03d2..63eb6841c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,9 @@ #### cordova-sqlite-storage 5.0.0-dev -TBD +- avoid incorrect default directory on iOS/macOS - to be extra safe (see ) + - ensure that default "nosync" directory *always* has resource value set for `NSURLIsExcludedFromBackupKey` + - add more checks for missing database directory #### cordova-sqlite-storage 4.0.0 diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index b0a6b08f3..97a0e08d7 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -51,29 +51,48 @@ -(void)pluginInitialize NSString *nosync = [libs stringByAppendingPathComponent:@"LocalDatabase"]; NSError *err; + + // GENERAL NOTE: no `nosync` directory path entry to be added + // to appDBPaths map in case of any isses creating the + // required directory or setting the resource value for + // NSURLIsExcludedFromBackupKey + // + // This is to avoid potential for issue raised here: + // https://github.com/xpbrew/cordova-sqlite-storage/issues/907 + if ([[NSFileManager defaultManager] fileExistsAtPath: nosync]) { - DLog(@"no cloud sync at path: %@", nosync); - [appDBPaths setObject: nosync forKey:@"nosync"]; + DLog(@"no cloud sync directory already exists at path: %@", nosync); } else { if ([[NSFileManager defaultManager] createDirectoryAtPath: nosync withIntermediateDirectories:NO attributes: nil error:&err]) { + DLog(@"no cloud sync directory created with path: %@", nosync); + } + else + { + // STOP HERE & LOG WITH INTERNAL PLUGIN ERROR: + NSLog(@"INTERNAL PLUGIN ERROR: could not create no cloud sync directory at path: %@", nosync); + return; + } + } + + { + { + // Set the resource value for NSURLIsExcludedFromBackupKey NSURL *nosyncURL = [ NSURL fileURLWithPath: nosync]; if (![nosyncURL setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &err]) { - DLog(@"IGNORED: error setting nobackup flag in LocalDatabase directory: %@", err); + // STOP HERE & LOG WITH INTERNAL PLUGIN ERROR: + NSLog(@"INTERNAL PLUGIN ERROR: error setting nobackup flag in LocalDatabase directory: %@", err); + return; } + + // now ready to add `nosync` entry to appDBPaths: DLog(@"no cloud sync at path: %@", nosync); [appDBPaths setObject: nosync forKey:@"nosync"]; } - else - { - // fallback: - DLog(@"WARNING: error adding LocalDatabase directory: %@", err); - [appDBPaths setObject: libs forKey:@"nosync"]; - } } } } @@ -84,6 +103,11 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey { } NSString *dbdir = [appDBPaths objectForKey:atkey]; + if (dbdir == NULL) { + // INTERNAL PLUGIN ERROR: + return NULL; + } + NSString *dbPath = [dbdir stringByAppendingPathComponent: dbFile]; return dbPath; } @@ -244,6 +268,14 @@ -(void)deleteNow: (CDVInvokedUrlCommand*)command } else { NSString *dbPath = [self getDBPath:dbFileName at:dblocation]; + if (dbPath == NULL) { + // INTERNAL PLUGIN ERROR - NOT EXPECTED: + NSLog(@"INTERNAL PLUGIN ERROR (NOT EXPECTED): delete with no valid database path found"); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"INTERNAL PLUGIN ERROR: delete with no valid database path found"]; + [self.commandDelegate sendPluginResult:pluginResult callbackId: command.callbackId]; + return; + } + if ([[NSFileManager defaultManager]fileExistsAtPath:dbPath]) { DLog(@"delete full db path: %@", dbPath); [[NSFileManager defaultManager]removeItemAtPath:dbPath error:nil];