Skip to content

Commit

Permalink
Merge pull request #850 from sptramer/timob-6273
Browse files Browse the repository at this point in the history
[TIMOB-6273] Update where databases are stored
Conflicts:

	apidoc/Titanium/Filesystem/File.yml
  • Loading branch information
srahim authored and sptramer committed Dec 7, 2011
1 parent 8f1ac99 commit 0ac5f0c
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions iphone/Classes/TiDatabaseProxy.m
Expand Up @@ -47,20 +47,45 @@ -(void)_configure

-(NSString*)dbDir
{
NSString *rootDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [[rootDir stringByAppendingPathComponent:@"database"] retain];
// See this apple tech note for why this changed: https://developer.apple.com/library/ios/#qa/qa1719/_index.html
// Apparently following these guidelines is now required for app submission

NSString *rootDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [rootDir stringByAppendingPathComponent:@"Private Documents"];
NSFileManager *fm = [NSFileManager defaultManager];

BOOL isDirectory;
BOOL exists = [fm fileExistsAtPath:dbPath isDirectory:&isDirectory];

// create folder
// Because of sandboxing, this should never happen, but we still need to handle it.
if (exists && !isDirectory) {
NSLog(@"[WARN] Recreating file %@... should be a directory and isn't.", dbPath);
[fm removeItemAtPath:dbPath error:nil];
exists = NO;
}

// create folder, and migrate the old one if necessary
if (!exists)
{
[fm createDirectoryAtPath:dbPath withIntermediateDirectories:YES attributes:nil error:nil];
}
[fm createDirectoryAtPath:dbPath withIntermediateDirectories:YES attributes:nil error:nil];
}

// Migrate any old data if available
NSString* oldRoot = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* oldPath = [oldRoot stringByAppendingString:@"database"];
BOOL oldCopyExists = [fm fileExistsAtPath:oldPath isDirectory:&isDirectory];
if (oldCopyExists && isDirectory) {
NSDirectoryEnumerator* contents = [fm enumeratorAtPath:oldPath];

for (NSString* oldFile in contents) {
[fm moveItemAtPath:oldFile toPath:[dbPath stringByAppendingPathComponent:[oldFile lastPathComponent]] error:nil];
}

// Remove the old copy after migrating everything
[fm removeItemAtPath:oldPath error:nil];
}

return [dbPath autorelease];
return dbPath;
}

-(NSString*)dbPath:(NSString*)name_
Expand Down

0 comments on commit 0ac5f0c

Please sign in to comment.