16 using namespace Davix;
21 open(name, flags, perms);
26 open(name.c_str(),
flags, perms);
35 if (m_davixPosix && m_fd) {
36 DavixError *err =
nullptr;
37 m_davixPosix->close(m_fd, &err);
39 std::unique_ptr<DavixError> davixErrManaged(err);
41 ex <<
"Davix::close(name='" << m_name <<
") failed with error " 42 << err->getErrMsg().c_str() <<
" and error code " << err->getStatus();
51 if (m_davixPosix && m_fd) {
52 DavixError *err =
nullptr;
53 m_davixPosix->close(m_fd, &err);
55 std::unique_ptr<DavixError> davixErrManaged(err);
57 ex <<
"Davix::abort(name='" << m_name <<
") failed with error " 58 << err->getErrMsg().c_str() <<
" and error code " << err->getStatus();
68 char *logptr =
nullptr;
69 char const *
const davixDebug = getenv(
"Davix_Debug");
70 if (davixDebug !=
nullptr) {
71 logLevel = strtol(davixDebug, &logptr, 0);
74 <<
"Davix_Debug env variable to integer. " 75 "Will use default log level 0";
78 if (logptr == davixDebug) {
80 <<
"Davix_Debug env variable; Will use default log level 0";
82 }
else if (*logptr !=
'\0') {
84 <<
"from Davix_Debug env variable. Will use default log level 0";
93 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_WARNING);
96 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_VERBOSE);
99 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_DEBUG);
102 std::call_once(
davixDebugInit, davix_set_log_level, DAVIX_LOG_ALL);
108 DavixError **davixErr) {
110 char default_proxy[64];
111 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d", geteuid());
113 if (getenv(
"X509_USER_PROXY")) {
114 edm::LogInfo(
"DavixFile") <<
"X509_USER_PROXY found in environment." 115 <<
" Will use it for authentication";
116 ucert = ukey = 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 (getenv(
"X509_USER_CERT")) {
126 ucert = getenv(
"X509_USER_CERT");
129 if (!ucert.empty() && getenv(
"X509_USER_KEY")) {
130 edm::LogInfo(
"DavixFile") <<
"X509_USER_{CERT|KEY} found in environment" 131 <<
" Will use it for authentication";
132 ukey = 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);
159 open(name.c_str(),
flags, perms);
164 if ((name ==
nullptr) || (*name == 0)) {
166 ex <<
"Cannot open a file without name";
174 ex <<
"Must open file '" << name <<
"' at least for read";
179 if (m_davixPosix && m_fd) {
181 ex <<
"Davix::open(name='" << m_name <<
"') failed on already open file";
185 configureDavixLogLevel();
189 if (flags & IOFlags::OpenRead)
190 openflags |= O_RDONLY;
192 DavixError *davixErr =
nullptr;
193 RequestParams davixReqParams;
197 const char *cert_dir =
nullptr;
198 if ((cert_dir = getenv(
"X509_CERT_DIR")) ==
nullptr)
199 cert_dir =
"/etc/grid-security/certificates";
200 davixReqParams.addCertificateAuthorityPath(cert_dir);
202 m_davixPosix = std::make_unique<DavPosix>(
new Context());
203 m_fd = m_davixPosix->open(&davixReqParams, name, openflags, &davixErr);
207 std::unique_ptr<DavixError> davixErrManaged(davixErr);
209 ex <<
"Davix::open(name='" << m_name <<
"') failed with " 210 <<
"error '" << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus();
216 ex <<
"Davix::open(name='" << m_name <<
"') failed as fd is NULL";
223 assert(!buffers || into);
229 DavixError *davixErr =
nullptr;
231 std::vector<DavIOVecInput> input_vector(buffers);
232 std::vector<DavIOVecOuput> output_vector(buffers);
235 input_vector[
i].diov_size = into[
i].
size();
236 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
237 total += into[
i].
size();
240 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
242 std::unique_ptr<DavixError> davixErrManaged(davixErr);
244 ex <<
"Davix::readv(name='" << m_name <<
"', buffers=" << (buffers)
245 <<
") failed with error " << davixErr->getErrMsg().c_str() <<
" and error code " 246 << davixErr->getStatus() <<
" and call returned " << s <<
" bytes";
258 ex <<
"Davix::readv(name='" << m_name <<
"') failed and call returned " <<
s;
269 assert(!buffers || into);
275 DavixError *davixErr =
nullptr;
277 std::vector<DavIOVecInput> input_vector(buffers);
278 std::vector<DavIOVecOuput> output_vector(buffers);
281 input_vector[
i].diov_offset = into[
i].
offset();
282 input_vector[
i].diov_size = into[
i].
size();
283 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
284 total += into[
i].
size();
286 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
288 std::unique_ptr<DavixError> davixErrManaged(davixErr);
290 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers <<
") failed with error " 291 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus()
292 <<
" and call returned " << s <<
" bytes";
304 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers
305 <<
") failed and call returned " <<
s;
316 DavixError *davixErr =
nullptr;
317 m_davixPosix->fadvise(m_fd, 0, n, AdviseRandom);
320 ssize_t
s = m_davixPosix->read(m_fd, (
char *)into + done, n - done, &davixErr);
322 std::unique_ptr<DavixError> davixErrManaged(davixErr);
324 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (n - done)
325 <<
") failed with error " << davixErr->getErrMsg().c_str() <<
" and error code " 326 << davixErr->getStatus() <<
" and call returned " << s <<
" bytes";
332 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (n - done)
333 <<
") failed and call returned " << s;
347 ex <<
"DavixFile::write(name='" << m_name <<
"') not implemented";
353 DavixError *davixErr =
nullptr;
354 if (whence != CURRENT && whence != SET && whence != END) {
356 ex <<
"DavixFile::position() called with incorrect 'whence' parameter";
357 ex.
addContext(
"Calling DavixFile::position()");
361 size_t mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
363 if ((result = m_davixPosix->lseek(m_fd, offset, mywhence, &davixErr)) == -1) {
365 ex <<
"Davix::lseek(name='" << m_name <<
"', offset=" << offset
366 <<
", whence=" << mywhence <<
") failed with error " << davixErr->getErrMsg().c_str()
367 <<
" and error code " << davixErr->getStatus() <<
" and " 368 <<
"call returned " <<
result;
369 ex.
addContext(
"Calling DavixFile::position()");
378 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