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