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