Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
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/IService.h"
11 #include "GaudiKernel/ClassID.h"
12 
13 #include <map>
14 #include <string>
15 #include <vector>
16 #include <fcntl.h>
17 #include <functional>
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 final {
40  public:
41  IoFlags() = default;
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 ) s += ' ';
77  }
78  return s;
79  }
80 
81  private:
82  int _f = INVALID;
83  };
84 
85  static std::string IoFlagName(IoFlags f) {
86  static const std::map<IoFlag, std::string> s_names = { {
87  { READ, "READ" }, { WRITE, "WRITE"}, { RDWR, "RDWR" },
88  { CREATE, "CREATE"}, { EXCL, "EXCL" }, { TRUNC, "TRUNC"},
89  { APPEND, "APPEND"}, { INVALID,"INVALID"} } };
90  if ( f.isRead() ) return s_names.at(READ);
91 
92  std::string ff;
93  for (int i=0; i<32; ++i) {
94  auto b = ( 1 << i );
95  if ( b & f ) ff += s_names.at((IoFlag)(b)) + "|";
96  }
97  ff.erase(ff.length()-1);
98  return ff;
99  }
100 
101 
103  static const std::map<std::string, IoFlag> s_n = { {
104  { "READ", Io::READ }, { "WRITE", Io::WRITE }, { "RDWR", Io::RDWR },
105  { "CREATE", Io::CREATE }, { "EXCL", Io::EXCL }, { "TRUNC", Io::TRUNC},
106  { "APPEND", Io::APPEND }, { "INVALID", Io::INVALID } } };
107 
108  IoFlags fl(Io::INVALID);
109  size_t j(0),k(0);
110  std::string fs;
111  while ( (k=f.find("|",j)) != std::string::npos) {
112  fs = f.substr(j,k-j);
113  if (s_n.find(fs) == s_n.end()) {
114  return Io::INVALID;
115  }
116  if (fl.isInvalid()) {
117  fl = s_n.at(fs);
118  } else {
119  fl = fl | s_n.at(fs);
120  }
121  j = k+1;
122  }
123  fs = f.substr(j);
124  if (s_n.find(fs) == s_n.end()) {
125  return Io::INVALID;
126  }
127  if (fl.isInvalid()) {
128  fl = s_n.at(fs);
129  } else {
130  fl = fl | s_n.at(fs);
131  }
132  return fl;
133 
134  }
135 
137  return s << IoFlagName(f);
138  }
140  return s << IoFlagName(f);
141 
142  }
143 
144 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
145 
146  //
147  // Technologies
148  //
149 
150  enum IoTech {
154  BS,
157  };
158 
160  static const std::array<const char*, SQLITE+1> tbl = { {
161  "UNKNOWN", "POSIX", "ROOT",
162  "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  public:
176 
177  FileAttr() = default;
179  IoFlags fa, void* p, bool o, bool s=false):
180  m_fd(f),m_name(std::move(n)),m_desc(std::move(d)),
181  m_tech(t),m_flags(fa),m_iflags(fa),m_fptr(p),
182  m_isOpen(o),m_shared(s){};
183 
184  Fd fd() const { return m_fd; }
185  const std::string& name() const { return m_name; }
186  const std::string& desc() const { return m_desc; }
187  IoTech tech() const { return m_tech; }
188  IoFlags flags() const { return m_flags; }
189  IoFlags iflags() const { return m_iflags; }
190  void* fptr() const { return m_fptr; }
191  bool isOpen() const { return m_isOpen; }
192  bool isShared() const { return m_shared; }
193 
194 
195  void fd(Fd f) { m_fd = f; }
196  void name(const std::string& n) { m_name = n; }
197  void desc(const std::string& d) { m_desc = d; }
198  void tech(const IoTech& t) { m_tech = t;}
199  void flags(const IoFlags& f) { m_flags = f; }
200  void iflags(const IoFlags& f) { m_iflags = f; }
201  void fptr(void* v) { m_fptr = v; }
202  void isOpen(bool b) { m_isOpen = b; }
203  void isShared(bool s) { m_shared = s; }
204 
205 
206  friend std::ostream& operator<< (std::ostream& os, const FileAttr& fa) {
207  os << "name: \"" << fa.name() << "\" tech: " << fa.tech()
208  << " desc: " << fa.desc()
209  << " flags: " << IoFlagName(fa.flags())
210  << " i_flags: " << IoFlagName(fa.iflags())
211  << " Fd: " << fa.fd() << " ptr: " << fa.fptr()
212  << (fa.isOpen() ? " [o]" : " [c]" )
213  << (fa.isShared() ? " [s]" : " [u]" );
214  return os;
215  }
216 
217  bool operator== (const FileAttr& fa) const {
218  return ( m_fd==fa.fd() && m_name==fa.name() && m_desc==fa.desc() &&
219  m_tech==fa.tech() && m_flags==fa.flags() && m_fptr==fa.fptr() &&
220  m_isOpen==fa.isOpen() && m_shared==fa.isShared() );
221  }
222 
223  bool operator< (const FileAttr& rhs) const {
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 
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 
248  //
249  // Handler functions
250  //
251 
252  typedef int open_t;
253  typedef int close_t;
254  typedef int reopen_t;
255 
261 
262  // file handler functions: open, close, reopen
263  struct FileHdlr final {
264  IoTech tech = UNKNOWN;
265 
266  bfcn_open_t b_open_fcn;
267  bfcn_close_t b_close_fcn;
268  bfcn_closeP_t b_closeP_fcn;
269  bfcn_reopen_t b_reopen_fcn;
270  bfcn_reopenP_t b_reopenP_fcn;
271 
272  FileHdlr() = default;
273  FileHdlr( IoTech t, bfcn_open_t o, bfcn_close_t c, bfcn_reopen_t r)
274  : tech(t), b_open_fcn(o), b_close_fcn(c), b_reopen_fcn(r){};
275  FileHdlr( IoTech t, bfcn_open_t o, bfcn_closeP_t c, bfcn_reopenP_t r)
276  : tech(t), b_open_fcn(o), b_closeP_fcn(c), b_reopenP_fcn(r){};
277  FileHdlr( IoTech t, bfcn_open_t o, bfcn_close_t c1, bfcn_closeP_t c2,
278  bfcn_reopen_t r1,bfcn_reopenP_t r2)
279  : tech(t), b_open_fcn(o), b_close_fcn(c1), b_closeP_fcn(c2),
280  b_reopen_fcn(r1), b_reopenP_fcn(r2){};
281 
282  };
283 
284 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
285 
286  //
287  // Callback Actions
288  //
289 
290  enum Action {
291  OPEN = 0,
298  };
299 
301  static const std::array<const char*, INVALID_ACTION+1> tbl = { {
302  "OPEN", "CLOSE", "REOPEN",
303  "OPEN_ERR","CLOSE_ERR", "REOPEN_ERR",
304  "INVALID_ACTION" } };
305  return t < tbl.size() ? s<< tbl[t] : s;
306  }
307 
308  #define FILEMGR_CALLBACK_ARGS const Io::FileAttr*, const std::string&
310 
311 }
312 
313 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
314 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
315 
316 class GAUDI_API IFileMgr: virtual public IService {
317 
318  public:
319 
321 
322  // register handler function for file technology
323  virtual StatusCode regHandler(Io::FileHdlr) = 0;
324 
325  virtual StatusCode deregHandler(const Io::IoTech&) = 0;
326 
327  virtual StatusCode hasHandler(const Io::IoTech&) const = 0;
328 
329  // get handler from technology
330  virtual StatusCode getHandler(const Io::IoTech&, Io::FileHdlr&) const = 0;
331  // get handler from file name
332  virtual StatusCode getHandler(const std::string&, Io::FileHdlr&) const = 0;
333 
334  // get file attributes from file name
335  virtual int getFileAttr(const std::string&,
337  // get file attributes from file descriptor
338  virtual StatusCode getFileAttr(const Io::Fd, const Io::FileAttr*&) const = 0;
339  // get file attributes from file ptr
340  virtual StatusCode getFileAttr(void*, const Io::FileAttr*&) const = 0;
341 
342  virtual void listHandlers() const = 0;
343  virtual void listFiles() const = 0;
344 
345  // get all files known to mgr. return numbers found.
346  // will replace contents of FILES
347  virtual int getFiles(std::vector<std::string> & FILES,
348  bool onlyOpen=true) const = 0;
349  virtual int getFiles(std::vector<const Io::FileAttr*> & FILES,
350  bool onlyOpen=true) const = 0;
351 
352  // get all files of specific IoTech. returns number found.
353  // will replace contents of FILES
354  virtual int getFiles(const Io::IoTech&, std::vector<std::string>& FILES,
355  bool onlyOpen=true) const = 0;
356  virtual int getFiles(const Io::IoTech&,
358  bool onlyOpen=true) const = 0;
359 
360  // get all file of specific IoTech and access mode.
361  // will replace contents of FILES
362  // If IoTech == UNKNOWN, get all. returns number found
363  virtual int getFiles(const Io::IoTech&, const Io::IoFlags& ,
365  bool onlyOpen=true) const =0;
366  virtual int getFiles(const Io::IoTech&, const Io::IoFlags& ,
368  bool onlyOpen=true) const =0;
369 
370 
371  // get all descriptors known to mgr. returns number found
372  virtual int getFd (std::vector<Io::Fd > & ) const = 0;
373  // get all descriptors of specific IoTech. return number found
374  virtual int getFd(const Io::IoTech&, std::vector<Io::Fd>&) const = 0;
375  // get all descriptors of specific IoTech and access mode.
376  // If IoTech == INVALID, get all. returns number found
377  virtual int getFd(const Io::IoTech&, const Io::IoFlags& ,
378  std::vector<Io::Fd> &) const = 0;
379 
380  // get file name given Fd or ptr. Returns empty string if fails
381  virtual const std::string& fname(const Io::Fd&) const = 0;
382  virtual const std::string& fname(void*) const = 0;
383 
384  // get Fd given file name. Returns -1 if fails
385  virtual Io::Fd fd(const std::string&) const = 0;
386  virtual Io::Fd fd(void* fptr) const = 0;
387 
388  // get ptr given file name. Returns 0 if fails
389  virtual void* fptr(const std::string&) const = 0;
390  virtual void* fptr(const Io::Fd&) const = 0;
391 
392 
393  virtual int getLastError(std::string&) const = 0;
394 
395 
396  // Open file, get Fd and ptr
397  virtual Io::open_t open(const Io::IoTech&, const std::string& caller,
398  const std::string& fname,
399  const Io::IoFlags&, Io::Fd&, void*&,
400  const std::string& desc,
401  const bool shared=false
402  ) = 0;
403 
404  // Open file, get Fd
405  virtual Io::open_t open(const Io::IoTech&, const std::string& caller,
406  const std::string& fname,
407  const Io::IoFlags&, Io::Fd&, const std::string& desc,
408  const bool shared=false
409  ) = 0;
410 
411  // Open file, get ptr
412  virtual Io::open_t open(const Io::IoTech&, const std::string& caller,
413  const std::string& fname,
414  const Io::IoFlags&, void*&, const std::string& desc,
415  const bool shared=false
416  ) = 0;
417 
418  // Close file by Fd or ptr
419  virtual Io::close_t close(const Io::Fd, const std::string& caller) = 0;
420  virtual Io::close_t close(void*, const std::string& caller) = 0;
421 
422  // Reopen file by Fd or ptr
423  virtual Io::reopen_t reopen(const Io::Fd, const Io::IoFlags&,
424  const std::string& ) = 0;
425  virtual Io::reopen_t reopen(void*, const Io::IoFlags&,
426  const std::string& ) = 0;
427 
428  // Callback actions
429  virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action&,
430  const std::string& d="") = 0;
431  virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action&,
432  const Io::IoTech&, const std::string& d="") = 0;
433 
434  // Suppress callback action(s) for specific file.
435  virtual void suppressAction(const std::string&) = 0;
436  virtual void suppressAction(const std::string&, const Io::Action&) = 0;
437 
438 };
439 #endif
IoTech
Definition: IFileMgr.h:150
void flags(const IoFlags &f)
Definition: IFileMgr.h:199
Definition: IFileMgr.h:19
GAUDI_API unsigned long getLastError()
Get last system known error.
Definition: System.cpp:246
int reopen_t
Definition: IFileMgr.h:254
int Fd
Definition: IFileMgr.h:172
bfcn_reopen_t b_reopen_fcn
Definition: IFileMgr.h:269
IoFlag
Definition: IFileMgr.h:25
void name(const std::string &n)
Definition: IFileMgr.h:196
void * fptr() const
Definition: IFileMgr.h:190
#define DeclareInterfaceID(iface, major, minor)
Macro to declare the interface ID when using the new mechanism of extending and implementing interfac...
Definition: IInterface.h:14
int open_t
Definition: IFileMgr.h:252
STL namespace.
bool isOpen() const
Definition: IFileMgr.h:191
T end(T...args)
Fd fd() const
Definition: IFileMgr.h:184
FileHdlr(IoTech t, bfcn_open_t o, bfcn_close_t c, bfcn_reopen_t r)
Definition: IFileMgr.h:273
STL class.
std::string bits() const
Definition: IFileMgr.h:67
void fptr(void *v)
Definition: IFileMgr.h:201
STL class.
IoFlags(int i)
Definition: IFileMgr.h:42
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:290
T at(T...args)
const std::string & name() const
Definition: IFileMgr.h:185
void iflags(const IoFlags &f)
Definition: IFileMgr.h:200
IoFlags IoFlagFromName(const std::string &f)
Definition: IFileMgr.h:102
const std::string & desc() const
Definition: IFileMgr.h:186
bool isShared() const
Definition: IFileMgr.h:192
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
void fd(Fd f)
Definition: IFileMgr.h:195
bool isWrite() const
Definition: IFileMgr.h:55
bfcn_close_t b_close_fcn
Definition: IFileMgr.h:267
bool operator==(const IoFlags &fa) const
Definition: IFileMgr.h:46
std::ostream & operator<<(std::ostream &s, const IoFlag &f)
Definition: IFileMgr.h:136
T erase(T...args)
bfcn_closeP_t b_closeP_fcn
Definition: IFileMgr.h:268
std::string m_desc
Definition: IFileMgr.h:235
std::function< Io::open_t(const std::string &, Io::IoFlags, const std::string &, Io::Fd &, void *&)> bfcn_open_t
Definition: IFileMgr.h:256
FileHdlr(IoTech t, bfcn_open_t o, bfcn_closeP_t c, bfcn_reopenP_t r)
Definition: IFileMgr.h:275
bfcn_reopenP_t b_reopenP_fcn
Definition: IFileMgr.h:270
std::function< Io::close_t(void *)> bfcn_closeP_t
Definition: IFileMgr.h:258
std::function< Io::close_t(Io::Fd)> bfcn_close_t
Definition: IFileMgr.h:257
void tech(const IoTech &t)
Definition: IFileMgr.h:198
IoFlags iflags() const
Definition: IFileMgr.h:189
std::function< StatusCode(FILEMGR_CALLBACK_ARGS) > bfcn_action_t
Definition: IFileMgr.h:309
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:185
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:277
T find(T...args)
T length(T...args)
bfcn_open_t b_open_fcn
Definition: IFileMgr.h:266
STL class.
IoTech tech() const
Definition: IFileMgr.h:187
bool match(const IoFlags &fa, bool strict=true) const
Definition: IFileMgr.h:59
string s
Definition: gaudirun.py:245
STL class.
void isOpen(bool b)
Definition: IFileMgr.h:202
bool isRead() const
Definition: IFileMgr.h:54
T substr(T...args)
void desc(const std::string &d)
Definition: IFileMgr.h:197
int f() const
Definition: IFileMgr.h:44
IoFlags operator|(const IoFlags &fa) const
Definition: IFileMgr.h:49
bool isInvalid() const
Definition: IFileMgr.h:57
IoFlags()=default
std::string m_name
Definition: IFileMgr.h:234
#define GAUDI_API
Definition: Kernel.h:107
IoFlags flags() const
Definition: IFileMgr.h:188
std::function< Io::reopen_t(void *, Io::IoFlags)> bfcn_reopenP_t
Definition: IFileMgr.h:260
STL class.
bool isRdWr() const
Definition: IFileMgr.h:56
int close_t
Definition: IFileMgr.h:253
std::function< Io::reopen_t(Io::Fd, Io::IoFlags)> bfcn_reopen_t
Definition: IFileMgr.h:259
void isShared(bool s)
Definition: IFileMgr.h:203