16 using namespace Davix;
21 open(name, flags, perms);
26 open(name.c_str(),
flags, perms);
35 if (m_davixPosix && m_fd) {
36 auto davixPosix =
std::move(m_davixPosix);
37 DavixError *err =
nullptr;
38 davixPosix->close(m_fd, &err);
41 std::unique_ptr<DavixError> davixErrManaged(err);
43 ex <<
"Davix::close(name='" << m_name <<
") failed with error " 44 << err->getErrMsg().c_str() <<
" and error code " << err->getStatus();
53 if (m_davixPosix && m_fd) {
54 DavixError *err =
nullptr;
55 m_davixPosix->close(m_fd, &err);
57 std::unique_ptr<DavixError> davixErrManaged(err);
59 ex <<
"Davix::abort(name='" << m_name <<
") failed with error " 60 << err->getErrMsg().c_str() <<
" and error code " << err->getStatus();
70 char *logptr =
nullptr;
71 char const *
const davixDebug = getenv(
"Davix_Debug");
72 if (davixDebug !=
nullptr) {
73 logLevel = strtol(davixDebug, &logptr, 0);
76 <<
"Davix_Debug env variable to integer. " 77 "Will use default log level 0";
80 if (logptr == davixDebug) {
82 <<
"Davix_Debug env variable; Will use default log level 0";
84 }
else if (*logptr !=
'\0') {
86 <<
"from Davix_Debug env variable. Will use default log level 0";
95 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_WARNING);
98 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_VERBOSE);
101 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_DEBUG);
104 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_ALL);
110 DavixError **davixErr) {
112 char default_proxy[64];
113 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d", geteuid());
115 if (getenv(
"X509_USER_PROXY")) {
116 edm::LogInfo(
"DavixFile") <<
"X509_USER_PROXY found in environment." 117 <<
" Will use it for authentication";
118 ucert = ukey = getenv(
"X509_USER_PROXY");
121 else if (access(default_proxy, R_OK) == 0) {
122 edm::LogInfo(
"DavixFile") <<
"Found proxy in default location " << default_proxy
123 <<
" Will use it for authentication";
124 ucert = ukey = default_proxy;
127 else if (getenv(
"X509_USER_CERT")) {
128 ucert = getenv(
"X509_USER_CERT");
131 if (!ucert.empty() && getenv(
"X509_USER_KEY")) {
132 edm::LogInfo(
"DavixFile") <<
"X509_USER_{CERT|KEY} found in environment" 133 <<
" Will use it for authentication";
134 ukey = getenv(
"X509_USER_KEY");
137 if (ucert.empty() || ukey.empty()) {
138 edm::LogWarning(
"DavixFile") <<
"Was not able to find proxy in $X509_USER_PROXY, " 139 <<
"X509_USER_{CERT|KEY} or default proxy creation location. " 140 <<
"Will try without authentication";
143 return cert->loadFromFilePEM(ukey, ucert,
"", davixErr);
161 open(name.c_str(),
flags, perms);
166 if ((name ==
nullptr) || (*name == 0)) {
168 ex <<
"Cannot open a file without name";
176 ex <<
"Must open file '" << name <<
"' at least for read";
181 if (m_davixPosix && m_fd) {
183 ex <<
"Davix::open(name='" << m_name <<
"') failed on already open file";
187 configureDavixLogLevel();
191 if (flags & IOFlags::OpenRead)
192 openflags |= O_RDONLY;
194 DavixError *davixErr =
nullptr;
195 RequestParams davixReqParams;
199 const char *cert_dir =
nullptr;
200 if ((cert_dir = getenv(
"X509_CERT_DIR")) ==
nullptr)
201 cert_dir =
"/etc/grid-security/certificates";
202 davixReqParams.addCertificateAuthorityPath(cert_dir);
204 m_davixPosix = std::make_unique<DavPosix>(
new Context());
205 m_fd = m_davixPosix->open(&davixReqParams, name, openflags, &davixErr);
209 std::unique_ptr<DavixError> davixErrManaged(davixErr);
211 ex <<
"Davix::open(name='" << m_name <<
"') failed with " 212 <<
"error '" << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus();
218 ex <<
"Davix::open(name='" << m_name <<
"') failed as fd is NULL";
225 assert(!buffers || into);
231 DavixError *davixErr =
nullptr;
233 std::vector<DavIOVecInput> input_vector(buffers);
234 std::vector<DavIOVecOuput> output_vector(buffers);
237 input_vector[
i].diov_size = into[
i].
size();
238 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
239 total += into[
i].
size();
242 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
244 std::unique_ptr<DavixError> davixErrManaged(davixErr);
246 ex <<
"Davix::readv(name='" << m_name <<
"', buffers=" << (buffers)
247 <<
") failed with error " << davixErr->getErrMsg().c_str() <<
" and error code " 248 << davixErr->getStatus() <<
" and call returned " << s <<
" bytes";
260 ex <<
"Davix::readv(name='" << m_name <<
"') failed and call returned " <<
s;
271 assert(!buffers || into);
277 DavixError *davixErr =
nullptr;
279 std::vector<DavIOVecInput> input_vector(buffers);
280 std::vector<DavIOVecOuput> output_vector(buffers);
283 input_vector[
i].diov_offset = into[
i].
offset();
284 input_vector[
i].diov_size = into[
i].
size();
285 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
286 total += into[
i].
size();
288 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
290 std::unique_ptr<DavixError> davixErrManaged(davixErr);
292 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers <<
") failed with error " 293 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus()
294 <<
" and call returned " << s <<
" bytes";
306 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers
307 <<
") failed and call returned " <<
s;
318 DavixError *davixErr =
nullptr;
319 m_davixPosix->fadvise(m_fd, 0, n, AdviseRandom);
322 ssize_t
s = m_davixPosix->read(m_fd, (
char *)into + done, n - done, &davixErr);
324 std::unique_ptr<DavixError> davixErrManaged(davixErr);
326 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (n - done)
327 <<
") failed with error " << davixErr->getErrMsg().c_str() <<
" and error code " 328 << davixErr->getStatus() <<
" and call returned " << s <<
" bytes";
334 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (n - done)
335 <<
") failed and call returned " << s;
349 ex <<
"DavixFile::write(name='" << m_name <<
"') not implemented";
355 DavixError *davixErr =
nullptr;
356 if (whence != CURRENT && whence != SET && whence != END) {
358 ex <<
"DavixFile::position() called with incorrect 'whence' parameter";
359 ex.
addContext(
"Calling DavixFile::position()");
363 size_t mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
365 if ((result = m_davixPosix->lseek(m_fd, offset, mywhence, &davixErr)) == -1) {
367 ex <<
"Davix::lseek(name='" << m_name <<
"', offset=" << offset
368 <<
", whence=" << mywhence <<
") failed with error " << davixErr->getErrMsg().c_str()
369 <<
" and error code " << davixErr->getStatus() <<
" and " 370 <<
"call returned " <<
result;
371 ex.
addContext(
"Calling DavixFile::position()");
380 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
std::vector< Variable::Flags > flags
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