18 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) ) 19 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) ) 32 constexpr
struct to_name_t {
37 const auto select1st = [](
auto&& x ) -> decltype(
auto ) {
return std::get<0>(
std::forward<decltype( x )>( x ) ); };
39 const auto select2nd = [](
auto&& x ) -> decltype(
auto ) {
return std::get<1>(
std::forward<decltype( x )>( x ) ); };
41 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
42 OutputIterator transform_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
43 UnaryPredicate pred ) {
44 while ( first != last ) {
45 if ( pred( *first ) ) *result++ = op( *first );
51 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
52 OutputIterator transform_copy_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
53 UnaryPredicate pred ) {
54 while ( first != last ) {
55 auto t = op( *first );
56 if ( pred( t ) ) *result++ =
std::move( t );
78 debug() <<
"Failed to initialize the base class (Service)" <<
endmsg;
85 if ( m_loadRootHandler.value() ) {
89 msgSvc()->setOutputLevel(
"RootFileHandler", m_outputLevel.value() );
90 m_rfh.emplace(
msgSvc(), m_ssl_proxy, m_ssl_cert );
92 auto& rfh = m_rfh.value();
96 void*& ptr ) ->
Io::open_t {
return rfh.openRootFile(
n, f, desc,
fd, ptr ); },
97 [&rfh](
void* ptr ) ->
Io::close_t {
return rfh.closeRootFile( ptr ); },
100 if ( regHandler( hdlr ).isFailure() ) { error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg; }
103 if ( m_loadPosixHandler.value() ) {
107 msgSvc()->setOutputLevel(
"POSIXFileHandler", m_outputLevel.value() );
108 m_pfh.emplace(
msgSvc() );
110 auto& pfh = m_pfh.value();
114 void*& ptr ) ->
Io::open_t {
return pfh.openPOSIXFile(
n, f, desc,
fd, ptr ); },
118 if ( regHandler( hdlp ).isFailure() ) { error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg; }
130 if ( m_printSummary || msgLevel(
MSG::DEBUG ) ) {
137 if ( !m_files.empty() ) {
138 auto&
log = warning();
139 log <<
"At finalize, the following files remained open:\n";
140 for (
const auto& itr : m_files )
log << *( itr.second ) <<
'\n';
144 if ( m_logfile.value() !=
"" ) {
146 ofs.
open( m_logfile.value().c_str() );
148 error() <<
"Unable to open output file \"" << m_logfile.value() <<
"\" for writing" <<
endmsg;
151 debug() <<
"Saving log to \"" << m_logfile.value() <<
"\"" <<
endmsg;
152 for (
const auto& itr : m_files ) {
153 ofs << itr.second->name() <<
" " << itr.second->tech() <<
" " << itr.second->desc() <<
" " 154 << itr.second->iflags() <<
'\n';
158 for (
const auto& it2 : m_oldFiles ) fs.
insert( *it2 );
159 for (
const auto& it3 : fs ) {
160 ofs << it3.name() <<
" " << it3.tech() <<
" " << it3.desc() <<
" " << it3.iflags()
161 << ( it3.isShared() ?
" SHARED" :
"" ) <<
'\n';
176 if ( status.
isSuccess() ) debug() <<
"Service finalised successfully" <<
endmsg;
191 if ( m_handlers.find( tech ) != m_handlers.end() ) {
192 warning() <<
"Handler for IoTech " << tech <<
" already registered. Ignoring." <<
endmsg;
197 error() <<
"open handler for tech " << tech <<
" is NULL" <<
endmsg;
202 error() <<
"no close handler for tech " << tech <<
" registered" <<
endmsg;
207 error() <<
"no reopen handler for tech " << tech <<
" registered" <<
endmsg;
212 debug() <<
"Successfully registered handler for tech \"" << tech <<
"\"" <<
endmsg;
214 m_handlers[tech] = fh;
224 auto itr = m_handlers.find( tech );
225 if ( itr == m_handlers.end() ) {
226 error() <<
"Can't de-register tech " << tech <<
" as it hasn't been registered!" <<
endmsg;
230 m_handlers.erase( itr );
237 auto itr = m_handlers.find( tech );
246 return open( tech, caller, fname, desc, flags,
fd, ptr, sh );
254 return open( tech, caller, fname, desc, flags,
fd, dummy, sh );
262 return open( tech, caller, fname, desc, flags, dummy, ptr, sh );
268 const IoFlags& flags,
Fd&
fd,
void*& ptr,
bool shared ) {
286 verbose() <<
"open(" << tech <<
"," << caller <<
",\"" << fname <<
"\",\"" << desc <<
"\"," << flags
287 << ( shared ?
",shared" :
",unshared" ) <<
")" <<
endmsg;
291 if ( getHandler( tech, fh ).isFailure() )
return r;
293 auto fitr = m_files.equal_range( fname );
298 std::find_if( fitr.first, fitr.second, [&]( fileMap::const_reference i ) { return i.second->tech() != tech; } );
299 if ( itr != fitr.second ) {
300 error() <<
"when calling open on " << fname <<
" with tech " << tech <<
", file already opened with different tech " 301 << itr->second->tech() <<
endmsg;
310 bool shareable(
true );
312 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
315 if ( !fa->
isShared() ) shareable =
false;
318 if ( shareable && fa->
flags().
match( flags,
false ) ) {
321 debug() <<
" found shared file: " << *fa <<
endmsg;
342 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught " << err.
what() <<
endmsg;
345 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught an unknown exception." 351 warning() <<
"open of file \"" << fname <<
"\", tech: \"" << tech <<
"\", flags: \"" << flags
352 <<
"\" requested by " << caller <<
" failed. return code: " << r <<
endmsg;
354 FileAttr xfa( -1, fname, desc, tech, flags, 0,
false );
367 debug() <<
"opened file " << *fa <<
endmsg;
369 if (
fd == -1 && ptr == 0 ) {
370 warning() <<
"when opening " << *fa <<
" both File Descriptor" 371 <<
" and File Ptr are invalid" <<
endmsg;
375 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
378 }
else if ( *fa == *( itr->second ) ) {
379 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with identical" 380 <<
" FileAttributes: " << *fa <<
endmsg;
383 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with different" 384 <<
" FileAttributes -\n" 385 <<
"old: " << *( itr->second ) <<
'\n' 386 <<
"new: " << *fa <<
endmsg;
391 m_files.emplace( fname, fa );
394 if ( execAction( fa, caller,
Io::OPEN ).isFailure() ) {
395 warning() <<
"at least one open callback action failed" <<
endmsg;
417 [&]( fileMap::const_reference i ) {
return i.second->fd() ==
fd; } );
420 error() <<
"unknown file descriptor \"" <<
fd <<
"\" when calling close()" <<
endmsg;
424 IoTech tech = itr->second->tech();
428 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
431 error() <<
"no close(" << tech <<
",Fd) function registered" <<
endmsg;
438 auto fitr = m_files.equal_range( fa->
name() );
439 int i =
std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fd() == fd; } );
447 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with Fd " <<
fd <<
endmsg;
448 m_files.erase( itr );
452 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
454 debug() <<
"closing " << *fa <<
endmsg;
459 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd <<
" caught " << err.
what()
464 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd 465 <<
" caught an unknown exception." <<
endmsg;
471 warning() <<
"close of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
479 m_files.erase( itr );
481 }
else if ( i <= 0 ) {
483 error() <<
"ref count < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
491 m_oldFiles.push_back( fa );
494 if ( execAction( fa, caller,
Io::CLOSE ).isFailure() ) {
495 warning() <<
"at least one close callback action failed" <<
endmsg;
516 [&]( fileMap::const_reference i ) {
return i.second->fptr() == vp; } );
518 if ( itr == m_files.end() ) {
519 error() <<
"unknown file ptr \"" << vp <<
"\" when calling close()" <<
endmsg;
523 IoTech tech = itr->second->tech();
527 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
529 error() <<
"no close(" << tech <<
",void*) function registered" <<
endmsg;
539 std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fptr() == vp; } );
547 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with fptr " << vp <<
endmsg;
548 m_files.erase( itr );
552 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
554 debug() <<
"closing: " << *fa <<
endmsg;
559 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name() <<
" caught " << err.
what()
561 execAction( fa, caller,
CLOSE_ERR ).ignore();
564 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name()
565 <<
" caught an unknown exception." <<
endmsg;
566 execAction( fa, caller,
CLOSE_ERR ).ignore();
571 warning() <<
"close of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
577 m_files.erase( itr );
581 error() <<
"ref count: " << i <<
" < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
589 m_oldFiles.push_back( fa );
592 if ( execAction( fa, caller,
CLOSE ).isFailure() ) {
593 warning() <<
"at least one close callback action failed" <<
endmsg;
604 verbose() <<
"reopen(" <<
fd <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
609 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
611 if ( itr == m_files.end() ) {
612 error() <<
"unregistered FD \"" <<
fd <<
"\" when calling reopen()" <<
endmsg;
621 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
626 error() <<
"no reopen(" << tech <<
",Fd) function registered" <<
endmsg;
635 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
639 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
640 <<
" caught an unknown exception." <<
endmsg;
645 warning() <<
"reopen of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
646 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
657 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
658 warning() <<
"at least one reopen callback action failed" <<
endmsg;
668 verbose() <<
"reopen(" << vp <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
673 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
674 if ( itr == m_files.end() ) {
675 error() <<
"unregistered file ptr \"" << vp <<
"\" when calling reopen()" <<
endmsg;
683 if ( getHandler( tech, fh ).isFailure() ) {
return r; }
686 error() <<
"no reopen(" << tech <<
",void*) function registered" <<
endmsg;
693 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
697 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
698 <<
" caught an unknown exception." <<
endmsg;
703 warning() <<
"reopen of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
704 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
715 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
716 warning() <<
"at least one reopen callback action failed" <<
endmsg;
728 auto fitr = m_files.equal_range( fname );
732 [&](
const FileAttr* f ) {
return f->
name() == fname; } );
742 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
750 if ( j !=
std::end( m_oldFiles ) ) {
763 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
771 if ( j !=
std::end( m_oldFiles ) ) {
809 if ( tech ==
UNKNOWN )
return getFiles( files, op );
813 [&]( fileMap::const_reference f ) {
814 return f.second->tech() == tech &&
816 [&](
const std::string& j ) {
return j == f.first; } );
833 if ( tech ==
UNKNOWN )
return getFiles( files, op );
835 auto matches_tech = [&](
const FileAttr* f ) {
return f->
tech() == tech; };
855 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
861 [&]( fileMap::const_reference f ) {
return matches_tech_and_flags( f.second ) && not_in_files( f.first ); } );
864 [&](
const FileAttr* f ) {
return matches_tech_and_flags( f ) && not_in_files( f->
name() ); } );
875 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
880 matches_tech_and_flags );
883 matches_tech_and_flags );
897 return m_descriptors.size();
907 if ( tech ==
UNKNOWN )
return getFd(
fd );
925 return ( d.second->tech() == tech || tech ==
UNKNOWN ) && ( d.second->flags() == flags );
935 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
936 return ( itr !=
std::end( m_files ) ) ? itr->second->name() : s_empty;
943 auto itr =
std::find_if( m_files.begin(), m_files.end(),
944 [&]( fileMap::const_reference f ) {
return f.second->
fptr() == vp; } );
945 return itr != m_files.end() ? itr->second->name() : s_empty;
952 auto fitr = m_files.equal_range( fname );
953 auto itr =
std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) { return f.second->fd() != -1; } );
954 return itr != fitr.second ? itr->second->fd() : -1;
961 auto itr =
std::find_if( m_files.begin(), m_files.end(),
962 [&]( fileMap::const_reference f ) {
return f.second->
fptr() == fptr; } );
963 return itr != m_files.end() ? itr->second->fd() : -1;
969 auto fitr = m_files.equal_range( fname );
971 std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) ->
bool { return f.second->fptr(); } );
972 return itr != fitr.second ? itr->second->fptr() :
nullptr;
979 auto itr =
std::find_if( m_files.begin(), m_files.end(),
980 [&]( fileMap::const_reference f ) {
return f.second->
fd() ==
fd; } );
981 return itr != m_files.end() ? itr->second->fptr() :
nullptr;
988 info() <<
"listing registered files [" << ( m_files.size() + m_oldFiles.size() ) <<
"]:\n";
990 for (
auto& itr : m_files ) info() << itr.second <<
'\n';
991 for (
auto& it2 : m_oldFiles ) info() << *it2 <<
'\n';
1008 auto itr = m_handlers.find( tech );
1009 if ( itr == m_handlers.end() ) {
1010 error() <<
"no handler for tech " << tech <<
" registered" <<
endmsg;
1021 auto fitr = m_files.equal_range( fname );
1022 if ( fitr.first == fitr.second ) {
1023 error() <<
"no file \"" << fname <<
"\" registered. Cannot determine tech" <<
endmsg;
1027 auto itr = fitr.first;
1028 IoTech tech = itr->second->tech();
1031 while ( itr != fitr.second ) {
1032 if ( itr->second->tech() != tech ) {
1033 error() <<
"multiple technologies registered for file \"" << fname <<
"\". Cannot determine handler" <<
endmsg;
1039 return getHandler( tech, hdlr );
1046 info() <<
"Listing registered handlers:\n";
1048 for (
const auto& itr : m_handlers ) info() <<
" " << itr.first <<
'\n';
1075 info() <<
"listing registered actions\n";
1077 for (
const auto& iit : m_actions ) {
1082 info() <<
" --- Tech: ";
1084 info() <<
"ALL ---\n";
1086 info() << t <<
" ---\n";
1088 for (
const auto& iia :
m ) {
1089 for (
const auto& it2 : iia.second ) { info() <<
" " << iia.first <<
" " << it2.second <<
'\n'; }
1106 if ( itr != m_actions.end() && !itr->second.empty() ) { s1 = execActs( fa, caller, a, itr->second ); }
1108 itr = m_actions.find( tech );
1109 if ( itr != m_actions.end() && !itr->second.empty() ) { s2 = execActs( fa, caller, a, itr->second ); }
1119 auto mitr =
m.find( a );
1123 debug() <<
"executing " << mitr->second.size() <<
" " << a <<
" actions on " << *fa <<
" from " << caller <<
endmsg;
1127 auto it2 = m_supMap.find( fa->
name() );
1128 if ( it2 != m_supMap.end() ) {
1131 debug() <<
" --> suppressing callback action for " << a <<
endmsg;
1136 for (
const auto& itr : mitr->second ) {
1139 debug() <<
"executing " << itr.second <<
endmsg;
1141 if ( ( ( ( itr.first ) )( fa, caller ) ).isFailure() ) {
1142 warning() <<
"execution of " << itr.second <<
" on " << *fa <<
" failed during " << a <<
" action" <<
endmsg;
1154 verbose() <<
"accessMatch old: " << fold <<
" new: " << fnew <<
endmsg;
1172 if ( m_supMap.empty() )
return;
1174 info() <<
"listing suppressed file actions\n";
1176 for (
const auto& sup : m_supMap ) {
1177 info() <<
" " << sup.first;
1181 for (
unsigned i = 0; i != sup.second.size(); ++i ) {
1182 if ( sup.second[i] ) info() <<
" " << (
Io::Action)i;
StatusCode regHandler(FileHdlr) override
StatusCode getHandler(const IoTech &, FileHdlr &) const override
Io::Fd fd(const std::string &) const override
StatusCode initialize() override
StatusCode execActs(Io::FileAttr *, const std::string &, const Io::Action &, const actionMap &m) const
StatusCode finalize() 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
const std::string & name() const
constexpr static const auto SUCCESS
int getLastError(std::string &) const override
StatusCode deregHandler(const IoTech &) override
virtual void listSuppression() const
StatusCode hasHandler(const IoTech &) const override
#define DECLARE_COMPONENT(type)
void handle(const Incident &) override
This class is used for returning status codes from appropriate routines.
StatusCode execAction(Io::FileAttr *, const std::string &, const Io::Action &) const
Io::reopen_t reopen(const Fd, const IoFlags &, const std::string &caller) override
bfcn_closeP_t b_closeP_fcn
Io::close_t close(const Fd, const std::string &caller) override
virtual void listActions() const
bfcn_reopenP_t b_reopenP_fcn
int getFileAttr(const std::string &, std::vector< const FileAttr * > &) const override
bool accessMatch(const Io::IoFlags &, const Io::IoFlags &, bool strict=false) const
bool match(const IoFlags &fa, bool strict=true) const
StatusCode regAction(Io::bfcn_action_t, const Io::Action &, const std::string &desc="") override
const std::string & fname(const Io::Fd &) const override
T back_inserter(T... args)
void * fptr(const std::string &) const override
Base class for all Incidents (computing events).
constexpr static const auto FAILURE
int getFiles(std::vector< std::string > &, bool onlyOpen=true) const override
void listFiles() const override
void listHandlers() 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