summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwight <dmerriman@gmail.com>2009-08-13 13:26:03 -0400
committerDwight <dmerriman@gmail.com>2009-08-13 13:26:03 -0400
commitf0c2c8003fc89a324e2e47aef0a218ddcd9e0d0d (patch)
tree3242386b170193a27ab8f1d31fdf85e8afcb7c00
parente6258aaa474984c6b0aeb08728fa972e3858cd31 (diff)
downloadmongo-f0c2c8003fc89a324e2e47aef0a218ddcd9e0d0d.tar.gz
don't allow dropping an index $ collection
http://jira.mongodb.org/browse/SERVER-217
-rw-r--r--client/dbclient.h2
-rw-r--r--db/dbcommands.cpp3
-rw-r--r--db/dbhelpers.cpp2
-rw-r--r--db/instance.cpp2
-rw-r--r--db/pdfile.cpp15
-rw-r--r--db/pdfile.h4
-rw-r--r--db/query.cpp32
-rw-r--r--jstests/indexd.js4
8 files changed, 34 insertions, 30 deletions
diff --git a/client/dbclient.h b/client/dbclient.h
index 9a65f643e5e..f758b23c36e 100644
--- a/client/dbclient.h
+++ b/client/dbclient.h
@@ -390,7 +390,7 @@ namespace mongo {
virtual bool dropCollection( const string &ns ){
string db = nsGetDB( ns );
string coll = nsGetCollection( ns );
- assert( coll.size() );
+ uassert( "no collection name", coll.size() );
BSONObj info;
diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp
index 98e3e3a10d9..0c72bdf5319 100644
--- a/db/dbcommands.cpp
+++ b/db/dbcommands.cpp
@@ -586,6 +586,7 @@ namespace mongo {
errmsg = "ns not found";
return false;
}
+ uassert( "can't drop collection with reserved $ character in name", strchr(nsToDrop.c_str(), '$') == 0 );
dropCollection( nsToDrop, errmsg, result );
return true;
}
@@ -1045,7 +1046,7 @@ namespace mongo {
auto_ptr< DBClientCursor > c = client.getMore( fromNs, id );
while( c->more() ) {
BSONObj obj = c->next();
- theDataFileMgr.insertAndLog( toNs.c_str(), obj );
+ theDataFileMgr.insertAndLog( toNs.c_str(), obj, true );
}
return true;
diff --git a/db/dbhelpers.cpp b/db/dbhelpers.cpp
index e09862483c4..3f5909b4a30 100644
--- a/db/dbhelpers.cpp
+++ b/db/dbhelpers.cpp
@@ -168,7 +168,7 @@ namespace mongo {
if ( val ) {
try {
BSONObj k = obj;
- theDataFileMgr.insert( name_.c_str(), k );
+ theDataFileMgr.insert( name_.c_str(), k, false );
} catch ( DBException& ) {
// dup key - already in set
}
diff --git a/db/instance.cpp b/db/instance.cpp
index 74b0712cb87..a63799a8cbf 100644
--- a/db/instance.cpp
+++ b/db/instance.cpp
@@ -471,7 +471,7 @@ namespace mongo {
while ( d.moreJSObjs() ) {
BSONObj js = d.nextJsObj();
- theDataFileMgr.insert(ns, js);
+ theDataFileMgr.insert(ns, js, false);
logOp("i", ns, js);
}
}
diff --git a/db/pdfile.cpp b/db/pdfile.cpp
index 8d69926d402..238d96a71fa 100644
--- a/db/pdfile.cpp
+++ b/db/pdfile.cpp
@@ -53,7 +53,7 @@ namespace mongo {
int MAGIC = 0x1000;
int curOp = -2;
int callDepth = 0;
- bool prealloc = true;
+ bool prealloc = true;
extern int otherTraceLevel;
void addNewNamespaceToCatalog(const char *ns, const BSONObj *options = 0);
@@ -1140,14 +1140,14 @@ assert( !eloc.isNull() );
} idToInsert;
#pragma pack()
- void DataFileMgr::insertAndLog( const char *ns, const BSONObj &o ) {
+ void DataFileMgr::insertAndLog( const char *ns, const BSONObj &o, bool god ) {
BSONObj tmp = o;
- insert( ns, tmp );
+ insert( ns, tmp, god );
logOp( "i", ns, tmp );
}
- DiskLoc DataFileMgr::insert(const char *ns, BSONObj &o) {
- DiskLoc loc = insert( ns, o.objdata(), o.objsize() );
+ DiskLoc DataFileMgr::insert(const char *ns, BSONObj &o, bool god) {
+ DiskLoc loc = insert( ns, o.objdata(), o.objsize(), god );
if ( !loc.isNull() )
o = BSONObj( loc.rec() );
return loc;
@@ -1155,6 +1155,9 @@ assert( !eloc.isNull() );
DiskLoc DataFileMgr::insert(const char *ns, const void *obuf, int len, bool god, const BSONElement &writeId, bool mayAddIndex) {
bool wouldAddIndex = false;
+// if( !god && strchr(ns,'$') )
+// cout << "TEMP";
+ uassert("cannot insert into reserved $ collection", god || strchr(ns, '$') == 0 );
const char *sys = strstr(ns, "system.");
if ( sys ) {
uassert("attempt to insert in reserved database name 'system'", sys != ns);
@@ -1173,7 +1176,7 @@ assert( !eloc.isNull() );
sys = 0;
}
- bool addIndex = wouldAddIndex && mayAddIndex;
+ bool addIndex = wouldAddIndex && mayAddIndex;
NamespaceDetails *d = nsdetails(ns);
if ( d == 0 ) {
diff --git a/db/pdfile.h b/db/pdfile.h
index 2672c008c22..d47e0f394dc 100644
--- a/db/pdfile.h
+++ b/db/pdfile.h
@@ -101,8 +101,8 @@ namespace mongo {
Record *toupdate, const DiskLoc& dl,
const char *buf, int len, stringstream& profiling);
// The object o may be updated if modified on insert.
- void insertAndLog( const char *ns, const BSONObj &o );
- DiskLoc insert(const char *ns, BSONObj &o);
+ void insertAndLog( const char *ns, const BSONObj &o, bool god = false );
+ DiskLoc insert(const char *ns, BSONObj &o, bool god = false);
DiskLoc insert(const char *ns, const void *buf, int len, bool god = false, const BSONElement &writeId = BSONElement(), bool mayAddIndex = true);
void deleteRecord(const char *ns, Record *todelete, const DiskLoc& dl, bool cappedOK = false, bool noWarn = false);
static auto_ptr<Cursor> findAll(const char *ns, const DiskLoc &startLoc = DiskLoc());
diff --git a/db/query.cpp b/db/query.cpp
index 204a1562963..98bbd048f30 100644
--- a/db/query.cpp
+++ b/db/query.cpp
@@ -106,17 +106,20 @@ namespace mongo {
justOne: stop after 1 match
*/
int deleteObjects(const char *ns, BSONObj pattern, bool justOne, bool logop, bool god) {
- if ( strstr(ns, ".system.") && !god ) {
- /* note a delete from system.indexes would corrupt the db
- if done here, as there are pointers into those objects in
- NamespaceDetails.
- */
- if( strstr(ns, ".system.users") )
- ;
- else {
- out() << "ERROR: attempt to delete in system namespace " << ns << endl;
- return -1;
+ if( !god ) {
+ if ( strstr(ns, ".system.") ) {
+ /* note a delete from system.indexes would corrupt the db
+ if done here, as there are pointers into those objects in
+ NamespaceDetails.
+ */
+ if( strstr(ns, ".system.users") )
+ ;
+ else {
+ uasserted("cannot delete from system namespace");
+ return -1;
+ }
}
+ uassert( "cannot delete from collection with reserved $ in name", strchr(ns, '$') == 0 );
}
int nDeleted = 0;
@@ -703,14 +706,9 @@ namespace mongo {
int __updateObjects(const char *ns, BSONObj updateobj, BSONObj &pattern, bool upsert, stringstream& ss, bool logop=false) {
int profile = database->profile;
+ uassert("cannot update reserved $ collection", strchr(ns, '$') == 0 );
if ( strstr(ns, ".system.") ) {
- if( strstr(ns, ".system.users") )
- ;
- else {
- out() << "\nERROR: attempt to update in system namespace " << ns << endl;
- ss << " can't update system namespace ";
- return 0;
- }
+ uassert("cannot update system collection", strstr(ns, ".system.users"));
}
QueryPlanSet qps( ns, pattern, BSONObj() );
diff --git a/jstests/indexd.js b/jstests/indexd.js
index 7f9dcf3f94b..c519b81bbd3 100644
--- a/jstests/indexd.js
+++ b/jstests/indexd.js
@@ -4,7 +4,9 @@ t.drop();
t.save( { a : 1 } );
t.ensureIndex( { a : 1 } );
-//db.indexd.$_id_.drop();
+db.indexd.$_id_.drop();
r = t.drop();
assert.eq( 1 , r.ok , "drop failed: " + tojson( r ) );
+
+//db.indexd.$_id_.remove({});