Permalink
Browse files

Fixed regression where databases fail to open

"PUT /dbname" was failing with a 500. Underlying causes was a mixup due
to "open" being both a verb and an adjective :-p. Renamed the property
"isOpen" to disambiguate.
  • Loading branch information...
1 parent f9f823d commit d7682a95f5ea50da740d4ef05d5096f873748023 @snej snej committed Feb 15, 2013
View
3 Listener/TouchServ.m
@@ -113,12 +113,13 @@ static bool doReplicate(CBLManager* dbm, const char* replArg,
}
db = [dbm createDatabaseNamed: dbName error: &error];
}
+ if (db && ![db open: &error])
+ db = nil;
if (!db) {
fprintf(stderr, "Couldn't open database '%s': %s\n",
dbName.UTF8String, error.localizedDescription.UTF8String);
return false;
}
- [db open];
repl = [[CBL_Replicator alloc] initWithDB: db remote: remote push: !pull
continuous: continuous];
if (createTarget && !pull)
View
6 Source/API/CBLDatabase.m
@@ -42,7 +42,7 @@ @implementation CBLDatabase
@synthesize manager=_manager, unsavedModelsMutable=_unsavedModelsMutable;
-@synthesize path=_path, name=_name, open=_open, thread=_thread;
+@synthesize path=_path, name=_name, isOpen=_isOpen, thread=_thread;
- (instancetype) initWithPath: (NSString*)path
@@ -72,7 +72,7 @@ - (instancetype) initWithPath: (NSString*)path
- (void)dealloc {
- if (_open) {
+ if (_isOpen) {
//Warn(@"%@ dealloced without being closed first!", self);
[self close];
}
@@ -131,7 +131,7 @@ - (BOOL) deleteDatabase: (NSError**)outError {
LogTo(CBLDatabase, @"Deleting %@", _path);
[[NSNotificationCenter defaultCenter] postNotificationName: CBL_DatabaseWillBeDeletedNotification
object: self];
- if (_open) {
+ if (_isOpen) {
if (![self close])
return NO;
} else if (!self.exists) {
View
2 Source/API/CBLManager.m
@@ -255,7 +255,7 @@ - (BOOL) replaceDatabaseNamed: (NSString*)databaseName
CBLDatabase* db = [self _databaseNamed: databaseName mustExist: NO error: outError];
if (!db)
return NO;
- Assert(!db.open, @"Already-open database cannot be replaced");
+ Assert(!db.isOpen, @"Already-open database cannot be replaced");
NSString* dstAttachmentsPath = db.attachmentStorePath;
NSFileManager* fmgr = [NSFileManager defaultManager];
return [fmgr copyItemAtPath: databasePath toPath: db.path error: outError] &&
View
4 Source/CBLDatabase+Internal.h
@@ -64,7 +64,7 @@ extern const CBLChangesOptions kDefaultCBLChangesOptions;
__weak CBLManager* _manager;
FMDatabase *_fmdb;
BOOL _readOnly;
- BOOL _open;
+ BOOL _isOpen;
int _transactionLevel;
NSThread* _thread;
NSMutableDictionary* _views;
@@ -82,7 +82,7 @@ extern const CBLChangesOptions kDefaultCBLChangesOptions;
@property (nonatomic, readwrite, copy) NSString* name; // make it settable
@property (nonatomic, readonly) NSString* path;
@property (nonatomic, readonly) NSThread* thread;
-@property (nonatomic, readonly) BOOL open;
+@property (nonatomic, readonly) BOOL isOpen;
- (void) postPublicChangeNotification: (CBL_DatabaseChange*)change; // implemented in CBLDatabase.m
@end
View
9 Source/CBLDatabase+Internal.m
@@ -148,8 +148,9 @@ - (BOOL) openFMDB: (NSError**)outError {
- (BOOL) open: (NSError**)outError {
- if (_open)
+ if (_isOpen)
return YES;
+ LogTo(CBLDatabase, @"Opening %@", self);
if (![self openFMDB: outError])
return NO;
@@ -288,12 +289,12 @@ CREATE TABLE replicators ( \
return NO;
}
- _open = YES;
+ _isOpen = YES;
return YES;
}
- (BOOL) close {
- if (!_open)
+ if (!_isOpen)
return NO;
LogTo(CBLDatabase, @"Close %@", _path);
@@ -310,7 +311,7 @@ - (BOOL) close {
if (![_fmdb close])
return NO;
- _open = NO;
+ _isOpen = NO;
_transactionLevel = 0;
return YES;
}
View
1 Source/CBLStatus.h
@@ -49,3 +49,4 @@ static inline bool CBLStatusIsError(CBLStatus status) {return status >= 400;}
int CBLStatusToHTTPStatus( CBLStatus status, NSString** outMessage );
NSError* CBLStatusToNSError( CBLStatus status, NSURL* url );
+CBLStatus CBLStatusFromNSError(NSError* error, CBLStatus defaultStatus);
View
9 Source/CBLStatus.m
@@ -69,3 +69,12 @@ int CBLStatusToHTTPStatus( CBLStatus status, NSString** outMessage ) {
{NSLocalizedDescriptionKey, $sprintf(@"%i %@", status, reason)});
return [NSError errorWithDomain: CBLHTTPErrorDomain code: status userInfo: info];
}
+
+
+CBLStatus CBLStatusFromNSError(NSError* error, CBLStatus defaultStatus) {
+ if (!error)
+ return kCBLStatusOK;
+ if (!$equal(error.domain, CBLHTTPErrorDomain))
+ return defaultStatus;
+ return (CBLStatus)error.code;
+}
View
5 Source/CBL_Router+Handlers.m
@@ -177,8 +177,9 @@ - (CBLStatus) do_GET: (CBLDatabase*)db {
- (CBLStatus) do_PUT: (CBLDatabase*)db {
if (db.exists)
return kCBLStatusDuplicate;
- if (![db open])
- return kCBLStatusDBError;
+ NSError* error;
+ if (![db open: &error])
+ return CBLStatusFromNSError(error, kCBLStatusDBError);
[self setResponseLocation: _request.URL];
return kCBLStatusCreated;
}
View
5 Source/CBL_Router.m
@@ -233,8 +233,9 @@ - (CBLStatus) openDB {
// As a special case, the _replicator db is created on demand (as though it already existed)
if (!_db.exists && !$equal(_db.name, kCBL_ReplicatorDatabaseName))
return kCBLStatusNotFound;
- if (![_db open])
- return kCBLStatusDBError;
+ NSError* error;
+ if (![_db open: &error])
+ return CBLStatusFromNSError(error, kCBLStatusDBError);
return kCBLStatusOK;
}

0 comments on commit d7682a9

Please sign in to comment.