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