summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Williams <louis.williams@mongodb.com>2018-11-14 13:32:42 -0500
committerLouis Williams <louis.williams@mongodb.com>2018-11-26 15:00:32 -0500
commit7e345a77bc19db3779dd57a4f54b9a638471bb26 (patch)
treead42966a3f5528578f5971b79dda7f02d6f6ae19
parentdae8ec57c63d280b057e60256d56f5b24e05f571 (diff)
downloadmongo-7e345a77bc19db3779dd57a4f54b9a638471bb26.tar.gz
SERVER-38066 Shell utils should allow files to be opened in binary mode
(cherry picked from commit e768373f878c43100c023565c01ed16ad810ad1b)
-rw-r--r--src/mongo/shell/shell_utils_extended.cpp43
1 files changed, 38 insertions, 5 deletions
diff --git a/src/mongo/shell/shell_utils_extended.cpp b/src/mongo/shell/shell_utils_extended.cpp
index 00324f9d191..92da27ceab9 100644
--- a/src/mongo/shell/shell_utils_extended.cpp
+++ b/src/mongo/shell/shell_utils_extended.cpp
@@ -157,15 +157,33 @@ BSONObj hostname(const BSONObj&, void* data) {
const int CANT_OPEN_FILE = 13300;
BSONObj cat(const BSONObj& args, void* data) {
- BSONElement e = singleArg(args);
+ BSONObjIterator it(args);
+
+ auto filePath = it.next();
+ uassert(51012,
+ "the first argument to cat() must be a string containing the path to the file",
+ filePath.type() == mongo::String);
+
+ std::ios::openmode mode = std::ios::in;
+
+ auto useBinary = it.next();
+ if (!useBinary.eoo()) {
+ uassert(51013,
+ "the second argument to cat(), must be a boolean indicating whether "
+ "or not to read the file in binary mode. If omitted, the default is 'false'.",
+ useBinary.type() == mongo::Bool);
+
+ if (useBinary.Bool())
+ mode |= std::ios::binary;
+ }
+
stringstream ss;
- ifstream f(e.valuestrsafe());
+ ifstream f(filePath.valuestrsafe(), mode);
uassert(CANT_OPEN_FILE, "couldn't open file", f.is_open());
std::streamsize sz = 0;
while (1) {
char ch = 0;
- // slow...maybe change one day
f.get(ch);
if (ch == 0)
break;
@@ -257,7 +275,9 @@ BSONObj writeFile(const BSONObj& args, void* data) {
// Parse the arguments.
uassert(
- 40340, "writeFile requires 2 arguments: writeFile(filePath, content)", args.nFields() == 2);
+ 40340,
+ "writeFile requires at least 2 arguments: writeFile(filePath, content, [useBinaryMode])",
+ args.nFields() >= 2);
BSONObjIterator it(args);
@@ -286,7 +306,20 @@ BSONObj writeFile(const BSONObj& args, void* data) {
"the file name must be compatible with POSIX and Windows",
boost::filesystem::portable_name(normalizedFilePath.filename().string()));
- boost::filesystem::ofstream ofs{normalizedFilePath};
+ std::ios::openmode mode = std::ios::out;
+
+ auto useBinary = it.next();
+ if (!useBinary.eoo()) {
+ uassert(51014,
+ "the third argument to writeFile(), must be a boolean indicating whether "
+ "or not to read the file in binary mode. If omitted, the default is 'false'.",
+ useBinary.type() == mongo::Bool);
+
+ if (useBinary.Bool())
+ mode |= std::ios::binary;
+ }
+
+ boost::filesystem::ofstream ofs{normalizedFilePath, mode};
uassert(40346,
str::stream() << "failed to open file " << normalizedFilePath.string()
<< " for writing",