All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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"
9 
10 
11 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
14  const std::string& c):
15  m_log(msg,"RootFileHandler"), m_userProxy(p), m_certDir(c),
16  m_ssl_setup(false) {
17 
18  // Protect against multiple instances of TROOT
19  if ( 0 == gROOT ) {
20  static TROOT root("root","ROOT I/O");
21  }
22  m_level = msg->outputLevel("RootFileHandler");
23 
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 = 0;
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  TFile *tf(0);
61 
62  if (n.find("https://") != std::string::npos ||
63  n.find("HTTPS://") != std::string::npos ||
64  n.find("http://") != std::string::npos ||
65  n.find("HTTP://") != std::string::npos ) {
66 
67 
68  if ( !f.isRead() ) {
69  m_log << MSG::ERROR << "can only open web files in READ mode. "
70  << "requested mode is: " << f
71  << endmsg;
72  return 1;
73  }
74 
75  if (!m_ssl_setup && (n.find("https://") != std::string::npos ||
76  n.find("HTTPS://") != std::string::npos )) {
77 
78  if (!setupSSL()) {
79  m_log << MSG::ERROR
80  << "Unable to setup TSSLSocket for ROOT TWebFile access over https"
81  << endmsg;
82  }
83  }
84 
85 
86  try {
87  tf = new TWebFile(n.c_str());
88  } catch (const std::exception& Exception) {
89  m_log << MSG::ERROR << "exception caught while trying to open root"
90  << " file for reading: " << Exception.what() << std::endl
91  << " -> file probably corrupt." << endmsg;
92  return 1;
93  } catch (...) {
94  m_log << MSG::ERROR << "Problems opening input file \"" << n
95  << "\": probably corrupt" << endmsg;
96  return 1;
97  }
98 
99  if (tf != 0 && tf->IsZombie()) {
100  m_log << MSG::ERROR << "Problems opening input file \"" << n
101  << "\": file does not exist or in not accessible" << endmsg;
102  delete tf;
103  tf = 0;
104  return 1;
105  }
106 
107 
108  } else {
109 
110  try {
111  tf = TFile::Open(n.c_str(),opt.c_str());
112  } catch (const std::exception& Exception) {
113  m_log << MSG::ERROR << "exception caught while trying to open root"
114  << " file for reading: " << Exception.what() << std::endl
115  << " -> file probably corrupt." << endmsg;
116  return 1;
117  } catch (...) {
118  m_log << MSG::ERROR << "Problems opening input file \"" << n
119  << "\": probably corrupt" << endmsg;
120  return 1;
121  }
122 
123  }
124 
125  if (tf == 0 || !tf->IsOpen()) {
126  m_log << MSG::ERROR << "Unable to open ROOT file \"" << n
127  << "\" with options \"" << opt << "\"" << endmsg;
128 
129  delete tf;
130  tf = 0;
131  return 1;
132  }
133 
134  fd = tf->GetFd();
135 
136  ptr = (void*) tf;
137 
138  if (m_log.level() <= MSG::DEBUG)
139  m_log << MSG::DEBUG << "opened TFile " << (void*) ptr << " Fd: " << fd
140  << endmsg;
141 
142 
143  return 0;
144 
145 }
146 
147 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
148 
151 
152  if (m_log.level() <= MSG::DEBUG)
153  m_log << MSG::DEBUG << "closeRootFile(ptr:" << ptr << ")"
154  << endmsg;
155 
156  if (ptr == 0) {
157  m_log << MSG::ERROR << "Unable to close file: ptr == 0"
158  << endmsg;
159  return -1;
160  }
161 
162  TFile* tf = (TFile*) ptr;
163 
164  try {
165  tf->Close();
166  } catch (const std::exception& Exception) {
167  m_log << MSG::ERROR << "exception caught while trying to close root"
168  << " file" << Exception.what()
169  << endmsg;
170  return -1;
171  } catch (...) {
172  m_log << MSG::ERROR << "Problems closing ROOT file \"" << tf->GetName()
173  << "\"" << endmsg;
174  return -1;
175  }
176 
177  return 0;
178 
179 }
180 
181 
182 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
183 
186 
187  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
188 
189  return -1;
190 
191 }
192 
193 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
194 
195 bool
197 
198  if (m_log.level() <= MSG::DEBUG)
199  m_log << MSG::DEBUG << "setupSSL"
200  << endmsg;
201 
202  // don't set anything up
203  if (m_userProxy == "NONE" || m_certDir == "NONE") {
204  m_ssl_setup = true;
205  return true;
206  }
207 
208  // get stuff from $X509_USER_PROXY and $X509_CERT_DIR env vars
209  if (m_userProxy == "X509") {
210  if (!System::getEnv("X509_USER_PROXY", m_userProxy)) {
211  m_log << MSG::ERROR << "env var X509_USER_PROXY not set" << endmsg;
212  return false;
213  }
214  }
215 
216  if (m_certDir == "X509") {
217  if (!System::getEnv("X509_CERT_DIR", m_certDir)) {
218  m_log << MSG::ERROR << "env var X509_CERT_DIR not set" << endmsg;
219  return false;
220  }
221  }
222 
223  if (m_log.level() <= MSG::DEBUG)
224  m_log << MSG::DEBUG << "userProxy: " << m_userProxy
225  << " certDir: " << m_certDir
226  << endmsg;
227 
228 
229  TSSLSocket::SetUpSSL(m_userProxy.c_str(), m_certDir.c_str(),
230  m_userProxy.c_str(), m_userProxy.c_str());
231 
232  m_ssl_setup = true;
233 
234  return true;
235 
236 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:608
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
int reopen_t
Definition: IFileMgr.h:280
int Fd
Definition: IFileMgr.h:197
tuple c
Definition: gaudirun.py:341
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:278
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
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
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
int close_t
Definition: IFileMgr.h:279
RootFileHandler(IMessageSvc *, const std::string &userProxy, const std::string &certDir)