Bumped copyright dates for 2013
[barry.git] / tools / bfuse.cc
blobb5add3b4f5a653911d2307df78494e31faf8a775
1 ///
2 /// \file bfuse.cc
3 /// FUSE filesystem for Blackberry databases, using Barry.
4 ///
6 /*
7 Copyright (C) 2008-2013, 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 #define FUSE_USE_VERSION 25
23 #include <fuse.h>
24 #include <fuse_opt.h>
26 #include <barry/barry.h>
27 #include <sstream>
28 #include <vector>
29 #include <list>
30 #include <string>
31 #include <stdexcept>
32 #include <memory>
33 #include <tr1/memory>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include "i18n.h"
41 #include "barrygetopt.h"
43 using namespace std;
44 using namespace std::tr1;
45 using namespace Barry;
47 // Global filenames
48 const char *error_log_filename = "error.log";
50 // Global command line args
51 string cmdline_pin;
52 string cmdline_password;
55 // Data from the command line
58 /////////////////////////////////////////////////////////////////////////////
59 // Command line option handling, through fuse
61 //struct opt {
62 // char
63 //};
65 void Blurb()
67 int logical, major, minor;
68 const char *Version = Barry::Version(logical, major, minor);
70 cerr << string_vprintf(
71 _("bfuse - FUSE filesystem for Blackberry databases\n"
72 " Copyright 2008-2013, Net Direct Inc. (http://www.netdirect.ca/)\n"
73 " Using: %s\n"), Version)
74 << endl;
77 void Usage()
79 cerr <<
80 _("\n"
81 "Barry specific options:\n"
82 " -p pin PIN of device to talk with\n"
83 " If only one device is plugged in, this flag is optional\n"
84 " -P pass Simplistic method to specify device password\n")
85 << endl;
87 << " -d db Specify which database to mount. If no -d options exist\n"
88 << " then all databases will be mounted.\n"
89 << " Can be used multiple times to mount more than one DB\n"
90 << " -h This help\n"
91 << " -n Use null parser on all databases.\n"
95 /////////////////////////////////////////////////////////////////////////////
96 // FUSE specific exception
98 class fuse_error : public std::runtime_error
100 int m_errno;
101 public:
102 fuse_error(int errno_, const std::string &msg)
103 : std::runtime_error(msg), m_errno(errno_)
106 int get_errno() const { return m_errno; }
110 /////////////////////////////////////////////////////////////////////////////
111 // Barry record parsers
113 class DataDumpParser : public Barry::Parser
115 uint32_t m_id;
116 std::ostream &m_os;
118 public:
119 explicit DataDumpParser(std::ostream &os)
120 : m_os(os)
124 virtual void ParseRecord(const Barry::DBData &data,
125 const Barry::IConverter *ic)
127 m_id = data.GetUniqueId();
128 m_os << _("Raw record dump for record: ")
129 << std::hex << m_id << std::endl;
130 m_os << data.GetData() << std::endl;
134 template <class Record>
135 struct Store
137 std::ostream &m_os;
139 explicit Store(std::ostream &os)
140 : m_os(os)
144 // storage operator
145 void operator()(const Record &rec)
147 m_os << rec;
151 typedef std::auto_ptr<Barry::Parser> ParserPtr;
153 ParserPtr GetParser(const string &name, std::ostream &os, bool null_parser)
155 if( null_parser ) {
156 // use null parser
157 return ParserPtr( new DataDumpParser(os) );
159 // check for recognized database names
160 else if( name == Contact::GetDBName() ) {
161 return ParserPtr(
162 new RecordParser<Contact, Store<Contact> > (
163 new Store<Contact>(os)));
165 else if( name == Message::GetDBName() ) {
166 return ParserPtr(
167 new RecordParser<Message, Store<Message> > (
168 new Store<Message>(os)));
170 else if( name == Calendar::GetDBName() ) {
171 return ParserPtr(
172 new RecordParser<Calendar, Store<Calendar> > (
173 new Store<Calendar>(os)));
175 else if( name == CalendarAll::GetDBName() ) {
176 return ParserPtr(
177 new RecordParser<CalendarAll, Store<CalendarAll> > (
178 new Store<CalendarAll>(os)));
180 else if( name == ServiceBook::GetDBName() ) {
181 return ParserPtr(
182 new RecordParser<ServiceBook, Store<ServiceBook> > (
183 new Store<ServiceBook>(os)));
186 else if( name == Memo::GetDBName() ) {
187 return ParserPtr(
188 new RecordParser<Memo, Store<Memo> > (
189 new Store<Memo>(os)));
191 else if( name == Task::GetDBName() ) {
192 return ParserPtr(
193 new RecordParser<Task, Store<Task> > (
194 new Store<Task>(os)));
196 else if( name == PINMessage::GetDBName() ) {
197 return ParserPtr(
198 new RecordParser<PINMessage, Store<PINMessage> > (
199 new Store<PINMessage>(os)));
201 else if( name == SavedMessage::GetDBName() ) {
202 return ParserPtr(
203 new RecordParser<SavedMessage, Store<SavedMessage> > (
204 new Store<SavedMessage>(os)));
206 else if( name == Folder::GetDBName() ) {
207 return ParserPtr(
208 new RecordParser<Folder, Store<Folder> > (
209 new Store<Folder>(os)));
211 else if( name == TimeZone::GetDBName() ) {
212 return ParserPtr(
213 new RecordParser<TimeZone, Store<TimeZone> > (
214 new Store<TimeZone>(os)));
216 else {
217 // unknown database, use null parser
218 return ParserPtr( new DataDumpParser(os) );
222 /////////////////////////////////////////////////////////////////////////////
223 // PathSplit class
225 class PathSplit
227 std::string m_pin, m_db, m_record, m_field, m_remainder;
229 int m_level; // the number of slashes, minus the first
230 // i.e. first level is 0
231 bool m_is_root;
233 public:
234 explicit PathSplit(const char *path)
235 : m_level(-1)
236 , m_is_root(false)
238 if( *path != '/' )
239 return; // return in a failed state
241 if( *(path+1) == 0 ) {
242 m_is_root = true;
243 return;
246 const char *s = path, *e = path;
247 while( *e ) {
248 while( *e && *e != '/' )
249 e++;
251 m_level++;
253 if( s != e && (s+1) != e ) {
254 string token(s+1, e);
256 switch( m_level )
258 case 0: // root level, should not have token here
259 m_level = -1;
260 return; // failed state
262 case 1: // have pin
263 m_pin = token;
264 break;
266 case 2: // have db
267 m_db = token;
268 break;
270 case 3: // have record
271 m_record = token;
272 break;
274 case 4: // have field
275 m_field = token;
276 break;
278 default: // too many, store remainder and done
279 m_remainder = s; // keeps slash
280 return;
283 // next
284 s = e;
285 if( *e )
286 e++;
288 else if( *e ) {
289 // next
290 e++;
295 bool IsRoot() const { return m_is_root; }
296 const std::string& Pin() const { return m_pin; }
297 const std::string& DB() const { return m_db; }
298 const std::string& Record() const { return m_record; }
299 const std::string& Field() const { return m_field; }
300 const std::string& Remainder() const { return m_remainder; }
301 int Level() const { return m_level; }
305 /////////////////////////////////////////////////////////////////////////////
306 // API classes
308 class Entry
310 public:
311 virtual ~Entry() {}
314 class Directory : public Entry
316 public:
317 virtual int ReadDir(void *buf, fuse_fill_dir_t filler) = 0;
318 virtual void FillDirStat(struct stat *st)
320 st->st_mode = S_IFDIR | 0555;
321 st->st_nlink = 2;
325 class File : public Entry
327 public:
328 virtual void FillFileStat(const char *path, struct stat *st) = 0;
329 virtual bool AccessOk(int flags)
331 // default to readonly files
332 return (flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDONLY;
334 virtual int ReadFile(const char *path, char *buf, size_t size, off_t offset) = 0;
337 typedef Directory* DirectoryPtr;
338 typedef File* FilePtr;
339 typedef std::string NameT;
340 typedef std::map<NameT, DirectoryPtr> DirMap;
341 typedef std::map<NameT, FilePtr> FileMap;
343 static DirMap g_dirmap;
344 static FileMap g_filemap;
346 static Directory* FindDir(const NameT &name)
348 DirMap::iterator di = g_dirmap.find(name);
349 return di == g_dirmap.end() ? 0 : di->second;
352 static File* FindFile(const NameT &name)
354 FileMap::iterator fi = g_filemap.find(name);
355 return fi == g_filemap.end() ? 0 : fi->second;
358 /////////////////////////////////////////////////////////////////////////////
359 // Context classes
361 class Database : public Directory, public File
363 public:
364 Barry::Mode::Desktop &m_desk;
365 std::string m_name;
366 const Barry::DatabaseItem *m_pdb;
368 public:
369 Database(Barry::Mode::Desktop &desktop,
370 const std::string &pin, const Barry::DatabaseItem *pdb)
371 : m_desk(desktop)
372 , m_pdb(pdb)
374 m_name = string("/") + pin + "/" + m_pdb->Name;
376 // add to directory list
377 g_dirmap[ m_name ] = this;
380 ~Database()
382 // remove any entries that point to us
383 FileMap::iterator b = g_filemap.begin(), e = g_filemap.end();
384 for( ; b != e; ++b ) {
385 if( b->second == this ) {
386 g_filemap.erase(b);
390 // erase ourselves from the directory list
391 g_dirmap.erase( m_name );
394 void AddFile(const std::string &recordId)
396 // FIXME - this is a hack to redirect all record files
397 // to this database class... next step is to possibly
398 // split out records into field files if we have a
399 // parser, or just dump the hex if we don't
400 string name = m_name + "/" + recordId;
401 g_filemap[ name ] = this;
404 virtual int ReadDir(void *buf, fuse_fill_dir_t filler)
406 filler(buf, ".", NULL, 0);
407 filler(buf, "..", NULL, 0);
409 // list all records in database, by recordId
410 Barry::RecordStateTable rst;
411 m_desk.GetRecordStateTable(m_pdb->Number, rst);
413 Barry::RecordStateTable::StateMapType::iterator
414 b = rst.StateMap.begin(),
415 e = rst.StateMap.end();
416 for( ; b != e; ++ b ) {
417 ostringstream oss;
418 oss << hex << b->second.RecordId;
419 filler(buf, oss.str().c_str(), NULL, 0);
421 AddFile(oss.str());
423 return 0;
426 virtual void FillFileStat(const char *path, struct stat *st)
428 // use the path to find the proper record
429 PathSplit ps(path);
431 string constructed = string("/") + ps.Pin() + "/" + ps.DB();
432 if( constructed != m_name ) {
433 // FIXME - this is shoddy error handling
434 throw std::logic_error(_("Constructed != name"));
437 string data = GetRecordData(ps.Record());
439 st->st_mode = S_IFREG | 0444;
440 st->st_nlink = 1;
441 st->st_size = data.size();
444 virtual int ReadFile(const char *path, char *buf, size_t size, off_t offset)
446 // use the path to find the proper record
447 PathSplit ps(path);
449 string constructed = string("/") + ps.Pin() + "/" + ps.DB();
450 if( constructed != m_name ) {
451 // FIXME - this is shoddy error handling
452 throw std::logic_error(_("Constructed != name"));
455 string data = GetRecordData(ps.Record());
457 size_t len = data.size();
458 if( offset >= 0 && offset < (off_t)len ) {
459 if( (offset + size) > len )
460 size = len - offset;
461 memcpy(buf, data.data() + offset, size);
463 else {
464 size = 0;
466 return size;
469 const std::string& GetDBName() const { return m_pdb->Name; }
471 std::string GetRecordData(const std::string &recordId)
473 string data;
475 Barry::RecordStateTable rst;
476 m_desk.GetRecordStateTable(m_pdb->Number, rst);
478 uint32_t recid = strtoul(recordId.c_str(), NULL, 16);
479 RecordStateTable::IndexType index;
480 if( rst.GetIndex(recid, &index) ) {
481 ostringstream oss;
482 ParserPtr parser = GetParser(m_pdb->Name, oss, false);
483 m_desk.GetRecord(m_pdb->Number, index, *parser);
484 data = oss.str();
487 return data;
491 class DesktopCon : public Directory
493 public:
494 typedef std::tr1::shared_ptr<Database> DatabasePtr;
495 typedef std::list<DatabasePtr> DBList;
496 public:
497 Barry::Controller m_con;
498 Barry::Mode::Desktop m_desk;
499 std::string m_pin;
500 DBList m_dblist;
502 DesktopCon(const Barry::ProbeResult &result, const std::string &pin)
503 : m_con(result)
504 , m_desk(m_con)
505 , m_pin(pin)
507 // add to directory list
508 g_dirmap[ string("/") + pin ] = this;
511 ~DesktopCon()
513 // remove from directory list
514 g_dirmap.erase( string("/") + m_pin );
517 virtual int ReadDir(void *buf, fuse_fill_dir_t filler)
519 filler(buf, ".", NULL, 0);
520 filler(buf, "..", NULL, 0);
522 // list all databases in list
523 DBList::const_iterator b = m_dblist.begin(), e = m_dblist.end();
524 for( ; b != e; ++ b ) {
525 filler(buf, (*b)->GetDBName().c_str(), NULL, 0);
527 return 0;
530 void Open(const char *password = 0)
532 // open our device
533 m_desk.Open(password);
535 // add all databases as directories
536 DatabaseDatabase::DatabaseArrayType::const_iterator
537 dbi = m_desk.GetDBDB().Databases.begin(),
538 dbe = m_desk.GetDBDB().Databases.end();
539 for( ; dbi != dbe; ++dbi ) {
540 DatabasePtr db = DatabasePtr(
541 new Database(m_desk, m_pin, &(*dbi)) );
542 m_dblist.push_back(db);
547 class Context : public Directory, public File
549 public:
550 typedef std::auto_ptr<Barry::Probe> ProbePtr;
551 typedef std::tr1::shared_ptr<DesktopCon> DesktopConPtr;
552 typedef std::string PinT;
553 typedef std::map<PinT, DesktopConPtr> PinMap;
555 ProbePtr m_probe;
556 PinMap m_pinmap;
558 string m_error_log;
560 string m_limit_pin; // only mount device with this pin
561 string m_password; // use this password when connecting
563 public:
564 Context(const string &limit_pin = "", const string &password = "")
565 : m_limit_pin(limit_pin)
566 , m_password(password)
568 g_dirmap["/"] = this;
569 g_filemap[string("/") + error_log_filename] = this;
571 m_error_log = _("Hello FUSE world. This is Barry. Pleased to meet you.\n");
574 ~Context()
576 g_dirmap.erase("/");
577 g_filemap.erase(string("/") + error_log_filename);
580 virtual int ReadDir(void *buf, fuse_fill_dir_t filler)
582 filler(buf, ".", NULL, 0);
583 filler(buf, "..", NULL, 0);
584 filler(buf, error_log_filename, NULL, 0);
586 // list all pins in map
587 PinMap::const_iterator b = m_pinmap.begin(), e = m_pinmap.end();
588 for( ; b != e; ++ b ) {
589 filler(buf, b->first.c_str(), NULL, 0);
591 return 0;
594 virtual void FillFileStat(const char *path, struct stat *st)
596 st->st_mode = S_IFREG | 0444;
597 st->st_nlink = 1;
598 st->st_size = m_error_log.size();
601 virtual int ReadFile(const char *path, char *buf, size_t size, off_t offset)
603 size_t len = m_error_log.size();
604 if( offset >= 0 && offset < (off_t)len ) {
605 if( (offset + size) > len )
606 size = len - offset;
607 memcpy(buf, m_error_log.data() + offset, size);
609 else {
610 size = 0;
612 return size;
615 void Log(const std::string &msg)
617 m_error_log += msg;
618 m_error_log += "\n";
621 const std::string& GetLog() const { return m_error_log; }
623 void ProbeAll()
625 // probe the USB bus for Blackberry devices
626 m_probe.reset( new Probe );
628 // connect to all PINs found, and add them to our map
629 for( int i = 0; i < m_probe->GetCount(); i++ ) {
630 string curpin = m_probe->Get(i).m_pin.Str();
632 // don't add a blank or pre-existing pin
633 if( !curpin.size() || m_pinmap.find(curpin) != m_pinmap.end() ) {
634 continue;
637 // don't add non-PIN device if pin specified
638 if( m_limit_pin.size() && curpin != m_limit_pin ) {
639 continue;
642 DesktopConPtr dev = DesktopConPtr (
643 new DesktopCon(m_probe->Get(i), curpin) );
644 dev->Open(m_password.c_str());
645 m_pinmap[ curpin ] = dev;
649 DesktopCon* FindPin(PinT pin)
651 PinMap::iterator pi = m_pinmap.find(pin);
652 return pi == m_pinmap.end() ? 0 : pi->second.get();
657 /////////////////////////////////////////////////////////////////////////////
658 // FUSE API hooks
660 static void* bfuse_init()
662 // Initialize the barry library. Must be called before
663 // anything else.
664 Barry::Init(false);
666 Context *ctx = 0;
668 try {
669 ctx = new Context(cmdline_pin, cmdline_password);
670 ctx->ProbeAll();
672 catch( std::exception &e ) {
673 if( ctx ) {
674 ctx->Log(e.what());
678 return ctx;
681 static void bfuse_destroy(void *data)
683 if( data ) {
684 Context *ctx = (Context*) data;
685 delete ctx;
689 static int bfuse_getattr(const char *path, struct stat *st)
691 memset(st, 0, sizeof(*st));
693 if( Directory *dir = FindDir(path) ) {
694 dir->FillDirStat(st);
695 return 0;
697 else if( File *file = FindFile(path) ) {
698 file->FillFileStat(path, st);
699 return 0;
701 else
702 return -ENOENT;
705 static int bfuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
706 off_t /*offset*/, struct fuse_file_info * /*fi*/)
708 Directory *dir = FindDir(path);
709 if( !dir )
710 return -ENOENT;
711 return dir->ReadDir(buf, filler);
714 static int bfuse_open(const char *path, struct fuse_file_info *fi)
716 File *file = FindFile(path);
717 if( !file )
718 return -ENOENT;
720 if( !file->AccessOk(fi->flags) )
721 return -EACCES;
723 return 0;
726 static int bfuse_read(const char *path, char *buf, size_t size, off_t offset,
727 struct fuse_file_info *fi)
729 File *file = FindFile(path);
730 if( !file )
731 return -ENOENT;
733 return file->ReadFile(path, buf, size, offset);
736 // static struct here automatically zeros data
737 static struct fuse_operations bfuse_oper;
740 /////////////////////////////////////////////////////////////////////////////
741 // main
743 int main(int argc, char *argv[])
745 INIT_I18N(PACKAGE);
747 cout.sync_with_stdio(true); // leave this on, since libusb uses
748 // stdio for debug messages
750 Blurb();
752 // initialize the operation hooks
753 bfuse_oper.init = bfuse_init;
754 bfuse_oper.destroy = bfuse_destroy;
755 bfuse_oper.getattr = bfuse_getattr;
756 bfuse_oper.readdir = bfuse_readdir;
757 bfuse_oper.open = bfuse_open;
758 bfuse_oper.read = bfuse_read;
760 // process command line options before FUSE does
761 // FUSE does its own command line processing, and
762 // doesn't seem to have a way to plug into it,
763 // so do our own first
764 int fuse_argc = 0;
765 char **fuse_argv = new char*[argc];
767 for( int i = 0; i < argc; i++ ) {
768 if( argv[i][0] == '-' ) {
770 switch( argv[i][1] )
772 // case 'd': // mount dbname
773 // dbNames.push_back(string(optarg));
774 // break;
776 // case 'n': // use null parser
777 // null_parser = true;
778 // break;
780 case 'p': // Blackberry PIN
781 if( i+1 < argc ) {
782 cmdline_pin = argv[++i];
784 continue;
786 case 'P': // Device password
787 if( i+1 < argc ) {
788 cmdline_password = argv[++i];
790 continue;
792 case 'h': // help
793 Usage();
794 break;
798 // if we get here, add this option to FUSE's
799 fuse_argv[fuse_argc] = argv[i];
800 fuse_argc++;
803 int ret = fuse_main(fuse_argc, fuse_argv, &bfuse_oper);
804 delete [] fuse_argv;
805 return ret;