int Tool::main( int argc , char ** argv ) { static StaticObserver staticObserver; cmdLine.prealloc = false; // The default value may vary depending on compile options, but for tools // we want durability to be disabled. cmdLine.dur = false; #if( BOOST_VERSION >= 104500 ) boost::filesystem::path::default_name_check( boost::filesystem2::no_check ); #else boost::filesystem::path::default_name_check( boost::filesystem::no_check ); #endif _name = argv[0]; /* using the same style as db.cpp */ int command_line_style = (((po::command_line_style::unix_style ^ po::command_line_style::allow_guessing) | po::command_line_style::allow_long_disguise) ^ po::command_line_style::allow_sticky); try { po::options_description all_options("all options"); all_options.add(*_options).add(*_hidden_options); po::store( po::command_line_parser( argc , argv ). options(all_options). positional( _positonalOptions ). style(command_line_style).run() , _params ); po::notify( _params ); } catch (po::error &e) { cerr << "ERROR: " << e.what() << endl << endl; printHelp(cerr); ::_exit(EXIT_BADOPTIONS); } // hide password from ps output for (int i=0; i < (argc-1); ++i) { if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--password")) { char* arg = argv[i+1]; while (*arg) { *arg++ = 'x'; } } } if ( _params.count( "help" ) ) { printHelp(cout); ::_exit(0); } if ( _params.count( "version" ) ) { printVersion(cout); ::_exit(0); } if ( _params.count( "verbose" ) ) { logLevel = 1; } for (string s = "vv"; s.length() <= 10; s.append("v")) { if (_params.count(s)) { logLevel = s.length(); } } #ifdef MONGO_SSL if (_params.count("ssl")) { mongo::cmdLine.sslOnNormalPorts = true; } #endif preSetup(); bool useDirectClient = hasParam( "dbpath" ); if ( ! useDirectClient ) { _host = "127.0.0.1"; if ( _params.count( "host" ) ) _host = _params["host"].as<string>(); if ( _params.count( "port" ) ) _host += ':' + _params["port"].as<string>(); if ( _noconnection ) { // do nothing } else { string errmsg; ConnectionString cs = ConnectionString::parse( _host , errmsg ); if ( ! cs.isValid() ) { cerr << "invalid hostname [" << _host << "] " << errmsg << endl; ::_exit(-1); } _conn = cs.connect( errmsg ); if ( ! _conn ) { cerr << "couldn't connect to [" << _host << "] " << errmsg << endl; ::_exit(-1); } (_usesstdout ? cout : cerr ) << "connected to: " << _host << endl; } } else { if ( _params.count( "directoryperdb" ) ) { directoryperdb = true; } verify( lastError.get( true ) ); if (_params.count("journal")){ cmdLine.dur = true; } Client::initThread("tools"); _conn = new DBDirectClient(); _host = "DIRECT"; static string myDbpath = getParam( "dbpath" ); dbpath = myDbpath.c_str(); try { acquirePathLock(); } catch ( DBException& ) { cerr << endl << "If you are running a mongod on the same " "path you should connect to that instead of direct data " "file access" << endl << endl; dbexit( EXIT_CLEAN ); ::_exit(-1); } FileAllocator::get()->start(); dur::startup(); } if ( _params.count( "db" ) ) _db = _params["db"].as<string>(); if ( _params.count( "collection" ) ) _coll = _params["collection"].as<string>(); if ( _params.count( "username" ) ) _username = _params["username"].as<string>(); if ( _params.count( "password" ) && ( _password.empty() ) ) { _password = askPassword(); } if (_params.count("ipv6")) enableIPv6(); int ret = -1; try { ret = run(); } catch ( DBException& e ) { cerr << "assertion: " << e.toString() << endl; ret = -1; } catch(const boost::filesystem::filesystem_error &fse) { /* https://jira.mongodb.org/browse/SERVER-2904 Simple tools that don't access the database, such as bsondump, aren't throwing DBExceptions, but are throwing boost exceptions. The currently available set of error codes don't seem to match boost documentation. boost::filesystem::not_found_error (from http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/exception.htm) doesn't seem to exist in our headers. Also, fse.code() isn't boost::system::errc::no_such_file_or_directory when this happens, as you would expect. And, determined from experimentation that the command-line argument gets turned into "\\?" instead of "/?" !!! */ #if defined(_WIN32) if (/*(fse.code() == boost::system::errc::no_such_file_or_directory) &&*/ (fse.path1() == "\\?")) printHelp(cerr); else #endif // _WIN32 cerr << "error: " << fse.what() << endl; ret = -1; } if ( currentClient.get() ) currentClient.get()->shutdown(); if ( useDirectClient ) dbexit( EXIT_CLEAN ); ::_exit(ret); }
int Tool::main( int argc , char ** argv ){ static StaticObserver staticObserver; cmdLine.prealloc = false; boost::filesystem::path::default_name_check( boost::filesystem::no_check ); _name = argv[0]; /* using the same style as db.cpp */ int command_line_style = (((po::command_line_style::unix_style ^ po::command_line_style::allow_guessing) | po::command_line_style::allow_long_disguise) ^ po::command_line_style::allow_sticky); try { po::options_description all_options("all options"); all_options.add(*_options).add(*_hidden_options); po::store( po::command_line_parser( argc , argv ). options(all_options). positional( _positonalOptions ). style(command_line_style).run() , _params ); po::notify( _params ); } catch (po::error &e) { cerr << "ERROR: " << e.what() << endl << endl; printHelp(cerr); return EXIT_BADOPTIONS; } // hide password from ps output for (int i=0; i < (argc-1); ++i){ if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--password")){ char* arg = argv[i+1]; while (*arg){ *arg++ = 'x'; } } } if ( _params.count( "help" ) ){ printHelp(cout); return 0; } if ( _params.count( "verbose" ) ) { logLevel = 1; } for (string s = "vv"; s.length() <= 10; s.append("v")) { if (_params.count(s)) { logLevel = s.length(); } } preSetup(); bool useDirectClient = hasParam( "dbpath" ); if ( ! useDirectClient ) { _host = "127.0.0.1"; if ( _params.count( "host" ) ) _host = _params["host"].as<string>(); if ( _params.count( "port" ) ) _host += ':' + _params["port"].as<string>(); if ( _noconnection ){ // do nothing } else if ( _host.find( "," ) == string::npos ){ DBClientConnection * c = new DBClientConnection( _autoreconnect ); _conn = c; string errmsg; if ( ! c->connect( _host , errmsg ) ){ cerr << "couldn't connect to [" << _host << "] " << errmsg << endl; return -1; } } else { log(1) << "using pairing" << endl; DBClientPaired * c = new DBClientPaired(); _paired = true; _conn = c; if ( ! c->connect( _host ) ){ cerr << "couldn't connect to paired server: " << _host << endl; return -1; } } (_usesstdout ? cout : cerr ) << "connected to: " << _host << endl; } else { if ( _params.count( "directoryperdb" ) ) { directoryperdb = true; } assert( lastError.get( true ) ); Client::initThread("tools"); _conn = new DBDirectClient(); _host = "DIRECT"; static string myDbpath = getParam( "dbpath" ); dbpath = myDbpath.c_str(); try { acquirePathLock(); } catch ( DBException& e ){ cerr << endl << "If you are running a mongod on the same " "path you should connect to that instead of direct data " "file access" << endl << endl; dbexit( EXIT_CLEAN ); return -1; } theFileAllocator().start(); } if ( _params.count( "db" ) ) _db = _params["db"].as<string>(); if ( _params.count( "collection" ) ) _coll = _params["collection"].as<string>(); if ( _params.count( "username" ) ) _username = _params["username"].as<string>(); if ( _params.count( "password" ) && ( _password.empty() ) ) { _password = askPassword(); } if (_params.count("ipv6")) enableIPv6(); int ret = -1; try { ret = run(); } catch ( DBException& e ){ cerr << "assertion: " << e.toString() << endl; ret = -1; } if ( currentClient.get() ) currentClient->shutdown(); if ( useDirectClient ) dbexit( EXIT_CLEAN ); return ret; }
int Tool::main( int argc , char ** argv, char ** envp ) { static StaticObserver staticObserver; setGlobalAuthorizationManager(new AuthorizationManager(new AuthzManagerExternalStateMock())); mongo::runGlobalInitializersOrDie(argc, argv, envp); // hide password from ps output for (int i=0; i < (argc-1); ++i) { if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--password")) { char* arg = argv[i+1]; while (*arg) { *arg++ = 'x'; } } } preSetup(); if (!toolGlobalParams.useDirectClient) { if (toolGlobalParams.noconnection) { // do nothing } else { string errmsg; ConnectionString cs = ConnectionString::parse(toolGlobalParams.connectionString, errmsg); if ( ! cs.isValid() ) { cerr << "invalid hostname [" << toolGlobalParams.connectionString << "] " << errmsg << endl; ::_exit(-1); } _conn = cs.connect( errmsg ); if ( ! _conn ) { std::cerr << "couldn't connect to [" << toolGlobalParams.connectionString << "] " << errmsg << std::endl; ::_exit(-1); } if (!toolGlobalParams.quiet) { (_usesstdout ? std::cout : std::cerr ) << "connected to: " << toolGlobalParams.connectionString << std::endl; } } } else { verify( lastError.get( true ) ); Client::initThread("tools"); _conn = new DBDirectClient(); storageGlobalParams.dbpath = toolGlobalParams.dbpath; try { acquirePathLock(); } catch ( DBException& ) { cerr << endl << "If you are running a mongod on the same " "path you should connect to that instead of direct data " "file access" << endl << endl; dbexit( EXIT_FS ); ::_exit(EXIT_FAILURE); } FileAllocator::get()->start(); dur::startup(); } int ret = -1; try { if (!toolGlobalParams.useDirectClient && !toolGlobalParams.noconnection) auth(); ret = run(); } catch ( DBException& e ) { cerr << "assertion: " << e.toString() << endl; ret = -1; } catch(const boost::filesystem::filesystem_error &fse) { /* https://jira.mongodb.org/browse/SERVER-2904 Simple tools that don't access the database, such as bsondump, aren't throwing DBExceptions, but are throwing boost exceptions. The currently available set of error codes don't seem to match boost documentation. boost::filesystem::not_found_error (from http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/exception.htm) doesn't seem to exist in our headers. Also, fse.code() isn't boost::system::errc::no_such_file_or_directory when this happens, as you would expect. And, determined from experimentation that the command-line argument gets turned into "\\?" instead of "/?" !!! */ #if defined(_WIN32) if (/*(fse.code() == boost::system::errc::no_such_file_or_directory) &&*/ (fse.path1() == "\\?")) printHelp(cerr); else #endif // _WIN32 cerr << "error: " << fse.what() << endl; ret = -1; } if ( currentClient.get() ) currentClient.get()->shutdown(); if (toolGlobalParams.useDirectClient) dbexit( EXIT_CLEAN ); fflush(stdout); fflush(stderr); ::_exit(ret); }