~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to tools/files.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// files.cpp
 
2
 
 
3
/**
 
4
*    Copyright (C) 2008 10gen Inc.
 
5
*
 
6
*    This program is free software: you can redistribute it and/or  modify
 
7
*    it under the terms of the GNU Affero General Public License, version 3,
 
8
*    as published by the Free Software Foundation.
 
9
*
 
10
*    This program is distributed in the hope that it will be useful,
 
11
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
*    GNU Affero General Public License for more details.
 
14
*
 
15
*    You should have received a copy of the GNU Affero General Public License
 
16
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include "stdafx.h"
 
20
#include "client/gridfs.h"
 
21
#include "client/dbclient.h"
 
22
 
 
23
#include "tool.h"
 
24
 
 
25
#include <fstream>
 
26
#include <iostream>
 
27
 
 
28
#include <boost/program_options.hpp>
 
29
 
 
30
using namespace mongo;
 
31
 
 
32
namespace po = boost::program_options;
 
33
 
 
34
class Files : public Tool {
 
35
public:
 
36
    Files() : Tool( "files" ){
 
37
        add_options()
 
38
            ( "local,l", po::value<string>(), "local filename for put|get (default is to use the same name as 'gridfs filename')")
 
39
            ( "type,t", po::value<string>(), "MIME type for put (default is to omit)")
 
40
            ( "replace,r", "Remove other files with same name after PUT")
 
41
            ;
 
42
        add_hidden_options()
 
43
            ( "command" , po::value<string>() , "command (list|search|put|get)" )
 
44
            ( "file" , po::value<string>() , "filename for get|put" )
 
45
            ;
 
46
        addPositionArg( "command" , 1 );
 
47
        addPositionArg( "file" , 2 );
 
48
    }
 
49
 
 
50
    virtual void printExtraHelp( ostream & out ){
 
51
        out << "usage: " << _name << " [options] command [gridfs filename]" << endl;
 
52
        out << "command:" << endl;
 
53
        out << "  one of (list|search|put|get)" << endl;
 
54
        out << "  list - list all files.  'gridfs filename' is an optional prefix " << endl;
 
55
        out << "         which listed filenames must begin with." << endl;
 
56
        out << "  search - search all files. 'gridfs filename' is a substring " << endl;
 
57
        out << "           which listed filenames must contain." << endl;
 
58
        out << "  put - add a file with filename 'gridfs filename'" << endl;
 
59
        out << "  get - get a file with filename 'gridfs filename'" << endl;
 
60
        out << "  delete - delete all files with filename 'gridfs filename'" << endl;
 
61
    }
 
62
 
 
63
    void display( GridFS * grid , BSONObj obj ){
 
64
        auto_ptr<DBClientCursor> c = grid->list( obj );
 
65
        while ( c->more() ){
 
66
            BSONObj obj = c->next();
 
67
            cout
 
68
                << obj["filename"].str() << "\t"
 
69
                << (long)obj["length"].number()
 
70
                << endl;
 
71
        }
 
72
    }
 
73
 
 
74
    int run(){
 
75
        string cmd = getParam( "command" );
 
76
        if ( cmd.size() == 0 ){
 
77
            cerr << "ERROR: need command" << endl << endl;
 
78
            printHelp(cout);
 
79
            return -1;
 
80
        }
 
81
 
 
82
        GridFS g( conn() , _db );
 
83
        auth();
 
84
 
 
85
        string filename = getParam( "file" );
 
86
 
 
87
        if ( cmd == "list" ){
 
88
            BSONObjBuilder b;
 
89
            if ( filename.size() )
 
90
                b.appendRegex( "filename" , ( (string)"^" + filename ).c_str() );
 
91
            display( &g , b.obj() );
 
92
            return 0;
 
93
        }
 
94
 
 
95
        if ( filename.size() == 0 ){
 
96
            cerr << "ERROR: need a filename" << endl << endl;
 
97
            printHelp(cout);
 
98
            return -1;
 
99
        }
 
100
 
 
101
        if ( cmd == "search" ){
 
102
            BSONObjBuilder b;
 
103
            b.appendRegex( "filename" , filename.c_str() );
 
104
            display( &g , b.obj() );
 
105
            return 0;
 
106
        }
 
107
 
 
108
        if ( cmd == "get" ){
 
109
            GridFile f = g.findFile( filename );
 
110
            if ( ! f.exists() ){
 
111
                cerr << "ERROR: file not found" << endl;
 
112
                return -2;
 
113
            }
 
114
 
 
115
            string out = getParam("local", f.getFilename());
 
116
            f.write( out );
 
117
 
 
118
            if (out != "-")
 
119
                cout << "done write to: " << out << endl;
 
120
 
 
121
            return 0;
 
122
        }
 
123
 
 
124
        if ( cmd == "put" ){
 
125
            const string& infile = getParam("local", filename);
 
126
            const string& type = getParam("type", "");
 
127
 
 
128
            BSONObj file = g.storeFile(infile, filename, type);
 
129
            cout << "added file: " << file << endl;
 
130
 
 
131
            if (hasParam("replace")){
 
132
                auto_ptr<DBClientCursor> cursor = conn().query(_db+".fs.files", BSON("filename" << filename << "_id" << NE << file["_id"] ));
 
133
                while (cursor->more()){
 
134
                    BSONObj o = cursor->nextSafe();
 
135
                    conn().remove(_db+".fs.files", BSON("_id" << o["_id"]));
 
136
                    conn().remove(_db+".fs.chunks", BSON("_id" << o["_id"]));
 
137
                    cout << "removed file: " << o << endl;
 
138
                }
 
139
 
 
140
            }
 
141
 
 
142
            cout << "done!";
 
143
            return 0;
 
144
        }
 
145
 
 
146
        if ( cmd == "delete" ){
 
147
            g.removeFile(filename);
 
148
            cout << "done!";
 
149
            return 0;
 
150
        }
 
151
 
 
152
        cerr << "ERROR: unknown command '" << cmd << "'" << endl << endl;
 
153
        printHelp(cout);
 
154
        return -1;
 
155
    }
 
156
};
 
157
 
 
158
int main( int argc , char ** argv ) {
 
159
    Files f;
 
160
    return f.main( argc , argv );
 
161
}