11 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) ) 12 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) ) 25 void set_bit(
int& f,
const unsigned int& b ) { f |= 1 << b; }
27 bool get_bit(
const int& f,
const unsigned int& b ) {
return f & ( 1 << b ); }
31 constexpr
struct to_name_t {
36 constexpr
struct select1st_t {
37 template <
typename T,
typename S>
44 constexpr
struct select2nd_t {
45 template <
typename T,
typename S>
52 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
53 OutputIterator transform_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
56 while ( first != last ) {
57 if ( pred( *first ) ) *result++ = op( *first );
63 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
64 OutputIterator transform_copy_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
67 while ( first != last ) {
68 auto t = op( *first );
69 if ( pred( t ) ) *result++ =
std::move( t );
93 debug() <<
"Failed to initialize the base class (Service)" <<
endmsg;
100 if ( m_loadRootHandler.value() ) {
104 msgSvc()->setOutputLevel(
"RootFileHandler", m_outputLevel.value() );
107 auto rfh = m_rfh.get();
110 void*& ptr ) ->
Io::open_t {
return rfh->openRootFile( n, f, desc, fd, ptr ); },
111 [rfh](
void* ptr ) ->
Io::close_t {
return rfh->closeRootFile( ptr ); },
114 if ( regHandler( hdlr ).isFailure() ) {
115 error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg;
119 if ( m_loadPosixHandler.value() ) {
123 msgSvc()->setOutputLevel(
"POSIXFileHandler", m_outputLevel.value() );
126 auto pfh = m_pfh.get();
129 void*& ptr ) ->
Io::open_t {
return pfh->openPOSIXFile( n, f, desc, fd, ptr ); },
133 if ( regHandler( hdlp ).isFailure() ) {
134 error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg;
148 if ( m_printSummary || msgLevel(
MSG::DEBUG ) ) {
155 if ( !m_files.empty() ) {
156 auto&
log = warning();
157 log <<
"At finalize, the following files remained open:" <<
endl;
158 for (
const auto& itr : m_files )
log << *( itr.second ) << endl;
162 if ( m_logfile.value() !=
"" ) {
164 ofs.
open( m_logfile.value().c_str() );
166 error() <<
"Unable to open output file \"" << m_logfile.value() <<
"\" for writing" <<
endmsg;
169 debug() <<
"Saving log to \"" << m_logfile.value() <<
"\"" <<
endmsg;
170 for (
const auto& itr : m_files ) {
171 ofs << itr.second->name() <<
" " << itr.second->tech() <<
" " << itr.second->desc() <<
" " 172 << itr.second->iflags() <<
endl;
176 for (
const auto& it2 : m_oldFiles ) fs.
insert( *it2 );
177 for (
const auto& it3 : fs ) {
178 ofs << it3.name() <<
" " << it3.tech() <<
" " << it3.desc() <<
" " << it3.iflags()
179 << ( it3.isShared() ?
" SHARED" :
"" ) <<
endl;
194 if ( status.
isSuccess() ) debug() <<
"Service finalised successfully" <<
endmsg;
210 if ( m_handlers.find( tech ) != m_handlers.end() ) {
211 warning() <<
"Handler for IoTech " << tech <<
" already registered. Ignoring." <<
endmsg;
216 error() <<
"open handler for tech " << tech <<
" is NULL" <<
endmsg;
221 error() <<
"no close handler for tech " << tech <<
" registered" <<
endmsg;
226 error() <<
"no reopen handler for tech " << tech <<
" registered" <<
endmsg;
231 debug() <<
"Successfully registered handler for tech \"" << tech <<
"\"" <<
endmsg;
233 m_handlers[tech] = fh;
244 auto itr = m_handlers.find( tech );
245 if ( itr == m_handlers.end() ) {
246 error() <<
"Can't de-register tech " << tech <<
" as it hasn't been registered!" <<
endmsg;
250 m_handlers.erase( itr );
258 auto itr = m_handlers.find( tech );
268 return open( tech, caller, fname, desc, flags, fd, ptr, sh );
277 return open( tech, caller, fname, desc, flags, fd, dummy, sh );
286 return open( tech, caller, fname, desc, flags, dummy, ptr, sh );
292 const IoFlags& flags,
Fd&
fd,
void*& ptr,
bool shared )
311 verbose() <<
"open(" << tech <<
"," << caller <<
",\"" << fname <<
"\",\"" << desc <<
"\"," << flags
312 << ( shared ?
",shared" :
",unshared" ) <<
")" <<
endmsg;
316 if ( getHandler( tech, fh ).isFailure() )
return r;
318 auto fitr = m_files.equal_range( fname );
323 std::find_if( fitr.first, fitr.second, [&]( fileMap::const_reference i ) { return i.second->tech() != tech; } );
324 if ( itr != fitr.second ) {
325 error() <<
"when calling open on " << fname <<
" with tech " << tech <<
", file already opened with different tech " 326 << itr->second->tech() <<
endmsg;
335 bool shareable(
true );
337 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
340 if ( !fa->
isShared() ) shareable =
false;
343 if ( shareable && fa->
flags().
match( flags,
false ) ) {
346 debug() <<
" found shared file: " << *fa <<
endmsg;
367 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught " << err.
what() <<
endmsg;
370 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught an unknown exception." 376 warning() <<
"open of file \"" << fname <<
"\", tech: \"" << tech <<
"\", flags: \"" << flags
377 <<
"\" requested by " << caller <<
" failed. return code: " << r <<
endmsg;
379 FileAttr xfa( -1, fname, desc, tech, flags, 0,
false );
392 debug() <<
"opened file " << *fa <<
endmsg;
394 if (
fd == -1 && ptr == 0 ) {
395 warning() <<
"when opening " << *fa <<
" both File Descriptor" 396 <<
" and File Ptr are invalid" <<
endmsg;
400 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
403 }
else if ( *fa == *( itr->second ) ) {
404 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with identical" 405 <<
" FileAttributes: " << *fa <<
endmsg;
408 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with different" 409 <<
" FileAttributes -" <<
endl 410 <<
"old: " << *( itr->second ) <<
endl 411 <<
"new: " << *fa << endmsg;
416 m_files.emplace( fname, fa );
419 if ( execAction( fa, caller,
Io::OPEN ).isFailure() ) {
420 warning() <<
"at least one open callback action failed" <<
endmsg;
443 [&]( fileMap::const_reference i ) {
return i.second->fd() ==
fd; } );
446 error() <<
"unknown file descriptor \"" <<
fd <<
"\" when calling close()" <<
endmsg;
450 IoTech tech = itr->second->tech();
454 if ( getHandler( tech, fh ).isFailure() ) {
459 error() <<
"no close(" << tech <<
",Fd) function registered" <<
endmsg;
466 auto fitr = m_files.equal_range( fa->
name() );
467 int i =
std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fd() == fd; } );
475 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with Fd " <<
fd <<
endmsg;
476 m_files.erase( itr );
480 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
482 debug() <<
"closing " << *fa <<
endmsg;
487 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd <<
" caught " << err.
what()
492 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd 493 <<
" caught an unknown exception." <<
endmsg;
499 warning() <<
"close of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
507 m_files.erase( itr );
509 }
else if ( i <= 0 ) {
511 error() <<
"ref count < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
519 m_oldFiles.push_back( fa );
522 if ( execAction( fa, caller,
Io::CLOSE ).isFailure() ) {
523 warning() <<
"at least one close callback action failed" <<
endmsg;
545 [&]( fileMap::const_reference i ) {
return i.second->fptr() == vp; } );
547 if ( itr == m_files.end() ) {
548 error() <<
"unknown file ptr \"" << vp <<
"\" when calling close()" <<
endmsg;
552 IoTech tech = itr->second->tech();
556 if ( getHandler( tech, fh ).isFailure() ) {
560 error() <<
"no close(" << tech <<
",void*) function registered" <<
endmsg;
570 std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fptr() == vp; } );
578 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with fptr " << vp <<
endmsg;
579 m_files.erase( itr );
583 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
585 debug() <<
"closing: " << *fa <<
endmsg;
590 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name() <<
" caught " << err.
what()
592 execAction( fa, caller,
CLOSE_ERR ).ignore();
595 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name()
596 <<
" caught an unknown exception." <<
endmsg;
597 execAction( fa, caller,
CLOSE_ERR ).ignore();
602 warning() <<
"close of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
608 m_files.erase( itr );
612 error() <<
"ref count: " << i <<
" < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
620 m_oldFiles.push_back( fa );
623 if ( execAction( fa, caller,
CLOSE ).isFailure() ) {
624 warning() <<
"at least one close callback action failed" <<
endmsg;
636 verbose() <<
"reopen(" <<
fd <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
641 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
643 if ( itr == m_files.end() ) {
644 error() <<
"unregistered FD \"" <<
fd <<
"\" when calling reopen()" <<
endmsg;
653 if ( getHandler( tech, fh ).isFailure() ) {
660 error() <<
"no reopen(" << tech <<
",Fd) function registered" <<
endmsg;
669 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
673 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
674 <<
" caught an unknown exception." <<
endmsg;
679 warning() <<
"reopen of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
680 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
691 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
692 warning() <<
"at least one reopen callback action failed" <<
endmsg;
703 verbose() <<
"reopen(" << vp <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
708 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
709 if ( itr == m_files.end() ) {
710 error() <<
"unregistered file ptr \"" << vp <<
"\" when calling reopen()" <<
endmsg;
718 if ( getHandler( tech, fh ).isFailure() ) {
723 error() <<
"no reopen(" << tech <<
",void*) function registered" <<
endmsg;
730 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
734 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
735 <<
" caught an unknown exception." <<
endmsg;
740 warning() <<
"reopen of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
741 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
752 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
753 warning() <<
"at least one reopen callback action failed" <<
endmsg;
766 auto fitr = m_files.equal_range( fname );
770 [&](
const FileAttr* f ) {
return f->
name() == fname; } );
781 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
789 if ( j !=
std::end( m_oldFiles ) ) {
803 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
811 if ( j !=
std::end( m_oldFiles ) ) {
854 if ( tech ==
UNKNOWN )
return getFiles( files, op );
858 [&]( fileMap::const_reference f ) {
859 return f.second->tech() == tech &&
861 [&](
const std::string& j ) {
return j == f.first; } );
879 if ( tech ==
UNKNOWN )
return getFiles( files, op );
881 auto matches_tech = [&](
const FileAttr* f ) {
return f->tech() == tech; };
902 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
903 return ( f->tech() == tech || tech ==
UNKNOWN ) && f->flags() == flags;
908 [&]( fileMap::const_reference f ) {
return matches_tech_and_flags( f.second ) && not_in_files( f.first ); } );
911 [&](
const FileAttr* f ) {
return matches_tech_and_flags( f ) && not_in_files( f->
name() ); } );
923 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
924 return ( f->tech() == tech || tech ==
UNKNOWN ) && f->
flags() == flags;
928 matches_tech_and_flags );
931 matches_tech_and_flags );
946 return m_descriptors.size();
957 if ( tech ==
UNKNOWN )
return getFd( fd );
974 transform_if( m_descriptors.begin(), m_descriptors.end(),
std::back_inserter( fd ), select1st,
976 return ( d.second->tech() == tech || tech ==
UNKNOWN ) && ( d.second->flags() == flags );
987 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
988 return ( itr !=
std::end( m_files ) ) ? itr->second->name() : s_empty;
996 auto itr =
std::find_if( m_files.begin(), m_files.end(),
997 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
998 return itr != m_files.end() ? itr->second->name() : s_empty;
1006 auto fitr = m_files.equal_range( fname );
1007 auto itr =
std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) { return f.second->fd() != -1; } );
1008 return itr != fitr.second ? itr->second->fd() : -1;
1016 auto itr =
std::find_if( m_files.begin(), m_files.end(),
1017 [&]( fileMap::const_reference f ) {
return f.second->fptr() == fptr; } );
1018 return itr != m_files.end() ? itr->second->fd() : -1;
1025 auto fitr = m_files.equal_range( fname );
1027 std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) ->
bool { return f.second->fptr(); } );
1028 return itr != fitr.second ? itr->second->fptr() :
nullptr;
1036 auto itr =
std::find_if( m_files.begin(), m_files.end(),
1037 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
1038 return itr != m_files.end() ? itr->second->fptr() :
nullptr;
1046 info() <<
"listing registered files [" << ( m_files.size() + m_oldFiles.size() ) <<
"]:" <<
endl;
1048 for (
auto& itr : m_files ) info() << itr.second <<
endl;
1049 for (
auto& it2 : m_oldFiles ) info() << *it2 <<
endl;
1068 auto itr = m_handlers.find( tech );
1069 if ( itr == m_handlers.end() ) {
1070 error() <<
"no handler for tech " << tech <<
" registered" <<
endmsg;
1082 auto fitr = m_files.equal_range( fname );
1083 if ( fitr.first == fitr.second ) {
1084 error() <<
"no file \"" << fname <<
"\" registered. Cannot determine tech" <<
endmsg;
1088 auto itr = fitr.first;
1089 IoTech tech = itr->second->tech();
1092 while ( itr != fitr.second ) {
1093 if ( itr->second->tech() != tech ) {
1094 error() <<
"multiple technologies registered for file \"" << fname <<
"\". Cannot determine handler" <<
endmsg;
1100 return getHandler( tech, hdlr );
1108 info() <<
"Listing registered handlers:" <<
endl;
1110 for (
const auto& itr : m_handlers ) info() <<
" " << itr.first <<
endl;
1140 info() <<
"listing registered actions" <<
endl;
1142 for (
const auto& iit : m_actions ) {
1147 info() <<
" --- Tech: ";
1149 info() <<
"ALL ---" <<
endl;
1151 info() << t <<
" ---" <<
endl;
1153 for (
const auto& iia : m ) {
1154 for (
const auto& it2 : iia.second ) {
1155 info() <<
" " << iia.first <<
" " << it2.second <<
endl;
1174 if ( itr != m_actions.end() && !itr->second.empty() ) {
1175 s1 = execActs( fa, caller, a, itr->second );
1178 itr = m_actions.find( tech );
1179 if ( itr != m_actions.end() && !itr->second.empty() ) {
1180 s2 = execActs( fa, caller, a, itr->second );
1192 auto mitr = m.
find( a );
1194 if ( mitr == m.
end() || mitr->second.empty() ) {
1199 debug() <<
"executing " << mitr->second.size() <<
" " << a <<
" actions on " << *fa <<
" from " << caller <<
endmsg;
1203 auto it2 = m_supMap.find( fa->
name() );
1204 if ( it2 != m_supMap.end() ) {
1207 debug() <<
" --> suppressing callback action for " << a <<
endmsg;
1212 for (
const auto& itr : mitr->second ) {
1215 debug() <<
"executing " << itr.second <<
endmsg;
1217 if ( ( ( ( itr.first ) )( fa, caller ) ).isFailure() ) {
1218 warning() <<
"execution of " << itr.second <<
" on " << *fa <<
" failed during " << a <<
" action" <<
endmsg;
1232 verbose() <<
"accessMatch old: " << fold <<
" new: " << fnew <<
endmsg;
1235 ( ( fold &
Io::WRITE ) != 0 && ( fnew & Io::WRITE ) != 0 ) ||
1236 ( ( fold &
Io::RDWR ) != 0 && ( fnew & Io::RDWR ) != 0 ) );
1248 auto it2 = m_supMap.find( f );
1249 if ( it2 == m_supMap.end() ) {
1254 set_bit( it2->second, a );
1263 if ( m_supMap.empty() )
return;
1265 info() <<
"listing suppressed file actions" <<
endl;
1267 for (
auto it2 = m_supMap.begin(); it2 != m_supMap.end(); ++it2 ) {
1268 info() <<
" " << it2->first;
1270 info() <<
" ALL" <<
endl;
1273 if ( get_bit( it2->second, i ) ) {
StatusCode regHandler(FileHdlr) override
void listHandlers() const override
StatusCode initialize() override
int getFileAttr(const std::string &, std::vector< const FileAttr * > &) const override
void listFiles() const override
StatusCode finalize() override
StatusCode hasHandler(const IoTech &) const override
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Io::open_t open(const Io::IoTech &, const std::string &caller, const std::string &fname, const Io::IoFlags &, Io::Fd &fd, void *&ptr, const std::string &desc="", const bool shared=false) override
bfcn_reopen_t b_reopen_fcn
void suppressAction(const std::string &) override
bool isSuccess() const
Test for a status code of SUCCESS.
int getFiles(std::vector< std::string > &, bool onlyOpen=true) const override
StatusCode deregHandler(const IoTech &) override
StatusCode execAction(Io::FileAttr *, const std::string &, const Io::Action &) const
bool isFailure() const
Test for a status code of FAILURE.
bool accessMatch(const Io::IoFlags &, const Io::IoFlags &, bool strict=false) const
virtual void listActions() const
Io::Fd fd(const std::string &) const override
const std::string & name() const
void handle(const Incident &) override
StatusCode getHandler(const IoTech &, FileHdlr &) const override
This class is used for returning status codes from appropriate routines.
Io::reopen_t reopen(const Fd, const IoFlags &, const std::string &caller) override
StatusCode execActs(Io::FileAttr *, const std::string &, const Io::Action &, const actionMap &m) const
bfcn_closeP_t b_closeP_fcn
Io::close_t close(const Fd, const std::string &caller) override
bfcn_reopenP_t b_reopenP_fcn
#define DECLARE_SERVICE_FACTORY(x)
StatusCode regAction(Io::bfcn_action_t, const Io::Action &, const std::string &desc="") override
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
virtual void listSuppression() const
bool match(const IoFlags &fa, bool strict=true) const
T back_inserter(T...args)
Base class for all Incidents (computing events).
const std::string & fname(const Io::Fd &) const override
int getFd(std::vector< Fd > &) const override
StatusCode finalize() override
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
StatusCode initialize() override
int getLastError(std::string &) const override
void * fptr(const std::string &) const override