51 #define MAX_FILE_IMAGES 16
82 TAlienFile::TAlienFile(
const char *purl, Option_t *option,
83 const char *ftitle, Int_t compress,
84 Bool_t parallelopen,
const char *lurl,
86 TXNetFile(purl, option, ftitle, compress, 0, parallelopen, lurl)
88 TUrl logicalurl(lurl);
89 fLfn = logicalurl.GetFile();
104 TAlienFile *TAlienFile::Open(
const char *url, Option_t *option,
105 const char *ftitle, Int_t compress,
109 ::Error(
"TAlienFileAccess",
"No GRID connection available!");
115 if (gMonitoringWriter)
116 gMonitoringWriter->SendFileOpenProgress(0, 0,
"alienopen", kFALSE);
118 TString name(TString(
"alien://") + TString(lUrl.GetFile()));
119 TString fAName = name;
120 TString fAOption = option;
122 TString sguid, pfnStr;
139 TString storageelement;
141 TString file = purl.GetFile();
143 Bool_t publicaccess = kFALSE;
144 storageelement = gSystem->Getenv(
"alien_CLOSE_SE");
147 TString urloptions = purl.GetOptions();
148 TObjArray *objOptions = urloptions.Tokenize(
"&");
149 for (Int_t n = 0; n < objOptions->GetEntries(); n++) {
150 TString loption = ((TObjString *) objOptions->At(n))->GetName();
151 TObjArray *objTags = loption.Tokenize(
"=");
152 if (objTags->GetEntries() == 2) {
153 TString key = ((TObjString *) objTags->At(0))->GetName();
154 TString value = ((TObjString *) objTags->At(1))->GetName();
155 if (!key.CompareTo(
"se", TString::kIgnoreCase)) {
156 storageelement = value;
158 if (!key.CompareTo(
"publicaccess")) {
159 if (atoi(value.Data()))
160 publicaccess = kTRUE;
170 TObjString *urlStr = 0;
172 TObjString *authzStr = 0;
173 TObjString *seStr = 0;
174 TString seStrs, snreplicas;
182 TAlienResult *alienResult;
191 if (fAOption ==
"NEW")
194 create = (fAOption ==
"CREATE") ? kTRUE : kFALSE;
195 recreate = (fAOption ==
"RECREATE") ? kTRUE : kFALSE;
196 update = (fAOption ==
"UPDATE") ? kTRUE : kFALSE;
197 read = (fAOption ==
"READ") ? kTRUE : kFALSE;
199 if (!create && !recreate && !update && !read) {
204 if (create || recreate || update) {
209 fAOption =
"RECREATE";
217 ::Error(
"TAlienFile::Open",
"no active GRID connection found");
221 if (gMonitoringWriter)
222 gMonitoringWriter->SendFileOpenProgress(0, 0, 0, kFALSE);
226 if ((strcmp(gGrid->GetGrid(),
"alien"))) {
227 ::Error(
"TAlienFile::Open",
"you don't have an active <alien> grid!");
231 if (gMonitoringWriter)
232 gMonitoringWriter->SendFileOpenProgress(0, 0, 0, kFALSE);
244 command = TString(
"access -p read ");
246 command = TString(
"access read ");
250 command = TString(
"access write-once ");
254 command = TString(
"access write-version ");
258 command = TString(
"access write-version ");
266 command += storageelement;
269 TString fALfn = file;
275 repcommand = command;
278 if (storageelement !=
"") {
280 repcommand += storageelement;
283 repcommand +=
"unknown";
286 repcommand += imagenr;
289 if (gSystem->Getenv(
"ALIEN_SITE")) {
291 repcommand += gSystem->Getenv(
"ALIEN_SITE");
293 result = gGrid->Command(repcommand.Data(), kFALSE, TAlien::kOUTPUT);
294 alienResult = dynamic_cast < TAlienResult * >(result);
295 list = dynamic_cast < TList * >(alienResult);
299 ::Error(
"TAlienFile::Open",
300 "cannot get the access envelope for %s and image %u in SE <%s>",
301 purl.GetUrl(), imagenr, seStr->GetName());
303 ::Error(
"TAlienFile::Open",
304 "cannot get the access envelope for %s and image %u",
305 purl.GetUrl(), imagenr);
312 if (gMonitoringWriter)
313 gMonitoringWriter->SendFileOpenProgress(0, 0, 0, kFALSE);
318 iter = list->MakeIterator();
321 Bool_t imageeof = kFALSE;
323 while ((
object = iter->Next()) != 0) {
324 TMap *map = dynamic_cast < TMap * >(object);
326 TObject *urlObject = map->GetValue(
"url");
327 urlStr = dynamic_cast < TObjString * >(urlObject);
328 if (urlStr) urlStrs = urlStr->GetName();
330 TObject *authzObject = map->GetValue(
"envelope");
331 authzStr = dynamic_cast < TObjString * >(authzObject);
333 TObject *seObject = map->GetValue(
"se");
334 seStr = dynamic_cast < TObjString * >(seObject);
335 if (seStr) seStrs = seStr->GetName();
337 TObject *nreplicasObject = map->GetValue(
"nSEs");
338 if (nreplicasObject) {
339 snreplicas = nreplicasObject->GetName();
340 nreplicas = snreplicas.Atoi();
343 TObject *guidObject = map->GetValue(
"guid");
344 if (guidObject) sguid = guidObject->GetName();
346 TObject *pfnObject = map->GetValue(
"pfn");
347 if (pfnObject) pfnStr = pfnObject->GetName();
349 if (map->GetValue(
"eof")) {
356 if ((!urlStr) || (!authzStr)) {
358 ::Error(
"TAlienFile::Open",
359 "didn't get the authorization to write %s",
363 ::Error(
"TAlienFile::Open",
364 "didn't get the authorization to read %s from location %u",
365 purl.GetUrl(), imagenr);
369 ::Info(
"TAlienFile::Open",
370 "Command::Stdout !!!");
372 ::Info(
"TAlienFile::Open",
373 "Command::Stderr !!!");
375 ::Info(
"TAlienFile::Open",
376 "End of Output !!!");
385 ::Error(
"TAlienFile::Open",
386 "No more images to try - giving up");
389 if (gMonitoringWriter)
390 gMonitoringWriter->SendFileOpenProgress(0, 0, 0, kFALSE);
398 authz = authzStr->GetName();
399 stringurl = urlStr->GetName();
401 tokens = stringurl.Tokenize(
"#");
403 if (tokens->GetEntries() == 2) {
404 anchor = ((TObjString *) tokens->At(1))->GetName();
405 urlStr->SetString(((TObjString *) tokens->At(0))->GetName());
410 newurl = urlStr->GetName();
411 stmp = purl.GetAnchor();
412 newurl += TString(
"?&authz=");
413 newurl += authzStr->GetName();
417 ::Info(
"TAlienFile::Open",
"Accessing image %u of %s in SE <%s>",
418 imagenr, purl.GetUrl(), seStr->GetName());
420 ::Info(
"TAlienFile::Open",
"Accessing image %u of %s", imagenr, purl.GetUrl());
429 newurl += purl.GetAnchor();
430 TString lUrlfile = lUrl.GetFile();
433 lUrloption += purl.GetAnchor();
434 lUrloption +=
"&mkpath=1";
435 lUrl.SetFile(lUrlfile);
436 lUrl.SetOptions(lUrloption);
438 if (anchor.Length()) {
441 TString lUrlfile = lUrl.GetFile();
444 lUrloption += anchor;
445 lUrloption +=
"&mkpath=1";
446 lUrl.SetFile(lUrlfile);
448 lUrl.SetOptions(lUrloption);
451 loption = lUrl.GetOptions();
452 if (loption.Length()) {
453 loption +=
"&mkpath=1";
454 lUrl.SetOptions(loption.Data());
456 lUrl.SetOptions(
"mkpath=1");
461 fAUrl = TUrl(newurl);
467 if (TString(fAUrl.GetUrl()) ==
"") {
471 if (gMonitoringWriter)
472 gMonitoringWriter->SendFileOpenProgress(0, 0, 0, kFALSE);
480 oldopt = oUrl.GetOptions();
481 newopt = nUrl.GetOptions();
484 if (oldopt.Length()) {
485 nUrl.SetOptions(newopt + TString(
"&") + oldopt);
487 nUrl.SetOptions(newopt);
493 ::Info(
"TAlienFile",
"Opening AUrl <%s> lUrl <%s>",fAUrl.GetUrl(),lUrl.GetUrl());
496 TAlienFile *alienfile =
497 new TAlienFile(fAUrl.GetUrl(), fAOption, ftitle, compress,
498 parallelopen, lUrl.GetUrl(), authz);
500 if (alienfile->IsZombie()) {
508 alienfile->SetSE(seStrs);
509 alienfile->SetPfn(pfnStr);
510 alienfile->SetImage(imagenr);
511 alienfile->SetNreplicas(nreplicas);
512 alienfile->SetGUID(sguid);
513 alienfile->SetUrl(urlStrs);
514 alienfile->SetElapsed(timer.RealTime());
517 }
while (imagenr < MAX_FILE_IMAGES);
520 ::Error(
"TAlienFile::Open",
521 "Couldn't open any of the file images of %s", lUrl.GetUrl());
525 if (gMonitoringWriter)
526 gMonitoringWriter->SendFileOpenProgress(0, 0, 0, kFALSE);
534 TAlienFile::~TAlienFile()
540 Info(
"~TAlienFile",
"dtor called for %s", GetName());
546 void TAlienFile::Close(Option_t * option)
548 if (!IsOpen())
return;
552 TXNetFile::Close(option);
554 if (fOption ==
"READ")
558 gSystem->Setenv(
"GCLIENT_EXTRA_ARG", fAuthz.Data());
561 TString command(
"commit ");
563 Long64_t siz = GetSize();
565 Error(
"Close",
"the reported size of the written file is <= 0");
571 TGridResult *result = gGrid->Command(command, kFALSE, TAlien::kOUTPUT);
572 TAlienResult *alienResult = dynamic_cast < TAlienResult * >(result);
573 TList *list = dynamic_cast < TList * >(alienResult);
578 Error(
"Close",
"cannot commit envelope for %s", fLfn.Data());
579 gSystem->Unlink(fLfn);
581 TIterator *iter = list->MakeIterator();
584 while ((
object = iter->Next()) != 0) {
585 TMap *map = dynamic_cast < TMap * >(object);
586 TObject *commitObject = map->GetValue(fLfn.Data());
588 TObjString *commitStr =
589 dynamic_cast < TObjString * >(commitObject);
590 if (!(strcmp(commitStr->GetName(),
"1"))) {
596 Error(
"Close",
"cannot register %s!", fLfn.Data());
597 gSystem->Unlink(fLfn);
605 gSystem->Unsetenv(
"GCLIENT_EXTRA_ARG");
612 TString TAlienFile::SUrl(
const char *lfn)
622 command =
"access -p read ";
623 command += lurl.GetFile();
628 ::Error(
"TAlienFile::SUrl",
"no grid connection");
632 result = gGrid->Command(command.Data(), kFALSE, TAlien::kOUTPUT);
634 ::Error(
"TAlienFile::SUrl",
"couldn't get access URL for alien file %s", lfn);
638 TIterator *iter = result->MakeIterator();
640 TObjString *urlStr=0;
642 object = iter->Next();
644 TMap *map = dynamic_cast < TMap * >(object);
645 TObject *urlObject = map->GetValue(
"url");
646 urlStr = dynamic_cast < TObjString * >(urlObject);
649 surl = urlStr->GetName();
655 ::Error(
"TAlienFile::SUrl",
"couldn't get surl for alien file %s", lfn);