16 using namespace Davix;
21 open(name, flags, perms);
25 open(name.c_str(),
flags, perms);
34 if (m_davixPosix && m_fd) {
35 auto davixPosix =
std::move(m_davixPosix);
36 DavixError *
err =
nullptr;
37 davixPosix->close(m_fd, &err);
40 std::unique_ptr<DavixError> davixErrManaged(err);
42 ex <<
"Davix::close(name='" << m_name <<
") failed with error " << err->getErrMsg().c_str() <<
" and error code " 52 if (m_davixPosix && m_fd) {
53 DavixError *
err =
nullptr;
54 m_davixPosix->close(m_fd, &err);
56 std::unique_ptr<DavixError> davixErrManaged(err);
58 ex <<
"Davix::abort(name='" << m_name <<
") failed with error " << err->getErrMsg().c_str() <<
" and error code " 69 char *logptr =
nullptr;
70 char const *
const davixDebug = std::getenv(
"Davix_Debug");
71 if (davixDebug !=
nullptr) {
72 logLevel = strtol(davixDebug, &logptr, 0);
75 <<
"Davix_Debug env variable to integer. " 76 "Will use default log level 0";
79 if (logptr == davixDebug) {
81 <<
"Davix_Debug env variable; Will use default log level 0";
83 }
else if (*logptr !=
'\0') {
85 <<
"from Davix_Debug env variable. Will use default log level 0";
94 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_WARNING);
97 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_VERBOSE);
100 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_DEBUG);
103 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_ALL);
110 char default_proxy[64];
111 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d", geteuid());
113 if (std::getenv(
"X509_USER_PROXY")) {
114 edm::LogInfo(
"DavixFile") <<
"X509_USER_PROXY found in environment." 115 <<
" Will use it for authentication";
116 ucert = ukey = std::getenv(
"X509_USER_PROXY");
119 else if (access(default_proxy, R_OK) == 0) {
120 edm::LogInfo(
"DavixFile") <<
"Found proxy in default location " << default_proxy
121 <<
" Will use it for authentication";
122 ucert = ukey = default_proxy;
125 else if (std::getenv(
"X509_USER_CERT")) {
126 ucert = std::getenv(
"X509_USER_CERT");
129 if (!ucert.empty() && std::getenv(
"X509_USER_KEY")) {
130 edm::LogInfo(
"DavixFile") <<
"X509_USER_{CERT|KEY} found in environment" 131 <<
" Will use it for authentication";
132 ukey = std::getenv(
"X509_USER_KEY");
135 if (ucert.empty() || ukey.empty()) {
136 edm::LogWarning(
"DavixFile") <<
"Was not able to find proxy in $X509_USER_PROXY, " 137 <<
"X509_USER_{CERT|KEY} or default proxy creation location. " 138 <<
"Will try without authentication";
141 return cert->loadFromFilePEM(ukey, ucert,
"", davixErr);
157 open(name.c_str(),
flags, perms);
162 if ((name ==
nullptr) || (*name == 0)) {
164 ex <<
"Cannot open a file without name";
172 ex <<
"Must open file '" << name <<
"' at least for read";
177 if (m_davixPosix && m_fd) {
179 ex <<
"Davix::open(name='" << m_name <<
"') failed on already open file";
183 configureDavixLogLevel();
187 if (flags & IOFlags::OpenRead)
188 openflags |= O_RDONLY;
190 DavixError *davixErr =
nullptr;
191 RequestParams davixReqParams;
195 const char *cert_dir =
nullptr;
196 if ((cert_dir = std::getenv(
"X509_CERT_DIR")) ==
nullptr)
197 cert_dir =
"/etc/grid-security/certificates";
198 davixReqParams.addCertificateAuthorityPath(cert_dir);
200 m_davixPosix = std::make_unique<DavPosix>(
new Context());
201 m_fd = m_davixPosix->open(&davixReqParams, name, openflags, &davixErr);
205 std::unique_ptr<DavixError> davixErrManaged(davixErr);
207 ex <<
"Davix::open(name='" << m_name <<
"') failed with " 208 <<
"error '" << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus();
214 ex <<
"Davix::open(name='" << m_name <<
"') failed as fd is NULL";
221 assert(!buffers || into);
227 DavixError *davixErr =
nullptr;
229 std::vector<DavIOVecInput> input_vector(buffers);
230 std::vector<DavIOVecOuput> output_vector(buffers);
233 input_vector[
i].diov_size = into[
i].
size();
234 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
235 total += into[
i].
size();
238 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
240 std::unique_ptr<DavixError> davixErrManaged(davixErr);
242 ex <<
"Davix::readv(name='" << m_name <<
"', buffers=" << (buffers) <<
") failed with error " 243 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and call returned " << s
256 ex <<
"Davix::readv(name='" << m_name <<
"') failed and call returned " <<
s;
267 assert(!buffers || into);
273 DavixError *davixErr =
nullptr;
275 std::vector<DavIOVecInput> input_vector(buffers);
276 std::vector<DavIOVecOuput> output_vector(buffers);
279 input_vector[
i].diov_offset = into[
i].
offset();
280 input_vector[
i].diov_size = into[
i].
size();
281 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
282 total += into[
i].
size();
284 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
286 std::unique_ptr<DavixError> davixErrManaged(davixErr);
288 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers <<
") failed with error " 289 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and call returned " << s
302 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers <<
") failed and call returned " <<
s;
313 DavixError *davixErr =
nullptr;
314 m_davixPosix->fadvise(m_fd, 0, n, AdviseRandom);
317 ssize_t
s = m_davixPosix->read(m_fd, (
char *)into + done, n - done, &davixErr);
319 std::unique_ptr<DavixError> davixErrManaged(davixErr);
321 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (n -
done) <<
") failed with error " 322 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and call returned " << s
329 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (n -
done) <<
") failed and call returned " << s;
343 ex <<
"DavixFile::write(name='" << m_name <<
"') not implemented";
349 DavixError *davixErr =
nullptr;
350 if (whence != CURRENT && whence !=
SET && whence != END) {
352 ex <<
"DavixFile::position() called with incorrect 'whence' parameter";
353 ex.
addContext(
"Calling DavixFile::position()");
357 size_t mywhence = (whence ==
SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
359 if ((result = m_davixPosix->lseek(m_fd, offset, mywhence, &davixErr)) == -1) {
361 ex <<
"Davix::lseek(name='" << m_name <<
"', offset=" << offset <<
", whence=" << mywhence <<
") failed with error " 362 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and " 363 <<
"call returned " <<
result;
364 ex.
addContext(
"Calling DavixFile::position()");
373 ex <<
"DavixFile::resize(name='" << m_name <<
"') not implemented";
static void configureDavixLogLevel()
IOSize write(const void *from, IOSize n) override
IOSize readv(IOBuffer *into, IOSize buffers) override
virtual void create(const char *name, bool exclusive=false, int perms=0666)
static int X509Authentication(void *userdata, const SessionInfo &info, X509Credential *cert, DavixError **davixErr)
virtual IOOffset position(void) const
void close(void) override
IOOffset offset(void) const
void addContext(std::string const &context)
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
void resize(IOOffset size) override
~DavixFile(void) override
static std::once_flag davixDebugInit