ATLAS Offline Software
fReadDavix.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "ers/ers.h"
6 #include <fcntl.h>
7 
8 #include "fReadDavix.h"
9 #include "EventStorage/EventStorageIssues.h"
10 
11 // http://dmc-docs.web.cern.ch/dmc-docs/docs/davix-epel/html/lib-examples.html
12 // https://root.cern.ch/doc/master/TDavixFile_8cxx_source.html
13 
14 static int TDavixFile_http_authn_cert_X509(void *userdata, const Davix::SessionInfo &info, Davix::X509Credential *cert, Davix::DavixError **err) {
15 
16  (void) userdata; // keep quiete compilation warnings
17  (void) info;
18  // user proxy
19  std::string ucert, ukey;
20  if ( std::getenv("X509_USER_PROXY")) {
21  ucert = ukey = std::getenv("X509_USER_PROXY");
22  }
23 
24  if (ucert.empty() || ukey.empty()) {
25  Davix::DavixError::setupError(err, "fReadDavix.cxx",
26  Davix::StatusCode::AuthentificationError,
27  "Could not set the user's proxy or certificate");
28  return -1;
29  }
30  return cert->loadFromFilePEM(ukey, ucert, "", err);
31 }
32 
34 {
35  m_pfd = 0;
36  m_offset = 0;
37  m_fd = nullptr;
38 
39  m_davixParam = new Davix::RequestParams();
40  m_err = NULL;
41  m_pos = new Davix::DavPosix(&m_c);
42  //m_pos = &m_c;
43 
44 
45  // enableGridMode
46  const char *env_var = NULL;
47  if( ( env_var = std::getenv("X509_CERT_DIR")) == NULL){
48  env_var = "/etc/grid-security/certificates/";
49  }
50  m_davixParam->addCertificateAuthorityPath(env_var);
51  m_davixParam->setTransparentRedirectionSupport(true);
52  m_cert = new Davix::X509Credential();
53  m_davixParam->setClientCertCallbackX509(&TDavixFile_http_authn_cert_X509, NULL);
54 
55 }
56 
58 {
59  this->closeFile();
60 }
61 
63 {
64  return m_pfd != 0;
65 }
66 
68 {
69 
70  return false;
71 }
72 
73 // cppcheck-suppress passedByValue; interface defined in EventStorage.
74 bool fReadDavix::fileExists(std::string fName) const
75 {
76  Davix::DavixError* err2 = NULL;
77  DAVIX_FD* pfd = m_pos->open(m_davixParam, fName.c_str(), O_RDONLY, &err2);
78  if(pfd == 0) return false;
79  m_pos->close(pfd, &err2);
80  return true;
81 }
82 
83 // cppcheck-suppress passedByValue; interface defined in EventStorage.
84 void fReadDavix::openFile(std::string fName)
85 {
86  if(this->isOpen()) this->closeFile();
87  m_fd = m_pos->open(m_davixParam, fName.c_str(), O_RDONLY, &m_err);
88  m_offset = 0;
89  m_pfd = 1;
90 }
91 
93 {
94  if(m_pfd != 0) m_pos->close(m_fd, &m_err);
95  m_pfd = 0;
96 }
97 
98 void fReadDavix::readData(char *buffer, unsigned int sizeBytes)
99 {
100  if (sizeBytes==0) return;
101  if(this->isOpen())
102  {
103  unsigned int totalRead=0,ntry=0;
104  while(sizeBytes > totalRead)
105  {
106  ssize_t ret = m_pos->pread(m_fd, buffer, sizeBytes, m_offset, &m_err);
107  if (ret < 0) {
108  std::stringstream mystream;
109  mystream << "fReadDavix::readData: can not read data with davix " << m_err->getErrMsg().c_str() << " " << m_err->getStatus();
110  Davix::DavixError::clearError(&m_err);
111  EventStorage::ReadingIssue ci(ERS_HERE, mystream.str().c_str());
112  ers::warning(ci);
113  return;
114  } else {
115  m_offset += ret;
116  }
117  totalRead += ret; ++ntry;
118  if(ntry>5) {
119  std::stringstream mystream;
120  mystream << "Problem reading from the data file. "
121  <<"fReadDavix::readData asked to read "<<sizeBytes
122  <<" bytes and managed to read only "<<totalRead
123  <<" bytes.";
124  EventStorage::ReadingIssue ci(ERS_HERE, mystream.str().c_str());
125  ers::warning(ci);
126  return;
127  }
128  }
129  }
130 }
131 
133 {
134  if(this->isOpen()) return m_offset;
135 
136  return -1;
137 }
138 
140 {
141  if(this->isOpen()) m_offset = p;
142 }
143 
145 {
146  dav_off_t ret;
147  if(this->isOpen()) {
148  ret = m_pos->lseek64(m_fd, p, SEEK_END, &m_err);
149  m_offset = ret;
150  }
151 }
152 
153 fRead * fReadDavix::newReader() const
154 {
155  fReadDavix * nfr = new fReadDavix();
156  return (fRead *)nfr;
157 }
158 
159 extern "C" {
160  fRead * fReadFactory()
161  {
162  fReadDavix * nfr = new fReadDavix();
163  return (fRead *)nfr;
164  }
165 }
grepfile.info
info
Definition: grepfile.py:38
fReadDavix::m_fd
DAVIX_FD * m_fd
Definition: fReadDavix.h:37
fReadDavix::setPositionFromEnd
void setPositionFromEnd(int64_t p)
Definition: fReadDavix.cxx:144
fReadDavix::m_err
Davix::DavixError * m_err
Definition: fReadDavix.h:34
fReadDavix::m_offset
int64_t m_offset
Definition: fReadDavix.h:31
fReadDavix.h
rootconvert.fName
string fName
Definition: rootconvert.py:5
fReadDavix::readData
void readData(char *buffer, unsigned int sizeBytes)
Definition: fReadDavix.cxx:98
fReadDavix::fReadDavix
fReadDavix()
Definition: fReadDavix.cxx:33
fReadDavix::getPosition
int64_t getPosition()
Definition: fReadDavix.cxx:132
fReadDavix::m_pfd
int m_pfd
Definition: fReadDavix.h:30
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
fReadDavix::m_c
Davix::Context m_c
Definition: fReadDavix.h:32
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
fReadDavix::isOpen
bool isOpen()
Definition: fReadDavix.cxx:62
fReadDavix::~fReadDavix
~fReadDavix()
Definition: fReadDavix.cxx:57
fReadDavix
Definition: fReadDavix.h:13
fReadDavix::m_cert
Davix::X509Credential * m_cert
Definition: fReadDavix.h:36
fReadDavix::setPosition
void setPosition(int64_t p)
Definition: fReadDavix.cxx:139
fReadDavix::closeFile
void closeFile()
Definition: fReadDavix.cxx:92
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
fReadDavix::fileExists
bool fileExists(std::string fName) const
Definition: fReadDavix.cxx:74
fReadFactory
fRead * fReadFactory()
Definition: fReadDavix.cxx:160
fReadDavix::newReader
fRead * newReader() const
Definition: fReadDavix.cxx:153
fReadDavix::openFile
void openFile(std::string fName)
Definition: fReadDavix.cxx:84
fReadDavix::isEoF
bool isEoF()
Definition: fReadDavix.cxx:67
fReadDavix::m_davixParam
Davix::RequestParams * m_davixParam
Definition: fReadDavix.h:33