From 0658e7ca4de567ae93b3e6c7d38b024f9b6d12ec Mon Sep 17 00:00:00 2001 From: vzaidman Date: Wed, 13 May 2015 09:25:42 +0300 Subject: [PATCH 1/4] added support to enabling database when ios is locked --- src/ios/SQLitePlugin.m | 16 ++++++++++++++++ www/SQLitePlugin.js | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index 6406e4bd7..db8f993c7 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -109,6 +109,16 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey { return dbPath; } +-(void)enableDatabaseWhenLocked:(BOOL)enable path:(NSString*)path{ + NSDictionary *attributes = @{NSFileProtectionKey: enable ? NSFileProtectionCompleteUnlessOpen : NSFileProtectionComplete}; + NSError *error; + if(![[NSFileManager defaultManager] setAttributes:attributes + ofItemAtPath:path + error:&error]){ + NSLog(@"DATABASE WARNING! could not set database file protection attributes. error: %@", [error localizedDescription]); + } +} + -(void)open: (CDVInvokedUrlCommand*)command { CDVPluginResult* pluginResult = nil; @@ -122,6 +132,8 @@ -(void)open: (CDVInvokedUrlCommand*)command NSString *dbname = [self getDBPath:dbfilename at:dblocation]; + BOOL enableDatabaseWhenLocked = [options valueForKey:@"enableDatabaseWhenLocked"] == nil ? NO : [[options valueForKey:@"enableDatabaseWhenLocked"] boolValue]; + if (dbname == NULL) { NSLog(@"No db name specified for open"); pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"You must specify database name"]; @@ -145,6 +157,10 @@ -(void)open: (CDVInvokedUrlCommand*)command [self createFromResource:dbfilename withDbname:dbname]; } + if(![enableDatabaseWhenLocked isEqual:[NSNull null]]){ + [self enableDatabaseWhenLocked:[enableDatabaseWhenLocked boolValue] path:dbname]; + } + if (sqlite3_open(name, &db) != SQLITE_OK) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unable to open DB"]; return; diff --git a/www/SQLitePlugin.js b/www/SQLitePlugin.js index 8b5a8f43c..a6e6fa75f 100644 --- a/www/SQLitePlugin.js +++ b/www/SQLitePlugin.js @@ -198,6 +198,7 @@ }; })(this); this.openDBs[this.dbname] = DB_STATE_INIT; + this.openargs.enableDatabaseWhenLocked = root.sqlitePlugin.enableDatabaseWhenLocked; cordova.exec(opensuccesscb, openerrorcb, "SQLitePlugin", "open", [this.openargs]); } }; @@ -567,6 +568,7 @@ sqliteFeatures: { isSQLitePlugin: true }, + enableDatabaseWhenLocked: null, openDatabase: SQLiteFactory.opendb, deleteDatabase: SQLiteFactory.deleteDb }; From e8e1421c6126f76c6a70e87c8a06f92fef0b1627 Mon Sep 17 00:00:00 2001 From: vzaidman Date: Wed, 13 May 2015 10:26:45 +0300 Subject: [PATCH 2/4] minor fix --- src/ios/SQLitePlugin.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index db8f993c7..a6e3b1620 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -132,7 +132,7 @@ -(void)open: (CDVInvokedUrlCommand*)command NSString *dbname = [self getDBPath:dbfilename at:dblocation]; - BOOL enableDatabaseWhenLocked = [options valueForKey:@"enableDatabaseWhenLocked"] == nil ? NO : [[options valueForKey:@"enableDatabaseWhenLocked"] boolValue]; + NSNumber *enableDatabaseWhenLocked = [options valueForKey:@"enableDatabaseWhenLocked"]; if (dbname == NULL) { NSLog(@"No db name specified for open"); From 76066bf02c9c28812ec33c06d07ffa8f089c07a8 Mon Sep 17 00:00:00 2001 From: Vitalik Zaidman Date: Mon, 25 May 2015 09:34:41 +0300 Subject: [PATCH 3/4] Update SQLitePlugin.m Enabling database when locked now disables database file security whatsoever so it can always be open. --- src/ios/SQLitePlugin.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index a6e3b1620..1351ce2df 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -110,7 +110,7 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey { } -(void)enableDatabaseWhenLocked:(BOOL)enable path:(NSString*)path{ - NSDictionary *attributes = @{NSFileProtectionKey: enable ? NSFileProtectionCompleteUnlessOpen : NSFileProtectionComplete}; + NSDictionary *attributes = @{NSFileProtectionKey: enable ? NSFileProtectionNone : NSFileProtectionComplete}; NSError *error; if(![[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:path From 79f520c11eddb928cded8b9dc0f56a861d6ad8b9 Mon Sep 17 00:00:00 2001 From: Vitalik Zaidman Date: Wed, 24 Jun 2015 12:45:42 +0300 Subject: [PATCH 4/4] fixed plugin and improved logging added logging of successful disabling of file protection of database. now disables file protection after database opening so it would apply on first launch as well. --- src/ios/SQLitePlugin.m | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index 1351ce2df..d44d6e5b8 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -115,7 +115,10 @@ -(void)enableDatabaseWhenLocked:(BOOL)enable path:(NSString*)path{ if(![[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:path error:&error]){ - NSLog(@"DATABASE WARNING! could not set database file protection attributes. error: %@", [error localizedDescription]); + NSLog(@"DATABASE WARNING! Database access was not enabled because of an error: %@", [error localizedDescription]); + } + else{ + NSLog(@"Database access enabled when locked."); } } @@ -133,14 +136,14 @@ -(void)open: (CDVInvokedUrlCommand*)command NSString *dbname = [self getDBPath:dbfilename at:dblocation]; NSNumber *enableDatabaseWhenLocked = [options valueForKey:@"enableDatabaseWhenLocked"]; - + if (dbname == NULL) { NSLog(@"No db name specified for open"); pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"You must specify database name"]; } else { NSValue *dbPointer = [openDBs objectForKey:dbfilename]; - + if (dbPointer != NULL) { NSLog(@"Reusing existing database connection for db name %@", dbfilename); pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Database opened"]; @@ -157,16 +160,16 @@ -(void)open: (CDVInvokedUrlCommand*)command [self createFromResource:dbfilename withDbname:dbname]; } - if(![enableDatabaseWhenLocked isEqual:[NSNull null]]){ - [self enableDatabaseWhenLocked:[enableDatabaseWhenLocked boolValue] path:dbname]; - } - if (sqlite3_open(name, &db) != SQLITE_OK) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unable to open DB"]; return; } else { + if(![enableDatabaseWhenLocked isEqual:[NSNull null]]){ + [self enableDatabaseWhenLocked:[enableDatabaseWhenLocked boolValue] path:dbname]; + } + sqlite3_create_function(db, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL); - + // for SQLCipher version: // NSString *dbkey = [options objectForKey:@"key"]; // const char *key = NULL; @@ -177,6 +180,7 @@ -(void)open: (CDVInvokedUrlCommand*)command if(sqlite3_exec(db, (const char*)"SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) { dbPointer = [NSValue valueWithPointer:db]; [openDBs setObject: dbPointer forKey: dbfilename]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Database opened"]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unable to open DB with key"]; @@ -210,10 +214,12 @@ -(void)createFromResource:(NSString *)dbfile withDbname:(NSString *)dbname { NSError *error; BOOL success = [[NSFileManager defaultManager] copyItemAtPath:prepopulatedDb toPath:dbname error:&error]; - if(success) + if(success){ NSLog(@"Copied prepopulated DB content to: %@", dbname); - else + } + else{ NSLog(@"Unable to copy DB file: %@", [error localizedDescription]); + } } }