56 static const std::string
VERSION =
"0.2.0";
58 static const std::string
gUserAgent =
"ROOT/" + std::string(
gROOT->GetVersion()) +
59 " TDavixFile/" +
VERSION +
" davix/" + Davix::version();
62 #define ENVPFX "Davix." 66 using namespace Davix;
88 if (!str)
return false;
90 if (!strcmp(str,
"n") || !strcmp(str,
"no") || !strcmp(str,
"0") || !strcmp(str,
"false"))
return true;
97 if(!str)
return defvalue;
99 if(strcmp(str,
"n") == 0 || strcmp(str,
"no") == 0 || strcmp(str,
"0") == 0 || strcmp(str,
"false") == 0)
return false;
100 if(strcmp(str,
"y") == 0 || strcmp(str,
"yes") == 0 || strcmp(str,
"1") == 0 || strcmp(str,
"true") == 0)
return true;
110 old_flag |= O_RDONLY;
113 old_flag |= (O_CREAT | O_WRONLY | O_TRUNC);
116 old_flag |= (O_RDWR);
129 davix_set_log_level(0);
132 davix_set_log_level(DAVIX_LOG_WARNING);
135 davix_set_log_level(DAVIX_LOG_VERBOSE);
138 davix_set_log_level(DAVIX_LOG_DEBUG);
141 davix_set_log_level(DAVIX_LOG_ALL);
153 char default_proxy[64];
154 const char *genvvar = 0, *genvvar1 = 0;
158 ucert = ukey = genvvar;
160 Info(
"TDavixFile_http_get_ucert",
"Found proxy in gEnv");
165 if (getenv(
"X509_USER_PROXY")) {
167 Info(
"TDavixFile_http_get_ucert",
"Found proxy in X509_USER_PROXY");
168 ucert = ukey = getenv(
"X509_USER_PROXY");
173 snprintf(default_proxy,
sizeof(default_proxy),
"/tmp/x509up_u%d",
176 if (access(default_proxy, R_OK) == 0) {
178 Info(
"TDavixFile_http_get_ucert",
"Found proxy in /tmp");
179 ucert = ukey = default_proxy;
184 genvvar =
gEnv->
GetValue(
"Davix.GSI.UserCert", (
const char *) NULL);
185 genvvar1 =
gEnv->
GetValue(
"Davix.GSI.UserKey", (
const char *) NULL);
186 if (genvvar || genvvar1) {
188 Info(
"TDavixFile_http_get_ucert",
"Found cert and key in gEnv");
196 if (getenv(
"X509_USER_CERT"))
197 ucert = getenv(
"X509_USER_CERT");
198 if (getenv(
"X509_USER_KEY"))
199 ukey = getenv(
"X509_USER_KEY");
201 if ((ucert.size() > 0) || (ukey.size() > 0)) {
203 Info(
"TDavixFile_http_get_ucert",
"Found cert and key in gEnv");
212 Davix::X509Credential *cert, Davix::DavixError **err)
216 std::string ucert, ukey;
219 if (ucert.empty() || ukey.empty()) {
220 Davix::DavixError::setupError(err,
"TDavixFile",
221 Davix::StatusCode::AuthentificationError,
222 "Could not set the user's proxy or certificate");
225 return cert->loadFromFilePEM(ukey, ucert,
"", err);
241 if (davix_context_s ==
NULL) {
243 if (davix_context_s ==
NULL) {
244 davix_context_s =
new Context();
254 DavixError *davixErr =
NULL;
255 Davix_fd *fd = davixPosix->open(davixParam, fUrl.GetUrl(), oflags, &davixErr);
262 DavixError *davixErr2 =
NULL;
264 DavFile
file(*davixContext, Davix::Uri(fUrl.GetUrl()));
265 std::vector<DavFile> replicasLocal = file.getReplicas(
NULL, &davixErr2);
266 for(
size_t i = 0; i < replicasLocal.size(); i++) {
267 replicas.push_back(replicasLocal[i].getUri().getString());
271 DavixError::clearError(&davixErr2);
273 if(replicas.empty()) {
276 Error(
"DavixOpen",
"can not open file with davix: %s (%d)",
277 davixErr->getErrMsg().c_str(), davixErr->getStatus());
279 DavixError::clearError(&davixErr);
282 davixPosix->fadvise(fd, 0, 300, Davix::AdviseRandom);
292 DavixError *davixErr =
NULL;
293 if (davixFd !=
NULL && davixPosix->close(davixFd, &davixErr)) {
294 Error(
"DavixClose",
"can not to close file with davix: %s (%d)",
295 davixErr->getErrMsg().c_str(), davixErr->getStatus());
296 DavixError::clearError(&davixErr);
304 const char *env_var =
NULL;
307 Info(
"enableGridMode",
" grid mode enabled !");
309 if( ( env_var = getenv(
"X509_CERT_DIR")) ==
NULL){
310 env_var=
"/etc/grid-security/certificates/";
312 davixParam->addCertificateAuthorityPath(env_var);
314 Info(
"enableGridMode",
"Adding CAdir %s", env_var);
327 template<
typename TRequestParams = Davix::RequestParams>
328 static auto awsRegion(TRequestParams *parameters,
const char *region)
329 -> decltype(parameters->setAwsRegion(region),
void())
331 if (
gDebug > 1)
Info(
"awsRegion",
"Setting S3 Region to '%s' - v4 signature will be used", region);
332 parameters->setAwsRegion(region);
335 template<
typename TRequestParams = Davix::RequestParams>
337 Warning(
"setAwsRegion",
"Unable to set AWS region, not supported by this version of davix");
341 template<
typename TRequestParams = Davix::RequestParams>
342 static auto awsToken(TRequestParams *parameters,
const char *token)
343 -> decltype(parameters->setAwsToken(token),
void())
345 if (
gDebug > 1)
Info(
"awsToken",
"Setting S3 STS temporary credentials");
346 parameters->setAwsToken(token);
349 template<
typename TRequestParams = Davix::RequestParams>
351 Warning(
"awsToken",
"Unable to set AWS token, not supported by this version of davix");
355 template<
typename TRequestParams = Davix::RequestParams>
357 -> decltype(parameters->setAwsAlternate(option),
void())
359 if (
gDebug > 1)
Info(
"awsAlternate",
"Setting S3 path-based bucket option (s3alternate)");
360 parameters->setAwsAlternate(option);
363 template<
typename TRequestParams = Davix::RequestParams>
365 Warning(
"awsAlternate",
"Unable to set AWS path-based bucket option (s3alternate), not supported by this version of davix");
369 if(!region.empty()) {
376 awsToken(davixParam, token.c_str());
386 const std::string ®ion,
const std::string &token)
389 Info(
"setS3Auth",
" Aws S3 tokens configured");
391 davixParam->setAwsAuthorizationKeys(secret, access);
392 davixParam->setProtocol(RequestProtocol::AwsS3);
394 setAwsRegion(region);
402 const char *env_var =
NULL, *env_var2 =
NULL;
404 davixParam->setTransparentRedirectionSupport(
true);
410 davixParam->addCertificateAuthorityPath(env_var);
412 Info(
"parseConfig",
"Add CAdir: %s", env_var);
415 bool ca_check_local = !
isno(
gEnv->
GetValue(
"Davix.GSI.CACheck", (
const char *)
"y"));
416 davixParam->setSSLCAcheck(ca_check_local);
418 Info(
"parseConfig",
"Setting CAcheck to %s", ((ca_check_local) ? (
"true") : (
"false")));
421 if (((env_var =
gEnv->
GetValue(
"Davix.S3.SecretKey", getenv(
"S3_SECRET_KEY"))) != NULL)
422 && ((env_var2 =
gEnv->
GetValue(
"Davix.S3.AccessKey", getenv(
"S3_ACCESS_KEY"))) != NULL)) {
423 Info(
"parseConfig",
"Setting S3 SecretKey and AccessKey. Access Key : %s ", env_var2);
424 davixParam->setAwsAuthorizationKeys(env_var, env_var2);
427 if ( (env_var =
gEnv->
GetValue(
"Davix.S3.Region", getenv(
"S3_REGION"))) != NULL) {
428 setAwsRegion(env_var);
431 if( (env_var =
gEnv->
GetValue(
"Davix.S3.Token", getenv(
"S3_TOKEN"))) != NULL) {
432 setAwsToken(env_var);
435 if( (env_var =
gEnv->
GetValue(
"Davix.S3.Alternate", getenv(
"S3_ALTERNATE"))) != NULL) {
436 setAwsAlternate(
strToBool(env_var,
false));
440 env_var =
gEnv->
GetValue(
"Davix.GSI.GridMode", (
const char *)
"y");
450 std::stringstream ss(option);
452 std::vector<std::string> parsed_options;
454 std::string s3seckey, s3acckey, s3region, s3token;
456 while (std::getline(ss, item,
' ')) {
457 parsed_options.push_back(item);
460 for (std::vector<std::string>::iterator it = parsed_options.begin(); it < parsed_options.end(); ++it) {
467 davixParam->setSSLCAcheck(
false);
470 if (strncasecmp(it->c_str(),
s3_seckey_opt, strlen(s3_seckey_opt)) == 0) {
471 s3seckey = std::string(it->c_str() + strlen(s3_seckey_opt));
474 if (strncasecmp(it->c_str(),
s3_acckey_opt, strlen(s3_acckey_opt)) == 0) {
475 s3acckey = std::string(it->c_str() + strlen(s3_acckey_opt));
478 if (strncasecmp(it->c_str(),
s3_region_opt, strlen(s3_region_opt)) == 0) {
479 s3region = std::string(it->c_str() + strlen(s3_region_opt));
482 if (strncasecmp(it->c_str(),
s3_token_opt, strlen(s3_token_opt)) == 0) {
483 s3token = std::string(it->c_str() + strlen(s3_token_opt));
486 if (strncasecmp(it->c_str(),
s3_alternate_opt, strlen(s3_alternate_opt)) == 0) {
487 setAwsAlternate(
strToBool(it->c_str() + strlen(s3_alternate_opt),
false));
493 if (s3seckey.size() > 0) {
494 setS3Auth(s3seckey, s3acckey, s3region, s3token);
505 davixPosix =
new DavPosix(davixContext);
506 davixParam =
new RequestParams();
508 davixParam->setMetalinkMode(Davix::MetalinkMode::Disable);
518 DavixError *davixErr =
NULL;
520 if (davixPosix->stat(davixParam, url, st, &davixErr) < 0) {
522 Error(
"DavixStat",
"can not stat the file with davix: %s (%d)",
523 davixErr->getErrMsg().c_str(), davixErr->getStatus());
524 DavixError::clearError(&davixErr);
571 if(!replicas.empty()) {
572 std::stringstream ss;
573 for(
size_t i = 0; i < replicas.size(); i++) {
575 if(i != replicas.size()-1) ss <<
"|";
598 Error(
"Seek",
"seeking from end in archive is not (yet) supported");
604 Info(
"Seek",
" move cursor to %lld" 623 Info(
"ReadBuffer",
"%lld bytes of data read sequentially" 624 " (%d requested)", ret, len);
642 Info(
"ReadBuffer",
"%lld bytes of data read from offset" 643 " %lld (%d requested)", ret, pos, len);
655 d_ptr->
davixPosix->fadvise(fd, static_cast<dav_off_t>(offs), static_cast<dav_size_t>(len), Davix::AdviseRandom);
658 Info(
"ReadBufferAsync",
"%d bytes of data prefected from offset" 659 " %lld ", len, offs);
676 Info(
"ReadBuffers",
"%lld bytes of data read from a list of %d buffers",
695 Info(
"WriteBuffer",
"%lld bytes of data write" 696 " %d requested", ret, len);
719 std::vector<void *>::iterator
f = std::find(dirdVec.begin(), dirdVec.end(), fd);
720 return (f != dirdVec.end());
728 dirdVec.push_back(fd);
736 std::vector<void *>::iterator
f = std::find(dirdVec.begin(), dirdVec.end(), fd);
737 if (f != dirdVec.end())
749 Info(
"GetSize",
"file size requested: %lld", (
Long64_t)st.st_size);
788 DavixError *davixErr =
NULL;
793 Error(
"DavixReadBuffer",
"can not read data with davix: %s (%d)",
794 davixErr->getErrMsg().c_str(), davixErr->getStatus());
795 DavixError::clearError(&davixErr);
808 DavixError *davixErr =
NULL;
813 Error(
"DavixWriteBuffer",
"can not write data with davix: %s (%d)",
814 davixErr->getErrMsg().c_str(), davixErr->getStatus());
815 DavixError::clearError(&davixErr);
828 DavixError *davixErr =
NULL;
833 Error(
"DavixPReadBuffer",
"can not read data with davix: %s (%d)",
834 davixErr->getErrMsg().c_str(), davixErr->getStatus());
835 DavixError::clearError(&davixErr);
848 DavixError *davixErr =
NULL;
850 DavIOVecInput in[nbuf];
851 DavIOVecOuput out[nbuf];
854 for (
Int_t i = 0; i < nbuf; ++i) {
855 in[i].diov_buffer = &buf[lastPos];
856 in[i].diov_offset = pos[i];
857 in[i].diov_size = len[i];
863 Error(
"DavixReadBuffers",
"can not read data with davix: %s (%d)",
864 davixErr->getErrMsg().c_str(), davixErr->getStatus());
865 DavixError::clearError(&davixErr);
static void ConfigureDavixLogLevel()
double read(const std::string &file_name)
reading
virtual Long64_t GetSize() const
Returns the current file size.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
static const std::string gUserAgent
const char * s3_seckey_opt
Long64_t fBytesWrite
Number of bytes written to this file.
const char * open_mode_read
const char * open_mode_update
void Init(Bool_t init)
Initialize a TFile object.
const char * s3_alternate_opt
static void TDavixFile_http_get_ucert(std::string &ucert, std::string &ukey)
void setS3Auth(const std::string &secret, const std::string &access, const std::string ®ion, const std::string &token)
static auto awsToken(TRequestParams *parameters, const char *token) -> decltype(parameters->setAwsToken(token), void())
Davix::RequestParams * davixParam
Small helper to keep current directory context.
static void SetFileReadCalls(Int_t readcalls=0)
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read specified byte range from remote file via HTTP.
bool strToBool(const char *str, bool defvalue)
TDavixFileInternal * d_ptr
Long64_t DavixReadBuffer(Davix_fd *fd, char *buf, Int_t len)
Int_t fReadCalls
Number of read calls ( not counting the cache calls )
virtual Bool_t WriteBuffer(const char *buffer, Int_t bufferLength)
Write a buffer to the file.
void setAwsAlternate(const bool &option)
Davix_fd * getDavixFileInstance()
void setAwsRegion(const std::string ®ion)
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
const char * open_mode_create
Long64_t DavixReadBuffers(Davix_fd *fd, char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
static void SetFileBytesWritten(Long64_t bytes=0)
static auto awsAlternate(TRequestParams *parameters, bool option) -> decltype(parameters->setAwsAlternate(option), void())
Davix::DavPosix * davixPosix
virtual void Seek(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
static Long64_t GetFileBytesWritten()
Static function returning the total number of bytes written to all files.
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
Int_t DavixStat(const char *url, struct stat *st)
static Context * davix_context_s
Long64_t DavixWriteBuffer(Davix_fd *fd, const char *buf, Int_t len)
Long64_t fEND
Last used byte in file.
virtual Bool_t ReadBufferAsync(Long64_t offs, Int_t len)
const char * s3_token_opt
virtual TString GetNewUrl()
static Davix::Context * getDavixInstance()
std::vector< std::string > getReplicas()
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
void removeDird(void *fd)
static auto awsRegion(TRequestParams *parameters, const char *region) -> decltype(parameters->setAwsRegion(region), void())
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
void eventStop(Double_t t, Long64_t len, bool read=true)
set TFile state info
void Warning(const char *location, const char *msgfmt,...)
bool isno(const char *str)
virtual void Init(Bool_t create)
Initialize a TFile object.
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.
const char * grid_mode_opt
static const std::string VERSION
The TTimeStamp encapsulates seconds and ns since EPOCH.
const char * s3_acckey_opt
int configure_open_flag(const std::string &str, int old_flag)
const char * s3_region_opt
void enableGridMode()
Enable the grid mode The grid Mode configure automatically all grid-CA path, VOMS authentication and ...
typedef void((*Func_t)())
void setAwsToken(const std::string &token)
const char * ca_check_opt
static int TDavixFile_http_authn_cert_X509(void *userdata, const Davix::SessionInfo &info, Davix::X509Credential *cert, Davix::DavixError **err)
Long64_t fArchiveOffset
!Offset at which file starts in archive
TDavixFile(const char *url, Option_t *option="", const char *ftitle="", Int_t compress=1)
Open function for TDavixFile.
Long64_t fOffset
!Seek offset cache
static void SetFileBytesRead(Long64_t bytes=0)
Long64_t DavixPReadBuffer(Davix_fd *fd, char *buf, Long64_t pos, Int_t len)
void setCACheck(Bool_t check)
Enable or disable certificate authority check.
void parseParams(Option_t *option)
intput params
Long64_t fBytesRead
Number of bytes read from this file.
const char * open_mode_new