ATLAS Offline Software
Loading...
Searching...
No Matches
fReadDavix.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
14static 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.
74bool 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.
84void 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
98void 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
154{
155 fReadDavix * nfr = new fReadDavix();
156 return nfr;
157}
158
159extern "C" {
160 fRead * fReadFactory()
161 {
162 fReadDavix * nfr = new fReadDavix();
163 return nfr;
164 }
165}
bool fileExists(std::string fName) const
void setPosition(int64_t p)
bool isEoF()
DAVIX_FD * m_fd
Definition fReadDavix.h:37
bool isOpen()
Davix::Context m_c
Definition fReadDavix.h:32
Davix::RequestParams * m_davixParam
Definition fReadDavix.h:33
void closeFile()
int64_t getPosition()
void openFile(std::string fName)
fRead * newReader() const
Davix::X509Credential * m_cert
Definition fReadDavix.h:36
void setPositionFromEnd(int64_t p)
void readData(char *buffer, unsigned int sizeBytes)
int64_t m_offset
Definition fReadDavix.h:31
Davix::DavixError * m_err
Definition fReadDavix.h:34
static int TDavixFile_http_authn_cert_X509(void *userdata, const Davix::SessionInfo &info, Davix::X509Credential *cert, Davix::DavixError **err)
fRead * fReadFactory()