8 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) ) 9 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) ) 23 constexpr
struct to_name_t {
28 const auto select1st = [](
auto&& x ) -> decltype(
auto ) {
return std::get<0>( std::forward<decltype( x )>( x ) ); };
30 const auto select2nd = [](
auto&& x ) -> decltype(
auto ) {
return std::get<1>( std::forward<decltype( x )>( x ) ); };
32 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
33 OutputIterator transform_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
36 while ( first != last ) {
37 if ( pred( *first ) ) *result++ = op( *first );
43 template <
typename InputIterator,
typename OutputIterator,
typename UnaryOperation,
typename UnaryPredicate>
44 OutputIterator transform_copy_if( InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op,
47 while ( first != last ) {
48 auto t = op( *first );
49 if ( pred( t ) ) *result++ =
std::move( t );
73 debug() <<
"Failed to initialize the base class (Service)" <<
endmsg;
80 if ( m_loadRootHandler.value() ) {
84 msgSvc()->setOutputLevel(
"RootFileHandler", m_outputLevel.value() );
85 m_rfh.emplace(
msgSvc(), m_ssl_proxy, m_ssl_cert );
87 auto& rfh = m_rfh.value();
90 void*& ptr ) ->
Io::open_t {
return rfh.openRootFile( n, f, desc, fd, ptr ); },
91 [&rfh](
void* ptr ) ->
Io::close_t {
return rfh.closeRootFile( ptr ); },
94 if ( regHandler( hdlr ).isFailure() ) {
95 error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg;
99 if ( m_loadPosixHandler.value() ) {
103 msgSvc()->setOutputLevel(
"POSIXFileHandler", m_outputLevel.value() );
104 m_pfh.emplace(
msgSvc() );
106 auto& pfh = m_pfh.value();
109 void*& ptr ) ->
Io::open_t {
return pfh.openPOSIXFile( n, f, desc, fd, ptr ); },
113 if ( regHandler( hdlp ).isFailure() ) {
114 error() <<
"unable to register ROOT file handler with FileMgr" <<
endmsg;
128 if ( m_printSummary || msgLevel(
MSG::DEBUG ) ) {
135 if ( !m_files.empty() ) {
136 auto&
log = warning();
137 log <<
"At finalize, the following files remained open:\n";
138 for (
const auto& itr : m_files )
log << *( itr.second ) <<
'\n';
142 if ( m_logfile.value() !=
"" ) {
144 ofs.
open( m_logfile.value().c_str() );
146 error() <<
"Unable to open output file \"" << m_logfile.value() <<
"\" for writing" <<
endmsg;
149 debug() <<
"Saving log to \"" << m_logfile.value() <<
"\"" <<
endmsg;
150 for (
const auto& itr : m_files ) {
151 ofs << itr.second->name() <<
" " << itr.second->tech() <<
" " << itr.second->desc() <<
" " 152 << itr.second->iflags() <<
'\n';
156 for (
const auto& it2 : m_oldFiles ) fs.
insert( *it2 );
157 for (
const auto& it3 : fs ) {
158 ofs << it3.name() <<
" " << it3.tech() <<
" " << it3.desc() <<
" " << it3.iflags()
159 << ( it3.isShared() ?
" SHARED" :
"" ) <<
'\n';
174 if ( status.
isSuccess() ) debug() <<
"Service finalised successfully" <<
endmsg;
190 if ( m_handlers.find( tech ) != m_handlers.end() ) {
191 warning() <<
"Handler for IoTech " << tech <<
" already registered. Ignoring." <<
endmsg;
196 error() <<
"open handler for tech " << tech <<
" is NULL" <<
endmsg;
201 error() <<
"no close handler for tech " << tech <<
" registered" <<
endmsg;
206 error() <<
"no reopen handler for tech " << tech <<
" registered" <<
endmsg;
211 debug() <<
"Successfully registered handler for tech \"" << tech <<
"\"" <<
endmsg;
213 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 );
238 auto itr = m_handlers.find( tech );
248 return open( tech, caller, fname, desc, flags, fd, ptr, sh );
257 return open( tech, caller, fname, desc, flags, fd, dummy, sh );
266 return open( tech, caller, fname, desc, flags, dummy, ptr, sh );
272 const IoFlags& flags,
Fd&
fd,
void*& ptr,
bool shared )
291 verbose() <<
"open(" << tech <<
"," << caller <<
",\"" << fname <<
"\",\"" << desc <<
"\"," << flags
292 << ( shared ?
",shared" :
",unshared" ) <<
")" <<
endmsg;
296 if ( getHandler( tech, fh ).isFailure() )
return r;
298 auto fitr = m_files.equal_range( fname );
303 std::find_if( fitr.first, fitr.second, [&]( fileMap::const_reference i ) { return i.second->tech() != tech; } );
304 if ( itr != fitr.second ) {
305 error() <<
"when calling open on " << fname <<
" with tech " << tech <<
", file already opened with different tech " 306 << itr->second->tech() <<
endmsg;
315 bool shareable(
true );
317 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
320 if ( !fa->
isShared() ) shareable =
false;
323 if ( shareable && fa->
flags().
match( flags,
false ) ) {
326 debug() <<
" found shared file: " << *fa <<
endmsg;
347 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught " << err.
what() <<
endmsg;
350 error() <<
"when calling open handler for " << tech <<
" on file " << fname <<
" caught an unknown exception." 356 warning() <<
"open of file \"" << fname <<
"\", tech: \"" << tech <<
"\", flags: \"" << flags
357 <<
"\" requested by " << caller <<
" failed. return code: " << r <<
endmsg;
359 FileAttr xfa( -1, fname, desc, tech, flags, 0,
false );
372 debug() <<
"opened file " << *fa <<
endmsg;
374 if (
fd == -1 && ptr == 0 ) {
375 warning() <<
"when opening " << *fa <<
" both File Descriptor" 376 <<
" and File Ptr are invalid" <<
endmsg;
380 for (
auto itr = fitr.first; itr != fitr.second; ++itr ) {
383 }
else if ( *fa == *( itr->second ) ) {
384 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with identical" 385 <<
" FileAttributes: " << *fa <<
endmsg;
388 warning() <<
"open call for file \"" << fname <<
"\" returned a pre-existing file with different" 389 <<
" FileAttributes -\n" 390 <<
"old: " << *( itr->second ) <<
'\n' 391 <<
"new: " << *fa << endmsg;
396 m_files.emplace( fname, fa );
399 if ( execAction( fa, caller,
Io::OPEN ).isFailure() ) {
400 warning() <<
"at least one open callback action failed" <<
endmsg;
423 [&]( fileMap::const_reference i ) {
return i.second->fd() ==
fd; } );
426 error() <<
"unknown file descriptor \"" <<
fd <<
"\" when calling close()" <<
endmsg;
430 IoTech tech = itr->second->tech();
434 if ( getHandler( tech, fh ).isFailure() ) {
439 error() <<
"no close(" << tech <<
",Fd) function registered" <<
endmsg;
446 auto fitr = m_files.equal_range( fa->
name() );
447 int i =
std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fd() == fd; } );
455 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with Fd " <<
fd <<
endmsg;
456 m_files.erase( itr );
460 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
462 debug() <<
"closing " << *fa <<
endmsg;
467 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd <<
" caught " << err.
what()
472 error() <<
"when calling close handler for " << tech <<
" on file descriptor " <<
fd 473 <<
" caught an unknown exception." <<
endmsg;
479 warning() <<
"close of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
487 m_files.erase( itr );
489 }
else if ( i <= 0 ) {
491 error() <<
"ref count < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
499 m_oldFiles.push_back( fa );
502 if ( execAction( fa, caller,
Io::CLOSE ).isFailure() ) {
503 warning() <<
"at least one close callback action failed" <<
endmsg;
525 [&]( fileMap::const_reference i ) {
return i.second->fptr() == vp; } );
527 if ( itr == m_files.end() ) {
528 error() <<
"unknown file ptr \"" << vp <<
"\" when calling close()" <<
endmsg;
532 IoTech tech = itr->second->tech();
536 if ( getHandler( tech, fh ).isFailure() ) {
540 error() <<
"no close(" << tech <<
",void*) function registered" <<
endmsg;
550 std::count_if( fitr.first, fitr.second, [&]( fileMap::const_reference f ) { return f.second->fptr() == vp; } );
558 debug() <<
"closing file " << fa->
name() <<
" opened " << i <<
" times with fptr " << vp <<
endmsg;
559 m_files.erase( itr );
563 }
else if ( i == 1 || ( i > 1 && !fa->
isShared() ) ) {
565 debug() <<
"closing: " << *fa <<
endmsg;
570 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name() <<
" caught " << err.
what()
572 execAction( fa, caller,
CLOSE_ERR ).ignore();
575 error() <<
"when calling close handler for " << tech <<
" on file " << fa->
name()
576 <<
" caught an unknown exception." <<
endmsg;
577 execAction( fa, caller,
CLOSE_ERR ).ignore();
582 warning() <<
"close of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
588 m_files.erase( itr );
592 error() <<
"ref count: " << i <<
" < 0 when closing " << fa <<
". This should never happen" <<
endmsg;
600 m_oldFiles.push_back( fa );
603 if ( execAction( fa, caller,
CLOSE ).isFailure() ) {
604 warning() <<
"at least one close callback action failed" <<
endmsg;
616 verbose() <<
"reopen(" <<
fd <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
621 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
623 if ( itr == m_files.end() ) {
624 error() <<
"unregistered FD \"" <<
fd <<
"\" when calling reopen()" <<
endmsg;
633 if ( getHandler( tech, fh ).isFailure() ) {
640 error() <<
"no reopen(" << tech <<
",Fd) function registered" <<
endmsg;
649 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
653 error() <<
"when calling reopen handler for " << tech <<
" on file descriptor " <<
fd <<
" with flags " << flags
654 <<
" caught an unknown exception." <<
endmsg;
659 warning() <<
"reopen of file with FD \"" <<
fd <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
660 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
671 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
672 warning() <<
"at least one reopen callback action failed" <<
endmsg;
683 verbose() <<
"reopen(" << vp <<
"," << flags <<
"," << caller <<
")" <<
endmsg;
688 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
689 if ( itr == m_files.end() ) {
690 error() <<
"unregistered file ptr \"" << vp <<
"\" when calling reopen()" <<
endmsg;
698 if ( getHandler( tech, fh ).isFailure() ) {
703 error() <<
"no reopen(" << tech <<
",void*) function registered" <<
endmsg;
710 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
714 error() <<
"when calling reopen handler for " << tech <<
" on file " << fa->
name() <<
" with flags " << flags
715 <<
" caught an unknown exception." <<
endmsg;
720 warning() <<
"reopen of file with ptr \"" << vp <<
"\", name: \"" << fa->
name() <<
"\", tech: \"" << tech
721 <<
"\", flags: \"" << flags <<
"\" failed" <<
endmsg;
732 if ( execAction( fa, caller,
Io::REOPEN ).isFailure() ) {
733 warning() <<
"at least one reopen callback action failed" <<
endmsg;
746 auto fitr = m_files.equal_range( fname );
750 [&](
const FileAttr* f ) {
return f->
name() == fname; } );
761 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
769 if ( j !=
std::end( m_oldFiles ) ) {
783 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
791 if ( j !=
std::end( m_oldFiles ) ) {
834 if ( tech ==
UNKNOWN )
return getFiles( files, op );
838 [&]( fileMap::const_reference f ) {
839 return f.second->tech() == tech &&
841 [&](
const std::string& j ) {
return j == f.first; } );
859 if ( tech ==
UNKNOWN )
return getFiles( files, op );
861 auto matches_tech = [&](
const FileAttr* f ) {
return f->tech() == tech; };
882 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
883 return ( f->tech() == tech || tech ==
UNKNOWN ) && f->flags() == flags;
888 [&]( fileMap::const_reference f ) {
return matches_tech_and_flags( f.second ) && not_in_files( f.first ); } );
891 [&](
const FileAttr* f ) {
return matches_tech_and_flags( f ) && not_in_files( f->
name() ); } );
903 auto matches_tech_and_flags = [&](
const FileAttr* f ) {
904 return ( f->tech() == tech || tech ==
UNKNOWN ) && f->
flags() == flags;
908 matches_tech_and_flags );
911 matches_tech_and_flags );
926 return m_descriptors.size();
937 if ( tech ==
UNKNOWN )
return getFd( fd );
954 transform_if( m_descriptors.begin(), m_descriptors.end(),
std::back_inserter( fd ), select1st,
956 return ( d.second->tech() == tech || tech ==
UNKNOWN ) && ( d.second->flags() == flags );
967 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
968 return ( itr !=
std::end( m_files ) ) ? itr->second->name() : s_empty;
976 auto itr =
std::find_if( m_files.begin(), m_files.end(),
977 [&]( fileMap::const_reference f ) {
return f.second->fptr() == vp; } );
978 return itr != m_files.end() ? itr->second->name() : s_empty;
986 auto fitr = m_files.equal_range( fname );
987 auto itr =
std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) { return f.second->fd() != -1; } );
988 return itr != fitr.second ? itr->second->fd() : -1;
996 auto itr =
std::find_if( m_files.begin(), m_files.end(),
997 [&]( fileMap::const_reference f ) {
return f.second->fptr() == fptr; } );
998 return itr != m_files.end() ? itr->second->fd() : -1;
1005 auto fitr = m_files.equal_range( fname );
1007 std::find_if( fitr.first, fitr.second, []( fileMap::const_reference f ) ->
bool { return f.second->fptr(); } );
1008 return itr != fitr.second ? itr->second->fptr() :
nullptr;
1016 auto itr =
std::find_if( m_files.begin(), m_files.end(),
1017 [&]( fileMap::const_reference f ) {
return f.second->fd() ==
fd; } );
1018 return itr != m_files.end() ? itr->second->fptr() :
nullptr;
1026 info() <<
"listing registered files [" << ( m_files.size() + m_oldFiles.size() ) <<
"]:\n";
1028 for (
auto& itr : m_files ) info() << itr.second <<
'\n';
1029 for (
auto& it2 : m_oldFiles ) info() << *it2 <<
'\n';
1048 auto itr = m_handlers.find( tech );
1049 if ( itr == m_handlers.end() ) {
1050 error() <<
"no handler for tech " << tech <<
" registered" <<
endmsg;
1062 auto fitr = m_files.equal_range( fname );
1063 if ( fitr.first == fitr.second ) {
1064 error() <<
"no file \"" << fname <<
"\" registered. Cannot determine tech" <<
endmsg;
1068 auto itr = fitr.first;
1069 IoTech tech = itr->second->tech();
1072 while ( itr != fitr.second ) {
1073 if ( itr->second->tech() != tech ) {
1074 error() <<
"multiple technologies registered for file \"" << fname <<
"\". Cannot determine handler" <<
endmsg;
1080 return getHandler( tech, hdlr );
1088 info() <<
"Listing registered handlers:\n";
1090 for (
const auto& itr : m_handlers ) info() <<
" " << itr.first <<
'\n';
1120 info() <<
"listing registered actions\n";
1122 for (
const auto& iit : m_actions ) {
1127 info() <<
" --- Tech: ";
1129 info() <<
"ALL ---\n";
1131 info() << t <<
" ---\n";
1133 for (
const auto& iia : m ) {
1134 for (
const auto& it2 : iia.second ) {
1135 info() <<
" " << iia.first <<
" " << it2.second <<
'\n';
1154 if ( itr != m_actions.end() && !itr->second.empty() ) {
1155 s1 = execActs( fa, caller, a, itr->second );
1158 itr = m_actions.find( tech );
1159 if ( itr != m_actions.end() && !itr->second.empty() ) {
1160 s2 = execActs( fa, caller, a, itr->second );
1172 auto mitr = m.
find( a );
1176 debug() <<
"executing " << mitr->second.size() <<
" " << a <<
" actions on " << *fa <<
" from " << caller <<
endmsg;
1180 auto it2 = m_supMap.find( fa->
name() );
1181 if ( it2 != m_supMap.end() ) {
1184 debug() <<
" --> suppressing callback action for " << a <<
endmsg;
1189 for (
const auto& itr : mitr->second ) {
1192 debug() <<
"executing " << itr.second <<
endmsg;
1194 if ( ( ( ( itr.first ) )( fa, caller ) ).isFailure() ) {
1195 warning() <<
"execution of " << itr.second <<
" on " << *fa <<
" failed during " << a <<
" action" <<
endmsg;
1208 verbose() <<
"accessMatch old: " << fold <<
" new: " << fnew <<
endmsg;
1211 ( ( fold &
Io::WRITE ) != 0 && ( fnew & Io::WRITE ) != 0 ) ||
1212 ( ( fold &
Io::RDWR ) != 0 && ( fnew & Io::RDWR ) != 0 ) );
1227 if ( m_supMap.empty() )
return;
1229 info() <<
"listing suppressed file actions\n";
1231 for (
const auto& sup : m_supMap ) {
1232 info() <<
" " << sup.first;
1236 for (
unsigned i = 0; i != sup.second.size(); ++i ) {
1237 if ( sup.second[i] ) info() <<
" " << (
Io::Action)i;
StatusCode regHandler(FileHdlr) override
constexpr static const auto FAILURE
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
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
constexpr static const auto SUCCESS
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