55static const std::string
VERSION =
"0.2.0";
58" TDavixFile/" +
VERSION +
" davix/" + Davix::version();
61#define ENVPFX "Davix."
94 if (!str)
return false;
96 if (!strcmp(str,
"n") || !strcmp(str,
"no") || !strcmp(str,
"0") || !strcmp(str,
"false"))
return true;
103 if(!str)
return defvalue;
105 if(strcmp(str,
"n") == 0 || strcmp(str,
"no") == 0 || strcmp(str,
"0") == 0 || strcmp(str,
"false") == 0)
return false;
106 if(strcmp(str,
"y") == 0 || strcmp(str,
"yes") == 0 || strcmp(str,
"1") == 0 || strcmp(str,
"true") == 0)
return true;
116 old_flag |= O_RDONLY;
119 old_flag |= (O_CREAT | O_WRONLY | O_TRUNC);
122 old_flag |= (O_RDWR);
135 davix_set_log_level(0);
138 davix_set_log_level(DAVIX_LOG_WARNING);
141 davix_set_log_level(DAVIX_LOG_VERBOSE);
144 davix_set_log_level(DAVIX_LOG_DEBUG);
147 davix_set_log_level(DAVIX_LOG_ALL);
156 static const std::string whitespace =
" \t\f\n\v\r";
159 static const std::string nonheader_whitespace =
"\r\n";
160 auto begin = input_token.find_first_not_of(whitespace);
162 if (begin == std::string::npos) {
167 std::string token = input_token.substr(begin);
168 auto end = token.find_last_not_of(whitespace);
169 token = token.substr(0, end + 1);
173 if (token.find(nonheader_whitespace) != std::string::npos) {
178 output_token = token;
185 int fd = open(token_file.c_str(), O_RDONLY);
188 if (errno == ENOENT) {
199 static const size_t max_size = 16384;
200 std::vector<char> input_buffer;
201 input_buffer.resize(max_size);
202 ssize_t retval = read(fd, &input_buffer[0], max_size);
207 R__LOG_ERROR(
TDavixLogChannel()) <<
"Token discovery failure: failed to read file " << token_file.c_str() <<
"', error: " << strerror(errno);
210 if (retval == 16384) {
216 std::string token(&input_buffer[0], retval);
225 const char *bearer_token = getenv(
"BEARER_TOKEN");
227 if (bearer_token && *bearer_token){
229 if (!token.empty()) {
return token;}
232 const char *bearer_token_file = getenv(
"BEARER_TOKEN_FILE");
233 if (bearer_token_file) {
235 if (!token.empty()) {
return token;}
239 uid_t euid = geteuid();
240 std::string fname =
"/bt_u";
241 fname += std::to_string(euid);
243 const char *xdg_runtime_dir = getenv(
"XDG_RUNTIME_DIR");
244 if (xdg_runtime_dir) {
245 std::string xdg_token_file = std::string(xdg_runtime_dir) + fname;
247 if (!token.empty()) {
return token;}
266 char default_proxy[64];
267 const char *genvvar = 0, *genvvar1 = 0;
269 genvvar =
gEnv->
GetValue(
"Davix.GSI.UserProxy", (
const char *) NULL);
271 ucert = ukey = genvvar;
273 Info(
"TDavixFile_http_get_ucert",
"Found proxy in gEnv");
278 if (getenv(
"X509_USER_PROXY")) {
280 Info(
"TDavixFile_http_get_ucert",
"Found proxy in X509_USER_PROXY");
281 ucert = ukey = getenv(
"X509_USER_PROXY");
286 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d",
289 if (access(default_proxy, R_OK) == 0) {
291 Info(
"TDavixFile_http_get_ucert",
"Found proxy in /tmp");
292 ucert = ukey = default_proxy;
297 genvvar =
gEnv->
GetValue(
"Davix.GSI.UserCert", (
const char *) NULL);
298 genvvar1 =
gEnv->
GetValue(
"Davix.GSI.UserKey", (
const char *) NULL);
299 if (genvvar || genvvar1) {
301 Info(
"TDavixFile_http_get_ucert",
"Found cert and key in gEnv");
309 if (getenv(
"X509_USER_CERT"))
310 ucert = getenv(
"X509_USER_CERT");
311 if (getenv(
"X509_USER_KEY"))
312 ukey = getenv(
"X509_USER_KEY");
314 if ((ucert.size() > 0) || (ukey.size() > 0)) {
316 Info(
"TDavixFile_http_get_ucert",
"Found cert and key in gEnv");
325 Davix::X509Credential *cert, Davix::DavixError **err)
329 std::string ucert, ukey;
332 if (ucert.empty() || ukey.empty()) {
333 Davix::DavixError::setupError(err,
"TDavixFile",
334 Davix::StatusCode::AuthentificationError,
335 "Could not set the user's proxy or certificate");
338 return cert->loadFromFilePEM(ukey, ucert,
"", err);
367 DavixError *davixErr = NULL;
375 DavixError *davixErr2 = NULL;
378 std::vector<DavFile> replicasLocal =
file.getReplicas(NULL, &davixErr2);
379 for(
size_t i = 0; i < replicasLocal.size(); i++) {
380 replicas.push_back(replicasLocal[i].getUri().getString());
384 DavixError::clearError(&davixErr2);
389 Error(
"DavixOpen",
"can not open file \"%s\" with davix: %s (%d)",
391 davixErr->getErrMsg().c_str(), davixErr->getStatus());
393 DavixError::clearError(&davixErr);
396 davixPosix->fadvise(fd, 0, 300, Davix::AdviseRandom);
406 DavixError *davixErr = NULL;
408 Error(
"DavixClose",
"can not to close file with davix: %s (%d)",
409 davixErr->getErrMsg().c_str(), davixErr->getStatus());
410 DavixError::clearError(&davixErr);
418 const char *env_var = NULL;
421 Info(
"enableGridMode",
" grid mode enabled !");
423 if( ( env_var = getenv(
"X509_CERT_DIR")) == NULL){
424 env_var=
"/etc/grid-security/certificates/";
426 davixParam->addCertificateAuthorityPath(env_var);
428 Info(
"enableGridMode",
"Adding CAdir %s", env_var);
441template<
typename TRequestParams = Davix::RequestParams>
442static auto awsRegion(TRequestParams *parameters,
const char *region)
443 ->
decltype(parameters->setAwsRegion(region),
void())
445 if (
gDebug > 1)
Info(
"awsRegion",
"Setting S3 Region to '%s' - v4 signature will be used", region);
446 parameters->setAwsRegion(region);
449template<
typename TRequestParams = Davix::RequestParams>
451 Warning(
"setAwsRegion",
"Unable to set AWS region, not supported by this version of davix");
455template<
typename TRequestParams = Davix::RequestParams>
456static auto awsToken(TRequestParams *parameters,
const char *token)
457 ->
decltype(parameters->setAwsToken(token),
void())
459 if (
gDebug > 1)
Info(
"awsToken",
"Setting S3 STS temporary credentials");
460 parameters->setAwsToken(token);
463template<
typename TRequestParams = Davix::RequestParams>
465 Warning(
"awsToken",
"Unable to set AWS token, not supported by this version of davix");
469template<
typename TRequestParams = Davix::RequestParams>
471 ->
decltype(parameters->setAwsAlternate(option),
void())
473 if (
gDebug > 1)
Info(
"awsAlternate",
"Setting S3 path-based bucket option (s3alternate)");
474 parameters->setAwsAlternate(option);
477template<
typename TRequestParams = Davix::RequestParams>
479 Warning(
"awsAlternate",
"Unable to set AWS path-based bucket option (s3alternate), not supported by this version of davix");
483 if(!region.empty()) {
500 const std::string ®ion,
const std::string &token)
503 Info(
"setS3Auth",
" Aws S3 tokens configured");
505 davixParam->setAwsAuthorizationKeys(secret, access);
506 davixParam->setProtocol(RequestProtocol::AwsS3);
516 const char *env_var = NULL, *env_var2 = NULL;
518 davixParam->setTransparentRedirectionSupport(
true);
522 env_var =
gEnv->
GetValue(
"Davix.GSI.CAdir", (
const char *) NULL);
524 davixParam->addCertificateAuthorityPath(env_var);
526 Info(
"parseConfig",
"Add CAdir: %s", env_var);
530 bool ca_check_local = !
isno(
gEnv->
GetValue(
"Davix.GSI.CACheck", (
const char *)
"y"));
533 Info(
"parseConfig",
"Setting CAcheck to %s", ((ca_check_local) ? (
"true") : (
"false")));
536 std::string prefix =
"Bearer ";
538 if (!token.empty()) {
541 davixParam->addHeader(
"Authorization", prefix + token);
545 if (((env_var =
gEnv->
GetValue(
"Davix.S3.SecretKey", getenv(
"S3_SECRET_KEY"))) != NULL)
546 && ((env_var2 =
gEnv->
GetValue(
"Davix.S3.AccessKey", getenv(
"S3_ACCESS_KEY"))) != NULL)) {
547 Info(
"parseConfig",
"Setting S3 SecretKey and AccessKey. Access Key : %s ", env_var2);
548 davixParam->setAwsAuthorizationKeys(env_var, env_var2);
551 if ( (env_var =
gEnv->
GetValue(
"Davix.S3.Region", getenv(
"S3_REGION"))) != NULL) {
555 if( (env_var =
gEnv->
GetValue(
"Davix.S3.Token", getenv(
"S3_TOKEN"))) != NULL) {
559 if( (env_var =
gEnv->
GetValue(
"Davix.S3.Alternate", getenv(
"S3_ALTERNATE"))) != NULL) {
564 env_var =
gEnv->
GetValue(
"Davix.GSI.GridMode", (
const char *)
"y");
574 std::stringstream ss(option);
576 std::vector<std::string> parsed_options;
578 std::string s3seckey, s3acckey, s3region, s3token;
580 while (std::getline(ss, item,
' ')) {
581 parsed_options.push_back(item);
584 for (std::vector<std::string>::iterator it = parsed_options.begin(); it < parsed_options.end(); ++it) {
607 s3token = std::string(it->c_str() + strlen(
s3_token_opt));
617 if (s3seckey.size() > 0) {
618 setS3Auth(s3seckey, s3acckey, s3region, s3token);
632 davixParam->setMetalinkMode(Davix::MetalinkMode::Disable);
642 DavixError *davixErr = NULL;
646 Error(
"DavixStat",
"can not stat the file with davix: %s (%d)",
647 davixErr->getErrMsg().c_str(), davixErr->getStatus());
648 DavixError::clearError(&davixErr);
695 if(!replicas.empty()) {
696 std::stringstream ss;
697 for(
size_t i = 0; i < replicas.size(); i++) {
699 if(i != replicas.size()-1) ss <<
"|";
722 Error(
"Seek",
"seeking from end in archive is not (yet) supported");
728 Info(
"Seek",
" move cursor to %lld"
747 Info(
"ReadBuffer",
"%lld bytes of data read sequentially"
748 " (%d requested)", ret, len);
766 Info(
"ReadBuffer",
"%lld bytes of data read from offset"
767 " %lld (%d requested)", ret, pos, len);
779 d_ptr->
davixPosix->fadvise(fd,
static_cast<dav_off_t
>(offs),
static_cast<dav_size_t
>(len), Davix::AdviseRandom);
782 Info(
"ReadBufferAsync",
"%d bytes of data prefected from offset"
783 " %lld ", len, offs);
800 Info(
"ReadBuffers",
"%lld bytes of data read from a list of %d buffers",
819 Info(
"WriteBuffer",
"%lld bytes of data write"
820 " %d requested", ret, len);
843 std::vector<void *>::iterator
f = std::find(
dirdVec.begin(),
dirdVec.end(), fd);
860 std::vector<void *>::iterator
f = std::find(
dirdVec.begin(),
dirdVec.end(), fd);
873 Info(
"GetSize",
"file size requested: %lld", (
Long64_t)st.st_size);
912 DavixError *davixErr = NULL;
917 Error(
"DavixReadBuffer",
"can not read data with davix: %s (%d)",
918 davixErr->getErrMsg().c_str(), davixErr->getStatus());
919 DavixError::clearError(&davixErr);
932 DavixError *davixErr = NULL;
937 Error(
"DavixWriteBuffer",
"can not write data with davix: %s (%d)",
938 davixErr->getErrMsg().c_str(), davixErr->getStatus());
939 DavixError::clearError(&davixErr);
952 DavixError *davixErr = NULL;
957 Error(
"DavixPReadBuffer",
"can not read data with davix: %s (%d)",
958 davixErr->getErrMsg().c_str(), davixErr->getStatus());
959 DavixError::clearError(&davixErr);
972 DavixError *davixErr = NULL;
974 DavIOVecInput in[nbuf];
975 DavIOVecOuput out[nbuf];
978 for (
Int_t i = 0; i < nbuf; ++i) {
979 in[i].diov_buffer = &buf[lastPos];
980 in[i].diov_offset = pos[i];
981 in[i].diov_size = len[i];
987 Error(
"DavixReadBuffers",
"can not read data with davix: %s (%d)",
988 davixErr->getErrMsg().c_str(), davixErr->getStatus());
989 DavixError::clearError(&davixErr);
#define R__LOG_ERROR(...)
const char * s3_seckey_opt
bool normalizeToken(const std::string &input_token, std::string &output_token)
static const std::string VERSION
static void ConfigureDavixLogLevel()
static auto awsRegion(TRequestParams *parameters, const char *region) -> decltype(parameters->setAwsRegion(region), void())
bool findTokenInFile(const std::string &token_file, std::string &output_token)
static auto awsAlternate(TRequestParams *parameters, bool option) -> decltype(parameters->setAwsAlternate(option), void())
std::string DiscoverToken()
static int TDavixFile_http_authn_cert_X509(void *userdata, const Davix::SessionInfo &info, Davix::X509Credential *cert, Davix::DavixError **err)
const char * s3_alternate_opt
static auto awsToken(TRequestParams *parameters, const char *token) -> decltype(parameters->setAwsToken(token), void())
const char * s3_token_opt
bool isno(const char *str)
const char * ca_check_opt
const char * open_mode_new
const char * open_mode_update
const char * open_mode_create
const char * s3_acckey_opt
static Context * davix_context_s
const char * grid_mode_opt
ROOT::Experimental::RLogChannel & TDavixLogChannel()
int configure_open_flag(const std::string &str, int old_flag)
static void TDavixFile_http_get_ucert(std::string &ucert, std::string &ukey)
static const std::string gUserAgent
bool strToBool(const char *str, bool defvalue)
const char * s3_region_opt
const char * open_mode_read
ROOT::Experimental::RLogChannel & TDavixLogChannel()
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
typedef void((*Func_t)())
A log configuration for a channel, e.g.
Davix::RequestParams * davixParam
std::vector< void * > dirdVec
void setAwsToken(const std::string &token)
void setS3Auth(const std::string &secret, const std::string &access, const std::string ®ion, const std::string &token)
void setAwsAlternate(const bool &option)
static Davix::Context * getDavixInstance()
Davix::DavPosix * davixPosix
void parseParams(Option_t *option)
intput params
std::vector< std::string > getReplicas()
Int_t DavixStat(const char *url, struct stat *st)
Davix_fd * getDavixFileInstance()
Davix::Context * davixContext
void setAwsRegion(const std::string ®ion)
std::vector< std::string > replicas
void removeDird(void *fd)
virtual Long64_t GetSize() const
Returns the current file size.
void Init(Bool_t init)
Initialize a TFile object.
Long64_t DavixReadBuffers(Davix_fd *fd, char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
Long64_t DavixReadBuffer(Davix_fd *fd, char *buf, Int_t len)
Long64_t DavixPReadBuffer(Davix_fd *fd, char *buf, Long64_t pos, Int_t len)
virtual Bool_t WriteBuffer(const char *buffer, Int_t bufferLength)
Write a buffer to the file.
virtual TString GetNewUrl()
Long64_t DavixWriteBuffer(Davix_fd *fd, const char *buf, Int_t len)
virtual Bool_t ReadBufferAsync(Long64_t offs, Int_t len)
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read specified byte range from remote file via HTTP.
TDavixFile(const char *url, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault)
Open function for TDavixFile.
void enableGridMode()
Enable the grid mode The grid Mode configure automatically all grid-CA path, VOMS authentication and ...
virtual void Seek(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
TDavixFileInternal * d_ptr
virtual Bool_t ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
Read the nbuf blocks described in arrays pos and len.
void eventStop(Double_t t, Long64_t len, bool read=true)
set TFile state info
void setCACheck(Bool_t check)
Enable or disable certificate authority check.
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Int_t fReadCalls
Number of read calls ( not counting the cache calls )
static void SetFileBytesWritten(Long64_t bytes=0)
Long64_t fBytesRead
Number of bytes read from this file.
static Long64_t GetFileBytesWritten()
Static function returning the total number of bytes written to all files.
static void SetFileBytesRead(Long64_t bytes=0)
static void SetFileReadCalls(Int_t readcalls=0)
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
Long64_t fArchiveOffset
!Offset at which file starts in archive
virtual void Init(Bool_t create)
Initialize a TFile object.
Long64_t fBytesWrite
Number of bytes written to this file.
Long64_t fOffset
!Seek offset cache
Long64_t fEND
Last used byte in file.
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
The TTimeStamp encapsulates seconds and ns since EPOCH.
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.