16 using namespace Davix;
19 DavixFile::DavixFile(
void) {}
21 DavixFile::DavixFile(
const char *
name,
int flags ,
int perms ) {
29 DavixFile::~DavixFile(
void) {
34 void DavixFile::close(
void) {
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 " <<
err->getErrMsg().c_str() <<
" and error code " 52 void DavixFile::abort(
void) {
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 " <<
err->getErrMsg().c_str() <<
" and error code " 68 void DavixFile::configureDavixLogLevel() {
70 char *logptr =
nullptr;
71 char const *
const davixDebug = std::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);
111 char default_proxy[64];
112 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d", geteuid());
114 if (std::getenv(
"X509_USER_PROXY")) {
115 edm::LogInfo(
"DavixFile") <<
"X509_USER_PROXY found in environment." 116 <<
" Will use it for authentication";
117 ucert = ukey = std::getenv(
"X509_USER_PROXY");
120 else if (access(default_proxy, R_OK) == 0) {
121 edm::LogInfo(
"DavixFile") <<
"Found proxy in default location " << default_proxy
122 <<
" Will use it for authentication";
123 ucert = ukey = default_proxy;
126 else if (std::getenv(
"X509_USER_CERT")) {
127 ucert = std::getenv(
"X509_USER_CERT");
130 if (!ucert.empty() && std::getenv(
"X509_USER_KEY")) {
131 edm::LogInfo(
"DavixFile") <<
"X509_USER_{CERT|KEY} found in environment" 132 <<
" Will use it for authentication";
133 ukey = std::getenv(
"X509_USER_KEY");
136 if (ucert.empty() || ukey.empty()) {
137 edm::LogWarning(
"DavixFile") <<
"Was not able to find proxy in $X509_USER_PROXY, " 138 <<
"X509_USER_{CERT|KEY} or default proxy creation location. " 139 <<
"Will try without authentication";
142 return cert->loadFromFilePEM(ukey, ucert,
"", davixErr);
161 void DavixFile::open(
const char *
name,
int flags ,
int perms ) {
163 if ((
name ==
nullptr) || (*
name == 0)) {
165 ex <<
"Cannot open a file without name";
173 ex <<
"Must open file '" <<
name <<
"' at least for read";
178 if (m_davixPosix && m_fd) {
180 ex <<
"Davix::open(name='" << m_name <<
"') failed on already open file";
184 configureDavixLogLevel();
189 openflags |= O_RDONLY;
191 DavixError *davixErr =
nullptr;
192 RequestParams davixReqParams;
196 const char *cert_dir =
nullptr;
197 if ((cert_dir = std::getenv(
"X509_CERT_DIR")) ==
nullptr)
198 cert_dir =
"/etc/grid-security/certificates";
199 davixReqParams.addCertificateAuthorityPath(cert_dir);
201 m_davixPosix = std::make_unique<DavPosix>(
new Context());
202 m_fd = m_davixPosix->open(&davixReqParams,
name, openflags, &davixErr);
206 std::unique_ptr<DavixError> davixErrManaged(davixErr);
208 ex <<
"Davix::open(name='" << m_name <<
"') failed with " 209 <<
"error '" << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus();
215 ex <<
"Davix::open(name='" << m_name <<
"') failed as fd is NULL";
228 DavixError *davixErr =
nullptr;
230 std::vector<DavIOVecInput> input_vector(buffers);
231 std::vector<DavIOVecOuput> output_vector(buffers);
234 input_vector[
i].diov_size = into[
i].
size();
235 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
239 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
241 std::unique_ptr<DavixError> davixErrManaged(davixErr);
243 ex <<
"Davix::readv(name='" << m_name <<
"', buffers=" << (buffers) <<
") failed with error " 244 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and call returned " <<
s 257 ex <<
"Davix::readv(name='" << m_name <<
"') failed and call returned " <<
s;
274 DavixError *davixErr =
nullptr;
276 std::vector<DavIOVecInput> input_vector(buffers);
277 std::vector<DavIOVecOuput> output_vector(buffers);
280 input_vector[
i].diov_offset = into[
i].
offset();
281 input_vector[
i].diov_size = into[
i].
size();
282 input_vector[
i].diov_buffer =
static_cast<char *
>(into[
i].
data());
285 ssize_t
s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
287 std::unique_ptr<DavixError> davixErrManaged(davixErr);
289 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers <<
") failed with error " 290 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and call returned " <<
s 303 ex <<
"Davix::readv(name='" << m_name <<
"', n=" << buffers <<
") failed and call returned " <<
s;
314 DavixError *davixErr =
nullptr;
315 m_davixPosix->fadvise(m_fd, 0,
n, AdviseRandom);
318 ssize_t
s = m_davixPosix->read(m_fd, (
char *)into +
done,
n -
done, &davixErr);
320 std::unique_ptr<DavixError> davixErrManaged(davixErr);
322 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (
n -
done) <<
") failed with error " 323 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and call returned " <<
s 330 ex <<
"Davix::read(name='" << m_name <<
"', n=" << (
n -
done) <<
") failed and call returned " <<
s;
344 ex <<
"DavixFile::write(name='" << m_name <<
"') not implemented";
350 DavixError *davixErr =
nullptr;
351 if (whence != CURRENT && whence !=
SET && whence != END) {
353 ex <<
"DavixFile::position() called with incorrect 'whence' parameter";
354 ex.
addContext(
"Calling DavixFile::position()");
358 size_t mywhence = (whence ==
SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
360 if ((
result = m_davixPosix->lseek(m_fd,
offset, mywhence, &davixErr)) == -1) {
362 ex <<
"Davix::lseek(name='" << m_name <<
"', offset=" <<
offset <<
", whence=" << mywhence <<
") failed with error " 363 << davixErr->getErrMsg().c_str() <<
" and error code " << davixErr->getStatus() <<
" and " 364 <<
"call returned " <<
result;
365 ex.
addContext(
"Calling DavixFile::position()");
374 ex <<
"DavixFile::resize(name='" << m_name <<
"') not implemented";
def create(alignables, pedeDump, additionalData, outputFile, config)
static int X509Authentication(void *userdata, const SessionInfo &info, X509Credential *cert, DavixError **davixErr)
Log< level::Info, false > LogInfo
void addContext(std::string const &context)
static int position[264][3]
Log< level::Warning, false > LogWarning
static std::once_flag davixDebugInit