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