Use a local NSFileManager insead of the defaultManager singleton, which is not thread safe.

This commit is contained in:
Andreas Linde 2012-03-24 00:16:58 +01:00
parent 333b6e6a8d
commit 7cc52cf4b9
2 changed files with 17 additions and 15 deletions

View File

@ -166,7 +166,8 @@ typedef enum CrashReportStatus {
int _analyzerStarted;
NSString *_crashesDir;
NSFileManager *_fileManager;
BOOL _crashIdenticalCurrentVersion;
BOOL _crashReportActivated;

View File

@ -76,6 +76,8 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
- (BOOL)hasNonApprovedCrashReports;
- (BOOL)hasPendingCrashReport;
@property (nonatomic, retain) NSFileManager *fileManager;
@end
@implementation BWQuincyManager
@ -91,6 +93,8 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
@synthesize appIdentifier = _appIdentifier;
@synthesize fileManager = _fileManager;
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
+(BWQuincyManager *)sharedQuincyManager {
static BWQuincyManager *sharedInstance = nil;
@ -128,6 +132,7 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
_languageStyle = nil;
_didCrashInLastSession = NO;
_loggingEnabled = NO;
_fileManager = [[NSFileManager alloc] init];
self.delegate = nil;
self.feedbackActivated = NO;
@ -155,13 +160,11 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
_crashesDir = [[NSString stringWithFormat:@"%@", [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/crashes/"]] retain];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:_crashesDir]) {
if (![self.fileManager fileExistsAtPath:_crashesDir]) {
NSDictionary *attributes = [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedLong: 0755] forKey: NSFilePosixPermissions];
NSError *theError = NULL;
[fm createDirectoryAtPath:_crashesDir withIntermediateDirectories: YES attributes: attributes error: &theError];
[self.fileManager createDirectoryAtPath:_crashesDir withIntermediateDirectories: YES attributes: attributes error: &theError];
}
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
@ -209,6 +212,9 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
[_crashesDir release];
[_crashFiles release];
[_fileManager release];
_fileManager = nil;
[super dealloc];
}
@ -301,16 +307,14 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
- (BOOL)hasPendingCrashReport {
if (_crashReportActivated) {
NSFileManager *fm = [NSFileManager defaultManager];
if ([_crashFiles count] == 0 && [fm fileExistsAtPath:_crashesDir]) {
if ([_crashFiles count] == 0 && [self.fileManager fileExistsAtPath:_crashesDir]) {
NSString *file = nil;
NSError *error = NULL;
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath: _crashesDir];
NSDirectoryEnumerator *dirEnum = [self.fileManager enumeratorAtPath: _crashesDir];
while ((file = [dirEnum nextObject])) {
NSDictionary *fileAttributes = [fm attributesOfItemAtPath:[_crashesDir stringByAppendingPathComponent:file] error:&error];
NSDictionary *fileAttributes = [self.fileManager attributesOfItemAtPath:[_crashesDir stringByAppendingPathComponent:file] error:&error];
if ([[fileAttributes objectForKey:NSFileSize] intValue] > 0) {
[_crashFiles addObject:file];
}
@ -462,7 +466,6 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
- (void)_performSendingCrashReports {
NSMutableDictionary *approvedCrashReports = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey: kApprovedCrashReports]];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = NULL;
NSString *userid = @"";
@ -523,7 +526,7 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
[approvedCrashReports setObject:[NSNumber numberWithBool:YES] forKey:[_crashFiles objectAtIndex:i]];
} else {
// we cannot do anything with this report, so delete it
[fm removeItemAtPath:filename error:&error];
[self.fileManager removeItemAtPath:filename error:&error];
}
}
@ -541,10 +544,8 @@ NSString *BWQuincyLocalize(NSString *stringToken) {
- (void)_cleanCrashReports {
NSError *error = NULL;
NSFileManager *fm = [NSFileManager defaultManager];
for (NSUInteger i=0; i < [_crashFiles count]; i++) {
[fm removeItemAtPath:[_crashesDir stringByAppendingPathComponent:[_crashFiles objectAtIndex:i]] error:&error];
[self.fileManager removeItemAtPath:[_crashesDir stringByAppendingPathComponent:[_crashFiles objectAtIndex:i]] error:&error];
}
[_crashFiles removeAllObjects];