3 /// Barry Input / Output
7 Copyright (C) 2010-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include <barry/barry.h>
23 #include <barry/barrysync.h>
24 #include <barry/barrybackup.h>
42 #include "barrygetopt.h"
45 using namespace std::tr1
;
46 using namespace Barry
;
48 // keeping a record of all the -i device / -o device pin numbers, so
49 // we can warn the poor user appropriately
50 std::vector
<Barry::Pin
> m_device_pins
;
52 bool IsPinUsed(const Barry::Pin
&pin
)
54 for( std::vector
<Barry::Pin
>::const_iterator b
= m_device_pins
.begin();
55 b
!= m_device_pins
.end(); ++b
)
65 int logical
, major
, minor
;
66 const char *Version
= Barry::Version(logical
, major
, minor
);
69 << "bio - Barry Input / Output\n"
70 << " Copyright 2010-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
71 << " Using: " << Version
<< "\n"
73 #ifdef __BARRY_BOOST_MODE__
80 << " Usage: bio -i <type> [options...] -o <type> [options...]\n"
82 << " -i type The input type (Builder) to use for producing records\n"
83 << " Can be one of: device, tar"
84 #ifdef __BARRY_BOOST_MODE__
88 << " -o type The output type (Parser) to use for processing records.\n"
89 << " Multiple outputs are allowed, as long as they don't\n"
90 << " conflict (such as two outputs writing to the same file\n"
92 << " Can be one of: device, tar"
93 #ifdef __BARRY_BOOST_MODE__
96 << ", ldif, mime, dump, sha1, cstore\n"
98 << " Options to use for 'device' type:\n"
99 << " -d db Name of input database. Can be used multiple times.\n"
100 << " -A Add all available device databases, instead of specifying\n"
101 << " them manually via -d\n"
102 << " -p pin PIN of device to talk to\n"
103 << " If only one device is plugged in, this flag is optional\n"
104 << " -P pass Simplistic method to specify device password\n"
105 << " -w mode Set write mode when using 'device' for output. Must be\n"
106 << " specified, or will not write anything.\n"
107 << " Can be one of: erase, overwrite, addonly, addnew\n"
109 << " Options to use for 'tar' backup type:\n"
110 << " -d db Name of input database. Can be used multiple times.\n"
111 << " Not available in output mode. Note that by default,\n"
112 << " all databases in the backup are selected, when reading,\n"
113 << " unless at least one -d is specified.\n"
114 << " -D db Name of input database to skip. If no -d options are used,\n"
115 << " then all databases are automatically selected. Using -D\n"
116 << " allows a filtering selection. If -d and -D are used for\n"
117 << " the same database, -D takes precedence.\n"
118 << " -f file Tar backup file to read from or write to\n"
119 #ifdef __BARRY_BOOST_MODE__
121 << " Options to use for 'boost' type:\n"
122 << " -f file Boost serialization filename to read from or write to\n"
123 << " Can use - to specify stdin/stdout\n"
126 << " Options to use for 'ldif' type:\n"
127 << " -c dn Convert address book database to LDIF format, using the\n"
128 << " specified baseDN\n"
129 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
130 << " Defaults to 'cn'\n"
134 << " -L List Contact field names\n"
135 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
136 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
137 << " Unmap: ldif name alone\n"
138 << " -M List current LDIF mapping\n"
141 << " Options to use for 'mime' type:\n"
142 << " -f file Filename to read from or write to. Use - to explicitly\n"
143 << " specify stdin/stdout, which is default.\n"
145 << " Options to use for 'dump' to stdout output type:\n"
146 << " -n Use hex dump parser on all databases.\n"
148 << " Options to use for 'sha1' sum stdout output type:\n"
149 << " -t Include DB Name, Type, and Unique record IDs in the checksums\n"
151 << " Options to use for 'cstore' output type:\n"
152 << " -l List filenames only\n"
153 << " -f file Filename from the above list, including path.\n"
154 << " If found, the file will be written to the current\n"
155 << " directory, using the base filename from the device.\n"
157 << " Standalone options:\n"
159 << " -I cs International charset for string conversions\n"
160 << " Valid values here are available with 'iconv --list'\n"
161 << " -S Show list of supported database parsers and builders.\n"
162 << " Use twice to show field names as well.\n"
163 << " -v Dump protocol data during operation\n"
171 virtual ~ModeBase() {}
173 virtual bool ProbeNeeded() const { return false; }
175 virtual void SetFilename(const std::string
&name
)
177 throw runtime_error("Filename not applicable for this mode");
180 virtual void AddDB(const std::string
&dbname
)
182 throw runtime_error("DB not applicable for this mode");
185 virtual void AddSkipDB(const std::string
&dbname
)
187 throw runtime_error("DB skipping not applicable for this mode");
190 virtual void AddAllDBs()
192 throw runtime_error("DBs not applicable for this mode");
195 virtual void SetPIN(const std::string
&pin
)
197 throw runtime_error("PIN not applicable for this mode");
200 virtual void SetPassword(const std::string
&password
)
202 throw runtime_error("Password not applicable for this mode");
205 virtual void SetWriteMode(DeviceParser::WriteMode mode
)
207 throw runtime_error("Device write behaviour not applicable for this mode");
210 virtual void SetDN(const std::string
&dn
)
212 throw runtime_error("DN not applicable for this mode");
215 virtual void SetAttribute(const std::string
&attr
)
217 throw runtime_error("Attribute not applicable for this mode");
220 virtual void SetHexDump()
222 throw runtime_error("No hex dump option in this mode");
225 virtual void IncludeIDs()
227 throw runtime_error("Including record IDs in the SHA1 sum is not applicable in this mode");
230 virtual void SetList()
232 throw runtime_error("List option not applicable for this mode");
236 class DeviceBase
: public virtual ModeBase
240 std::string m_password
;
243 bool ProbeNeeded() const { return true; }
245 void SetPIN(const std::string
&pin
)
247 istringstream
iss(pin
);
250 throw runtime_error("Invalid PIN: " + pin
);
253 void SetPassword(const std::string
&password
)
255 m_password
= password
;
259 //////////////////////////////////////////////////////////////////////////////
260 // Base class for Input Mode
262 class InputBase
: public virtual ModeBase
265 virtual Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
) = 0;
268 class DeviceInputBase
: public DeviceBase
, public InputBase
272 //////////////////////////////////////////////////////////////////////////////
273 // Mode: Input, Type: device
275 class DeviceInput
: public DeviceInputBase
277 auto_ptr
<Controller
> m_con
;
278 auto_ptr
<Mode::Desktop
> m_desktop
;
279 auto_ptr
<DeviceBuilder
> m_builder
;
280 vector
<string
> m_dbnames
;
289 void AddDB(const std::string
&dbname
)
291 m_dbnames
.push_back(dbname
);
299 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
301 int i
= probe
->FindActive(m_pin
);
304 throw runtime_error("PIN not found: " + m_pin
.Str());
306 throw runtime_error("PIN not specified, and more than one device exists.");
309 if( IsPinUsed(probe
->Get(i
).m_pin
) ) {
310 throw runtime_error("It seems you are trying to use the same device for multiple input or outputs.");
312 m_device_pins
.push_back(probe
->Get(i
).m_pin
);
314 m_con
.reset( new Controller(probe
->Get(i
)) );
315 m_desktop
.reset( new Mode::Desktop(*m_con
, ic
) );
316 m_desktop
->Open(m_password
.c_str());
317 m_builder
.reset( new DeviceBuilder(*m_desktop
) );
320 m_builder
->Add(m_desktop
->GetDBDB());
323 for( size_t i
= 0; i
< m_dbnames
.size(); i
++ ) {
324 if( !m_builder
->Add(m_dbnames
[i
]) )
325 throw runtime_error("Database not found: " + m_dbnames
[i
]);
333 //////////////////////////////////////////////////////////////////////////////
334 // Mode: Input, Type: tar
336 class TarInput
: public InputBase
338 auto_ptr
<Restore
> m_restore
;
340 vector
<string
> m_dbnames
, m_dbskips
;
343 void SetFilename(const std::string
&name
)
347 throw runtime_error("Cannot use stdin as tar source file, sorry.");
350 void AddDB(const std::string
&dbname
)
352 m_dbnames
.push_back(dbname
);
355 void AddSkipDB(const std::string
&dbname
)
357 m_dbskips
.push_back(dbname
);
360 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
362 m_restore
.reset( new Restore(m_tarpath
, true) );
363 for( size_t i
= 0; i
< m_dbnames
.size(); i
++ ) {
364 m_restore
->AddDB(m_dbnames
[i
]);
366 for( size_t i
= 0; i
< m_dbskips
.size(); i
++ ) {
367 m_restore
->AddSkipDB(m_dbskips
[i
]);
374 //////////////////////////////////////////////////////////////////////////////
375 // Mode: Input, Type: boost
377 #ifdef __BARRY_BOOST_MODE__
378 class BoostInput
: public InputBase
380 auto_ptr
<BoostBuilder
> m_builder
;
385 : m_filename("-") // default to stdin/stdout
389 void SetFilename(const std::string
&name
)
394 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
396 if( m_filename
== "-" ) {
398 m_builder
.reset( new BoostBuilder(cin
) );
401 m_builder
.reset( new BoostBuilder(m_filename
) );
409 //////////////////////////////////////////////////////////////////////////////
410 // Mode: Input, Type: ldif
412 class LdifInput
: public InputBase
414 auto_ptr
<Builder
> m_builder
;
423 void SetFilename(const std::string
&name
)
428 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
430 if( m_filename
== "-" ) {
433 new RecordBuilder
<Contact
, LdifStore
>(
434 new LdifStore(cin
)) );
438 new RecordBuilder
<Contact
, LdifStore
>(
439 new LdifStore(m_filename
)) );
447 //////////////////////////////////////////////////////////////////////////////
448 // Mode: Input, Type: mime
450 class MimeInput
: public InputBase
452 auto_ptr
<MimeBuilder
> m_builder
;
461 void SetFilename(const std::string
&name
)
466 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
468 if( m_filename
== "-" ) {
470 m_builder
.reset( new MimeBuilder(cin
) );
473 m_builder
.reset( new MimeBuilder(m_filename
) );
480 //////////////////////////////////////////////////////////////////////////////
481 // Base class for Output Mode
483 class OutputBase
: public virtual ModeBase
486 virtual Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
) = 0;
489 class DeviceOutputBase
: public DeviceBase
, public OutputBase
493 //////////////////////////////////////////////////////////////////////////////
494 // Mode: Output, Type: device
496 class DeviceOutput
: public DeviceOutputBase
498 auto_ptr
<Controller
> m_con
;
499 auto_ptr
<Mode::Desktop
> m_desktop
;
500 auto_ptr
<DeviceParser
> m_parser
;
501 DeviceParser::WriteMode m_mode
;
505 : m_mode(DeviceParser::DROP_RECORD
)
509 void SetWriteMode(DeviceParser::WriteMode mode
)
514 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
516 if( m_mode
== DeviceParser::DROP_RECORD
) {
517 cerr
<< "Warning: the -w switch was not specified: no data will be written to the device." << endl
;
520 int i
= probe
->FindActive(m_pin
);
523 throw runtime_error("PIN not found: " + m_pin
.Str());
525 throw runtime_error("PIN not specified, and more than one device exists.");
528 if( IsPinUsed(probe
->Get(i
).m_pin
) ) {
529 throw runtime_error("It seems you are trying to use the same device for multiple input or outputs.");
531 m_device_pins
.push_back(probe
->Get(i
).m_pin
);
533 m_con
.reset( new Controller(probe
->Get(i
)) );
534 m_desktop
.reset( new Mode::Desktop(*m_con
, ic
) );
535 m_desktop
->Open(m_password
.c_str());
536 m_parser
.reset( new DeviceParser(*m_desktop
, m_mode
) );
542 //////////////////////////////////////////////////////////////////////////////
543 // Mode: Output, Type: tar
545 class TarOutput
: public OutputBase
547 auto_ptr
<Backup
> m_backup
;
551 void SetFilename(const std::string
&name
)
555 throw runtime_error("Cannot use stdout as tar backup file, sorry.");
558 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
560 m_backup
.reset( new Backup(m_tarpath
) );
565 //////////////////////////////////////////////////////////////////////////////
566 // Mode: Output, Type: boost
568 #ifdef __BARRY_BOOST_MODE__
569 class BoostOutput
: public OutputBase
571 auto_ptr
<BoostParser
> m_parser
;
575 void SetFilename(const std::string
&name
)
580 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
582 if( !m_filename
.size() )
583 throw runtime_error("Boost output requires a specific output file (-f switch)");
585 if( m_filename
== "-" ) {
587 m_parser
.reset( new BoostParser(cout
) );
590 m_parser
.reset( new BoostParser(m_filename
) );
598 //////////////////////////////////////////////////////////////////////////////
599 // Mode: Output, Type: ldif
601 class LdifOutput
: public OutputBase
603 auto_ptr
<Parser
> m_parser
;
614 void SetFilename(const std::string
&name
)
619 void SetDN(const std::string
&dn
)
624 void SetAttribute(const std::string
&attr
)
629 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
631 if( m_filename
== "-" ) {
634 new RecordParser
<Contact
, LdifStore
>(
635 new LdifStore(cout
, m_baseDN
,
640 new RecordParser
<Contact
, LdifStore
>(
641 new LdifStore(m_filename
, m_baseDN
,
648 //////////////////////////////////////////////////////////////////////////////
649 // Mode: Output, Type: mime
651 class MimeStore
: public AllRecordStore
656 MimeStore(std::ostream
&os
)
662 #define HANDLE_PARSER(tname) \
663 void operator() (const Barry::tname &r) \
665 MimeDump<tname>::Dump(m_os, r); \
668 ALL_KNOWN_PARSER_TYPES
671 class MimeOutput
: public OutputBase
673 auto_ptr
<std::ofstream
> m_file
;
674 auto_ptr
<Parser
> m_parser
;
675 std::string m_filename
;
679 : m_filename("-") // default to stdout
683 void SetFilename(const std::string
&name
)
688 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
690 if( m_filename
== "-" ) {
691 m_parser
.reset( new AllRecordParser(cout
,
692 new HexDumpParser(cout
),
693 new MimeStore(cout
)) );
696 m_file
.reset( new std::ofstream(m_filename
.c_str()) );
697 m_parser
.reset( new AllRecordParser(*m_file
,
698 new HexDumpParser(*m_file
),
699 new MimeStore(*m_file
)) );
705 //////////////////////////////////////////////////////////////////////////////
706 // Mode: Output, Type: dump
708 class DumpOutput
: public OutputBase
710 auto_ptr
<Parser
> m_parser
;
724 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
727 m_parser
.reset( new HexDumpParser(cout
) );
730 m_parser
.reset( new AllRecordParser(cout
,
731 new HexDumpParser(cout
),
732 new AllRecordDumpStore(cout
)) );
738 //////////////////////////////////////////////////////////////////////////////
739 // Mode: Output, Type: sha1
741 class Sha1Output
: public OutputBase
743 auto_ptr
<Parser
> m_parser
;
748 : m_include_ids(false)
754 m_include_ids
= true;
757 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
759 m_parser
.reset( new ChecksumParser(m_include_ids
) );
764 //////////////////////////////////////////////////////////////////////////////
765 // Mode: Output, Type: cstore
767 class ContentStoreOutput
: public OutputBase
769 auto_ptr
<Parser
> m_parser
;
771 vector
<string
> m_filenames
;
779 void SetFilename(const std::string
&name
)
781 m_filenames
.push_back(name
);
789 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
791 m_parser
.reset( new RecordParser
<ContentStore
, ContentStoreOutput
>(*this) );
796 void operator() (const ContentStore
&rec
)
799 cout
<< rec
.Filename
;
800 if( rec
.FolderFlag
) {
806 // check if this record matches one of the filenames
808 vector
<string
>::iterator i
= find(m_filenames
.begin(),
809 m_filenames
.end(), rec
.Filename
);
810 if( i
!= m_filenames
.end() ) {
816 void SaveFile(const ContentStore
&rec
)
818 size_t slash
= rec
.Filename
.rfind('/');
820 if( slash
== string::npos
)
821 filename
= rec
.Filename
;
823 filename
= rec
.Filename
.substr(slash
+ 1);
825 // modify filename until we find one that doesn't
827 string freshname
= filename
;
829 while( access(freshname
.c_str(), F_OK
) == 0 ) {
831 oss
<< filename
<< count
++;
832 freshname
= oss
.str();
836 cout
<< "Saving: " << rec
.Filename
837 << " as " << freshname
<< endl
;
838 ofstream
ofs(freshname
.c_str());
839 ofs
<< rec
.FileContent
;
842 cout
<< "Error during write!" << endl
;
849 //////////////////////////////////////////////////////////////////////////////
850 // Main application class
855 typedef shared_ptr
<OutputBase
> OutputPtr
;
856 typedef vector
<OutputPtr
> OutputsType
;
859 auto_ptr
<InputBase
> Input
;
864 bool ParseInMode(const string
&mode
);
865 bool ParseOutMode(const string
&mode
);
866 DeviceParser::WriteMode
ParseWriteMode(const std::string
&mode
);
867 // returns true if any of the items in Outputs needs a probe
868 bool OutputsProbeNeeded();
869 int main(int argc
, char *argv
[]);
872 bool App::ParseInMode(const string
&mode
)
874 if( mode
== "device" ) {
875 Input
.reset( new DeviceInput
);
878 else if( mode
== "tar" ) {
879 Input
.reset( new TarInput
);
882 #ifdef __BARRY_BOOST_MODE__
883 else if( mode
== "boost" ) {
884 Input
.reset( new BoostInput
);
888 else if( mode
== "ldif" ) {
889 Input
.reset( new LdifInput
);
892 else if( mode
== "mime" ) {
893 Input
.reset( new MimeInput
);
900 bool App::ParseOutMode(const string
&mode
)
902 if( mode
== "device" ) {
903 Outputs
.push_back( OutputPtr(new DeviceOutput
) );
906 else if( mode
== "tar" ) {
907 Outputs
.push_back( OutputPtr(new TarOutput
) );
910 #ifdef __BARRY_BOOST_MODE__
911 else if( mode
== "boost" ) {
912 Outputs
.push_back( OutputPtr(new BoostOutput
) );
916 else if( mode
== "ldif" ) {
917 Outputs
.push_back( OutputPtr(new LdifOutput
) );
920 else if( mode
== "mime" ) {
921 Outputs
.push_back( OutputPtr(new MimeOutput
) );
924 else if( mode
== "dump" ) {
925 Outputs
.push_back( OutputPtr(new DumpOutput
) );
928 else if( mode
== "sha1" ) {
929 Outputs
.push_back( OutputPtr(new Sha1Output
) );
932 else if( mode
== "cstore" ) {
933 Outputs
.push_back( OutputPtr(new ContentStoreOutput
) );
940 DeviceParser::WriteMode
App::ParseWriteMode(const std::string
&mode
)
942 if( mode
== "erase" )
943 return DeviceParser::ERASE_ALL_WRITE_ALL
;
944 else if( mode
== "overwrite" )
945 return DeviceParser::INDIVIDUAL_OVERWRITE
;
946 else if( mode
== "addonly" )
947 return DeviceParser::ADD_BUT_NO_OVERWRITE
;
948 else if( mode
== "addnew" )
949 return DeviceParser::ADD_WITH_NEW_ID
;
951 throw runtime_error("Unknown device output mode. Must be one of: erase, overwrite, addonly, addnew");
954 bool App::OutputsProbeNeeded()
956 for( OutputsType::iterator i
= Outputs
.begin();
960 if( (*i
)->ProbeNeeded() )
966 int App::main(int argc
, char *argv
[])
968 bool verbose
= false;
969 bool show_parsers
= false, show_fields
= false;
972 // process command line options
973 ModeBase
*current
= 0;
975 int cmd
= getopt(argc
, argv
, "hi:o:nvI:f:p:P:d:D:c:C:ASw:tl");
979 // first option must be in or out, or a global option
994 case 'i': // set input mode
995 // must be first time used
996 if( Input
.get() || !ParseInMode(optarg
) ) {
1000 current
= Input
.get();
1003 case 'o': // set output mode
1004 // can be used multiple times
1005 if( !ParseOutMode(optarg
) ) {
1009 current
= Outputs
[Outputs
.size() - 1].get();
1013 case 'c': // set ldif dn
1014 current
->SetDN(optarg
);
1017 case 'C': // set ldif attr
1018 current
->SetAttribute(optarg
);
1021 case 'd': // database name
1022 current
->AddDB(optarg
);
1025 case 'D': // database name to skip
1026 current
->AddSkipDB(optarg
);
1029 case 'f': // filename
1030 current
->SetFilename(optarg
);
1033 case 'p': // device PIN
1034 current
->SetPIN(optarg
);
1037 case 'P': // password
1038 current
->SetPassword(optarg
);
1041 case 'w': // device write mode
1042 current
->SetWriteMode(ParseWriteMode(optarg
));
1045 case 'A': // add all DB names to the device builder
1046 current
->AddAllDBs();
1049 case 't': // include type and IDs in sha1 mode
1050 current
->IncludeIDs();
1053 case 'l': // list only
1057 case 'S': // show parsers and builders
1061 show_parsers
= true;
1064 case 'I': // international charset (iconv)
1065 iconvCharset
= optarg
;
1068 case 'n': // use null hex dump parser only
1069 current
->SetHexDump();
1072 case 'v': // verbose
1083 if( show_parsers
) {
1084 ShowParsers(show_fields
, true);
1089 if( !Input
.get() || !Outputs
.size() ) {
1094 // Initialize the Barry library
1095 Barry::Init(verbose
);
1097 // Create an IConverter object if needed
1098 auto_ptr
<IConverter
> ic
;
1099 if( iconvCharset
.size() ) {
1100 ic
.reset( new IConverter(iconvCharset
.c_str(), true) );
1103 // Probe for devices only if needed
1104 auto_ptr
<Probe
> probe
;
1105 if( Input
->ProbeNeeded() || OutputsProbeNeeded() ) {
1106 // Probe for available devices
1107 probe
.reset( new Probe
);
1110 // Setup the input first (builder)
1111 Builder
&builder
= Input
->GetBuilder(probe
.get(), *ic
);
1113 // Setup a TeeParser with all Outputs
1115 for( OutputsType::iterator i
= Outputs
.begin(); i
!= Outputs
.end(); ++i
) {
1116 Parser
&parser
= (*i
)->GetParser(probe
.get(), *ic
);
1122 pipe
.PumpFile(tee
, ic
.get());
1127 int main(int argc
, char *argv
[])
1131 return app
.main(argc
, argv
);
1133 catch( std::exception
&e
) {
1134 cerr
<< "Exception: " << e
.what() << endl
;