| The Gaudi Framework v32r1 (f65d50dc)

RootFileHandler.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IFileMgr.h"
2 #include "TFile.h"
3 #include "TROOT.h"
4 #include "TSSLSocket.h"
5 #include "TWebFile.h"
6
7 #include "GaudiKernel/MsgStream.h"
8 #include "RootFileHandler.h"
9 #include "boost/algorithm/string.hpp"
10
11 namespace ba = boost::algorithm;
12
13 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
14
15 RootFileHandler::RootFileHandler( IMessageSvc* msg, const std::string& p, const std::string& c )
16 : m_log( msg, "RootFileHandler" ), m_userProxy( p ), m_certDir( c ) {
17 // Protect against multiple instances of TROOT
18 if ( !gROOT ) { static TROOT root( "root", "ROOT I/O" ); }
19 m_level = msg->outputLevel( "RootFileHandler" );
20 }
21
22 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23
24 Io::open_t RootFileHandler::openRootFile( const std::string& n, const Io::IoFlags& f, const std::string& desc,
25 Io::Fd& fd, void*& ptr ) {
26
27 m_log.setLevel( m_level );
28
29 if ( m_log.level() <= MSG::DEBUG )
30 m_log << MSG::DEBUG << "openRootFile(\"" << n << "\"," << f << "," << desc << ")" << endmsg;
31
32 ptr = nullptr;
33 fd = -1;
34
35 std::string opt;
36
37 if ( f == Io::READ ) {
38 opt = "READ";
39 } else if ( f == ( Io::WRITE | Io::CREATE | Io::EXCL ) ) {
40 opt = "NEW";
41 } else if ( f == ( Io::WRITE | Io::CREATE ) ) {
42 opt = "RECREATE";
43 } else if ( ( f | Io::APPEND ) != 0 ) {
44 opt = "UPDATE";
45 } else {
46 m_log << MSG::ERROR << "Don't know how to handle IoFlag " << f << endmsg;
47 return 1;
48 }
49
50 std::unique_ptr<TFile> tf;
51
52 if ( ba::starts_with( n, "https://", ba::is_iequal{} ) || ba::starts_with( n, "http://", ba::is_iequal{} ) ) {
53
54 if ( !f.isRead() ) {
55 m_log << MSG::ERROR << "can only open web files in READ mode. "
56 << "requested mode is: " << f << endmsg;
57 return 1;
58 }
59
60 if ( !m_ssl_setup && ba::starts_with( n, "https://", ba::is_iequal{} ) ) {
61
62 if ( !setupSSL() ) {
63 m_log << MSG::ERROR << "Unable to setup TSSLSocket for ROOT TWebFile access over https" << endmsg;
64 }
65 }
66
67 try {
68 tf.reset( new TWebFile( n.c_str() ) );
69 } catch ( const std::exception& Exception ) {
70 m_log << MSG::ERROR << "exception caught while trying to open root"
71 << " file for reading: " << Exception.what() << std::endl
72 << " -> file probably corrupt." << endmsg;
73 return 1;
74 } catch ( ... ) {
75 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": probably corrupt" << endmsg;
76 return 1;
77 }
78
79 if ( tf && tf->IsZombie() ) {
80 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": file does not exist or in not accessible"
81 << endmsg;
82 tf.reset();
83 return 1;
84 }
85
86 } else {
87
88 try {
89 tf.reset( TFile::Open( n.c_str(), opt.c_str() ) );
90 } catch ( const std::exception& Exception ) {
91 m_log << MSG::ERROR << "exception caught while trying to open root"
92 << " file for reading: " << Exception.what() << std::endl
93 << " -> file probably corrupt." << endmsg;
94 return 1;
95 } catch ( ... ) {
96 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": probably corrupt" << endmsg;
97 return 1;
98 }
99 }
100
101 if ( !tf || !tf->IsOpen() ) {
102 m_log << MSG::ERROR << "Unable to open ROOT file \"" << n << "\" with options \"" << opt << "\"" << endmsg;
103
104 tf.reset();
105 return 1;
106 }
107
108 fd = tf->GetFd();
109
110 ptr = tf.release();
111
112 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "opened TFile " << ptr << " Fd: " << fd << endmsg;
113
114 return 0;
115 }
116
117 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
118
119 Io::close_t RootFileHandler::closeRootFile( void* ptr ) {
120
121 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "closeRootFile(ptr:" << ptr << ")" << endmsg;
122
123 if ( !ptr ) {
124 m_log << MSG::ERROR << "Unable to close file: ptr == 0" << endmsg;
125 return -1;
126 }
127
128 TFile* tf = static_cast<TFile*>( ptr );
129
130 try {
131 tf->Close();
132 } catch ( const std::exception& Exception ) {
133 m_log << MSG::ERROR << "exception caught while trying to close root"
134 << " file" << Exception.what() << endmsg;
135 return -1;
136 } catch ( ... ) {
137 m_log << MSG::ERROR << "Problems closing ROOT file \"" << tf->GetName() << "\"" << endmsg;
138 return -1;
139 }
140
141 return 0;
142 }
143
144 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
145
146 Io::reopen_t RootFileHandler::reopenRootFile( void*, const Io::IoFlags& ) {
147
148 m_log << MSG::ERROR << "reopen not implemented" << endmsg;
149 return -1;
150 }
151
152 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
153
154 bool RootFileHandler::setupSSL() {
155
156 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "setupSSL" << endmsg;
157
158 // don't set anything up
159 if ( m_userProxy == "NONE" || m_certDir == "NONE" ) {
160 m_ssl_setup = true;
161 return true;
162 }
163
164 // get stuff from $X509_USER_PROXY and $X509_CERT_DIR env vars
165 if ( m_userProxy == "X509" ) {
166 if ( !System::getEnv( "X509_USER_PROXY", m_userProxy ) ) {
167 m_log << MSG::ERROR << "env var X509_USER_PROXY not set" << endmsg;
168 return false;
169 }
170 }
171
172 if ( m_certDir == "X509" ) {
173 if ( !System::getEnv( "X509_CERT_DIR", m_certDir ) ) {
174 m_log << MSG::ERROR << "env var X509_CERT_DIR not set" << endmsg;
175 return false;
176 }
177 }
178
179 if ( m_log.level() <= MSG::DEBUG )
180 m_log << MSG::DEBUG << "userProxy: " << m_userProxy << " certDir: " << m_certDir << endmsg;
181
182 TSSLSocket::SetUpSSL( m_userProxy.c_str(), m_certDir.c_str(), m_userProxy.c_str(), m_userProxy.c_str() );
183
184 m_ssl_setup = true;
185
186 return true;
187 }
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:369
gaudirun.fd
fd
Definition: gaudirun.py:559
gaudirun.c
c
Definition: gaudirun.py:487
MsgStream.h
Io::reopen_t
int reopen_t
Definition: IFileMgr.h:235
Io::Fd
int Fd
Definition: IFileMgr.h:159
MSG::DEBUG
Definition: IMessageSvc.h:15
std::endl
T endl(T... args)
RootFileHandler::setupSSL
bool setupSSL()
Definition: RootFileHandler.cpp:154
RootFileHandler::openRootFile
Io::open_t openRootFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
Definition: RootFileHandler.cpp:24
Io::open_t
int open_t
Definition: IFileMgr.h:233
Io::EXCL
Definition: IFileMgr.h:32
RootFileHandler::closeRootFile
Io::close_t closeRootFile(void *ptr)
Definition: RootFileHandler.cpp:119
std::unique_ptr::release
T release(T... args)
RootFileHandler::reopenRootFile
Io::reopen_t reopenRootFile(void *, const Io::IoFlags &)
Definition: RootFileHandler.cpp:146
std::string
STL class.
RootFileHandler::m_userProxy
std::string m_userProxy
Definition: RootFileHandler.h:34
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
RootFileHandler::m_certDir
std::string m_certDir
Definition: RootFileHandler.h:34
std::exception::what
T what(T... args)
RootFileHandler::m_ssl_setup
bool m_ssl_setup
Definition: RootFileHandler.h:35
Io::WRITE
Definition: IFileMgr.h:28
Io::IoFlags
Definition: IFileMgr.h:40
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
Io::READ
Definition: IFileMgr.h:27
RootFileHandler::m_level
int m_level
Definition: RootFileHandler.h:32
std::unique_ptr::reset
T reset(T... args)
std::exception
STL class.
Io::APPEND
Definition: IFileMgr.h:35
std::unique_ptr< TFile >
Io::CREATE
Definition: IFileMgr.h:31
Io::IoFlags::isRead
bool isRead() const
Definition: IFileMgr.h:55
std::string::c_str
T c_str(T... args)
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
MSG::ERROR
Definition: IMessageSvc.h:15
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:27
IFileMgr.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Io::close_t
int close_t
Definition: IFileMgr.h:234
RootFileHandler.h
RootFileHandler::RootFileHandler
RootFileHandler(IMessageSvc *, const std::string &userProxy, const std::string &certDir)
Definition: RootFileHandler.cpp:15
RootFileHandler::m_log
MsgStream m_log
Definition: RootFileHandler.h:31