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