Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TDavixFile.cxx
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: Adrien Devresse and Tigran Mkrtchyan
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TDavixFile //
15 // //
16 // A TDavixFile is like a normal TFile except that it uses //
17 // libdavix to read/write remote files. //
18 // It supports HTTP and HTTPS in a number of dialects and options //
19 // e.g. S3 is one of them //
20 // Other caracteristics come from the full support of Davix, //
21 // e.g. full redirection support in any circumstance //
22 // //
23 // Authors: Adrien Devresse (CERN IT/SDC) //
24 // Tigran Mkrtchyan (DESY) //
25 // //
26 // Checks and ROOT5 porting: //
27 // Fabrizio Furano (CERN IT/SDC) //
28 // //
29 // September 2013 //
30 // //
31 //////////////////////////////////////////////////////////////////////////
32 
33 
34 #include "TDavixFile.h"
35 #include "TROOT.h"
36 #include "TSocket.h"
37 #include "Bytes.h"
38 #include "TError.h"
39 #include "TSystem.h"
40 #include "TEnv.h"
41 #include "TBase64.h"
42 #include "TVirtualPerfStats.h"
43 #include "TDavixFileInternal.h"
44 #include "TSocket.h"
45 
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <davix.hpp>
51 #include <sstream>
52 #include <string>
53 #include <cstring>
54 
55 
56 static const std::string VERSION = "0.2.0";
57 
58 static const std::string gUserAgent = "ROOT/" + std::string(gROOT->GetVersion()) +
59 " TDavixFile/" + VERSION + " davix/" + Davix::version();
60 
61 // The prefix that is used to find the variables in the gEnv
62 #define ENVPFX "Davix."
63 
65 
66 using namespace Davix;
67 
68 const char* grid_mode_opt = "grid_mode=yes";
69 const char* ca_check_opt = "ca_check=no";
70 const char* s3_seckey_opt = "s3seckey=";
71 const char* s3_acckey_opt = "s3acckey=";
72 const char* s3_region_opt = "s3region=";
73 const char* s3_token_opt = "s3token=";
74 const char* s3_alternate_opt = "s3alternate=";
75 const char* open_mode_read = "READ";
76 const char* open_mode_create = "CREATE";
77 const char* open_mode_new = "NEW";
78 const char* open_mode_update = "UPDATE";
79 
82 
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 
86 bool isno(const char *str)
87 {
88  if (!str) return false;
89 
90  if (!strcmp(str, "n") || !strcmp(str, "no") || !strcmp(str, "0") || !strcmp(str, "false")) return true;
91 
92  return false;
93 
94 }
95 
96 bool strToBool(const char *str, bool defvalue) {
97  if(!str) return defvalue;
98 
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;
101 
102  return defvalue;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 
107 int configure_open_flag(const std::string &str, int old_flag)
108 {
109  if (strcasecmp(str.c_str(), open_mode_read) == 0)
110  old_flag |= O_RDONLY;
111  if ((strcasecmp(str.c_str(), open_mode_create) == 0)
112  || (strcasecmp(str.c_str(), open_mode_new) == 0)) {
113  old_flag |= (O_CREAT | O_WRONLY | O_TRUNC);
114  }
115  if ((strcasecmp(str.c_str(), open_mode_update) == 0)) {
116  old_flag |= (O_RDWR);
117  }
118  return old_flag;
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 
124 {
125  Int_t log_level = (gEnv) ? gEnv->GetValue("Davix.Debug", 0) : 0;
126 
127  switch (log_level) {
128  case 0:
129  davix_set_log_level(0);
130  break;
131  case 1:
132  davix_set_log_level(DAVIX_LOG_WARNING);
133  break;
134  case 2:
135  davix_set_log_level(DAVIX_LOG_VERBOSE);
136  break;
137  case 3:
138  davix_set_log_level(DAVIX_LOG_DEBUG);
139  break;
140  default:
141  davix_set_log_level(DAVIX_LOG_ALL);
142  break;
143  }
144 }
145 
146 ///////////////////////////////////////////////////////////////////
147 // Authn implementation, Locate and get VOMS cred if exist
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 
151 static void TDavixFile_http_get_ucert(std::string &ucert, std::string &ukey)
152 {
153  char default_proxy[64];
154  const char *genvvar = 0, *genvvar1 = 0;
155  // The gEnv has higher priority, let's look for a proxy cert
156  genvvar = gEnv->GetValue("Davix.GSI.UserProxy", (const char *) NULL);
157  if (genvvar) {
158  ucert = ukey = genvvar;
159  if (gDebug > 0)
160  Info("TDavixFile_http_get_ucert", "Found proxy in gEnv");
161  return;
162  }
163 
164  // Try explicit environment for proxy
165  if (getenv("X509_USER_PROXY")) {
166  if (gDebug > 0)
167  Info("TDavixFile_http_get_ucert", "Found proxy in X509_USER_PROXY");
168  ucert = ukey = getenv("X509_USER_PROXY");
169  return;
170  }
171 
172  // Try with default location
173  snprintf(default_proxy, sizeof(default_proxy), "/tmp/x509up_u%d",
174  geteuid());
175 
176  if (access(default_proxy, R_OK) == 0) {
177  if (gDebug > 0)
178  Info("TDavixFile_http_get_ucert", "Found proxy in /tmp");
179  ucert = ukey = default_proxy;
180  return;
181  }
182 
183  // It seems we got no proxy, let's try to gather the keys
184  genvvar = gEnv->GetValue("Davix.GSI.UserCert", (const char *) NULL);
185  genvvar1 = gEnv->GetValue("Davix.GSI.UserKey", (const char *) NULL);
186  if (genvvar || genvvar1) {
187  if (gDebug > 0)
188  Info("TDavixFile_http_get_ucert", "Found cert and key in gEnv");
189 
190  ucert = genvvar;
191  ukey = genvvar1;
192  return;
193  }
194 
195  // try with X509_* environment
196  if (getenv("X509_USER_CERT"))
197  ucert = getenv("X509_USER_CERT");
198  if (getenv("X509_USER_KEY"))
199  ukey = getenv("X509_USER_KEY");
200 
201  if ((ucert.size() > 0) || (ukey.size() > 0)) {
202  if (gDebug > 0)
203  Info("TDavixFile_http_get_ucert", "Found cert and key in gEnv");
204  }
205  return;
206 
207 }
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 
211 static int TDavixFile_http_authn_cert_X509(void *userdata, const Davix::SessionInfo &info,
212  Davix::X509Credential *cert, Davix::DavixError **err)
213 {
214  (void) userdata; // keep quiete compilation warnings
215  (void) info;
216  std::string ucert, ukey;
217  TDavixFile_http_get_ucert(ucert, ukey);
218 
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");
223  return -1;
224  }
225  return cert->loadFromFilePEM(ukey, ucert, "", err);
226 }
227 /////////////////////////////////////////////////////////////////////////////////////////////
228 
229 ////////////////////////////////////////////////////////////////////////////////
230 
232 {
233  delete davixPosix;
234  delete davixParam;
235 }
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 
240 {
241  if (davix_context_s == NULL) {
242  TLockGuard guard(&createLock);
243  if (davix_context_s == NULL) {
244  davix_context_s = new Context();
245  }
246  }
247  return davix_context_s;
248 }
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 
253 {
254  DavixError *davixErr = NULL;
255  Davix_fd *fd = davixPosix->open(davixParam, fUrl.GetUrl(), oflags, &davixErr);
256  if (fd == NULL) {
257  Error("DavixOpen", "can not open file with davix: %s (%d)",
258  davixErr->getErrMsg().c_str(), davixErr->getStatus());
259  DavixError::clearError(&davixErr);
260  } else {
261  // setup ROOT style read
262  davixPosix->fadvise(fd, 0, 300, Davix::AdviseRandom);
263  }
264 
265  return fd;
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 
271 {
272  DavixError *davixErr = NULL;
273  if (davixFd != NULL && davixPosix->close(davixFd, &davixErr)) {
274  Error("DavixClose", "can not to close file with davix: %s (%d)",
275  davixErr->getErrMsg().c_str(), davixErr->getStatus());
276  DavixError::clearError(&davixErr);
277  }
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 
283 {
284  const char *env_var = NULL;
285 
286  if (gDebug > 1)
287  Info("enableGridMode", " grid mode enabled !");
288 
289  if( ( env_var = getenv("X509_CERT_DIR")) == NULL){
290  env_var= "/etc/grid-security/certificates/";
291  }
292  davixParam->addCertificateAuthorityPath(env_var);
293  if (gDebug > 0)
294  Info("enableGridMode", "Adding CAdir %s", env_var);
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 
299 // Only newer versions of davix support setting the S3 region and STS tokens.
300 // But it's only possible to check the davix version through a #define starting from
301 // 0.6.4.
302 // I have no way to check if setAwsRegion is available, so let's use SFINAE. :-)
303 // The first overload will always take priority - if "substitution" fails, meaning
304 // setAwsRegion is not there, the compiler will pick the second overload with
305 // the ellipses. (...)
306 
307 template<typename TRequestParams = Davix::RequestParams>
308 static auto awsRegion(TRequestParams *parameters, const char *region)
309  -> decltype(parameters->setAwsRegion(region), void())
310 {
311  if (gDebug > 1) Info("awsRegion", "Setting S3 Region to '%s' - v4 signature will be used", region);
312  parameters->setAwsRegion(region);
313 }
314 
315 template<typename TRequestParams = Davix::RequestParams>
316 static void awsRegion(...) {
317  Warning("setAwsRegion", "Unable to set AWS region, not supported by this version of davix");
318 }
319 
320 // Identical SFINAE trick as above for setAwsToken
321 template<typename TRequestParams = Davix::RequestParams>
322 static auto awsToken(TRequestParams *parameters, const char *token)
323  -> decltype(parameters->setAwsToken(token), void())
324 {
325  if (gDebug > 1) Info("awsToken", "Setting S3 STS temporary credentials");
326  parameters->setAwsToken(token);
327 }
328 
329 template<typename TRequestParams = Davix::RequestParams>
330 static void awsToken(...) {
331  Warning("awsToken", "Unable to set AWS token, not supported by this version of davix");
332 }
333 
334 // Identical SFINAE trick as above for setAwsAlternate
335 template<typename TRequestParams = Davix::RequestParams>
336 static auto awsAlternate(TRequestParams *parameters, bool option)
337  -> decltype(parameters->setAwsAlternate(option), void())
338 {
339  if (gDebug > 1) Info("awsAlternate", "Setting S3 path-based bucket option (s3alternate)");
340  parameters->setAwsAlternate(option);
341 }
342 
343 template<typename TRequestParams = Davix::RequestParams>
344 static void awsAlternate(...) {
345  Warning("awsAlternate", "Unable to set AWS path-based bucket option (s3alternate), not supported by this version of davix");
346 }
347 
348 void TDavixFileInternal::setAwsRegion(const std::string & region) {
349  if(!region.empty()) {
350  awsRegion(davixParam, region.c_str());
351  }
352 }
353 
354 void TDavixFileInternal::setAwsToken(const std::string & token) {
355  if(!token.empty()) {
356  awsToken(davixParam, token.c_str());
357  }
358 }
359 
360 void TDavixFileInternal::setAwsAlternate(const bool & option) {
361  awsAlternate(davixParam, option);
362 }
363 
364 
365 void TDavixFileInternal::setS3Auth(const std::string &secret, const std::string &access,
366  const std::string &region, const std::string &token)
367 {
368  if (gDebug > 1) {
369  Info("setS3Auth", " Aws S3 tokens configured");
370  }
371  davixParam->setAwsAuthorizationKeys(secret, access);
372  davixParam->setProtocol(RequestProtocol::AwsS3);
373 
374  setAwsRegion(region);
375  setAwsToken(token);
376 }
377 
378 ////////////////////////////////////////////////////////////////////////////////
379 
381 {
382  const char *env_var = NULL, *env_var2 = NULL;
383  // default opts
384  davixParam->setTransparentRedirectionSupport(true);
385  davixParam->setClientCertCallbackX509(&TDavixFile_http_authn_cert_X509, NULL);
386 
387  // setup CADIR
388  env_var = gEnv->GetValue("Davix.GSI.CAdir", (const char *) NULL);
389  if (env_var) {
390  davixParam->addCertificateAuthorityPath(env_var);
391  if (gDebug > 0)
392  Info("parseConfig", "Add CAdir: %s", env_var);
393  }
394  // CA Check
395  bool ca_check_local = !isno(gEnv->GetValue("Davix.GSI.CACheck", (const char *)"y"));
396  davixParam->setSSLCAcheck(ca_check_local);
397  if (gDebug > 0)
398  Info("parseConfig", "Setting CAcheck to %s", ((ca_check_local) ? ("true") : ("false")));
399 
400  // S3 Auth
401  if (((env_var = gEnv->GetValue("Davix.S3.SecretKey", getenv("S3_SECRET_KEY"))) != NULL)
402  && ((env_var2 = gEnv->GetValue("Davix.S3.AccessKey", getenv("S3_ACCESS_KEY"))) != NULL)) {
403  Info("parseConfig", "Setting S3 SecretKey and AccessKey. Access Key : %s ", env_var2);
404  davixParam->setAwsAuthorizationKeys(env_var, env_var2);
405 
406  // need to set region?
407  if ( (env_var = gEnv->GetValue("Davix.S3.Region", getenv("S3_REGION"))) != NULL) {
408  setAwsRegion(env_var);
409  }
410  // need to set STS token?
411  if( (env_var = gEnv->GetValue("Davix.S3.Token", getenv("S3_TOKEN"))) != NULL) {
412  setAwsToken(env_var);
413  }
414  // need to set aws alternate?
415  if( (env_var = gEnv->GetValue("Davix.S3.Alternate", getenv("S3_ALTERNATE"))) != NULL) {
416  setAwsAlternate(strToBool(env_var, false));
417  }
418  }
419 
420  env_var = gEnv->GetValue("Davix.GSI.GridMode", (const char *)"y");
421  if (!isno(env_var))
422  enableGridMode();
423 }
424 
425 ////////////////////////////////////////////////////////////////////////////////
426 /// intput params
427 
429 {
430  std::stringstream ss(option);
431  std::string item;
432  std::vector<std::string> parsed_options;
433  // parameters
434  std::string s3seckey, s3acckey, s3region, s3token;
435 
436  while (std::getline(ss, item, ' ')) {
437  parsed_options.push_back(item);
438  }
439 
440  for (std::vector<std::string>::iterator it = parsed_options.begin(); it < parsed_options.end(); ++it) {
441  // grid mode option
442  if ((strcasecmp(it->c_str(), grid_mode_opt)) == 0) {
443  enableGridMode();
444  }
445  // ca check option
446  if ((strcasecmp(it->c_str(), ca_check_opt)) == 0) {
447  davixParam->setSSLCAcheck(false);
448  }
449  // s3 sec key
450  if (strncasecmp(it->c_str(), s3_seckey_opt, strlen(s3_seckey_opt)) == 0) {
451  s3seckey = std::string(it->c_str() + strlen(s3_seckey_opt));
452  }
453  // s3 access key
454  if (strncasecmp(it->c_str(), s3_acckey_opt, strlen(s3_acckey_opt)) == 0) {
455  s3acckey = std::string(it->c_str() + strlen(s3_acckey_opt));
456  }
457  // s3 region
458  if (strncasecmp(it->c_str(), s3_region_opt, strlen(s3_region_opt)) == 0) {
459  s3region = std::string(it->c_str() + strlen(s3_region_opt));
460  }
461  // s3 sts token
462  if (strncasecmp(it->c_str(), s3_token_opt, strlen(s3_token_opt)) == 0) {
463  s3token = std::string(it->c_str() + strlen(s3_token_opt));
464  }
465  // s3 alternate option
466  if (strncasecmp(it->c_str(), s3_alternate_opt, strlen(s3_alternate_opt)) == 0) {
467  setAwsAlternate(strToBool(it->c_str() + strlen(s3_alternate_opt), false));
468  }
469  // open mods
471  }
472 
473  if (s3seckey.size() > 0) {
474  setS3Auth(s3seckey, s3acckey, s3region, s3token);
475  }
476 
477  if (oflags == 0) // default open mode
478  oflags = O_RDONLY;
479 }
480 
481 ////////////////////////////////////////////////////////////////////////////////
482 
484 {
485  davixPosix = new DavPosix(davixContext);
486  davixParam = new RequestParams();
487  davixParam->setUserAgent(gUserAgent);
489  parseConfig();
490  parseParams(opt);
491 }
492 
493 ////////////////////////////////////////////////////////////////////////////////
494 
495 Int_t TDavixFileInternal::DavixStat(const char *url, struct stat *st)
496 {
497  DavixError *davixErr = NULL;
498 
499  if (davixPosix->stat(davixParam, url, st, &davixErr) < 0) {
500 
501  Error("DavixStat", "can not stat the file with davix: %s (%d)",
502  davixErr->getErrMsg().c_str(), davixErr->getStatus());
503  DavixError::clearError(&davixErr);
504  return 0;
505  }
506  return 1;
507 }
508 
509 /////////////////////////////////////////////////////////////////////////////////////////////
510 
511 ////////////////////////////////////////////////////////////////////////////////
512 
513 TDavixFile::TDavixFile(const char *url, Option_t *opt, const char *ftitle, Int_t compress) : TFile(url, "WEB"),
514  d_ptr(new TDavixFileInternal(fUrl, opt))
515 {
516  (void) ftitle;
517  (void) compress;
518  Init(kFALSE);
519 }
520 
521 ////////////////////////////////////////////////////////////////////////////////
522 
524 {
525  d_ptr->Close();
526  delete d_ptr;
527 }
528 
529 ////////////////////////////////////////////////////////////////////////////////
530 
532 {
533  (void) init;
534  //initialize davix
535  d_ptr->init();
536  // pre-open file
537  if ((d_ptr->getDavixFileInstance()) == NULL){
538  MakeZombie();
539  gDirectory = gROOT;
540  return;
541  }
543  fOffset = 0;
544  fD = -2; // so TFile::IsOpen() will return true when in TFile::~TFi */
545 }
546 
547 ////////////////////////////////////////////////////////////////////////////////
548 /// Set position from where to start reading.
549 
551 {
552  TLockGuard guard(&(d_ptr->positionLock));
553  switch (pos) {
554  case kBeg:
555  fOffset = offset + fArchiveOffset;
556  break;
557  case kCur:
558  fOffset += offset;
559  break;
560  case kEnd:
561  // this option is not used currently in the ROOT code
562  if (fArchiveOffset)
563  Error("Seek", "seeking from end in archive is not (yet) supported");
564  fOffset = fEND - offset; // is fEND really EOF or logical EOF?
565  break;
566  }
567 
568  if (gDebug > 1)
569  Info("Seek", " move cursor to %lld"
570  , fOffset);
571 }
572 
573 ////////////////////////////////////////////////////////////////////////////////
574 /// Read specified byte range from remote file via HTTP.
575 /// Returns kTRUE in case of error.
576 
578 {
579  TLockGuard guard(&(d_ptr->positionLock));
580  Davix_fd *fd;
581  if ((fd = d_ptr->getDavixFileInstance()) == NULL)
582  return kTRUE;
583  Long64_t ret = DavixReadBuffer(fd, buf, len);
584  if (ret < 0)
585  return kTRUE;
586 
587  if (gDebug > 1)
588  Info("ReadBuffer", "%lld bytes of data read sequentially"
589  " (%d requested)", ret, len);
590 
591  return kFALSE;
592 }
593 
594 ////////////////////////////////////////////////////////////////////////////////
595 
597 {
598  Davix_fd *fd;
599  if ((fd = d_ptr->getDavixFileInstance()) == NULL)
600  return kTRUE;
601 
602  Long64_t ret = DavixPReadBuffer(fd, buf, pos, len);
603  if (ret < 0)
604  return kTRUE;
605 
606  if (gDebug > 1)
607  Info("ReadBuffer", "%lld bytes of data read from offset"
608  " %lld (%d requested)", ret, pos, len);
609  return kFALSE;
610 }
611 
612 ////////////////////////////////////////////////////////////////////////////////
613 
615 {
616  Davix_fd *fd;
617  if ((fd = d_ptr->getDavixFileInstance()) == NULL)
618  return kFALSE;
619 
620  d_ptr->davixPosix->fadvise(fd, static_cast<dav_off_t>(offs), static_cast<dav_size_t>(len), Davix::AdviseRandom);
621 
622  if (gDebug > 1)
623  Info("ReadBufferAsync", "%d bytes of data prefected from offset"
624  " %lld ", len, offs);
625  return kFALSE;
626 }
627 
628 ////////////////////////////////////////////////////////////////////////////////
629 
630 Bool_t TDavixFile::ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
631 {
632  Davix_fd *fd;
633  if ((fd = d_ptr->getDavixFileInstance()) == NULL)
634  return kTRUE;
635 
636  Long64_t ret = DavixReadBuffers(fd, buf, pos, len, nbuf);
637  if (ret < 0)
638  return kTRUE;
639 
640  if (gDebug > 1)
641  Info("ReadBuffers", "%lld bytes of data read from a list of %d buffers",
642  ret, nbuf);
643 
644  return kFALSE;
645 }
646 
647 ////////////////////////////////////////////////////////////////////////////////
648 
649 Bool_t TDavixFile::WriteBuffer(const char *buf, Int_t len)
650 {
651  Davix_fd *fd;
652  if ((fd = d_ptr->getDavixFileInstance()) == NULL)
653  return kTRUE;
654 
655  Long64_t ret = DavixWriteBuffer(fd, buf, len);
656  if (ret < 0)
657  return kTRUE;
658 
659  if (gDebug > 1)
660  Info("WriteBuffer", "%lld bytes of data write"
661  " %d requested", ret, len);
662  return kFALSE;
663 }
664 
665 ////////////////////////////////////////////////////////////////////////////////
666 
668 {
669  d_ptr->davixParam->setSSLCAcheck((bool)check);
670 }
671 
672 ////////////////////////////////////////////////////////////////////////////////
673 
675 {
677 }
678 
679 ////////////////////////////////////////////////////////////////////////////////
680 
682 {
683  TLockGuard l(&(openLock));
684  std::vector<void *>::iterator f = std::find(dirdVec.begin(), dirdVec.end(), fd);
685  return (f != dirdVec.end());
686 }
687 
688 ////////////////////////////////////////////////////////////////////////////////
689 
691 {
692  TLockGuard l(&(openLock));
693  dirdVec.push_back(fd);
694 }
695 
696 ////////////////////////////////////////////////////////////////////////////////
697 
699 {
700  TLockGuard l(&(openLock));
701  std::vector<void *>::iterator f = std::find(dirdVec.begin(), dirdVec.end(), fd);
702  if (f != dirdVec.end())
703  dirdVec.erase(f);
704 }
705 
706 ////////////////////////////////////////////////////////////////////////////////
707 
709 {
710  struct stat st;
711  Int_t ret = d_ptr->DavixStat(fUrl.GetUrl(), &st);
712  if (ret) {
713  if (gDebug > 1)
714  Info("GetSize", "file size requested: %lld", (Long64_t)st.st_size);
715  return st.st_size;
716  }
717  return -1;
718 }
719 
720 ////////////////////////////////////////////////////////////////////////////////
721 
723 {
724  if (gPerfStats)
725  return TTimeStamp();
726  return 0;
727 }
728 
729 ////////////////////////////////////////////////////////////////////////////////
730 /// set TFile state info
731 
732 void TDavixFile::eventStop(Double_t t_start, Long64_t len, bool read)
733 {
734  if(read) {
735  fBytesRead += len;
736  fReadCalls += 1;
737 
740 
741  if (gPerfStats)
742  gPerfStats->FileReadEvent(this, (Int_t) len, t_start);
743  } else {
744  fBytesWrite += len;
746  }
747 }
748 
749 ////////////////////////////////////////////////////////////////////////////////
750 
751 Long64_t TDavixFile::DavixReadBuffer(Davix_fd *fd, char *buf, Int_t len)
752 {
753  DavixError *davixErr = NULL;
754  Double_t start_time = eventStart();
755 
756  Long64_t ret = d_ptr->davixPosix->pread(fd, buf, len, fOffset, &davixErr);
757  if (ret < 0) {
758  Error("DavixReadBuffer", "can not read data with davix: %s (%d)",
759  davixErr->getErrMsg().c_str(), davixErr->getStatus());
760  DavixError::clearError(&davixErr);
761  } else {
762  fOffset += ret;
763  eventStop(start_time, ret);
764  }
765 
766  return ret;
767 }
768 
769 ////////////////////////////////////////////////////////////////////////////////
770 
771 Long64_t TDavixFile::DavixWriteBuffer(Davix_fd *fd, const char *buf, Int_t len)
772 {
773  DavixError *davixErr = NULL;
774  Double_t start_time = eventStart();
775 
776  Long64_t ret = d_ptr->davixPosix->pwrite(fd, buf, len, fOffset, &davixErr);
777  if (ret < 0) {
778  Error("DavixWriteBuffer", "can not write data with davix: %s (%d)",
779  davixErr->getErrMsg().c_str(), davixErr->getStatus());
780  DavixError::clearError(&davixErr);
781  } else {
782  fOffset += ret;
783  eventStop(start_time, ret, false);
784  }
785 
786  return ret;
787 }
788 
789 ////////////////////////////////////////////////////////////////////////////////
790 
791 Long64_t TDavixFile::DavixPReadBuffer(Davix_fd *fd, char *buf, Long64_t pos, Int_t len)
792 {
793  DavixError *davixErr = NULL;
794  Double_t start_time = eventStart();
795 
796  Long64_t ret = d_ptr->davixPosix->pread(fd, buf, len, pos, &davixErr);
797  if (ret < 0) {
798  Error("DavixPReadBuffer", "can not read data with davix: %s (%d)",
799  davixErr->getErrMsg().c_str(), davixErr->getStatus());
800  DavixError::clearError(&davixErr);
801  } else {
802  eventStop(start_time, ret);
803  }
804 
805 
806  return ret;
807 }
808 
809 ////////////////////////////////////////////////////////////////////////////////
810 
811 Long64_t TDavixFile::DavixReadBuffers(Davix_fd *fd, char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
812 {
813  DavixError *davixErr = NULL;
814  Double_t start_time = eventStart();
815  DavIOVecInput in[nbuf];
816  DavIOVecOuput out[nbuf];
817 
818  int lastPos = 0;
819  for (Int_t i = 0; i < nbuf; ++i) {
820  in[i].diov_buffer = &buf[lastPos];
821  in[i].diov_offset = pos[i];
822  in[i].diov_size = len[i];
823  lastPos += len[i];
824  }
825 
826  Long64_t ret = d_ptr->davixPosix->preadVec(fd, in, out, nbuf, &davixErr);
827  if (ret < 0) {
828  Error("DavixReadBuffers", "can not read data with davix: %s (%d)",
829  davixErr->getErrMsg().c_str(), davixErr->getStatus());
830  DavixError::clearError(&davixErr);
831  } else {
832  eventStop(start_time, ret);
833  }
834 
835  return ret;
836 }
static void ConfigureDavixLogLevel()
Definition: TDavixFile.cxx:123
Definition: TMutex.h:30
double read(const std::string &file_name)
reading
static const std::string gUserAgent
Definition: TDavixFile.cxx:58
const char * s3_seckey_opt
Definition: TDavixFile.cxx:70
long long Long64_t
Definition: RtypesCore.h:69
Long64_t fBytesWrite
Number of bytes written to this file.
Definition: TFile.h:68
const char * open_mode_read
Definition: TDavixFile.cxx:75
const char * open_mode_update
Definition: TDavixFile.cxx:78
void Init(Bool_t init)
Initialize a TFile object.
Definition: TDavixFile.cxx:531
const char Option_t
Definition: RtypesCore.h:62
const char * s3_alternate_opt
Definition: TDavixFile.cxx:74
static void TDavixFile_http_get_ucert(std::string &ucert, std::string &ukey)
Definition: TDavixFile.cxx:151
void setS3Auth(const std::string &secret, const std::string &access, const std::string &region, const std::string &token)
Definition: TDavixFile.cxx:365
static auto awsToken(TRequestParams *parameters, const char *token) -> decltype(parameters->setAwsToken(token), void())
Definition: TDavixFile.cxx:322
Davix::RequestParams * davixParam
Small helper to keep current directory context.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:847
Davix_fd * Open()
Definition: TDavixFile.cxx:252
static void SetFileReadCalls(Int_t readcalls=0)
Definition: TFile.cxx:4417
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read specified byte range from remote file via HTTP.
Definition: TDavixFile.cxx:577
#define gROOT
Definition: TROOT.h:375
void addDird(void *fd)
Definition: TDavixFile.cxx:690
bool strToBool(const char *str, bool defvalue)
Definition: TDavixFile.cxx:96
Double_t eventStart()
Definition: TDavixFile.cxx:722
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TDavixFileInternal * d_ptr
Definition: TDavixFile.h:68
Long64_t DavixReadBuffer(Davix_fd *fd, char *buf, Int_t len)
Definition: TDavixFile.cxx:751
ERelativeTo
Definition: TFile.h:171
#define NULL
Definition: RtypesCore.h:88
Int_t fReadCalls
Number of read calls ( not counting the cache calls )
Definition: TFile.h:82
virtual Bool_t WriteBuffer(const char *buffer, Int_t bufferLength)
Write a buffer to the file.
Definition: TDavixFile.cxx:649
void setAwsAlternate(const bool &option)
Definition: TDavixFile.cxx:360
Davix_fd * getDavixFileInstance()
void setAwsRegion(const std::string &region)
Definition: TDavixFile.cxx:348
const char * open_mode_create
Definition: TDavixFile.cxx:76
Long64_t DavixReadBuffers(Davix_fd *fd, char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
Definition: TDavixFile.cxx:811
static void SetFileBytesWritten(Long64_t bytes=0)
Definition: TFile.cxx:4414
TUrl fUrl
!URL of file
Definition: TFile.h:103
Int_t fD
File descriptor.
Definition: TFile.h:75
static auto awsAlternate(TRequestParams *parameters, bool option) -> decltype(parameters->setAwsAlternate(option), void())
Definition: TDavixFile.cxx:336
Davix::DavPosix * davixPosix
virtual void Seek(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
Definition: TDavixFile.cxx:550
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
Definition: TFile.cxx:4377
virtual Long64_t GetSize() const
Returns the current file size.
Definition: TDavixFile.cxx:708
static Long64_t GetFileBytesWritten()
Static function returning the total number of bytes written to all files.
Definition: TFile.cxx:4386
void Info(const char *location, const char *msgfmt,...)
static TMutex createLock
Definition: TDavixFile.cxx:80
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
void Error(const char *location, const char *msgfmt,...)
Int_t DavixStat(const char *url, struct stat *st)
Definition: TDavixFile.cxx:495
static Context * davix_context_s
Definition: TDavixFile.cxx:81
Long64_t DavixWriteBuffer(Davix_fd *fd, const char *buf, Int_t len)
Definition: TDavixFile.cxx:771
Long64_t fEND
Last used byte in file.
Definition: TFile.h:72
virtual Bool_t ReadBufferAsync(Long64_t offs, Int_t len)
Definition: TDavixFile.cxx:614
const char * s3_token_opt
Definition: TDavixFile.cxx:73
static Davix::Context * getDavixInstance()
Definition: TDavixFile.cxx:239
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:482
void removeDird(void *fd)
Definition: TDavixFile.cxx:698
static auto awsRegion(TRequestParams *parameters, const char *region) -> decltype(parameters->setAwsRegion(region), void())
Definition: TDavixFile.cxx:308
bool isMyDird(void *fd)
Definition: TDavixFile.cxx:681
TLine * l
Definition: textangle.C:4
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
Definition: TFile.cxx:4394
void eventStop(Double_t t, Long64_t len, bool read=true)
set TFile state info
Definition: TDavixFile.cxx:732
void Warning(const char *location, const char *msgfmt,...)
bool isno(const char *str)
Definition: TDavixFile.cxx:86
virtual void Init(Bool_t create)
Initialize a TFile object.
Definition: TFile.cxx:584
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.
Definition: TDavixFile.cxx:630
#define gPerfStats
const Bool_t kFALSE
Definition: RtypesCore.h:92
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:387
std::vector< void * > dirdVec
const char * grid_mode_opt
Definition: TDavixFile.cxx:68
static const std::string VERSION
Definition: TDavixFile.cxx:56
#define ClassImp(name)
Definition: Rtypes.h:336
double f(double x)
static Int_t init()
double Double_t
Definition: RtypesCore.h:55
R__EXTERN TEnv * gEnv
Definition: TEnv.h:170
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
const char * s3_acckey_opt
Definition: TDavixFile.cxx:71
int configure_open_flag(const std::string &str, int old_flag)
Definition: TDavixFile.cxx:107
const char * s3_region_opt
Definition: TDavixFile.cxx:72
void enableGridMode()
Enable the grid mode The grid Mode configure automatically all grid-CA path, VOMS authentication and ...
Definition: TDavixFile.cxx:674
typedef void((*Func_t)())
void setAwsToken(const std::string &token)
Definition: TDavixFile.cxx:354
const char * ca_check_opt
Definition: TDavixFile.cxx:69
void MakeZombie()
Definition: TObject.h:49
#define snprintf
Definition: civetweb.c:822
static int TDavixFile_http_authn_cert_X509(void *userdata, const Davix::SessionInfo &info, Davix::X509Credential *cert, Davix::DavixError **err)
Definition: TDavixFile.cxx:211
R__EXTERN Int_t gDebug
Definition: Rtypes.h:83
Long64_t fArchiveOffset
!Offset at which file starts in archive
Definition: TFile.h:94
#define gDirectory
Definition: TDirectory.h:211
TDavixFile(const char *url, Option_t *option="", const char *ftitle="", Int_t compress=1)
Open function for TDavixFile.
Definition: TDavixFile.cxx:513
Long64_t fOffset
!Seek offset cache
Definition: TFile.h:89
static void SetFileBytesRead(Long64_t bytes=0)
Definition: TFile.cxx:4411
Long64_t DavixPReadBuffer(Davix_fd *fd, char *buf, Long64_t pos, Int_t len)
Definition: TDavixFile.cxx:791
void setCACheck(Bool_t check)
Enable or disable certificate authority check.
Definition: TDavixFile.cxx:667
const Bool_t kTRUE
Definition: RtypesCore.h:91
void parseParams(Option_t *option)
intput params
Definition: TDavixFile.cxx:428
Long64_t fBytesRead
Number of bytes read from this file.
Definition: TFile.h:69
Davix::Context * davixContext
const char * open_mode_new
Definition: TDavixFile.cxx:77