56static const std::string
VERSION =
"0.2.0";
59" TDavixFile/" +
VERSION +
" davix/" + Davix::version();
62#define ENVPFX "Davix."
94 if(!str)
return defvalue;
96 if(strcmp(str,
"n") == 0 || strcmp(str,
"no") == 0 || strcmp(str,
"0") == 0 || strcmp(str,
"false") == 0)
return false;
97 if(strcmp(str,
"y") == 0 || strcmp(str,
"yes") == 0 || strcmp(str,
"1") == 0 || strcmp(str,
"true") == 0)
return true;
107 old_flag |= O_RDONLY;
110 old_flag |= (O_CREAT | O_WRONLY | O_TRUNC);
113 old_flag |= (O_RDWR);
122 Int_t log_level = (
gEnv) ?
gEnv->GetValue(
"Davix.Debug", 0) : 0;
126 davix_set_log_level(0);
129 davix_set_log_level(DAVIX_LOG_WARNING);
132 davix_set_log_level(DAVIX_LOG_VERBOSE);
135 davix_set_log_level(DAVIX_LOG_DEBUG);
138 davix_set_log_level(DAVIX_LOG_ALL);
147 static const std::string whitespace =
" \t\f\n\v\r";
150 static const std::string nonheader_whitespace =
"\r\n";
151 auto begin = input_token.find_first_not_of(whitespace);
153 if (begin == std::string::npos) {
158 std::string token = input_token.substr(begin);
159 auto end = token.find_last_not_of(whitespace);
160 token = token.substr(0, end + 1);
164 if (token.find(nonheader_whitespace) != std::string::npos) {
169 output_token = token;
176 int fd = open(token_file.c_str(), O_RDONLY);
179 if (errno == ENOENT) {
190 static const size_t max_size = 16384;
191 std::vector<char> input_buffer;
192 input_buffer.resize(max_size);
193 ssize_t retval = read(fd, &input_buffer[0], max_size);
198 R__LOG_ERROR(
TDavixLogChannel()) <<
"Token discovery failure: failed to read file " << token_file.c_str() <<
"', error: " << strerror(errno);
201 if (retval == 16384) {
207 std::string token(&input_buffer[0], retval);
216 const char *bearer_token = getenv(
"BEARER_TOKEN");
218 if (bearer_token && *bearer_token){
220 if (!token.empty()) {
return token;}
223 const char *bearer_token_file = getenv(
"BEARER_TOKEN_FILE");
224 if (bearer_token_file) {
226 if (!token.empty()) {
return token;}
230 uid_t euid = geteuid();
231 std::string fname =
"/bt_u";
232 fname += std::to_string(euid);
234 const char *xdg_runtime_dir = getenv(
"XDG_RUNTIME_DIR");
235 if (xdg_runtime_dir) {
236 std::string xdg_token_file = std::string(xdg_runtime_dir) + fname;
238 if (!token.empty()) {
return token;}
257 char default_proxy[64];
258 const char *genvvar = 0, *genvvar1 = 0;
260 genvvar =
gEnv->GetValue(
"Davix.GSI.UserProxy", (
const char *) NULL);
262 ucert = ukey = genvvar;
264 Info(
"TDavixFile_http_get_ucert",
"Found proxy in gEnv");
269 if (getenv(
"X509_USER_PROXY")) {
271 Info(
"TDavixFile_http_get_ucert",
"Found proxy in X509_USER_PROXY");
272 ucert = ukey = getenv(
"X509_USER_PROXY");
277 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d",
280 if (access(default_proxy, R_OK) == 0) {
282 Info(
"TDavixFile_http_get_ucert",
"Found proxy in /tmp");
283 ucert = ukey = default_proxy;
288 genvvar =
gEnv->GetValue(
"Davix.GSI.UserCert", (
const char *) NULL);
289 genvvar1 =
gEnv->GetValue(
"Davix.GSI.UserKey", (
const char *) NULL);
290 if (genvvar || genvvar1) {
292 Info(
"TDavixFile_http_get_ucert",
"Found cert and key in gEnv");
300 if (getenv(
"X509_USER_CERT"))
301 ucert = getenv(
"X509_USER_CERT");
302 if (getenv(
"X509_USER_KEY"))
303 ukey = getenv(
"X509_USER_KEY");
305 if ((ucert.size() > 0) || (ukey.size() > 0)) {
307 Info(
"TDavixFile_http_get_ucert",
"Found cert and key in gEnv");
316 Davix::X509Credential *cert, Davix::DavixError **err)
320 std::string ucert, ukey;
323 if (ucert.empty() || ukey.empty()) {
324 Davix::DavixError::setupError(err,
"TDavixFile",
325 Davix::StatusCode::AuthentificationError,
326 "Could not set the user's proxy or certificate");
329 return cert->loadFromFilePEM(ukey, ucert,
"", err);
358 DavixError *davixErr = NULL;
366 DavixError *davixErr2 = NULL;
369 std::vector<DavFile> replicasLocal = file.getReplicas(NULL, &davixErr2);
370 for(
size_t i = 0;
i < replicasLocal.size();
i++) {
371 replicas.push_back(replicasLocal[
i].getUri().getString());
375 DavixError::clearError(&davixErr2);
380 Error(
"DavixOpen",
"can not open file \"%s\" with davix: %s (%d)",
382 davixErr->getErrMsg().c_str(), davixErr->getStatus());
384 DavixError::clearError(&davixErr);
387 davixPosix->fadvise(fd, 0, 300, Davix::AdviseRandom);
397 DavixError *davixErr = NULL;
399 Error(
"DavixClose",
"can not to close file with davix: %s (%d)",
400 davixErr->getErrMsg().c_str(), davixErr->getStatus());
401 DavixError::clearError(&davixErr);
409 const char *env_var = NULL;
412 Info(
"enableGridMode",
" grid mode enabled !");
414 if( ( env_var = getenv(
"X509_CERT_DIR")) == NULL){
415 env_var=
"/etc/grid-security/certificates/";
417 davixParam->addCertificateAuthorityPath(env_var);
419 Info(
"enableGridMode",
"Adding CAdir %s", env_var);
432template<
typename TRequestParams = Davix::RequestParams>
433static auto awsRegion(TRequestParams *parameters,
const char *region)
434 ->
decltype(parameters->setAwsRegion(region),
void())
436 if (
gDebug > 1)
Info(
"awsRegion",
"Setting S3 Region to '%s' - v4 signature will be used", region);
437 parameters->setAwsRegion(region);
440template<
typename TRequestParams = Davix::RequestParams>
442 Warning(
"setAwsRegion",
"Unable to set AWS region, not supported by this version of davix");
446template<
typename TRequestParams = Davix::RequestParams>
447static auto awsToken(TRequestParams *parameters,
const char *token)
448 ->
decltype(parameters->setAwsToken(token),
void())
450 if (
gDebug > 1)
Info(
"awsToken",
"Setting S3 STS temporary credentials");
451 parameters->setAwsToken(token);
454template<
typename TRequestParams = Davix::RequestParams>
456 Warning(
"awsToken",
"Unable to set AWS token, not supported by this version of davix");
460template<
typename TRequestParams = Davix::RequestParams>
462 ->
decltype(parameters->setAwsAlternate(
option),
void())
464 if (
gDebug > 1)
Info(
"awsAlternate",
"Setting S3 path-based bucket option (s3alternate)");
465 parameters->setAwsAlternate(
option);
468template<
typename TRequestParams = Davix::RequestParams>
470 Warning(
"awsAlternate",
"Unable to set AWS path-based bucket option (s3alternate), not supported by this version of davix");
474 if(!region.empty()) {
491 const std::string ®ion,
const std::string &token)
494 Info(
"setS3Auth",
" Aws S3 tokens configured");
496 davixParam->setAwsAuthorizationKeys(secret, access);
497 davixParam->setProtocol(RequestProtocol::AwsS3);
507 const char *env_var = NULL, *env_var2 = NULL;
509 davixParam->setTransparentRedirectionSupport(
true);
513 env_var =
gEnv->GetValue(
"Davix.GSI.CAdir", (
const char *) NULL);
515 davixParam->addCertificateAuthorityPath(env_var);
517 Info(
"parseConfig",
"Add CAdir: %s", env_var);
521 bool ca_check_local = !
isno(
gEnv->GetValue(
"Davix.GSI.CACheck", (
const char *)
"y"));
524 Info(
"parseConfig",
"Setting CAcheck to %s", ((ca_check_local) ? (
"true") : (
"false")));
527 std::string prefix =
"Bearer ";
529 if (!token.empty()) {
532 davixParam->addHeader(
"Authorization", prefix + token);
536 if (((env_var =
gEnv->GetValue(
"Davix.S3.SecretKey", getenv(
"S3_SECRET_KEY"))) != NULL)
537 && ((env_var2 =
gEnv->GetValue(
"Davix.S3.AccessKey", getenv(
"S3_ACCESS_KEY"))) != NULL)) {
538 Info(
"parseConfig",
"Setting S3 SecretKey and AccessKey. Access Key : %s ", env_var2);
539 davixParam->setAwsAuthorizationKeys(env_var, env_var2);
542 if ( (env_var =
gEnv->GetValue(
"Davix.S3.Region", getenv(
"S3_REGION"))) != NULL) {
546 if( (env_var =
gEnv->GetValue(
"Davix.S3.Token", getenv(
"S3_TOKEN"))) != NULL) {
550 if( (env_var =
gEnv->GetValue(
"Davix.S3.Alternate", getenv(
"S3_ALTERNATE"))) != NULL) {
555 env_var =
gEnv->GetValue(
"Davix.GSI.GridMode", (
const char *)
"y");
565 std::stringstream ss(
option);
567 std::vector<std::string> parsed_options;
569 std::string s3seckey, s3acckey, s3region, s3token;
571 while (std::getline(ss, item,
' ')) {
572 parsed_options.push_back(item);
575 for (std::vector<std::string>::iterator it = parsed_options.begin(); it < parsed_options.end(); ++it) {
598 s3token = std::string(it->c_str() + strlen(
s3_token_opt));
608 if (s3seckey.size() > 0) {
609 setS3Auth(s3seckey, s3acckey, s3region, s3token);
623 davixParam->setMetalinkMode(Davix::MetalinkMode::Disable);
633 DavixError *davixErr = NULL;
637 Error(
"DavixStat",
"can not stat the file with davix: %s (%d)",
638 davixErr->getErrMsg().c_str(), davixErr->getStatus());
639 DavixError::clearError(&davixErr);
650 :
TFile(url, strstr(opt,
"_WITHOUT_GLOBALREGISTRATION") != nullptr ?
"WEB_WITHOUT_GLOBALREGISTRATION" :
"WEB"),
674 if ((
d_ptr->getDavixFileInstance()) == NULL){
685 std::vector<std::string> replicas =
d_ptr->getReplicas();
687 if(!replicas.empty()) {
688 std::stringstream ss;
689 for(
size_t i = 0;
i < replicas.size();
i++) {
691 if(
i != replicas.size()-1) ss <<
"|";
714 Error(
"Seek",
"seeking from end in archive is not (yet) supported");
720 Info(
"Seek",
" move cursor to %lld"
732 if ((fd =
d_ptr->getDavixFileInstance()) == NULL)
739 Info(
"ReadBuffer",
"%lld bytes of data read sequentially"
740 " (%d requested)", ret,
len);
750 if ((fd =
d_ptr->getDavixFileInstance()) == NULL)
758 Info(
"ReadBuffer",
"%lld bytes of data read from offset"
759 " %lld (%d requested)", ret, pos,
len);
768 if ((fd =
d_ptr->getDavixFileInstance()) == NULL)
771 d_ptr->davixPosix->fadvise(fd,
static_cast<dav_off_t
>(offs),
static_cast<dav_size_t
>(
len), Davix::AdviseRandom);
774 Info(
"ReadBufferAsync",
"%d bytes of data prefected from offset"
775 " %lld ",
len, offs);
784 if ((fd =
d_ptr->getDavixFileInstance()) == NULL)
792 Info(
"ReadBuffers",
"%lld bytes of data read from a list of %d buffers",
803 if ((fd =
d_ptr->getDavixFileInstance()) == NULL)
811 Info(
"WriteBuffer",
"%lld bytes of data write"
812 " %d requested", ret,
len);
820 d_ptr->davixParam->setSSLCAcheck((
bool)check);
827 d_ptr->enableGridMode();
835 std::vector<void *>::iterator
f = std::find(
dirdVec.begin(),
dirdVec.end(), fd);
852 std::vector<void *>::iterator
f = std::find(
dirdVec.begin(),
dirdVec.end(), fd);
865 Info(
"GetSize",
"file size requested: %lld", (
Long64_t)st.st_size);
904 DavixError *davixErr = NULL;
909 Error(
"DavixReadBuffer",
"can not read data with davix: %s (%d)",
910 davixErr->getErrMsg().c_str(), davixErr->getStatus());
911 DavixError::clearError(&davixErr);
924 DavixError *davixErr = NULL;
929 Error(
"DavixWriteBuffer",
"can not write data with davix: %s (%d)",
930 davixErr->getErrMsg().c_str(), davixErr->getStatus());
931 DavixError::clearError(&davixErr);
944 DavixError *davixErr = NULL;
949 Error(
"DavixPReadBuffer",
"can not read data with davix: %s (%d)",
950 davixErr->getErrMsg().c_str(), davixErr->getStatus());
951 DavixError::clearError(&davixErr);
964 DavixError *davixErr = NULL;
966 DavIOVecInput in[nbuf];
967 DavIOVecOuput out[nbuf];
971 in[
i].diov_buffer = &buf[lastPos];
972 in[
i].diov_offset = pos[
i];
973 in[
i].diov_size =
len[
i];
977 Long64_t ret =
d_ptr->davixPosix->preadVec(fd, in, out, nbuf, &davixErr);
979 Error(
"DavixReadBuffers",
"can not read data with davix: %s (%d)",
980 davixErr->getErrMsg().c_str(), davixErr->getStatus());
981 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
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.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
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
Int_t DavixStat(const char *url, struct stat *st)
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.
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.
TFile(const TFile &)=delete
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.
bool isno(const char *str)