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