8 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) ) 9 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) ) 22 constexpr
struct to_name_t {
27 const auto select1st = [](
auto&& x ) -> decltype(
auto ) {
return std::get<0>( std::forward<decltype( x )>( x ) ); };
29 const auto select2nd = [](
auto&& x ) -> decltype(
auto ) {
return std::get<1>( std::forward<decltype( x )>( x ) ); };
31 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
32 OutputIterator transform_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
33 UnaryPredicate pred ) {
34 while ( first != last ) {
35 if ( pred( *first ) ) *result++ = op( *first );
41 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
42 OutputIterator transform_copy_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
43 UnaryPredicate pred ) {
44 while ( first != last ) {
45 auto t = op( *first );
46 if ( pred( t ) ) *result++ =
std::move( t );
68 debug() <<
"Failed to initialize the base class (Service)" <<
endmsg;
75 if ( m_loadRootHandler.value() ) {
79 msgSvc()->setOutputLevel(
"RootFileHandler", m_outputLevel.value() );
80 m_rfh.emplace(
msgSvc(), m_ssl_proxy, m_ssl_cert );
82 auto& rfh = m_rfh.value();
86 void*& ptr ) ->
Io::open_t {
return rfh.openRootFile( n, f, desc, fd, ptr ); },
87 [&rfh](
void* ptr ) ->
Io::close_t {
return rfh.closeRootFile( ptr ); },
90 if ( regHandler( hdlr ).isFailure() ) { error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg; }
93 if ( m_loadPosixHandler.value() ) {
97 msgSvc()->setOutputLevel(
"POSIXFileHandler", m_outputLevel.value() );
100 auto& pfh = m_pfh.value();
104 void*& ptr ) ->
Io::open_t {
return pfh.openPOSIXFile( n, f, desc, fd, ptr ); },
108 if ( regHandler( hdlp ).isFailure() ) { error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg; }
120 if ( m_printSummary || msgLevel(
MSG::DEBUG ) ) {
127 if ( !m_files.empty() ) {
128 auto&
log = warning();
129 log <<
"At finalize, the following files remained open:\n";
130 for (
const auto& itr : m_files )
log << *( itr.second ) <<
'\n';
134 if ( m_logfile.value() !=
"" ) {
136 ofs.
open( m_logfile.value().c_str() );
138 error() <<
"Unable to open output file \"" << m_logfile.value() <<
"\" for writing" <<
endmsg;
141 debug() <<
"Saving log to \"" << m_logfile.value() <<
"\"" <<
endmsg;
142 for (
const auto& itr : m_files ) {
143 ofs << itr.second->name() <<
" " << itr.second->tech() <<
" " << itr.second->desc() <<
" " 144 << itr.second->iflags() <<
'\n';
148 for (
const auto& it2 : m_oldFiles ) fs.
insert( *it2 );
149 for (
const auto& it3 : fs ) {
150 ofs << it3.name() <<
" " << it3.tech() <<
" " << it3.desc() <<
" " << it3.iflags()
151 << ( it3.isShared() ?
" SHARED" :
"" ) <<
'\n';
166 if ( status.
isSuccess() ) debug() <<
"Service finalised successfully" <<
endmsg;
181 if ( m_handlers.find( tech ) != m_handlers.end() ) {
182 warning() <<
"Handler for IoTech " << tech <<
" already registered. Ignoring." <<
endmsg;
187 error() <<
"open handler for tech " << tech <<
" is NULL" <<
endmsg;
192 error() <<
"no close handler for tech " << tech <<
" registered" <<
endmsg;
197 error() <<
"no reopen handler for tech " << tech <<
" registered" <<
endmsg;
202 debug() <<
"Successfully registered handler for tech \"" << tech <<
"\"" <<
endmsg;
204 m_handlers[tech] = fh;
214 auto itr = m_handlers.find( tech );
215 if ( itr == m_handlers.end() ) {
216 error() <<
"Can't de-register tech " << tech <<
" as it hasn't been registered!" <<
endmsg;
220 m_handlers.erase( itr );
227 auto itr = m_handlers.find( tech );
236 return open( tech, caller, fname, desc, flags, fd, ptr, sh );
244 return open( tech, caller, fname, desc, flags, fd, dummy, sh );
252 return open( tech, caller, fname, desc, flags, dummy, ptr, sh );
258 const IoFlags& flags,
Fd&
fd,
void*& ptr,
bool shared ) {
276 verbose() <<
"open(" << tech <<
"," << caller <<
",\"" << fname <<
"\",\"" << desc <<
"\"," << flags
277 << ( shared ?
",shared" :
",unshared" ) <<
")" <<
endmsg;
281 if ( getHandler( tech, fh ).isFailure() )
return r;
283 auto fitr = m_files.equal_range( fname );
288 std::find_if( fitr.first, fitr.second, [&]( fileMap::const_reference i ) { return i.second->tech() != tech; } );
289 if ( itr != fitr.second ) {
290 error() <<
"when calling open on " << fname <<
" with tech " << tech <<
", file already opened with different tech " 291 << itr->second->tech() <<
endmsg;
300 bool shareable(
true );
302 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
305 if ( !fa->
isShared() ) shareable =
false;
308 if ( shareable && fa->
flags().
match( flags,
false ) ) {
311 debug() <<
" found shared file: " << *fa <<
endmsg;
332 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught " << err.
what() <<
endmsg;
335 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught an unknown exception." 341 warning() <<
"open of file \"" << fname <<
"\", tech: \"" << tech <<
"\", flags: \"" << flags
342 <<
"\" requested by " << caller <<
" failed. return code: " << r <<
endmsg;
344 FileAttr xfa( -1, fname, desc, tech, flags, 0,
false );
357 debug() <<
"opened file " << *fa <<
endmsg;
359 if (
fd == -1 && ptr == 0 ) {
360 warning() <<
"when opening " << *fa <<
" both File Descriptor" 361 <<
" and File Ptr are invalid" <<
endmsg;
365 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
368 }
else if ( *fa == *( itr->second ) ) {
369 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with identical" 370 <<
" FileAttributes: " << *fa <<
endmsg;
373 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with different" 374 <<
" FileAttributes -\n" 375 <<
"old: " << *( itr->second ) <<
'\n' 376 <<
"new: " << *fa << endmsg;
381 m_files.emplace( fname, fa );
384 if ( execAction( fa, caller,
Io::OPEN ).isFailure() ) {
385 warning() <<
"at least one open callback action failed" <<
endmsg;
407 [&]( fileMap::const_reference i ) {
return i.second->fd() ==
fd; } );
410 error() <<
"unknown file descriptor \"" <<
fd <<
"\" when calling close()" <<
endmsg;
414 IoTech tech = itr->second->tech();
418 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
421 error() <<
"no close(" << tech <<
",Fd) function registered" <<
endmsg;
428 auto fitr = m_files.equal_range( fa->
name() );
429 int i =
std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fd() == fd; } );
437 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with Fd " <<
fd <<
endmsg;
438 m_files.erase( itr );
442 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
444 debug() <<
"closing " << *fa <<
endmsg;
449 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd <<
" caught " << err.
what()
454 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd 455 <<
" caught an unknown exception." <<
endmsg;
461 warning() <<
"close of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
469 m_files.erase( itr );
471 }
else if ( i <= 0 ) {
473 error() <<
"ref count < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
481 m_oldFiles.push_back( fa );
484 if ( execAction( fa, caller,
Io::CLOSE ).isFailure() ) {
485 warning() <<
"at least one close callback action failed" <<
endmsg;
506 [&]( fileMap::const_reference i ) {
return i.second->fptr() == vp; } );
508 if ( itr == m_files.end() ) {
509 error() <<
"unknown file ptr \"" << vp <<
"\" when calling close()" <<
endmsg;
513 IoTech tech = itr->second->tech();
517 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
519 error() <<
"no close(" << tech <<
",void*) function registered" <<
endmsg;
529 std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fptr() == vp; } );
537 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with fptr " << vp <<
endmsg;
538 m_files.erase( itr );
542 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
544 debug() <<
"closing: " << *fa <<
endmsg;
549 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name() <<
" caught " << err.
what()
551 execAction( fa, caller,
CLOSE_ERR ).ignore();
554 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name()
555 <<
" caught an unknown exception." <<
endmsg;
556 execAction( fa, caller,
CLOSE_ERR ).ignore();
561 warning() <<
"close of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
567 m_files.erase( itr );
571 error() <<
"ref count: " << i <<
" < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
579 m_oldFiles.push_back( fa );
582 if ( execAction( fa, caller,
CLOSE ).isFailure() ) {
583 warning() <<
"at least one close callback action failed" <<
endmsg;
594 verbose() <<
"reopen(" <<
fd <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
599 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
601 if ( itr == m_files.end() ) {
602 error() <<
"unregistered FD \"" <<
fd <<
"\" when calling reopen()" <<
endmsg;
611 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
616 error() <<
"no reopen(" << tech <<
",Fd) function registered" <<
endmsg;
625 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
629 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
630 <<
" caught an unknown exception." <<
endmsg;
635 warning() <<
"reopen of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
636 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
647 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
648 warning() <<
"at least one reopen callback action failed" <<
endmsg;
658 verbose() <<
"reopen(" << vp <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
663 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
664 if ( itr == m_files.end() ) {
665 error() <<
"unregistered file ptr \"" << vp <<
"\" when calling reopen()" <<
endmsg;
673 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
676 error() <<
"no reopen(" << tech <<
",void*) function registered" <<
endmsg;
683 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
687 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
688 <<
" caught an unknown exception." <<
endmsg;
693 warning() <<
"reopen of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
694 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
705 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
706 warning() <<
"at least one reopen callback action failed" <<
endmsg;
718 auto fitr = m_files.equal_range( fname );
722 [&](
const FileAttr* f ) {
return f->
name() == fname; } );
732 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
740 if ( j !=
std::end( m_oldFiles ) ) {
753 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
761 if ( j !=
std::end( m_oldFiles ) ) {
799 if ( tech ==
UNKNOWN )
return getFiles( files, op );
803 [&]( fileMap::const_reference f ) {
804 return f.second->tech() == tech &&
806 [&](
const std::string& j ) {
return j == f.first; } );
823 if ( tech ==
UNKNOWN )
return getFiles( files, op );
825 auto matches_tech = [&](
const FileAttr* f ) {
return f->tech() == tech; };
845 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
846 return ( f->tech() == tech || tech ==
UNKNOWN ) && f->flags() == flags;
851 [&]( fileMap::const_reference f ) {
return matches_tech_and_flags( f.second ) && not_in_files( f.first ); } );
854 [&](
const FileAttr* f ) {
return matches_tech_and_flags( f ) && not_in_files( f->
name() ); } );
865 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
866 return ( f->tech() == tech || tech ==
UNKNOWN ) && f->
flags() == flags;
870 matches_tech_and_flags );
873 matches_tech_and_flags );
887 return m_descriptors.size();
897 if ( tech ==
UNKNOWN )
return getFd( fd );
913 transform_if( m_descriptors.begin(), m_descriptors.end(),
std::back_inserter( fd ), select1st,
915 return ( d.second->tech() == tech || tech ==
UNKNOWN ) && ( d.second->flags() == flags );
925 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
926 return ( itr !=
std::end( m_files ) ) ? itr->second->name() : s_empty;
933 auto itr =
std::find_if( m_files.begin(), m_files.end(),
934 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
935 return itr != m_files.end() ? itr->second->name() : s_empty;
942 auto fitr = m_files.equal_range( fname );
943 auto itr =
std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) { return f.second->fd() != -1; } );
944 return itr != fitr.second ? itr->second->fd() : -1;
951 auto itr =
std::find_if( m_files.begin(), m_files.end(),
952 [&]( fileMap::const_reference f ) {
return f.second->fptr() == fptr; } );
953 return itr != m_files.end() ? itr->second->fd() : -1;
959 auto fitr = m_files.equal_range( fname );
961 std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) ->
bool { return f.second->fptr(); } );
962 return itr != fitr.second ? itr->second->fptr() :
nullptr;
969 auto itr =
std::find_if( m_files.begin(), m_files.end(),
970 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
971 return itr != m_files.end() ? itr->second->fptr() :
nullptr;
978 info() <<
"listing registered files [" << ( m_files.size() + m_oldFiles.size() ) <<
"]:\n";
980 for (
auto& itr : m_files ) info() << itr.second <<
'\n';
981 for (
auto& it2 : m_oldFiles ) info() << *it2 <<
'\n';
998 auto itr = m_handlers.find( tech );
999 if ( itr == m_handlers.end() ) {
1000 error() <<
"no handler for tech " << tech <<
" registered" <<
endmsg;
1011 auto fitr = m_files.equal_range( fname );
1012 if ( fitr.first == fitr.second ) {
1013 error() <<
"no file \"" << fname <<
"\" registered. Cannot determine tech" <<
endmsg;
1017 auto itr = fitr.first;
1018 IoTech tech = itr->second->tech();
1021 while ( itr != fitr.second ) {
1022 if ( itr->second->tech() != tech ) {
1023 error() <<
"multiple technologies registered for file \"" << fname <<
"\". Cannot determine handler" <<
endmsg;
1029 return getHandler( tech, hdlr );
1036 info() <<
"Listing registered handlers:\n";
1038 for (
const auto& itr : m_handlers ) info() <<
" " << itr.first <<
'\n';
1065 info() <<
"listing registered actions\n";
1067 for (
const auto& iit : m_actions ) {
1072 info() <<
" --- Tech: ";
1074 info() <<
"ALL ---\n";
1076 info() << t <<
" ---\n";
1078 for (
const auto& iia : m ) {
1079 for (
const auto& it2 : iia.second ) { info() <<
" " << iia.first <<
" " << it2.second <<
'\n'; }
1096 if ( itr != m_actions.end() && !itr->second.empty() ) { s1 = execActs( fa, caller, a, itr->second ); }
1098 itr = m_actions.find( tech );
1099 if ( itr != m_actions.end() && !itr->second.empty() ) { s2 = execActs( fa, caller, a, itr->second ); }
1109 auto mitr = m.
find( a );
1113 debug() <<
"executing " << mitr->second.size() <<
" " << a <<
" actions on " << *fa <<
" from " << caller <<
endmsg;
1117 auto it2 = m_supMap.find( fa->
name() );
1118 if ( it2 != m_supMap.end() ) {
1121 debug() <<
" --> suppressing callback action for " << a <<
endmsg;
1126 for (
const auto& itr : mitr->second ) {
1129 debug() <<
"executing " << itr.second <<
endmsg;
1131 if ( ( ( ( itr.first ) )( fa, caller ) ).isFailure() ) {
1132 warning() <<
"execution of " << itr.second <<
" on " << *fa <<
" failed during " << a <<
" action" <<
endmsg;
1144 verbose() <<
"accessMatch old: " << fold <<
" new: " << fnew <<
endmsg;
1147 ( ( fold &
Io::WRITE ) != 0 && ( fnew & Io::WRITE ) != 0 ) ||
1148 ( ( fold &
Io::RDWR ) != 0 && ( fnew & Io::RDWR ) != 0 ) );
1162 if ( m_supMap.empty() )
return;
1164 info() <<
"listing suppressed file actions\n";
1166 for (
const auto& sup : m_supMap ) {
1167 info() <<
" " << sup.first;
1171 for (
unsigned i = 0; i != sup.second.size(); ++i ) {
1172 if ( sup.second[i] ) info() <<
" " << (
Io::Action)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
int getFiles(std::vector< std::string > &, bool onlyOpen=true) const override
constexpr static const auto SUCCESS
StatusCode deregHandler(const IoTech &) override
StatusCode execAction(Io::FileAttr *, const std::string &, const Io::Action &) const
bool accessMatch(const Io::IoFlags &, const Io::IoFlags &, bool strict=false) const
virtual void listActions() const
Io::Fd fd(const std::string &) const override
#define DECLARE_COMPONENT(type)
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
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).
constexpr static const auto FAILURE
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