Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
8 #include "RootFileHandler.h"
9 #include "boost/algorithm/string.hpp"
10 
11 namespace ba = boost::algorithm;
12 
13 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
14 
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 
25  Io::Fd& fd, void*& ptr ) {
26 
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 
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 
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 
147 
148  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
149  return -1;
150 }
151 
152 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
153 
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 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:380
int reopen_t
Definition: IFileMgr.h:235
int Fd
Definition: IFileMgr.h:159
T endl(T...args)
Io::open_t openRootFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
int open_t
Definition: IFileMgr.h:233
Io::close_t closeRootFile(void *ptr)
T release(T...args)
Io::reopen_t reopenRootFile(void *, const Io::IoFlags &)
STL class.
std::string m_userProxy
std::string m_certDir
T what(T...args)
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
T reset(T...args)
STL class.
T c_str(T...args)
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
bool isRead() const
Definition: IFileMgr.h:55
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
int close_t
Definition: IFileMgr.h:234
RootFileHandler(IMessageSvc *, const std::string &userProxy, const std::string &certDir)
virtual int outputLevel() const =0
Retrieve the current output level threshold.