1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// restore.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/>.
*/
#include "../pch.h"
#include "../client/dbclient.h"
#include "../util/mmap.h"
#include "../util/text.h"
#include "tool.h"
#include <boost/program_options.hpp>
#include <fcntl.h>
using namespace mongo;
namespace po = boost::program_options;
class BSONDump : public BSONTool {
enum OutputType { JSON , DEBUG } _type;
public:
BSONDump() : BSONTool( "bsondump", NONE ) {
add_options()
("type" , po::value<string>()->default_value("json") , "type of output: json,debug" )
;
add_hidden_options()
("file" , po::value<string>() , ".bson file" )
;
addPositionArg( "file" , 1 );
_noconnection = true;
}
virtual void printExtraHelp(ostream& out) {
out << "Display BSON objects in a data file.\n" << endl;
out << "usage: " << _name << " [options] <bson filename>" << endl;
}
virtual int doRun() {
{
string t = getParam( "type" );
if ( t == "json" )
_type = JSON;
else if ( t == "debug" )
_type = DEBUG;
else {
cerr << "bad type: " << t << endl;
return 1;
}
}
path root = getParam( "file" );
if ( root == "" ) {
printExtraHelp(cout);
return 1;
}
processFile( root );
return 0;
}
bool debug( const BSONObj& o , int depth=0) {
string prefix = "";
for ( int i=0; i<depth; i++ ) {
prefix += "\t\t\t";
}
int read = 4;
try {
cout << prefix << "--- new object ---\n";
cout << prefix << "\t size : " << o.objsize() << "\n";
BSONObjIterator i(o);
while ( i.more() ) {
BSONElement e = i.next();
cout << prefix << "\t\t " << e.fieldName() << "\n" << prefix << "\t\t\t type:" << setw(3) << e.type() << " size: " << e.size() << endl;
if ( ( read + e.size() ) > o.objsize() ) {
cout << prefix << " SIZE DOES NOT WORK" << endl;
return false;
}
read += e.size();
try {
e.validate();
if ( e.isABSONObj() ) {
if ( ! debug( e.Obj() , depth + 1 ) )
return false;
}
else if ( e.type() == String && ! isValidUTF8( e.valuestr() ) ) {
cout << prefix << "\t\t\t" << "bad utf8 String!" << endl;
}
else if ( logLevel > 0 ) {
cout << prefix << "\t\t\t" << e << endl;
}
}
catch ( std::exception& e ) {
cout << prefix << "\t\t\t bad value: " << e.what() << endl;
}
}
}
catch ( std::exception& e ) {
cout << prefix << "\t" << e.what() << endl;
}
return true;
}
virtual void gotObject( const BSONObj& o ) {
switch ( _type ) {
case JSON:
cout << o.jsonString( TenGen ) << endl;
break;
case DEBUG:
debug(o);
break;
default:
cerr << "bad type? : " << _type << endl;
}
}
};
int main( int argc , char ** argv ) {
BSONDump dump;
return dump.main( argc , argv );
}
|