Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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/ClassID.h"
11 #include "GaudiKernel/IService.h"
12 
13 #include <bitset>
14 #include <fcntl.h>
15 #include <functional>
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 namespace Io {
21 
22  //
23  // Io modes
24  //
25 
26  enum IoFlag {
27  READ = O_RDONLY,
28  WRITE = O_WRONLY,
29  RDWR = O_RDWR,
30 
31  CREATE = O_CREAT,
32  EXCL = O_EXCL,
33  TRUNC = O_TRUNC,
34 
35  APPEND = O_APPEND,
36 
37  INVALID = 1 << 31
38  };
39 
40  class IoFlags final {
41  public:
42  IoFlags() = default;
43  IoFlags( int i ) : _f( i ){};
44 
45  int f() const { return _f; }
46 
47  bool operator==( const IoFlags& fa ) const { return ( _f == fa.f() ); }
48  bool operator==( const int& fa ) const { return ( _f == fa ); }
49 
50  IoFlags operator|( const IoFlags& fa ) const { return ( _f | fa.f() ); }
51  IoFlags operator|( const int& fa ) const { return ( _f | fa ); }
52 
53  operator int() const { return _f; }
54 
55  bool isRead() const { return ( _f == READ ); }
56  bool isWrite() const { return ( ( _f & WRITE ) != 0 ); }
57  bool isRdWr() const { return ( ( _f & RDWR ) != 0 ); }
58  bool isInvalid() const { return ( ( _f & INVALID ) != 0 ); }
59 
60  bool match( const IoFlags& fa, bool strict = true ) const {
61  if ( strict ) { return ( _f == fa ); }
62  // only look at first 2 bits
63  return ( ( _f & 3 ) == ( fa & 3 ) );
64  }
65 
66  std::string bits() const {
67  std::string s;
68  int f( _f );
69  const int SHIFT = 8 * sizeof( int ) - 1;
70  const unsigned MASK = 1 << SHIFT;
71 
72  for ( int i = 1; i <= SHIFT + 1; ++i ) {
73  s += ( f & MASK ? '1' : '0' );
74  f <<= 1;
75  if ( i % 8 == 0 ) s += ' ';
76  }
77  return s;
78  }
79 
80  private:
81  int _f = INVALID;
82  };
83 
84  static std::string IoFlagName( IoFlags f ) {
85  static const std::map<IoFlag, std::string> s_names = {{{READ, "READ"},
86  {WRITE, "WRITE"},
87  {RDWR, "RDWR"},
88  {CREATE, "CREATE"},
89  {EXCL, "EXCL"},
90  {TRUNC, "TRUNC"},
91  {APPEND, "APPEND"},
92  {INVALID, "INVALID"}}};
93  if ( f.isRead() ) return s_names.at( READ );
94 
95  std::string ff;
96  for ( int i = 0; i < 32; ++i ) {
97  auto b = ( 1 << i );
98  if ( b & f ) ff += s_names.at( ( IoFlag )( b ) ) + "|";
99  }
100  ff.erase( ff.length() - 1 );
101  return ff;
102  }
103 
104  inline IoFlags IoFlagFromName( const std::string& f ) {
105  static const std::map<std::string, IoFlag> s_n = {{{"READ", Io::READ},
106  {"WRITE", Io::WRITE},
107  {"RDWR", Io::RDWR},
108  {"CREATE", Io::CREATE},
109  {"EXCL", Io::EXCL},
110  {"TRUNC", Io::TRUNC},
111  {"APPEND", Io::APPEND},
112  {"INVALID", Io::INVALID}}};
113 
114  IoFlags fl( Io::INVALID );
115  size_t j( 0 ), k( 0 );
116  std::string fs;
117  while ( ( k = f.find( "|", j ) ) != std::string::npos ) {
118  fs = f.substr( j, k - j );
119  if ( s_n.find( fs ) == s_n.end() ) { return Io::INVALID; }
120  if ( fl.isInvalid() ) {
121  fl = s_n.at( fs );
122  } else {
123  fl = fl | s_n.at( fs );
124  }
125  j = k + 1;
126  }
127  fs = f.substr( j );
128  if ( s_n.find( fs ) == s_n.end() ) { return Io::INVALID; }
129  if ( fl.isInvalid() ) {
130  fl = s_n.at( fs );
131  } else {
132  fl = fl | s_n.at( fs );
133  }
134  return fl;
135  }
136 
137  inline std::ostream& operator<<( std::ostream& s, const IoFlag& f ) { return s << IoFlagName( f ); }
138  inline std::ostream& operator<<( std::ostream& s, const IoFlags& f ) { return s << IoFlagName( f ); }
139 
140  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
141 
142  //
143  // Technologies
144  //
145 
147 
148  inline std::ostream& operator<<( std::ostream& s, const IoTech& t ) {
149  static const std::array<const char*, SQLITE + 1> tbl = {{"UNKNOWN", "POSIX", "ROOT", "BS", "HDF5", "SQLITE"}};
150  return t < tbl.size() ? s << tbl[t] : s;
151  }
152 
153  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
154 
155  //
156  // File Attributes
157  //
158 
159  typedef int Fd;
160 
161  class FileAttr final {
162  public:
163  FileAttr() = default;
164  FileAttr( Fd f, std::string n, std::string d, IoTech t, IoFlags fa, void* p, bool o, bool s = false )
165  : m_fd( f )
166  , m_name( std::move( n ) )
167  , m_desc( std::move( d ) )
168  , m_tech( t )
169  , m_flags( fa )
170  , m_iflags( fa )
171  , m_fptr( p )
172  , m_isOpen( o )
173  , m_shared( s ){};
174 
175  Fd fd() const { return m_fd; }
176  const std::string& name() const { return m_name; }
177  const std::string& desc() const { return m_desc; }
178  IoTech tech() const { return m_tech; }
179  IoFlags flags() const { return m_flags; }
180  IoFlags iflags() const { return m_iflags; }
181  void* fptr() const { return m_fptr; }
182  bool isOpen() const { return m_isOpen; }
183  bool isShared() const { return m_shared; }
184 
185  void fd( Fd f ) { m_fd = f; }
186  void name( const std::string& n ) { m_name = n; }
187  void desc( const std::string& d ) { m_desc = d; }
188  void tech( const IoTech& t ) { m_tech = t; }
189  void flags( const IoFlags& f ) { m_flags = f; }
190  void iflags( const IoFlags& f ) { m_iflags = f; }
191  void fptr( void* v ) { m_fptr = v; }
192  void isOpen( bool b ) { m_isOpen = b; }
193  void isShared( bool s ) { m_shared = s; }
194 
195  friend std::ostream& operator<<( std::ostream& os, const FileAttr& fa ) {
196  os << "name: \"" << fa.name() << "\" tech: " << fa.tech() << " desc: " << fa.desc()
197  << " flags: " << IoFlagName( fa.flags() ) << " i_flags: " << IoFlagName( fa.iflags() ) << " Fd: " << fa.fd()
198  << " ptr: " << fa.fptr() << ( fa.isOpen() ? " [o]" : " [c]" ) << ( fa.isShared() ? " [s]" : " [u]" );
199  return os;
200  }
201 
202  bool operator==( const FileAttr& fa ) const {
203  return ( m_fd == fa.fd() && m_name == fa.name() && m_desc == fa.desc() && m_tech == fa.tech() &&
204  m_flags == fa.flags() && m_fptr == fa.fptr() && m_isOpen == fa.isOpen() && m_shared == fa.isShared() );
205  }
206 
207  bool operator<( const FileAttr& rhs ) const {
208  if ( m_name != rhs.name() ) {
209  return ( m_name < rhs.name() );
210  } else {
211  return ( m_flags < rhs.iflags() );
212  }
213  }
214 
215  private:
216  Fd m_fd = -1;
219  IoTech m_tech = UNKNOWN;
220  IoFlags m_flags = INVALID;
221  IoFlags m_iflags = INVALID;
222  void* m_fptr = nullptr;
223  bool m_isOpen = false;
224  bool m_shared = false;
225  };
226 
227  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
228 
229  //
230  // Handler functions
231  //
232 
233  typedef int open_t;
234  typedef int close_t;
235  typedef int reopen_t;
236 
242 
243  // file handler functions: open, close, reopen
244  struct FileHdlr final {
245  IoTech tech = UNKNOWN;
246 
247  bfcn_open_t b_open_fcn;
248  bfcn_close_t b_close_fcn;
249  bfcn_closeP_t b_closeP_fcn;
250  bfcn_reopen_t b_reopen_fcn;
251  bfcn_reopenP_t b_reopenP_fcn;
252 
253  FileHdlr() = default;
254  FileHdlr( IoTech t, bfcn_open_t o, bfcn_close_t c, bfcn_reopen_t r )
255  : tech( t ), b_open_fcn( o ), b_close_fcn( c ), b_reopen_fcn( r ){};
256  FileHdlr( IoTech t, bfcn_open_t o, bfcn_closeP_t c, bfcn_reopenP_t r )
257  : tech( t ), b_open_fcn( o ), b_closeP_fcn( c ), b_reopenP_fcn( r ){};
258  FileHdlr( IoTech t, bfcn_open_t o, bfcn_close_t c1, bfcn_closeP_t c2, bfcn_reopen_t r1, bfcn_reopenP_t r2 )
259  : tech( t ), b_open_fcn( o ), b_close_fcn( c1 ), b_closeP_fcn( c2 ), b_reopen_fcn( r1 ), b_reopenP_fcn( r2 ){};
260  };
261 
262  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
263 
264  //
265  // Callback Actions
266  //
267 
270 
271  inline std::ostream& operator<<( std::ostream& s, const Action& t ) {
273  {"OPEN", "CLOSE", "REOPEN", "OPEN_ERR", "CLOSE_ERR", "REOPEN_ERR", "INVALID_ACTION"}};
274  return t < tbl.size() ? s << tbl[t] : s;
275  }
276 
277 #define FILEMGR_CALLBACK_ARGS const Io::FileAttr*, const std::string&
279 } // namespace Io
280 
281 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
282 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
283 
284 class GAUDI_API IFileMgr : virtual public IService {
285 
286 public:
287  DeclareInterfaceID( IFileMgr, 1, 0 );
288 
289  // register handler function for file technology
290  virtual StatusCode regHandler( Io::FileHdlr ) = 0;
291 
292  virtual StatusCode deregHandler( const Io::IoTech& ) = 0;
293 
294  virtual StatusCode hasHandler( const Io::IoTech& ) const = 0;
295 
296  // get handler from technology
297  virtual StatusCode getHandler( const Io::IoTech&, Io::FileHdlr& ) const = 0;
298  // get handler from file name
299  virtual StatusCode getHandler( const std::string&, Io::FileHdlr& ) const = 0;
300 
301  // get file attributes from file name
302  virtual int getFileAttr( const std::string&, std::vector<const Io::FileAttr*>& ) const = 0;
303  // get file attributes from file descriptor
304  virtual StatusCode getFileAttr( const Io::Fd, const Io::FileAttr*& ) const = 0;
305  // get file attributes from file ptr
306  virtual StatusCode getFileAttr( void*, const Io::FileAttr*& ) const = 0;
307 
308  virtual void listHandlers() const = 0;
309  virtual void listFiles() const = 0;
310 
311  // get all files known to mgr. return numbers found.
312  // will replace contents of FILES
313  virtual int getFiles( std::vector<std::string>& FILES, bool onlyOpen = true ) const = 0;
314  virtual int getFiles( std::vector<const Io::FileAttr*>& FILES, bool onlyOpen = true ) const = 0;
315 
316  // get all files of specific IoTech. returns number found.
317  // will replace contents of FILES
318  virtual int getFiles( const Io::IoTech&, std::vector<std::string>& FILES, bool onlyOpen = true ) const = 0;
319  virtual int getFiles( const Io::IoTech&, std::vector<const Io::FileAttr*>& FILES, bool onlyOpen = true ) const = 0;
320 
321  // get all file of specific IoTech and access mode.
322  // will replace contents of FILES
323  // If IoTech == UNKNOWN, get all. returns number found
324  virtual int getFiles( const Io::IoTech&, const Io::IoFlags&, std::vector<std::string>& FILES,
325  bool onlyOpen = true ) const = 0;
326  virtual int getFiles( const Io::IoTech&, const Io::IoFlags&, std::vector<const Io::FileAttr*>& FILES,
327  bool onlyOpen = true ) const = 0;
328 
329  // get all descriptors known to mgr. returns number found
330  virtual int getFd( std::vector<Io::Fd>& ) const = 0;
331  // get all descriptors of specific IoTech. return number found
332  virtual int getFd( const Io::IoTech&, std::vector<Io::Fd>& ) const = 0;
333  // get all descriptors of specific IoTech and access mode.
334  // If IoTech == INVALID, get all. returns number found
335  virtual int getFd( const Io::IoTech&, const Io::IoFlags&, std::vector<Io::Fd>& ) const = 0;
336 
337  // get file name given Fd or ptr. Returns empty string if fails
338  virtual const std::string& fname( const Io::Fd& ) const = 0;
339  virtual const std::string& fname( void* ) const = 0;
340 
341  // get Fd given file name. Returns -1 if fails
342  virtual Io::Fd fd( const std::string& ) const = 0;
343  virtual Io::Fd fd( void* fptr ) const = 0;
344 
345  // get ptr given file name. Returns 0 if fails
346  virtual void* fptr( const std::string& ) const = 0;
347  virtual void* fptr( const Io::Fd& ) const = 0;
348 
349  virtual int getLastError( std::string& ) const = 0;
350 
351  // Open file, get Fd and ptr
352  virtual Io::open_t open( const Io::IoTech&, const std::string& caller, const std::string& fname, const Io::IoFlags&,
353  Io::Fd&, void*&, const std::string& desc, const bool shared = false ) = 0;
354 
355  // Open file, get Fd
356  virtual Io::open_t open( const Io::IoTech&, const std::string& caller, const std::string& fname, const Io::IoFlags&,
357  Io::Fd&, const std::string& desc, const bool shared = false ) = 0;
358 
359  // Open file, get ptr
360  virtual Io::open_t open( const Io::IoTech&, const std::string& caller, const std::string& fname, const Io::IoFlags&,
361  void*&, const std::string& desc, const bool shared = false ) = 0;
362 
363  // Close file by Fd or ptr
364  virtual Io::close_t close( const Io::Fd, const std::string& caller ) = 0;
365  virtual Io::close_t close( void*, const std::string& caller ) = 0;
366 
367  // Reopen file by Fd or ptr
368  virtual Io::reopen_t reopen( const Io::Fd, const Io::IoFlags&, const std::string& ) = 0;
369  virtual Io::reopen_t reopen( void*, const Io::IoFlags&, const std::string& ) = 0;
370 
371  // Callback actions
372  virtual StatusCode regAction( Io::bfcn_action_t, const Io::Action&, const std::string& d = "" ) = 0;
373  virtual StatusCode regAction( Io::bfcn_action_t, const Io::Action&, const Io::IoTech&,
374  const std::string& d = "" ) = 0;
375 
376  // Suppress callback action(s) for specific file.
377  virtual void suppressAction( const std::string& ) = 0;
378  virtual void suppressAction( const std::string&, const Io::Action& ) = 0;
379 };
380 #endif
std::function< Io::reopen_t(void *, Io::IoFlags)> bfcn_reopenP_t
Definition: IFileMgr.h:241
IoTech
Definition: IFileMgr.h:146
void flags(const IoFlags &f)
Definition: IFileMgr.h:189
Definition: IFileMgr.h:20
GAUDI_API unsigned long getLastError()
Get last system known error.
Definition: System.cpp:263
int reopen_t
Definition: IFileMgr.h:235
int Fd
Definition: IFileMgr.h:159
bfcn_reopen_t b_reopen_fcn
Definition: IFileMgr.h:250
IoFlag
Definition: IFileMgr.h:26
void name(const std::string &n)
Definition: IFileMgr.h:186
std::function< Io::close_t(Io::Fd)> bfcn_close_t
Definition: IFileMgr.h:238
void * fptr() const
Definition: IFileMgr.h:181
int open_t
Definition: IFileMgr.h:233
STL namespace.
bool isOpen() const
Definition: IFileMgr.h:182
std::function< Io::reopen_t(Io::Fd, Io::IoFlags)> bfcn_reopen_t
Definition: IFileMgr.h:240
T end(T...args)
std::function< Io::open_t(const std::string &, Io::IoFlags, const std::string &, Io::Fd &, void *&)> bfcn_open_t
Definition: IFileMgr.h:237
bool operator==(const FileAttr &fa) const
Definition: IFileMgr.h:202
Fd fd() const
Definition: IFileMgr.h:175
FileHdlr(IoTech t, bfcn_open_t o, bfcn_close_t c, bfcn_reopen_t r)
Definition: IFileMgr.h:254
STL class.
std::string bits() const
Definition: IFileMgr.h:66
void fptr(void *v)
Definition: IFileMgr.h:191
STL class.
IoFlags(int i)
Definition: IFileMgr.h:43
std::function< Io::close_t(void *)> bfcn_closeP_t
Definition: IFileMgr.h:239
FileAttr(Fd f, std::string n, std::string d, IoTech t, IoFlags fa, void *p, bool o, bool s=false)
Definition: IFileMgr.h:164
Action
Definition: IFileMgr.h:268
T at(T...args)
const std::string & name() const
Definition: IFileMgr.h:176
void iflags(const IoFlags &f)
Definition: IFileMgr.h:190
bool operator==(const int &fa) const
Definition: IFileMgr.h:48
IoFlags IoFlagFromName(const std::string &f)
Definition: IFileMgr.h:104
bool operator<(const FileAttr &rhs) const
Definition: IFileMgr.h:207
const std::string & desc() const
Definition: IFileMgr.h:177
bool isShared() const
Definition: IFileMgr.h:183
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
#define DeclareInterfaceID(iface, major, minor)
Macro to declare the interface ID when using the new mechanism of extending and implementing interfac...
Definition: IInterface.h:13
void fd(Fd f)
Definition: IFileMgr.h:185
bool isWrite() const
Definition: IFileMgr.h:56
bfcn_close_t b_close_fcn
Definition: IFileMgr.h:248
bool operator==(const IoFlags &fa) const
Definition: IFileMgr.h:47
std::ostream & operator<<(std::ostream &s, const IoFlag &f)
Definition: IFileMgr.h:137
T erase(T...args)
bfcn_closeP_t b_closeP_fcn
Definition: IFileMgr.h:249
std::string m_desc
Definition: IFileMgr.h:218
FileHdlr(IoTech t, bfcn_open_t o, bfcn_closeP_t c, bfcn_reopenP_t r)
Definition: IFileMgr.h:256
bfcn_reopenP_t b_reopenP_fcn
Definition: IFileMgr.h:251
std::function< StatusCode(FILEMGR_CALLBACK_ARGS)> bfcn_action_t
Definition: IFileMgr.h:278
void tech(const IoTech &t)
Definition: IFileMgr.h:188
IoFlags iflags() const
Definition: IFileMgr.h:180
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:258
T find(T...args)
T length(T...args)
bfcn_open_t b_open_fcn
Definition: IFileMgr.h:247
STL class.
IoTech tech() const
Definition: IFileMgr.h:178
bool match(const IoFlags &fa, bool strict=true) const
Definition: IFileMgr.h:60
string s
Definition: gaudirun.py:312
STL class.
void isOpen(bool b)
Definition: IFileMgr.h:192
bool isRead() const
Definition: IFileMgr.h:55
IoFlags operator|(const int &fa) const
Definition: IFileMgr.h:51
T substr(T...args)
void desc(const std::string &d)
Definition: IFileMgr.h:187
int f() const
Definition: IFileMgr.h:45
IoFlags operator|(const IoFlags &fa) const
Definition: IFileMgr.h:50
bool isInvalid() const
Definition: IFileMgr.h:58
IoFlags()=default
std::string m_name
Definition: IFileMgr.h:217
#define GAUDI_API
Definition: Kernel.h:71
IoFlags flags() const
Definition: IFileMgr.h:179
STL class.
bool isRdWr() const
Definition: IFileMgr.h:57
STL class.
int close_t
Definition: IFileMgr.h:234
friend std::ostream & operator<<(std::ostream &os, const FileAttr &fa)
Definition: IFileMgr.h:195
void isShared(bool s)
Definition: IFileMgr.h:193