All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IFileMgr.h
Go to the documentation of this file.
1 // IFileMgr.h
3 // Manages all file open/reopen/close
4 // Author: C.Leggett
6 
7 #ifndef GAUDIKERNEL_IFILEMGR_H
8 #define GAUDIKERNEL_IFILEMGR_H 1
9 
10 #include "GaudiKernel/IService.h"
11 #include "GaudiKernel/ClassID.h"
12 
13 #include <map>
14 #include <string>
15 #include <vector>
16 #include <fcntl.h>
17 #include "boost/function.hpp"
18 
19 namespace Io {
20 
21  //
22  // Io modes
23  //
24 
25  enum IoFlag {
26  READ = O_RDONLY,
27  WRITE = O_WRONLY,
28  RDWR = O_RDWR,
29 
30  CREATE = O_CREAT,
31  EXCL = O_EXCL,
32  TRUNC = O_TRUNC,
33 
34  APPEND = O_APPEND,
35 
36  INVALID = 1<<31
37  };
38 
39  class IoFlags {
40  public:
42  IoFlags(int i):_f(i){};
43 
44  int f() const { return _f; }
45 
46  bool operator== (const IoFlags& fa) const { return ( _f == fa.f() ); }
47  bool operator== (const int& fa) const { return ( _f == fa ); }
48 
49  IoFlags operator| (const IoFlags& fa) const { return (_f | fa.f()); }
50  IoFlags operator| (const int& fa) const { return (_f | fa); }
51 
52  operator int() const { return _f; }
53 
54  bool isRead() const { return (_f == READ); }
55  bool isWrite() const { return ( (_f & WRITE) != 0 ); }
56  bool isRdWr() const { return ( (_f & RDWR) != 0 ); }
57  bool isInvalid() const { return ( (_f & INVALID) != 0); }
58 
59  bool match (const IoFlags& fa, bool strict=true) const {
60  if ( strict ) {
61  return ( _f == fa );
62  }
63  // only look at first 2 bits
64  return ( (_f&3) == (fa&3) );
65  }
66 
67  std::string bits() const {
68  std::string s;
69  int f(_f);
70  const int SHIFT = 8 * sizeof( int ) - 1;
71  const unsigned MASK = 1 << SHIFT;
72 
73  for ( int i = 1; i <= SHIFT + 1; i++ ) {
74  s += ( f & MASK ? '1' : '0' );
75  f <<= 1;
76  if ( i % 8 == 0 )
77  s += ' ';
78  }
79  return s;
80  }
81 
82  private:
83  int _f;
84  };
85 
86  static std::string IoFlagName(IoFlags f) {
87  static std::map<IoFlag, std::string> s_names;
88  if (s_names.empty()) {
89  s_names[READ] = "READ";
90  s_names[WRITE] = "WRITE";
91  s_names[RDWR] = "RDWR";
92  s_names[CREATE] = "CREATE";
93  s_names[EXCL] = "EXCL";
94  s_names[TRUNC] = "TRUNC";
95  s_names[APPEND] = "APPEND";
96  s_names[INVALID] = "INVALID";
97  }
98 
99  if ( f.isRead() ) {
100  return s_names[READ];
101  }
102 
103  std::string ff;
104  for (int i=0; i<32; ++i) {
105  if ( ( (1<<i) & f) != 0) {
106  ff += s_names[ (IoFlag)(1<<i) ] + "|";
107  }
108  }
109  ff.erase(ff.length()-1);
110  return ff;
111  }
112 
113 
114  inline IoFlags IoFlagFromName(const std::string& f) {
115  static std::map<std::string, IoFlag> s_n;
116  if (s_n.empty()) {
117  s_n["READ"] = Io::READ;
118  s_n["WRITE"] = Io::WRITE;
119  s_n["RDWR"] = Io::RDWR;
120  s_n["CREATE"] = Io::CREATE;
121  s_n["EXCL"] = Io::EXCL;
122  s_n["TRUNC"] = Io::TRUNC;
123  s_n["APPEND"] = Io::APPEND;
124  s_n["INVALID"] = Io::INVALID;
125  }
126 
127  IoFlags fl(Io::INVALID);
128  size_t j(0),k(0);
129  std::string fs;
130  while ( (k=f.find("|",j)) != std::string::npos) {
131  fs = f.substr(j,k-j);
132  if (s_n.find(fs) == s_n.end()) {
133  return Io::INVALID;
134  }
135  if (fl.isInvalid()) {
136  fl = s_n[fs];
137  } else {
138  fl = fl | s_n[fs];
139  }
140  j = k+1;
141  }
142  fs = f.substr(j,f.length()-j);
143  if (s_n.find(fs) == s_n.end()) {
144  return Io::INVALID;
145  }
146  if (fl.isInvalid()) {
147  fl = s_n[fs];
148  } else {
149  fl = fl | s_n[fs];
150  }
151 
152  return fl;
153 
154  }
155 
156  inline std::ostream & operator << (std::ostream & s, const IoFlag &f) {
157  return s << IoFlagName(f);
158  }
159  inline std::ostream& operator << (std::ostream &s, const IoFlags &f) {
160  return s << IoFlagName(f);
161 
162  }
163 
164 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
165 
166  //
167  // Technologies
168  //
169 
170  enum IoTech {
174  BS,
177  };
178 
179  inline std::ostream & operator << (std::ostream & s, const IoTech &t) {
180  switch (t) {
181  case UNKNOWN : return s << "UNKNOWN";
182  case POSIX : return s << "POSIX";
183  case ROOT : return s << "ROOT";
184  case BS : return s << "BS";
185  case HDF5 : return s << "HDF5";
186  case SQLITE : return s << "SQLITE";
187  }
188  return s;
189  }
190 
191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
192 
193  //
194  // File Attributes
195  //
196 
197  typedef int Fd;
198 
199  class FileAttr {
200  public:
201 
203  m_iflags(INVALID),m_fptr(0),m_isOpen(false), m_shared(false){};
204  FileAttr(Fd f, const std::string& n, const std::string& d, IoTech t,
205  IoFlags fa, void* p, bool o, bool s=false):
206  m_fd(f),m_name(n),m_desc(d),m_tech(t),m_flags(fa),m_iflags(fa),m_fptr(p),
207  m_isOpen(o),m_shared(s){};
208 
209  Fd fd() const { return m_fd; }
210  const std::string& name() const { return m_name; }
211  const std::string& desc() const { return m_desc; }
212  IoTech tech() const { return m_tech; }
213  IoFlags flags() const { return m_flags; }
214  IoFlags iflags() const { return m_iflags; }
215  void* fptr() const { return m_fptr; }
216  bool isOpen() const { return m_isOpen; }
217  bool isShared() const { return m_shared; }
218 
219 
220  void fd(Fd f) { m_fd = f; }
221  void name(const std::string& n) { m_name = n; }
222  void desc(const std::string& d) { m_desc = d; }
223  void tech(const IoTech& t) { m_tech = t;}
224  void flags(const IoFlags& f) { m_flags = f; }
225  void iflags(const IoFlags& f) { m_iflags = f; }
226  void fptr(void* v) { m_fptr = v; }
227  void isOpen(bool b) { m_isOpen = b; }
228  void isShared(bool s) { m_shared = s; }
229 
230 
231  friend std::ostream& operator<< (std::ostream& os, const FileAttr& fa) {
232  os << "name: \"" << fa.name() << "\" tech: " << fa.tech()
233  << " desc: " << fa.desc()
234  << " flags: " << IoFlagName(fa.flags())
235  << " i_flags: " << IoFlagName(fa.iflags())
236  << " Fd: " << fa.fd() << " ptr: " << fa.fptr()
237  << (fa.isOpen() ? " [o]" : " [c]" )
238  << (fa.isShared() ? " [s]" : " [u]" );
239 
240  return os;
241  }
242 
243  bool operator== (const FileAttr& fa) const {
244  return ( m_fd==fa.fd() && m_name==fa.name() && m_desc==fa.desc() &&
245  m_tech==fa.tech() && m_flags==fa.flags() && m_fptr==fa.fptr() &&
246  m_isOpen==fa.isOpen() && m_shared==fa.isShared() );
247  }
248 
249  bool operator< (const FileAttr& rhs) const {
250  if ( m_name != rhs.name() ) {
251  return ( m_name < rhs.name() );
252  } else {
253  return ( m_flags < rhs.iflags() );
254  }
255  }
256 
257  private:
258 
260  std::string m_name;
261  std::string m_desc;
265  void* m_fptr;
266  bool m_isOpen;
267  bool m_shared;
268 
269  };
270 
271 
272 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
273 
274  //
275  // Handler functions
276  //
277 
278  typedef int open_t;
279  typedef int close_t;
280  typedef int reopen_t;
281 
282  typedef boost::function<Io::open_t(const std::string&, Io::IoFlags, const std::string&, Io::Fd&, void*&)> bfcn_open_t;
283  typedef boost::function<Io::close_t(Io::Fd)> bfcn_close_t;
284  typedef boost::function<Io::close_t(void*)> bfcn_closeP_t;
285  typedef boost::function<Io::reopen_t(Io::Fd,Io::IoFlags)> bfcn_reopen_t;
286  typedef boost::function<Io::reopen_t(void*,Io::IoFlags)> bfcn_reopenP_t;
287 
288  // file handler functions: open, close, reopen
289  struct FileHdlr {
291 
297 
300  : tech(t), b_open_fcn(o), b_close_fcn(c), b_reopen_fcn(r){};
302  : tech(t), b_open_fcn(o), b_closeP_fcn(c), b_reopenP_fcn(r){};
305  : tech(t), b_open_fcn(o), b_close_fcn(c1), b_closeP_fcn(c2),
306  b_reopen_fcn(r1), b_reopenP_fcn(r2){};
307 
308  };
309 
310 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
311 
312  //
313  // Callback Actions
314  //
315 
316  enum Action {
317  OPEN = 0,
324  };
325 
326  inline std::ostream & operator << (std::ostream & s, const Action &t) {
327  switch (t) {
328  case OPEN : return s << "OPEN";
329  case CLOSE : return s << "CLOSE";
330  case REOPEN : return s << "REOPEN";
331  case OPEN_ERR : return s << "OPEN_ERR";
332  case CLOSE_ERR : return s << "CLOSE_ERR";
333  case REOPEN_ERR : return s << "REOPEN_ERR";
334  case INVALID_ACTION : return s << "INVALID_ACTION";
335  }
336  return s;
337  }
338 
339  #define FILEMGR_CALLBACK_ARGS const Io::FileAttr*, const std::string&
340  typedef boost::function<StatusCode (FILEMGR_CALLBACK_ARGS) > bfcn_action_t;
341 
342 }
343 
344 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
345 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
346 
347 class GAUDI_API IFileMgr: virtual public IService {
348 
349  public:
350 
351  virtual ~IFileMgr();
352 
354 
355  public:
356 
357  // register handler function for file technology
358  virtual StatusCode regHandler(Io::FileHdlr) = 0;
359 
360  virtual StatusCode deregHandler(const Io::IoTech&) = 0;
361 
362  virtual StatusCode hasHandler(const Io::IoTech&) const = 0;
363 
364  // get handler from technology
365  virtual StatusCode getHandler(const Io::IoTech&, Io::FileHdlr&) const = 0;
366  // get handler from file name
367  virtual StatusCode getHandler(const std::string&, Io::FileHdlr&) const = 0;
368 
369  // get file attributes from file name
370  virtual int getFileAttr(const std::string&,
371  std::vector<const Io::FileAttr*>&) const = 0;
372  // get file attributes from file descriptor
373  virtual StatusCode getFileAttr(const Io::Fd, const Io::FileAttr*&) const = 0;
374  // get file attributes from file ptr
375  virtual StatusCode getFileAttr(void*, const Io::FileAttr*&) const = 0;
376 
377  virtual void listHandlers() const = 0;
378  virtual void listFiles() const = 0;
379 
380  // get all files known to mgr. return numbers found.
381  // will replace contents of FILES
382  virtual int getFiles(std::vector<std::string> & FILES,
383  bool onlyOpen=true) const = 0;
384  virtual int getFiles(std::vector<const Io::FileAttr*> & FILES,
385  bool onlyOpen=true) const = 0;
386 
387  // get all files of specific IoTech. returns number found.
388  // will replace contents of FILES
389  virtual int getFiles(const Io::IoTech&, std::vector<std::string>& FILES,
390  bool onlyOpen=true) const = 0;
391  virtual int getFiles(const Io::IoTech&,
392  std::vector<const Io::FileAttr*>& FILES,
393  bool onlyOpen=true) const = 0;
394 
395  // get all file of specific IoTech and access mode.
396  // will replace contents of FILES
397  // If IoTech == UNKNOWN, get all. returns number found
398  virtual int getFiles(const Io::IoTech&, const Io::IoFlags& ,
399  std::vector<std::string> &FILES,
400  bool onlyOpen=true) const =0;
401  virtual int getFiles(const Io::IoTech&, const Io::IoFlags& ,
402  std::vector<const Io::FileAttr*> &FILES,
403  bool onlyOpen=true) const =0;
404 
405 
406  // get all descriptors known to mgr. returns number found
407  virtual int getFd (std::vector<Io::Fd > & ) const = 0;
408  // get all descriptors of specific IoTech. return number found
409  virtual int getFd(const Io::IoTech&, std::vector<Io::Fd>&) const = 0;
410  // get all descriptors of specific IoTech and access mode.
411  // If IoTech == INVALID, get all. returns number found
412  virtual int getFd(const Io::IoTech&, const Io::IoFlags& ,
413  std::vector<Io::Fd> &) const = 0;
414 
415  // get file name given Fd or ptr. Returns empty string if fails
416  virtual const std::string& fname(const Io::Fd&) const = 0;
417  virtual const std::string& fname(void*) const = 0;
418 
419  // get Fd given file name. Returns -1 if fails
420  virtual Io::Fd fd(const std::string&) const = 0;
421  virtual Io::Fd fd(void* fptr) const = 0;
422 
423  // get ptr given file name. Returns 0 if fails
424  virtual void* fptr(const std::string&) const = 0;
425  virtual void* fptr(const Io::Fd&) const = 0;
426 
427 
428  virtual int getLastError(std::string&) const = 0;
429 
430 
431  // Open file, get Fd and ptr
432  virtual Io::open_t open(const Io::IoTech&, const std::string& caller,
433  const std::string& fname,
434  const Io::IoFlags&, Io::Fd&, void*&,
435  const std::string& desc,
436  const bool shared=false
437  ) = 0;
438 
439  // Open file, get Fd
440  virtual Io::open_t open(const Io::IoTech&, const std::string& caller,
441  const std::string& fname,
442  const Io::IoFlags&, Io::Fd&, const std::string& desc,
443  const bool shared=false
444  ) = 0;
445 
446  // Open file, get ptr
447  virtual Io::open_t open(const Io::IoTech&, const std::string& caller,
448  const std::string& fname,
449  const Io::IoFlags&, void*&, const std::string& desc,
450  const bool shared=false
451  ) = 0;
452 
453 
454  // Close file by Fd or ptr
455  virtual Io::close_t close(const Io::Fd, const std::string& caller) = 0;
456  virtual Io::close_t close(void*, const std::string& caller) = 0;
457 
458  // Reopen file by Fd or ptr
459  virtual Io::reopen_t reopen(const Io::Fd, const Io::IoFlags&,
460  const std::string& ) = 0;
461  virtual Io::reopen_t reopen(void*, const Io::IoFlags&,
462  const std::string& ) = 0;
463 
464 
465  // Callback actions
466  virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action&,
467  const std::string& d="") = 0;
468  virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action&,
469  const Io::IoTech&, const std::string& d="") = 0;
470 
471  // Suppress callback action(s) for specific file.
472  virtual void suppressAction(const std::string&) = 0;
473  virtual void suppressAction(const std::string&, const Io::Action&) = 0;
474 
475 
476 };
477 
478 #endif
IoTech
Definition: IFileMgr.h:170
boost::function< Io::open_t(const std::string &, Io::IoFlags, const std::string &, Io::Fd &, void *&)> bfcn_open_t
Definition: IFileMgr.h:282
void flags(const IoFlags &f)
Definition: IFileMgr.h:224
GAUDI_API unsigned long getLastError()
Get last system known error.
Definition: System.cpp:243
boost::function< Io::close_t(void *)> bfcn_closeP_t
Definition: IFileMgr.h:284
IoFlags m_flags
Definition: IFileMgr.h:263
int reopen_t
Definition: IFileMgr.h:280
int Fd
Definition: IFileMgr.h:197
IoTech m_tech
Definition: IFileMgr.h:262
IoFlags m_iflags
Definition: IFileMgr.h:264
bfcn_reopen_t b_reopen_fcn
Definition: IFileMgr.h:295
IoFlag
Definition: IFileMgr.h:25
void name(const std::string &n)
Definition: IFileMgr.h:221
boost::function< Io::close_t(Io::Fd)> bfcn_close_t
Definition: IFileMgr.h:283
tuple c
Definition: gaudirun.py:341
void * fptr() const
Definition: IFileMgr.h:215
int open_t
Definition: IFileMgr.h:278
bool isOpen() const
Definition: IFileMgr.h:216
bool operator==(const FileAttr &fa) const
Definition: IFileMgr.h:243
Fd fd() const
Definition: IFileMgr.h:209
FileHdlr(IoTech t, bfcn_open_t o, bfcn_close_t c, bfcn_reopen_t r)
Definition: IFileMgr.h:299
std::string bits() const
Definition: IFileMgr.h:67
void fptr(void *v)
Definition: IFileMgr.h:226
IoFlags(int i)
Definition: IFileMgr.h:42
Action
Definition: IFileMgr.h:316
const std::string & name() const
Definition: IFileMgr.h:210
void iflags(const IoFlags &f)
Definition: IFileMgr.h:225
void * m_fptr
Definition: IFileMgr.h:265
DeclareInterfaceID(IService, 3, 0)
InterfaceID.
IoFlags IoFlagFromName(const std::string &f)
Definition: IFileMgr.h:114
bool operator<(const FileAttr &rhs) const
Definition: IFileMgr.h:249
const std::string & desc() const
Definition: IFileMgr.h:211
bool isShared() const
Definition: IFileMgr.h:217
boost::function< StatusCode(FILEMGR_CALLBACK_ARGS) > bfcn_action_t
Definition: IFileMgr.h:340
bool m_isOpen
Definition: IFileMgr.h:266
General service interface definition.
Definition: IService.h:19
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
void fd(Fd f)
Definition: IFileMgr.h:220
bool isWrite() const
Definition: IFileMgr.h:55
bfcn_close_t b_close_fcn
Definition: IFileMgr.h:293
bool operator==(const IoFlags &fa) const
Definition: IFileMgr.h:46
std::ostream & operator<<(std::ostream &s, const IoFlag &f)
Definition: IFileMgr.h:156
bfcn_closeP_t b_closeP_fcn
Definition: IFileMgr.h:294
std::string m_desc
Definition: IFileMgr.h:261
FileHdlr(IoTech t, bfcn_open_t o, bfcn_closeP_t c, bfcn_reopenP_t r)
Definition: IFileMgr.h:301
bfcn_reopenP_t b_reopenP_fcn
Definition: IFileMgr.h:296
FileAttr(Fd f, const std::string &n, const std::string &d, IoTech t, IoFlags fa, void *p, bool o, bool s=false)
Definition: IFileMgr.h:204
void tech(const IoTech &t)
Definition: IFileMgr.h:223
IoFlags iflags() const
Definition: IFileMgr.h:214
FileHdlr(IoTech t, bfcn_open_t o, bfcn_close_t c1, bfcn_closeP_t c2, bfcn_reopen_t r1, bfcn_reopenP_t r2)
Definition: IFileMgr.h:303
bfcn_open_t b_open_fcn
Definition: IFileMgr.h:292
IoTech tech() const
Definition: IFileMgr.h:212
bool match(const IoFlags &fa, bool strict=true) const
Definition: IFileMgr.h:59
string s
Definition: gaudirun.py:210
boost::function< Io::reopen_t(Io::Fd, Io::IoFlags)> bfcn_reopen_t
Definition: IFileMgr.h:285
void isOpen(bool b)
Definition: IFileMgr.h:227
bool isRead() const
Definition: IFileMgr.h:54
void desc(const std::string &d)
Definition: IFileMgr.h:222
int f() const
Definition: IFileMgr.h:44
IoFlags operator|(const IoFlags &fa) const
Definition: IFileMgr.h:49
bool isInvalid() const
Definition: IFileMgr.h:57
boost::function< Io::reopen_t(void *, Io::IoFlags)> bfcn_reopenP_t
Definition: IFileMgr.h:286
std::string m_name
Definition: IFileMgr.h:260
#define GAUDI_API
Definition: Kernel.h:108
list i
Definition: ana.py:128
IoFlags flags() const
Definition: IFileMgr.h:213
bool isRdWr() const
Definition: IFileMgr.h:56
IoTech tech
Definition: IFileMgr.h:290
int close_t
Definition: IFileMgr.h:279
friend std::ostream & operator<<(std::ostream &os, const FileAttr &fa)
Definition: IFileMgr.h:231
bool m_shared
Definition: IFileMgr.h:267
void isShared(bool s)
Definition: IFileMgr.h:228