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>
41 #include "barrygetopt.h"
44 using namespace std::tr1
;
45 using namespace Barry
;
47 // keeping a record of all the -i device / -o device pin numbers, so
48 // we can warn the poor user appropriately
49 std::vector
<Barry::Pin
> m_device_pins
;
51 bool IsPinUsed(const Barry::Pin
&pin
)
53 for( std::vector
<Barry::Pin
>::const_iterator b
= m_device_pins
.begin();
54 b
!= m_device_pins
.end(); ++b
)
64 int logical
, major
, minor
;
65 const char *Version
= Barry::Version(logical
, major
, minor
);
68 << "bio - Barry Input / Output\n"
69 << " Copyright 2010-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
70 << " Using: " << Version
<< "\n"
72 #ifdef __BARRY_BOOST_MODE__
79 << " Usage: bio -i <type> [options...] -o <type> [options...]\n"
81 << " -i type The input type (Builder) to use for producing records\n"
82 << " Can be one of: device, tar"
83 #ifdef __BARRY_BOOST_MODE__
87 << " -o type The output type (Parser) to use for processing records.\n"
88 << " Multiple outputs are allowed, as long as they don't\n"
89 << " conflict (such as two outputs writing to the same file\n"
91 << " Can be one of: device, tar"
92 #ifdef __BARRY_BOOST_MODE__
95 << ", ldif, mime, dump, sha1, cstore\n"
97 << " Options to use for 'device' type:\n"
98 << " -d db Name of input database. Can be used multiple times.\n"
99 << " -A Add all available device databases, instead of specifying\n"
100 << " them manually via -d\n"
101 << " -p pin PIN of device to talk to\n"
102 << " If only one device is plugged in, this flag is optional\n"
103 << " -P pass Simplistic method to specify device password\n"
104 << " -w mode Set write mode when using 'device' for output. Must be\n"
105 << " specified, or will not write anything.\n"
106 << " Can be one of: erase, overwrite, addonly, addnew\n"
108 // FIXME - modifiers not yet implemented
110 << " Input database modifiers: (can be used multiple times for more than 1 record)\n"
112 << " -r # Record index number as seen in the -T state table.\n"
113 << " This overrides the default -d behaviour, and only\n"
114 << " downloads the one specified record, sending to stdout.\n"
115 << " -R # Same as -r, but also clears the record's dirty flags.\n"
116 << " -D # Record index number as seen in the -T state table,\n"
117 << " which indicates the record to delete. Used with the -d\n"
118 << " command to specify the database.\n"
121 << " Options to use for 'tar' backup type:\n"
122 << " -d db Name of input database. Can be used multiple times.\n"
123 << " Not available in output mode. Note that by default,\n"
124 << " all databases in the backup are selected, when reading,\n"
125 << " unless at least one -d is specified.\n"
126 << " -f file Tar backup file to read from or write to\n"
127 #ifdef __BARRY_BOOST_MODE__
129 << " Options to use for 'boost' type:\n"
130 << " -f file Boost serialization filename to read from or write to\n"
131 << " Can use - to specify stdin/stdout\n"
134 << " Options to use for 'ldif' type:\n"
135 << " -c dn Convert address book database to LDIF format, using the\n"
136 << " specified baseDN\n"
137 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
138 << " Defaults to 'cn'\n"
142 << " -L List Contact field names\n"
143 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
144 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
145 << " Unmap: ldif name alone\n"
146 << " -M List current LDIF mapping\n"
149 << " Options to use for 'mime' type:\n"
150 << " -f file Filename to read from or write to. Use - to explicitly\n"
151 << " specify stdin/stdout, which is default.\n"
153 << " Options to use for 'dump' to stdout output type:\n"
154 << " -n Use hex dump parser on all databases.\n"
156 << " Options to use for 'sha1' sum stdout output type:\n"
157 << " -t Include DB Name, Type, and Unique record IDs in the checksums\n"
159 << " Options to use for 'cstore' output type:\n"
160 << " -l List filenames only\n"
161 << " -f file Filename from the above list, including path.\n"
162 << " If found, the file will be written to the current\n"
163 << " directory, using the base filename from the device.\n"
165 << " Standalone options:\n"
167 << " -I cs International charset for string conversions\n"
168 << " Valid values here are available with 'iconv --list'\n"
169 << " -S Show list of supported database parsers and builders.\n"
170 << " Use twice to show field names as well.\n"
171 << " -v Dump protocol data during operation\n"
179 virtual ~ModeBase() {}
181 virtual bool ProbeNeeded() const { return false; }
183 virtual void SetFilename(const std::string
&name
)
185 throw runtime_error("Filename not applicable for this mode");
188 virtual void AddDB(const std::string
&dbname
)
190 throw runtime_error("DB not applicable for this mode");
193 virtual void AddAllDBs()
195 throw runtime_error("DBs not applicable for this mode");
198 virtual void SetPIN(const std::string
&pin
)
200 throw runtime_error("PIN not applicable for this mode");
203 virtual void SetPassword(const std::string
&password
)
205 throw runtime_error("Password not applicable for this mode");
208 virtual void SetWriteMode(DeviceParser::WriteMode mode
)
210 throw runtime_error("Device write behaviour not applicable for this mode");
213 virtual void SetDN(const std::string
&dn
)
215 throw runtime_error("DN not applicable for this mode");
218 virtual void SetAttribute(const std::string
&attr
)
220 throw runtime_error("Attribute not applicable for this mode");
223 virtual void SetHexDump()
225 throw runtime_error("No hex dump option in this mode");
228 virtual void IncludeIDs()
230 throw runtime_error("Including record IDs in the SHA1 sum is not applicable in this mode");
233 virtual void SetList()
235 throw runtime_error("List option not applicable for this mode");
239 class DeviceBase
: public virtual ModeBase
243 std::string m_password
;
246 bool ProbeNeeded() const { return true; }
248 void SetPIN(const std::string
&pin
)
250 istringstream
iss(pin
);
253 throw runtime_error("Invalid PIN: " + pin
);
256 void SetPassword(const std::string
&password
)
258 m_password
= password
;
262 //////////////////////////////////////////////////////////////////////////////
263 // Base class for Input Mode
265 class InputBase
: public virtual ModeBase
268 virtual Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
) = 0;
271 class DeviceInputBase
: public DeviceBase
, public InputBase
275 //////////////////////////////////////////////////////////////////////////////
276 // Mode: Input, Type: device
278 class DeviceInput
: public DeviceInputBase
280 auto_ptr
<Controller
> m_con
;
281 auto_ptr
<Mode::Desktop
> m_desktop
;
282 auto_ptr
<DeviceBuilder
> m_builder
;
283 vector
<string
> m_dbnames
;
292 void AddDB(const std::string
&dbname
)
294 m_dbnames
.push_back(dbname
);
302 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
304 int i
= probe
->FindActive(m_pin
);
307 throw runtime_error("PIN not found: " + m_pin
.Str());
309 throw runtime_error("PIN not specified, and more than one device exists.");
312 if( IsPinUsed(probe
->Get(i
).m_pin
) ) {
313 throw runtime_error("It seems you are trying to use the same device for multiple input or outputs.");
315 m_device_pins
.push_back(probe
->Get(i
).m_pin
);
317 m_con
.reset( new Controller(probe
->Get(i
)) );
318 m_desktop
.reset( new Mode::Desktop(*m_con
, ic
) );
319 m_desktop
->Open(m_password
.c_str());
320 m_builder
.reset( new DeviceBuilder(*m_desktop
) );
323 m_builder
->Add(m_desktop
->GetDBDB());
326 for( size_t i
= 0; i
< m_dbnames
.size(); i
++ ) {
327 if( !m_builder
->Add(m_dbnames
[i
]) )
328 throw runtime_error("Database not found: " + m_dbnames
[i
]);
336 //////////////////////////////////////////////////////////////////////////////
337 // Mode: Input, Type: tar
339 class TarInput
: public InputBase
341 auto_ptr
<Restore
> m_restore
;
343 vector
<string
> m_dbnames
;
346 void SetFilename(const std::string
&name
)
350 throw runtime_error("Cannot use stdin as tar source file, sorry.");
353 void AddDB(const std::string
&dbname
)
355 m_dbnames
.push_back(dbname
);
358 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
360 m_restore
.reset( new Restore(m_tarpath
, true) );
361 for( size_t i
= 0; i
< m_dbnames
.size(); i
++ ) {
362 m_restore
->AddDB(m_dbnames
[i
]);
369 //////////////////////////////////////////////////////////////////////////////
370 // Mode: Input, Type: boost
372 #ifdef __BARRY_BOOST_MODE__
373 class BoostInput
: public InputBase
375 auto_ptr
<BoostBuilder
> m_builder
;
380 : m_filename("-") // default to stdin/stdout
384 void SetFilename(const std::string
&name
)
389 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
391 if( m_filename
== "-" ) {
393 m_builder
.reset( new BoostBuilder(cin
) );
396 m_builder
.reset( new BoostBuilder(m_filename
) );
404 //////////////////////////////////////////////////////////////////////////////
405 // Mode: Input, Type: ldif
407 class LdifInput
: public InputBase
409 auto_ptr
<Builder
> m_builder
;
418 void SetFilename(const std::string
&name
)
423 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
425 if( m_filename
== "-" ) {
428 new RecordBuilder
<Contact
, LdifStore
>(
429 new LdifStore(cin
)) );
433 new RecordBuilder
<Contact
, LdifStore
>(
434 new LdifStore(m_filename
)) );
442 //////////////////////////////////////////////////////////////////////////////
443 // Mode: Input, Type: mime
445 class MimeInput
: public InputBase
447 auto_ptr
<MimeBuilder
> m_builder
;
456 void SetFilename(const std::string
&name
)
461 Builder
& GetBuilder(Barry::Probe
*probe
, IConverter
&ic
)
463 if( m_filename
== "-" ) {
465 m_builder
.reset( new MimeBuilder(cin
) );
468 m_builder
.reset( new MimeBuilder(m_filename
) );
475 //////////////////////////////////////////////////////////////////////////////
476 // Base class for Output Mode
478 class OutputBase
: public virtual ModeBase
481 virtual Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
) = 0;
484 class DeviceOutputBase
: public DeviceBase
, public OutputBase
488 //////////////////////////////////////////////////////////////////////////////
489 // Mode: Output, Type: device
491 class DeviceOutput
: public DeviceOutputBase
493 auto_ptr
<Controller
> m_con
;
494 auto_ptr
<Mode::Desktop
> m_desktop
;
495 auto_ptr
<DeviceParser
> m_parser
;
496 DeviceParser::WriteMode m_mode
;
500 : m_mode(DeviceParser::DROP_RECORD
)
504 void SetWriteMode(DeviceParser::WriteMode mode
)
509 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
511 if( m_mode
== DeviceParser::DROP_RECORD
) {
512 cerr
<< "Warning: the -w switch was not specified: no data will be written to the device." << endl
;
515 int i
= probe
->FindActive(m_pin
);
518 throw runtime_error("PIN not found: " + m_pin
.Str());
520 throw runtime_error("PIN not specified, and more than one device exists.");
523 if( IsPinUsed(probe
->Get(i
).m_pin
) ) {
524 throw runtime_error("It seems you are trying to use the same device for multiple input or outputs.");
526 m_device_pins
.push_back(probe
->Get(i
).m_pin
);
528 m_con
.reset( new Controller(probe
->Get(i
)) );
529 m_desktop
.reset( new Mode::Desktop(*m_con
, ic
) );
530 m_desktop
->Open(m_password
.c_str());
531 m_parser
.reset( new DeviceParser(*m_desktop
, m_mode
) );
537 //////////////////////////////////////////////////////////////////////////////
538 // Mode: Output, Type: tar
540 class TarOutput
: public OutputBase
542 auto_ptr
<Backup
> m_backup
;
546 void SetFilename(const std::string
&name
)
550 throw runtime_error("Cannot use stdout as tar backup file, sorry.");
553 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
555 m_backup
.reset( new Backup(m_tarpath
) );
560 //////////////////////////////////////////////////////////////////////////////
561 // Mode: Output, Type: boost
563 #ifdef __BARRY_BOOST_MODE__
564 class BoostOutput
: public OutputBase
566 auto_ptr
<BoostParser
> m_parser
;
570 void SetFilename(const std::string
&name
)
575 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
577 if( !m_filename
.size() )
578 throw runtime_error("Boost output requires a specific output file (-f switch)");
580 if( m_filename
== "-" ) {
582 m_parser
.reset( new BoostParser(cout
) );
585 m_parser
.reset( new BoostParser(m_filename
) );
593 //////////////////////////////////////////////////////////////////////////////
594 // Mode: Output, Type: ldif
596 class LdifOutput
: public OutputBase
598 auto_ptr
<Parser
> m_parser
;
609 void SetFilename(const std::string
&name
)
614 void SetDN(const std::string
&dn
)
619 void SetAttribute(const std::string
&attr
)
624 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
626 if( m_filename
== "-" ) {
629 new RecordParser
<Contact
, LdifStore
>(
630 new LdifStore(cout
, m_baseDN
,
635 new RecordParser
<Contact
, LdifStore
>(
636 new LdifStore(m_filename
, m_baseDN
,
643 //////////////////////////////////////////////////////////////////////////////
644 // Mode: Output, Type: mime
646 class MimeStore
: public AllRecordStore
651 MimeStore(std::ostream
&os
)
657 #define HANDLE_PARSER(tname) \
658 void operator() (const Barry::tname &r) \
660 MimeDump<tname>::Dump(m_os, r); \
663 ALL_KNOWN_PARSER_TYPES
666 class MimeOutput
: public OutputBase
668 auto_ptr
<std::ofstream
> m_file
;
669 auto_ptr
<Parser
> m_parser
;
670 std::string m_filename
;
674 : m_filename("-") // default to stdout
678 void SetFilename(const std::string
&name
)
683 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
685 if( m_filename
== "-" ) {
686 m_parser
.reset( new AllRecordParser(cout
,
687 new HexDumpParser(cout
),
688 new MimeStore(cout
)) );
691 m_file
.reset( new std::ofstream(m_filename
.c_str()) );
692 m_parser
.reset( new AllRecordParser(*m_file
,
693 new HexDumpParser(*m_file
),
694 new MimeStore(*m_file
)) );
700 //////////////////////////////////////////////////////////////////////////////
701 // Mode: Output, Type: dump
703 class DumpOutput
: public OutputBase
705 auto_ptr
<Parser
> m_parser
;
719 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
722 m_parser
.reset( new HexDumpParser(cout
) );
725 m_parser
.reset( new AllRecordParser(cout
,
726 new HexDumpParser(cout
),
727 new AllRecordDumpStore(cout
)) );
733 //////////////////////////////////////////////////////////////////////////////
734 // Mode: Output, Type: sha1
736 class Sha1Output
: public OutputBase
738 auto_ptr
<Parser
> m_parser
;
743 : m_include_ids(false)
749 m_include_ids
= true;
752 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
754 m_parser
.reset( new ChecksumParser(m_include_ids
) );
759 //////////////////////////////////////////////////////////////////////////////
760 // Mode: Output, Type: cstore
762 class ContentStoreOutput
: public OutputBase
764 auto_ptr
<Parser
> m_parser
;
766 vector
<string
> m_filenames
;
774 void SetFilename(const std::string
&name
)
776 m_filenames
.push_back(name
);
784 Parser
& GetParser(Barry::Probe
*probe
, IConverter
&ic
)
786 m_parser
.reset( new RecordParser
<ContentStore
, ContentStoreOutput
>(*this) );
791 void operator() (const ContentStore
&rec
)
794 cout
<< rec
.Filename
;
795 if( rec
.FolderFlag
) {
801 // check if this record matches one of the filenames
803 vector
<string
>::iterator i
= find(m_filenames
.begin(),
804 m_filenames
.end(), rec
.Filename
);
805 if( i
!= m_filenames
.end() ) {
811 void SaveFile(const ContentStore
&rec
)
813 size_t slash
= rec
.Filename
.rfind('/');
815 if( slash
== string::npos
)
816 filename
= rec
.Filename
;
818 filename
= rec
.Filename
.substr(slash
+ 1);
820 // modify filename until we find one that doesn't
822 string freshname
= filename
;
824 while( access(freshname
.c_str(), F_OK
) == 0 ) {
826 oss
<< filename
<< count
++;
827 freshname
= oss
.str();
831 cout
<< "Saving: " << rec
.Filename
832 << " as " << freshname
<< endl
;
833 ofstream
ofs(freshname
.c_str());
834 ofs
<< rec
.FileContent
;
837 cout
<< "Error during write!" << endl
;
844 //////////////////////////////////////////////////////////////////////////////
845 // Main application class
850 typedef shared_ptr
<OutputBase
> OutputPtr
;
851 typedef vector
<OutputPtr
> OutputsType
;
854 auto_ptr
<InputBase
> Input
;
859 bool ParseInMode(const string
&mode
);
860 bool ParseOutMode(const string
&mode
);
861 DeviceParser::WriteMode
ParseWriteMode(const std::string
&mode
);
862 // returns true if any of the items in Outputs needs a probe
863 bool OutputsProbeNeeded();
864 int main(int argc
, char *argv
[]);
867 bool App::ParseInMode(const string
&mode
)
869 if( mode
== "device" ) {
870 Input
.reset( new DeviceInput
);
873 else if( mode
== "tar" ) {
874 Input
.reset( new TarInput
);
877 #ifdef __BARRY_BOOST_MODE__
878 else if( mode
== "boost" ) {
879 Input
.reset( new BoostInput
);
883 else if( mode
== "ldif" ) {
884 Input
.reset( new LdifInput
);
887 else if( mode
== "mime" ) {
888 Input
.reset( new MimeInput
);
895 bool App::ParseOutMode(const string
&mode
)
897 if( mode
== "device" ) {
898 Outputs
.push_back( OutputPtr(new DeviceOutput
) );
901 else if( mode
== "tar" ) {
902 Outputs
.push_back( OutputPtr(new TarOutput
) );
905 #ifdef __BARRY_BOOST_MODE__
906 else if( mode
== "boost" ) {
907 Outputs
.push_back( OutputPtr(new BoostOutput
) );
911 else if( mode
== "ldif" ) {
912 Outputs
.push_back( OutputPtr(new LdifOutput
) );
915 else if( mode
== "mime" ) {
916 Outputs
.push_back( OutputPtr(new MimeOutput
) );
919 else if( mode
== "dump" ) {
920 Outputs
.push_back( OutputPtr(new DumpOutput
) );
923 else if( mode
== "sha1" ) {
924 Outputs
.push_back( OutputPtr(new Sha1Output
) );
927 else if( mode
== "cstore" ) {
928 Outputs
.push_back( OutputPtr(new ContentStoreOutput
) );
935 DeviceParser::WriteMode
App::ParseWriteMode(const std::string
&mode
)
937 if( mode
== "erase" )
938 return DeviceParser::ERASE_ALL_WRITE_ALL
;
939 else if( mode
== "overwrite" )
940 return DeviceParser::INDIVIDUAL_OVERWRITE
;
941 else if( mode
== "addonly" )
942 return DeviceParser::ADD_BUT_NO_OVERWRITE
;
943 else if( mode
== "addnew" )
944 return DeviceParser::ADD_WITH_NEW_ID
;
946 throw runtime_error("Unknown device output mode. Must be one of: erase, overwrite, addonly, addnew");
949 bool App::OutputsProbeNeeded()
951 for( OutputsType::iterator i
= Outputs
.begin();
955 if( (*i
)->ProbeNeeded() )
961 int App::main(int argc
, char *argv
[])
963 bool verbose
= false;
964 bool show_parsers
= false, show_fields
= false;
967 // process command line options
968 ModeBase
*current
= 0;
970 int cmd
= getopt(argc
, argv
, "hi:o:nvI:f:p:P:d:c:C:ASw:tl");
974 // first option must be in or out, or a global option
989 case 'i': // set input mode
990 // must be first time used
991 if( Input
.get() || !ParseInMode(optarg
) ) {
995 current
= Input
.get();
998 case 'o': // set output mode
999 // can be used multiple times
1000 if( !ParseOutMode(optarg
) ) {
1004 current
= Outputs
[Outputs
.size() - 1].get();
1008 case 'c': // set ldif dn
1009 current
->SetDN(optarg
);
1012 case 'C': // set ldif attr
1013 current
->SetAttribute(optarg
);
1016 case 'd': // database name
1017 current
->AddDB(optarg
);
1020 case 'f': // filename
1021 current
->SetFilename(optarg
);
1024 case 'p': // device PIN
1025 current
->SetPIN(optarg
);
1028 case 'P': // password
1029 current
->SetPassword(optarg
);
1032 case 'w': // device write mode
1033 current
->SetWriteMode(ParseWriteMode(optarg
));
1036 case 'A': // add all DB names to the device builder
1037 current
->AddAllDBs();
1040 case 't': // include type and IDs in sha1 mode
1041 current
->IncludeIDs();
1044 case 'l': // list only
1048 case 'S': // show parsers and builders
1052 show_parsers
= true;
1055 case 'I': // international charset (iconv)
1056 iconvCharset
= optarg
;
1059 case 'n': // use null hex dump parser only
1060 current
->SetHexDump();
1063 case 'v': // verbose
1074 if( show_parsers
) {
1075 ShowParsers(show_fields
, true);
1080 if( !Input
.get() || !Outputs
.size() ) {
1085 // Initialize the Barry library
1086 Barry::Init(verbose
);
1088 // Create an IConverter object if needed
1089 auto_ptr
<IConverter
> ic
;
1090 if( iconvCharset
.size() ) {
1091 ic
.reset( new IConverter(iconvCharset
.c_str(), true) );
1094 // Probe for devices only if needed
1095 auto_ptr
<Probe
> probe
;
1096 if( Input
->ProbeNeeded() || OutputsProbeNeeded() ) {
1097 // Probe for available devices
1098 probe
.reset( new Probe
);
1101 // Setup the input first (builder)
1102 Builder
&builder
= Input
->GetBuilder(probe
.get(), *ic
);
1104 // Setup a TeeParser with all Outputs
1106 for( OutputsType::iterator i
= Outputs
.begin(); i
!= Outputs
.end(); ++i
) {
1107 Parser
&parser
= (*i
)->GetParser(probe
.get(), *ic
);
1113 pipe
.PumpFile(tee
, ic
.get());
1118 int main(int argc
, char *argv
[])
1122 return app
.main(argc
, argv
);
1124 catch( std::exception
&e
) {
1125 cerr
<< "Exception: " << e
.what() << endl
;