Skip to content

Commit 9afb77a

Browse files
author
Christopher J. Brody
committed
avoid incorrect default directory on iOS/macOS
- to be extra safe (see <storesafe#907>) - ensure that default "nosync" directory *always* has resource value set for `NSURLIsExcludedFromBackupKey` - add more checks for missing database directory
1 parent 3aece4b commit 9afb77a

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#### cordova-sqlite-storage 5.0.0-dev
44

5-
TBD
5+
- avoid incorrect default directory on iOS/macOS - to be extra safe (see <https://github.com/xpbrew/cordova-sqlite-storage/issues/907>)
6+
- ensure that default "nosync" directory *always* has resource value set for `NSURLIsExcludedFromBackupKey`
7+
- add more checks for missing database directory
68

79
#### cordova-sqlite-storage 4.0.0
810

src/ios/SQLitePlugin.m

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,48 @@ -(void)pluginInitialize
5151

5252
NSString *nosync = [libs stringByAppendingPathComponent:@"LocalDatabase"];
5353
NSError *err;
54+
55+
// GENERAL NOTE: no `nosync` directory path entry to be added
56+
// to appDBPaths map in case of any isses creating the
57+
// required directory or setting the resource value for
58+
// NSURLIsExcludedFromBackupKey
59+
//
60+
// This is to avoid potential for issue raised here:
61+
// https://github.com/xpbrew/cordova-sqlite-storage/issues/907
62+
5463
if ([[NSFileManager defaultManager] fileExistsAtPath: nosync])
5564
{
56-
DLog(@"no cloud sync at path: %@", nosync);
57-
[appDBPaths setObject: nosync forKey:@"nosync"];
65+
DLog(@"no cloud sync directory already exists at path: %@", nosync);
5866
}
5967
else
6068
{
6169
if ([[NSFileManager defaultManager] createDirectoryAtPath: nosync withIntermediateDirectories:NO attributes: nil error:&err])
6270
{
71+
DLog(@"no cloud sync directory created with path: %@", nosync);
72+
}
73+
else
74+
{
75+
// STOP HERE & LOG WITH INTERNAL PLUGIN ERROR:
76+
NSLog(@"INTERNAL PLUGIN ERROR: could not create no cloud sync directory at path: %@", nosync);
77+
return;
78+
}
79+
}
80+
81+
{
82+
{
83+
// Set the resource value for NSURLIsExcludedFromBackupKey
6384
NSURL *nosyncURL = [ NSURL fileURLWithPath: nosync];
6485
if (![nosyncURL setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &err])
6586
{
66-
DLog(@"IGNORED: error setting nobackup flag in LocalDatabase directory: %@", err);
87+
// STOP HERE & LOG WITH INTERNAL PLUGIN ERROR:
88+
NSLog(@"INTERNAL PLUGIN ERROR: error setting nobackup flag in LocalDatabase directory: %@", err);
89+
return;
6790
}
91+
92+
// now ready to add `nosync` entry to appDBPaths:
6893
DLog(@"no cloud sync at path: %@", nosync);
6994
[appDBPaths setObject: nosync forKey:@"nosync"];
7095
}
71-
else
72-
{
73-
// fallback:
74-
DLog(@"WARNING: error adding LocalDatabase directory: %@", err);
75-
[appDBPaths setObject: libs forKey:@"nosync"];
76-
}
7796
}
7897
}
7998
}
@@ -84,6 +103,11 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey {
84103
}
85104

86105
NSString *dbdir = [appDBPaths objectForKey:atkey];
106+
if (dbdir == NULL) {
107+
// INTERNAL PLUGIN ERROR:
108+
return NULL;
109+
}
110+
87111
NSString *dbPath = [dbdir stringByAppendingPathComponent: dbFile];
88112
return dbPath;
89113
}
@@ -244,6 +268,14 @@ -(void)deleteNow: (CDVInvokedUrlCommand*)command
244268
} else {
245269
NSString *dbPath = [self getDBPath:dbFileName at:dblocation];
246270

271+
if (dbPath == NULL) {
272+
// INTERNAL PLUGIN ERROR - NOT EXPECTED:
273+
NSLog(@"INTERNAL PLUGIN ERROR (NOT EXPECTED): delete with no valid database path found");
274+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"INTERNAL PLUGIN ERROR: delete with no valid database path found"];
275+
[self.commandDelegate sendPluginResult:pluginResult callbackId: command.callbackId];
276+
return;
277+
}
278+
247279
if ([[NSFileManager defaultManager]fileExistsAtPath:dbPath]) {
248280
DLog(@"delete full db path: %@", dbPath);
249281
[[NSFileManager defaultManager]removeItemAtPath:dbPath error:nil];

0 commit comments

Comments
 (0)