diff options
-rw-r--r-- | src/mongo/db/dbhelpers.cpp | 33 | ||||
-rw-r--r-- | src/mongo/db/dbhelpers.h | 12 |
2 files changed, 26 insertions, 19 deletions
diff --git a/src/mongo/db/dbhelpers.cpp b/src/mongo/db/dbhelpers.cpp index 69b3d4c7e5f..2119d975e4b 100644 --- a/src/mongo/db/dbhelpers.cpp +++ b/src/mongo/db/dbhelpers.cpp @@ -62,6 +62,7 @@ #include "mongo/db/s/collection_metadata.h" #include "mongo/db/s/sharding_state.h" #include "mongo/s/shard_key_pattern.h" +#include "mongo/util/mongoutils/str.h" #include "mongo/util/log.h" #include "mongo/util/scopeguard.h" @@ -564,7 +565,7 @@ void Helpers::emptyCollection(OperationContext* txn, const char* ns) { deleteObjects(txn, context.db(), ns, BSONObj(), PlanExecutor::YIELD_MANUAL, false); } -Helpers::RemoveSaver::RemoveSaver(const string& a, const string& b, const string& why) : _out(0) { +Helpers::RemoveSaver::RemoveSaver(const string& a, const string& b, const string& why) { static int NUM = 0; _root = storageGlobalParams.dbpath; @@ -581,27 +582,27 @@ Helpers::RemoveSaver::RemoveSaver(const string& a, const string& b, const string _file /= ss.str(); } -Helpers::RemoveSaver::~RemoveSaver() { - if (_out) { - _out->close(); - delete _out; - _out = 0; - } -} - -void Helpers::RemoveSaver::goingToDelete(const BSONObj& o) { +Status Helpers::RemoveSaver::goingToDelete(const BSONObj& o) { if (!_out) { boost::filesystem::create_directories(_root); - _out = new ofstream(); - _out->open(_file.string().c_str(), ios_base::out | ios_base::binary); - if (!_out->good()) { - error() << "couldn't create file: " << _file.string() << " for remove saving" << endl; - delete _out; + _out.reset(new ofstream(_file.string().c_str(), ios_base::out | ios_base::binary)); + if (_out->fail()) { + string msg = str::stream() << "couldn't create file: " << _file.string() + << " for remove saving: " << errnoWithDescription(); + error() << msg; + _out.reset(); _out = 0; - return; + return Status(ErrorCodes::FileNotOpen, msg); } } _out->write(o.objdata(), o.objsize()); + if (_out->fail()) { + string msg = str::stream() << "couldn't write document to file: " << _file.string() + << " for remove saving: " << errnoWithDescription(); + error() << msg; + return Status(ErrorCodes::OperationFailed, msg); + } + return Status::OK(); } diff --git a/src/mongo/db/dbhelpers.h b/src/mongo/db/dbhelpers.h index a3e5f735afa..2c0d448cf01 100644 --- a/src/mongo/db/dbhelpers.h +++ b/src/mongo/db/dbhelpers.h @@ -28,6 +28,8 @@ #pragma once +#include <iosfwd> +#include <memory> #include <boost/filesystem/path.hpp> #include "mongo/db/db.h" @@ -216,14 +218,18 @@ struct Helpers { public: RemoveSaver(const std::string& type, const std::string& ns, const std::string& why); - ~RemoveSaver(); - void goingToDelete(const BSONObj& o); + /** + * Writes document to file. File is created lazily before writing the first document. + * Returns error status if the file could not be created or if there were errors writing + * to the file. + */ + Status goingToDelete(const BSONObj& o); private: boost::filesystem::path _root; boost::filesystem::path _file; - std::ofstream* _out; + std::unique_ptr<std::ostream> _out; }; }; |