Files
mongo/tools/files.cpp

164 lines
5.2 KiB
C++
Raw Normal View History

2009-02-02 17:19:55 -05:00
// files.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2009-02-02 17:19:55 -05:00
#include "client/gridfs.h"
#include "client/dbclient.h"
2009-09-24 11:02:00 -04:00
#include "tool.h"
2009-02-02 17:19:55 -05:00
#include <fstream>
#include <iostream>
#include <boost/program_options.hpp>
using namespace mongo;
namespace po = boost::program_options;
class Files : public Tool {
public:
2011-01-04 00:40:41 -05:00
Files() : Tool( "files" ) {
add_options()
2011-01-04 00:40:41 -05:00
( "local,l", po::value<string>(), "local filename for put|get (default is to use the same name as 'gridfs filename')")
( "type,t", po::value<string>(), "MIME type for put (default is to omit)")
( "replace,r", "Remove other files with same name after PUT")
;
add_hidden_options()
2011-01-04 00:40:41 -05:00
( "command" , po::value<string>() , "command (list|search|put|get)" )
( "file" , po::value<string>() , "filename for get|put" )
;
2009-02-02 17:19:55 -05:00
addPositionArg( "command" , 1 );
addPositionArg( "file" , 2 );
}
2011-01-04 00:40:41 -05:00
virtual void printExtraHelp( ostream & out ) {
out << "usage: " << _name << " [options] command [gridfs filename]" << endl;
out << "command:" << endl;
out << " one of (list|search|put|get)" << endl;
out << " list - list all files. 'gridfs filename' is an optional prefix " << endl;
2009-09-11 14:14:31 -04:00
out << " which listed filenames must begin with." << endl;
out << " search - search all files. 'gridfs filename' is a substring " << endl;
2009-09-11 14:14:31 -04:00
out << " which listed filenames must contain." << endl;
out << " put - add a file with filename 'gridfs filename'" << endl;
out << " get - get a file with filename 'gridfs filename'" << endl;
2009-10-14 18:19:14 -04:00
out << " delete - delete all files with filename 'gridfs filename'" << endl;
}
2009-09-01 15:53:31 -04:00
2011-01-04 00:40:41 -05:00
void display( GridFS * grid , BSONObj obj ) {
auto_ptr<DBClientCursor> c = grid->list( obj );
2011-01-04 00:40:41 -05:00
while ( c->more() ) {
BSONObj obj = c->next();
2009-09-01 15:53:31 -04:00
cout
2011-01-04 00:40:41 -05:00
<< obj["filename"].str() << "\t"
<< (long)obj["length"].number()
<< endl;
}
}
2009-09-01 15:53:31 -04:00
2011-01-04 00:40:41 -05:00
int run() {
2009-02-02 17:19:55 -05:00
string cmd = getParam( "command" );
2011-01-04 00:40:41 -05:00
if ( cmd.size() == 0 ) {
cerr << "ERROR: need command" << endl << endl;
printHelp(cout);
2009-02-02 17:19:55 -05:00
return -1;
}
2009-09-01 15:53:31 -04:00
GridFS g( conn() , _db );
2009-08-12 16:31:22 -04:00
auth();
string filename = getParam( "file" );
2009-02-02 17:19:55 -05:00
2011-01-04 00:40:41 -05:00
if ( cmd == "list" ) {
BSONObjBuilder b;
if ( filename.size() )
b.appendRegex( "filename" , ( (string)"^" + filename ) );
2009-02-09 13:04:32 -05:00
display( &g , b.obj() );
2009-02-02 17:19:55 -05:00
return 0;
}
2011-01-04 00:40:41 -05:00
if ( filename.size() == 0 ) {
cerr << "ERROR: need a filename" << endl << endl;
printHelp(cout);
2009-02-02 17:19:55 -05:00
return -1;
}
2011-01-04 00:40:41 -05:00
if ( cmd == "search" ) {
BSONObjBuilder b;
b.appendRegex( "filename" , filename );
2009-02-09 13:04:32 -05:00
display( &g , b.obj() );
return 0;
}
2011-01-04 00:40:41 -05:00
if ( cmd == "get" ) {
2009-02-02 17:19:55 -05:00
GridFile f = g.findFile( filename );
2011-01-04 00:40:41 -05:00
if ( ! f.exists() ) {
cerr << "ERROR: file not found" << endl;
2009-02-02 17:19:55 -05:00
return -2;
}
string out = getParam("local", f.getFilename());
2009-02-02 17:19:55 -05:00
f.write( out );
if (out != "-")
cout << "done write to: " << out << endl;
2009-02-02 17:19:55 -05:00
return 0;
}
2009-09-01 15:53:31 -04:00
2011-01-04 00:40:41 -05:00
if ( cmd == "put" ) {
const string& infile = getParam("local", filename);
const string& type = getParam("type", "");
2009-11-20 01:57:19 -05:00
BSONObj file = g.storeFile(infile, filename, type);
cout << "added file: " << file << endl;
2011-01-04 00:40:41 -05:00
if (hasParam("replace")) {
2009-11-20 01:57:19 -05:00
auto_ptr<DBClientCursor> cursor = conn().query(_db+".fs.files", BSON("filename" << filename << "_id" << NE << file["_id"] ));
2011-01-04 00:40:41 -05:00
while (cursor->more()) {
2009-11-20 01:57:19 -05:00
BSONObj o = cursor->nextSafe();
conn().remove(_db+".fs.files", BSON("_id" << o["_id"]));
conn().remove(_db+".fs.chunks", BSON("_id" << o["_id"]));
cout << "removed file: " << o << endl;
}
}
conn().getLastError();
2010-04-29 14:49:01 -04:00
cout << "done!" << endl;
2009-02-02 17:19:55 -05:00
return 0;
}
2009-09-01 15:53:31 -04:00
2011-01-04 00:40:41 -05:00
if ( cmd == "delete" ) {
2009-10-14 18:19:14 -04:00
g.removeFile(filename);
conn().getLastError();
2010-04-29 14:49:01 -04:00
cout << "done!" << endl;
2009-10-14 18:19:14 -04:00
return 0;
}
cerr << "ERROR: unknown command '" << cmd << "'" << endl << endl;
printHelp(cout);
2009-02-02 17:19:55 -05:00
return -1;
}
};
int main( int argc , char ** argv ) {
Files f;
return f.main( argc , argv );
}