summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Carey <jcarey@argv.me>2019-03-15 17:54:31 -0400
committerJason Carey <jcarey@argv.me>2019-03-15 17:54:31 -0400
commit6664b30dc1cc38b5985ce9920e1d00a490b280a5 (patch)
tree524a44eeefdcc6a436943c4451272d71b7557caa
parent9acc250dc9b6ce92f1cee13b404a408e17804564 (diff)
downloadmongo-6664b30dc1cc38b5985ce9920e1d00a490b280a5.tar.gz
Revert "SERVER-39195 Make shell history file placement more correct."
This reverts commit 29dace0aaeb8299c759a15ea687cce841d509b6b.
-rw-r--r--src/mongo/shell/dbshell.cpp18
-rw-r--r--src/mongo/shell/shell_utils.cpp106
-rw-r--r--src/mongo/shell/shell_utils.h9
3 files changed, 42 insertions, 91 deletions
diff --git a/src/mongo/shell/dbshell.cpp b/src/mongo/shell/dbshell.cpp
index 46cf674fea5..91359c8b94d 100644
--- a/src/mongo/shell/dbshell.cpp
+++ b/src/mongo/shell/dbshell.cpp
@@ -84,6 +84,7 @@
using namespace std::literals::string_literals;
using namespace mongo;
+std::string historyFile;
bool gotInterrupted = false;
bool inMultiLine = false;
static AtomicWord<bool> atPrompt(false); // can eval before getting to prompt
@@ -198,7 +199,14 @@ void completionHook(const char* text, linenoiseCompletions* lc) {
}
void shellHistoryInit() {
- Status res = linenoiseHistoryLoad(shell_utils::getHistoryFilePath().string().c_str());
+ std::stringstream ss;
+ const char* h = shell_utils::getUserDir();
+ if (h)
+ ss << h << "/";
+ ss << ".dbshell";
+ historyFile = ss.str();
+
+ Status res = linenoiseHistoryLoad(historyFile.c_str());
if (!res.isOK()) {
error() << "Error loading history file: " << res;
}
@@ -206,7 +214,7 @@ void shellHistoryInit() {
}
void shellHistoryDone() {
- Status res = linenoiseHistorySave(shell_utils::getHistoryFilePath().string().c_str());
+ Status res = linenoiseHistorySave(historyFile.c_str());
if (!res.isOK()) {
error() << "Error saving history file: " << res;
}
@@ -857,14 +865,14 @@ int _main(int argc, char* argv[], char** envp) {
std::stringstream ss;
ss << "DB.prototype._defaultAuthenticationMechanism = \"" << escape(authMechanisms.get())
<< "\";" << std::endl;
- mongo::shell_utils::dbConnect += ss.str();
+ mongo::shell_utils::_dbConnect += ss.str();
}
if (const auto gssapiServiveName = parsedURI.getOption("gssapiServiceName")) {
std::stringstream ss;
ss << "DB.prototype._defaultGssapiServiceName = \"" << escape(gssapiServiveName.get())
<< "\";" << std::endl;
- mongo::shell_utils::dbConnect += ss.str();
+ mongo::shell_utils::_dbConnect += ss.str();
}
if (!shellGlobalParams.nodb) { // connect to db
@@ -901,7 +909,7 @@ int _main(int argc, char* argv[], char** envp) {
ss << "db = db.getMongo().startSession().getDatabase(db.getName());" << std::endl;
}
- mongo::shell_utils::dbConnect += ss.str();
+ mongo::shell_utils::_dbConnect += ss.str();
}
mongo::ScriptEngine::setConnectCallback(mongo::shell_utils::onConnect);
diff --git a/src/mongo/shell/shell_utils.cpp b/src/mongo/shell/shell_utils.cpp
index 1954a1105a1..6f6c15e6eeb 100644
--- a/src/mongo/shell/shell_utils.cpp
+++ b/src/mongo/shell/shell_utils.cpp
@@ -33,20 +33,6 @@
#include "mongo/shell/shell_utils.h"
-#include <algorithm>
-#include <boost/filesystem.hpp>
-#include <memory>
-#include <set>
-#include <stdlib.h>
-#include <string>
-#include <vector>
-
-#ifndef _WIN32
-#include <pwd.h>
-#include <sys/types.h>
-#endif
-
-#include "mongo/base/shim.h"
#include "mongo/client/dbclient_base.h"
#include "mongo/client/replica_set_monitor.h"
#include "mongo/db/hasher.h"
@@ -56,73 +42,26 @@
#include "mongo/shell/shell_options.h"
#include "mongo/shell/shell_utils_extended.h"
#include "mongo/shell/shell_utils_launcher.h"
-#include "mongo/stdx/mutex.h"
#include "mongo/util/fail_point_service.h"
#include "mongo/util/log.h"
#include "mongo/util/processinfo.h"
#include "mongo/util/quick_exit.h"
#include "mongo/util/text.h"
#include "mongo/util/version.h"
-#include <pwd.h>
-
-namespace mongo::shell_utils {
-namespace {
-boost::filesystem::path getUserDir() {
-#ifdef _WIN32
- auto envp = getenv("USERPROFILE");
- if (envp)
- return envp;
-
- return "./";
-#else
- const auto homeDir = getenv("HOME");
- if (homeDir)
- return homeDir;
-
- // The storage for these variables has to live until the value is captured into a std::string at
- // the end of this function. This is because getpwuid_r(3) doesn't use static storage, but
- // storage provided by the caller. As a fallback, reserve enough space to store 8 paths, on the
- // theory that the pwent buffer probably needs about that many paths, to fully describe a user
- // -- shell paths, home directory paths, etc.
-
- const long pwentBufferSize = std::max<long>(sysconf(_SC_GETPW_R_SIZE_MAX), PATH_MAX * 8);
- struct passwd pwent;
- struct passwd* res;
-
- std::vector<char> buffer(pwentBufferSize);
-
- do {
- if (!getpwuid_r(getuid(), &pwent, &buffer[0], buffer.size(), &res))
- break;
-
- if (errno != EINTR)
- uasserted(mongo::ErrorCodes::InternalError,
- "Unable to get home directory for the current user.");
- } while (errno == EINTR);
-
- return pwent.pw_dir;
-#endif
-}
-
-} // namespace
-} // namespace mongo::shell_utils
-
-boost::filesystem::path mongo::shell_utils::getHistoryFilePath() {
- static const auto& historyFile = *new boost::filesystem::path(getUserDir() / ".dbshell");
-
- return historyFile;
-}
+namespace mongo {
+using std::set;
+using std::map;
+using std::string;
-namespace mongo {
namespace JSFiles {
extern const JSFile servers;
extern const JSFile shardingtest;
extern const JSFile servers_misc;
extern const JSFile replsettest;
extern const JSFile bridge;
-} // namespace JSFiles
+}
MONGO_REGISTER_SHIM(BenchRunConfig::createConnectionImpl)
(const BenchRunConfig& config)->std::unique_ptr<DBClientBase> {
@@ -137,10 +76,9 @@ MONGO_REGISTER_SHIM(BenchRunConfig::createConnectionImpl)
namespace shell_utils {
-std::string dbConnect;
-
-static const char* argv0 = 0;
+std::string _dbConnect;
+const char* argv0 = 0;
void RecordMyLocation(const char* _argv0) {
argv0 = _argv0;
}
@@ -159,6 +97,14 @@ BSONElement singleArg(const BSONObj& args) {
return args.firstElement();
}
+const char* getUserDir() {
+#ifdef _WIN32
+ return getenv("USERPROFILE");
+#else
+ return getenv("HOME");
+#endif
+}
+
// real methods
BSONObj JSGetMemInfo(const BSONObj& args, void* data) {
@@ -398,12 +344,12 @@ void initScope(Scope& scope) {
scope.injectNative("benchStart", BenchRunner::benchStart);
scope.injectNative("benchFinish", BenchRunner::benchFinish);
- if (!dbConnect.empty()) {
- uassert(12513, "connect failed", scope.exec(dbConnect, "(connect)", false, true, false));
+ if (!_dbConnect.empty()) {
+ uassert(12513, "connect failed", scope.exec(_dbConnect, "(connect)", false, true, false));
}
}
-Prompter::Prompter(const std::string& prompt) : _prompt(prompt), _confirmed() {}
+Prompter::Prompter(const string& prompt) : _prompt(prompt), _confirmed() {}
bool Prompter::confirm() {
if (_confirmed) {
@@ -426,7 +372,7 @@ ConnectionRegistry::ConnectionRegistry() = default;
void ConnectionRegistry::registerConnection(DBClientBase& client) {
BSONObj info;
if (client.runCommand("admin", BSON("whatsmyuri" << 1), info)) {
- std::string connstr = client.getServerAddress();
+ string connstr = client.getServerAddress();
stdx::lock_guard<stdx::mutex> lk(_mutex);
_connectionUris[connstr].insert(info["you"].str());
}
@@ -435,21 +381,23 @@ void ConnectionRegistry::registerConnection(DBClientBase& client) {
void ConnectionRegistry::killOperationsOnAllConnections(bool withPrompt) const {
Prompter prompter("do you want to kill the current op(s) on the server?");
stdx::lock_guard<stdx::mutex> lk(_mutex);
- for (auto& connection : _connectionUris) {
- auto status = ConnectionString::parse(connection.first);
+ for (map<string, set<string>>::const_iterator i = _connectionUris.begin();
+ i != _connectionUris.end();
+ ++i) {
+ auto status = ConnectionString::parse(i->first);
if (!status.isOK()) {
continue;
}
const ConnectionString cs(status.getValue());
- std::string errmsg;
+ string errmsg;
std::unique_ptr<DBClientBase> conn(cs.connect("MongoDB Shell", errmsg));
if (!conn) {
continue;
}
- const std::set<std::string>& uris = connection.second;
+ const set<string>& uris = i->second;
BSONObj currentOpRes;
conn->runPseudoCommand("admin", "currentOp", "$cmd.sys.inprog", {}, currentOpRes);
@@ -460,7 +408,7 @@ void ConnectionRegistry::killOperationsOnAllConnections(bool withPrompt) const {
auto inprog = currentOpRes["inprog"].embeddedObject();
for (const auto op : inprog) {
// For sharded clusters, `client_s` is used instead and `client` is not present.
- std::string client;
+ string client;
if (auto elem = op["client"]) {
// mongod currentOp client
if (elem.type() != String) {
@@ -530,5 +478,5 @@ bool fileExists(const std::string& file) {
stdx::mutex& mongoProgramOutputMutex(*(new stdx::mutex()));
-} // namespace shell_utils
+}
} // namespace mongo
diff --git a/src/mongo/shell/shell_utils.h b/src/mongo/shell/shell_utils.h
index b3abfda9114..55f574cfcca 100644
--- a/src/mongo/shell/shell_utils.h
+++ b/src/mongo/shell/shell_utils.h
@@ -29,11 +29,6 @@
#pragma once
-#include <boost/filesystem.hpp>
-#include <map>
-#include <set>
-#include <string>
-
#include "mongo/db/jsobj.h"
#include "mongo/stdx/mutex.h"
#include "mongo/util/concurrency/mutex.h"
@@ -45,7 +40,7 @@ class DBClientBase;
namespace shell_utils {
-extern std::string dbConnect;
+extern std::string _dbConnect;
void RecordMyLocation(const char* _argv0);
void installShellUtils(Scope& scope);
@@ -53,7 +48,7 @@ void installShellUtils(Scope& scope);
void initScope(Scope& scope);
void onConnect(DBClientBase& c);
-boost::filesystem::path getHistoryFilePath();
+const char* getUserDir();
BSONElement singleArg(const BSONObj& args);
extern const BSONObj undefinedReturn;