Bug fix. Removed delete statement
[u/mrichter/AliRoot.git] / STEER / AliCDBLocal.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /////////////////////////////////////////////////////////////////////////////////////////////////
17 //                                                                                             //
18 // AliCDBLocal                                                                                 //
19 // access class to a DataBase in a local storage                                               //
20 //                                                                                             //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
22
23 #include <cstdlib>
24 #include <TSystem.h>
25 #include <TObjString.h>
26 #include <TRegexp.h>
27 #include <TFile.h>
28 #include <TKey.h>
29
30 #include "AliCDBLocal.h"
31 #include "AliCDBEntry.h"
32 #include "AliLog.h"
33
34 ClassImp(AliCDBLocal)
35
36 //_____________________________________________________________________________
37 AliCDBLocal::AliCDBLocal(const char* baseDir):
38 fBaseDirectory(baseDir) 
39 {
40 // constructor
41
42         // check baseDire: trying to cd to baseDir; if it does not exist, create it
43         void* dir = gSystem->OpenDirectory(baseDir);
44         if (dir == NULL) {
45                 if (gSystem->mkdir(baseDir, kTRUE)) {
46                         AliError(Form("Can't open directory <%s>!", baseDir));
47                 }
48
49         } else {
50                 AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data()));
51                 gSystem->FreeDirectory(dir);
52         }
53         fType="local";
54         fBaseFolder = fBaseDirectory;
55 }
56
57 //_____________________________________________________________________________
58 AliCDBLocal::~AliCDBLocal() {
59 // destructor
60
61 }
62
63
64 //_____________________________________________________________________________
65 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
66         Int_t& version, Int_t& subVersion) {
67 // build AliCDBId from filename numbers
68
69
70         Ssiz_t mSize;
71
72         // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root
73         TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
74         keyPattern.Index(filename, &mSize);
75         if (!mSize) {
76                 AliDebug(2, Form("Bad filename <%s>.", filename));
77                 return kFALSE;
78         }
79
80         TString idString(filename);
81         idString.Resize(idString.Length() - sizeof(".root") + 1);
82
83         TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
84
85         TString firstRunString(((TObjString*) strArray->At(0))->GetString());
86         runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
87         runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
88         
89         TString verString(((TObjString*) strArray->At(2))->GetString());
90         version = atoi(verString.Data() + 1);
91
92         TString subVerString(((TObjString*) strArray->At(3))->GetString());
93         subVersion = atoi(subVerString.Data() + 1);
94
95         delete strArray;
96
97         return kTRUE;
98 }
99
100
101 //_____________________________________________________________________________
102 Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
103 // build file name from AliCDBId data (run range, version, subVersion)
104
105         if (!id.GetAliCDBRunRange().IsValid()) {
106                 AliDebug(2,Form("Invalid run range <%d, %d>.", 
107                         id.GetFirstRun(), id.GetLastRun()));
108                 return kFALSE;
109         }
110
111         if (id.GetVersion() < 0) {
112                 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
113                 return kFALSE;
114         }
115
116         if (id.GetSubVersion() < 0) {
117                 AliDebug(2,Form("Invalid subversion <%s>.", id.GetSubVersion()));
118                 return kFALSE;
119         }
120  
121         filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(),
122                                                  id.GetVersion(), id.GetSubVersion());
123
124         filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
125
126         return kTRUE;
127 }
128
129 //_____________________________________________________________________________
130 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
131 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
132
133         TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
134
135         // go to the path; if directory does not exist, create it
136         void* dirPtr = gSystem->OpenDirectory(dirName);
137         if (!dirPtr) {
138                 gSystem->mkdir(dirName, kTRUE);
139                 dirPtr = gSystem->OpenDirectory(dirName);
140
141                 if (!dirPtr) {
142                         AliError(Form("Can't create directory <%s>!", 
143                                         dirName.Data()));
144                         return kFALSE;
145                 }
146         }
147
148         const char* filename;
149         AliCDBRunRange aRunRange; // the runRange got from filename
150         AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
151         Int_t aVersion, aSubVersion; // the version subVersion got from filename
152         Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
153
154         if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
155                                 
156                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
157
158                         TString aString(filename);
159                         if (aString == "." || aString == "..") continue;
160         
161                         if (!FilenameToId(filename, aRunRange, aVersion, 
162                                 aSubVersion)) {
163                                 AliDebug(2,Form(
164                                         "Bad filename <%s>! I'll skip it.", 
165                                         filename));
166                                 continue;
167                         }
168                         
169                         if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
170                         if(aVersion < lastVersion) continue;
171                         if(aVersion > lastVersion) lastSubVersion = -1;
172                         if(aSubVersion < lastSubVersion) continue;
173                         lastVersion = aVersion;
174                         lastSubVersion = aSubVersion;
175                         lastRunRange = aRunRange;
176                 }
177
178                 id.SetVersion(lastVersion);
179                 id.SetSubVersion(lastSubVersion + 1);
180
181         } else { // version specified, look for highest subVersion only
182                 
183                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
184                         
185                         TString aString(filename);
186                         if (aString == "." || aString == "..") {
187                                 continue;
188                         }
189
190                         if (!FilenameToId(filename, aRunRange, aVersion, 
191                                 aSubVersion)) {
192                                 AliDebug(2,Form(
193                                         "Bad filename <%s>!I'll skip it.",
194                                         filename));     
195                                 continue;
196                         }
197
198                         if (aRunRange.Overlaps(id.GetAliCDBRunRange()) 
199                                 && aVersion == id.GetVersion()
200                                 && aSubVersion > lastSubVersion) {
201                                 lastSubVersion = aSubVersion;
202                                 lastRunRange = aRunRange;
203                         }
204         
205                 }
206                 
207                 id.SetSubVersion(lastSubVersion + 1);
208         }
209
210         gSystem->FreeDirectory(dirPtr);
211
212         TString lastStorage = id.GetLastStorage();
213         if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
214            id.GetSubVersion() > 0 ){
215                 AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
216                 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
217                 return kFALSE;
218         }
219
220         if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
221            id.GetSubVersion() > 0 ){
222                 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
223                                         id.GetVersion(),id.GetSubVersion()));
224                 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
225                                         id.GetVersion(),id.GetSubVersion()-1));
226         }
227
228         if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange()))) 
229                 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
230                         lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), 
231                         id.GetVersion(), id.GetSubVersion()-1));
232
233         return kTRUE;
234 }
235
236 // //_____________________________________________________________________________
237 // Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
238 // // look for filename matching query (called by GetEntry)
239 // 
240 //      TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
241 // 
242 //      void* dirPtr = gSystem->OpenDirectory(dirName);
243 //      if (!dirPtr) {
244 //              AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
245 //              AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
246 //              return kFALSE;
247 //      }
248 // 
249 //      const char* filename;
250 // 
251 //      AliCDBRunRange aRunRange; // the runRange got from filename
252 //      Int_t aVersion, aSubVersion; // the version and subVersion got from filename
253 // 
254 //      if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
255 // 
256 //              while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
257 // 
258 //                      TString aString(filename);
259 //                      if (aString == "." || aString == "..") continue;
260 // 
261 //                      if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
262 //                         // aRunRange, aVersion, aSubVersion filled from filename
263 // 
264 //                      if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
265 //                      // aRunRange contains requested run!
266 // 
267 //                      if (result.GetVersion() < aVersion) {
268 //                              result.SetVersion(aVersion);
269 //                              result.SetSubVersion(aSubVersion);
270 // 
271 //                              result.SetFirstRun(
272 //                                      aRunRange.GetFirstRun());
273 //                              result.SetLastRun(
274 //                                      aRunRange.GetLastRun());
275 // 
276 //                      } else if (result.GetVersion() == aVersion
277 //                              && result.GetSubVersion()
278 //                                      < aSubVersion) {
279 // 
280 //                              result.SetSubVersion(aSubVersion);
281 // 
282 //                              result.SetFirstRun(
283 //                                      aRunRange.GetFirstRun());
284 //                              result.SetLastRun(
285 //                                      aRunRange.GetLastRun());
286 //                      } else if (result.GetVersion() == aVersion
287 //                              && result.GetSubVersion() == aSubVersion){
288 //                                      AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
289 //                                      query.GetFirstRun(), aVersion, aSubVersion));
290 //                              gSystem->FreeDirectory(dirPtr);
291 //                              return kFALSE;
292 //                              }
293 //              }
294 // 
295 //      } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
296 // 
297 //              result.SetVersion(query.GetVersion());
298 // 
299 //              while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
300 // 
301 //                         TString aString(filename);
302 //                         if (aString == "." || aString == "..") continue;
303 // 
304 //                      if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
305 //                         // aRunRange, aVersion, aSubVersion filled from filename
306 // 
307 //                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; 
308 //                      // aRunRange contains requested run!
309 // 
310 //                      if(query.GetVersion() != aVersion) continue;
311 //                      // aVersion is requested version!
312 // 
313 //                      if(result.GetSubVersion() == aSubVersion){
314 //                                      AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
315 //                                      query.GetFirstRun(), aVersion, aSubVersion));
316 //                              gSystem->FreeDirectory(dirPtr);
317 //                              return kFALSE; 
318 //                      }
319 //                      if( result.GetSubVersion() < aSubVersion) {
320 // 
321 //                                 result.SetSubVersion(aSubVersion);
322 // 
323 //                                 result.SetFirstRun(
324 //                                      aRunRange.GetFirstRun());
325 //                                 result.SetLastRun(
326 //                                      aRunRange.GetLastRun());
327 //                      } 
328 //                 }
329 // 
330 //      } else { // both version and subversion specified
331 // 
332 //              while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
333 // 
334 //                         TString aString(filename);
335 //                         if (aString == "." || aString == "..") continue;
336 // 
337 //                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
338 //                         // aRunRange, aVersion, aSubVersion filled from filename
339 // 
340 //                      if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
341 //                      // aRunRange contains requested run!
342 // 
343 //                      if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
344 //                      // aVersion and aSubVersion are requested version and subVersion!
345 // 
346 //                      if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
347 //                                      AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
348 //                                      query.GetFirstRun(), aVersion, aSubVersion));
349 //                              gSystem->FreeDirectory(dirPtr);
350 //                              return kFALSE;
351 //                      }
352 //                      result.SetVersion(aVersion);
353 //                      result.SetSubVersion(aSubVersion);
354 //                      result.SetFirstRun(aRunRange.GetFirstRun());
355 //                      result.SetLastRun(aRunRange.GetLastRun());
356 // 
357 //              }
358 //      }
359 // 
360 //      gSystem->FreeDirectory(dirPtr);
361 // 
362 //      return kTRUE;
363 // }
364
365 //_____________________________________________________________________________
366 AliCDBId* AliCDBLocal::GetId(const AliCDBId& query) {
367 // look for filename matching query (called by GetEntryId)
368
369         TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
370
371         void* dirPtr = gSystem->OpenDirectory(dirName);
372         if (!dirPtr) {
373                 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
374                 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
375                 return NULL;
376         }
377
378         const char* filename;
379         AliCDBId *result = new AliCDBId();
380         result->SetPath(query.GetPath());
381
382         AliCDBRunRange aRunRange; // the runRange got from filename
383         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
384
385         if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
386
387                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
388
389                         TString aString(filename);
390                         if (aString == "." || aString == "..") continue;
391
392                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
393                         // aRunRange, aVersion, aSubVersion filled from filename
394
395                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
396                         // aRunRange contains requested run!
397
398                         if (result->GetVersion() < aVersion) {
399                                 result->SetVersion(aVersion);
400                                 result->SetSubVersion(aSubVersion);
401
402                                 result->SetFirstRun(
403                                         aRunRange.GetFirstRun());
404                                 result->SetLastRun(
405                                         aRunRange.GetLastRun());
406
407                         } else if (result->GetVersion() == aVersion
408                                 && result->GetSubVersion()
409                                         < aSubVersion) {
410
411                                 result->SetSubVersion(aSubVersion);
412
413                                 result->SetFirstRun(
414                                         aRunRange.GetFirstRun());
415                                 result->SetLastRun(
416                                         aRunRange.GetLastRun());
417                         } else if (result->GetVersion() == aVersion
418                                 && result->GetSubVersion() == aSubVersion){
419                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
420                                         query.GetFirstRun(), aVersion, aSubVersion));
421                                 gSystem->FreeDirectory(dirPtr);
422                                 delete result;
423                                 return NULL;
424                                 }
425                 }
426
427         } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
428
429                 result->SetVersion(query.GetVersion());
430
431                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
432
433                         TString aString(filename);
434                         if (aString == "." || aString == "..") continue;
435
436                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
437                         // aRunRange, aVersion, aSubVersion filled from filename
438
439                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; 
440                         // aRunRange contains requested run!
441
442                         if(query.GetVersion() != aVersion) continue;
443                         // aVersion is requested version!
444
445                         if(result->GetSubVersion() == aSubVersion){
446                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
447                                         query.GetFirstRun(), aVersion, aSubVersion));
448                                 gSystem->FreeDirectory(dirPtr);
449                                 delete result;
450                                 return NULL;
451                         }
452                         if( result->GetSubVersion() < aSubVersion) {
453
454                                 result->SetSubVersion(aSubVersion);
455
456                                 result->SetFirstRun(
457                                         aRunRange.GetFirstRun());
458                                 result->SetLastRun(
459                                         aRunRange.GetLastRun());
460                         } 
461                 }
462
463         } else { // both version and subversion specified
464
465                 //AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
466         //Bool_t result;
467         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
468
469                         TString aString(filename);
470                         if (aString == "." || aString == "..") continue;
471
472                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
473                         // aRunRange, aVersion, aSubVersion filled from filename
474
475                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
476                         // aRunRange contains requested run!
477
478                         if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
479                         // aVersion and aSubVersion are requested version and subVersion!
480
481                         if(result->GetVersion() == aVersion && result->GetSubVersion() == aSubVersion){
482                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
483                                         query.GetFirstRun(), aVersion, aSubVersion));
484                                 gSystem->FreeDirectory(dirPtr);
485                                 delete result;
486                                 return NULL;
487                         }
488                         result->SetVersion(aVersion);
489                         result->SetSubVersion(aSubVersion);
490                         result->SetFirstRun(aRunRange.GetFirstRun());
491                         result->SetLastRun(aRunRange.GetLastRun());
492                         
493                 }
494         }
495
496         gSystem->FreeDirectory(dirPtr);
497
498         return result;
499 }
500
501 //_____________________________________________________________________________
502 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
503 // get AliCDBEntry from the database
504
505         AliCDBId* dataId = GetEntryId(queryId);
506
507         if (!dataId || !dataId->IsSpecified()) return NULL;
508
509         TString filename;
510         if (!IdToFilename(*dataId, filename)) {
511
512                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
513                 delete dataId;
514                 return NULL;
515         }
516
517         TFile file(filename, "READ"); // open file
518         if (!file.IsOpen()) {
519                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
520                 delete dataId;
521                 return NULL;
522         }
523
524         // get the only AliCDBEntry object from the file
525         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
526
527         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
528         if (!anEntry) {
529                 AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
530                 file.Close();
531                 delete dataId;
532                 return NULL;
533         }
534
535         AliCDBId& entryId = anEntry->GetId();
536
537         // The object's Id are not reset during storage
538         // If object's Id runRange or version do not match with filename,
539         // it means that someone renamed file by hand. In this case a warning msg is issued.
540
541         anEntry-> SetLastStorage("local");
542
543         if(!entryId.IsEqual(dataId)){
544                 AliWarning(Form("Mismatch between file name and object's Id!"));
545                 AliWarning(Form("File name: %s", dataId->ToString().Data()));
546                 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
547         }
548
549         // Check whether entry contains a TTree. In case load the tree in memory!
550         LoadTreeFromFile(anEntry);
551
552         // close file, return retieved entry
553         file.Close();
554         delete dataId;
555         return anEntry;
556 }
557
558 //_____________________________________________________________________________
559 AliCDBId* AliCDBLocal::GetEntryId(const AliCDBId& queryId) {
560 // get AliCDBId from the database
561
562         AliCDBId* dataId = 0;
563
564         // look for a filename matching query requests (path, runRange, version, subVersion)
565         if (!queryId.HasVersion()) {
566                 // if version is not specified, first check the selection criteria list
567                 AliCDBId selectedId(queryId);
568                 GetSelection(&selectedId);
569                 dataId = GetId(selectedId);
570         } else {
571                 dataId = GetId(queryId);
572         }
573
574         if (dataId && !dataId->IsSpecified()) {
575                 delete dataId;
576                 return NULL;
577         }
578
579         return dataId;
580 }
581
582 //_____________________________________________________________________________
583 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
584         const AliCDBId& queryId, TList* result) {
585 // multiple request (AliCDBStorage::GetAll)
586
587         TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
588
589         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
590         if (!level0DirPtr) {
591                 AliDebug(2,Form("Can't open level0 directory <%s>!",
592                         level0Dir.Data()));
593                 return;
594         }
595
596         const char* level1;
597         Long_t flag=0;
598         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
599
600                 TString level1Str(level1);
601                 if (level1Str == "." || level1Str == "..") {
602                         continue;
603                 }
604                 
605                 TString fullPath = Form("%s/%s",level0Dir.Data(), level1); 
606
607                 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
608                 
609                 if(res){
610                         AliDebug(2, Form("Error reading entry %s !",level1Str.Data()));
611                         continue;
612                 }
613                 if(!(flag&2)) continue; // bit 1 of flag = directory!
614                 
615                 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
616                         GetEntriesForLevel1(level0, level1, queryId, result);
617                 }
618         }
619
620         gSystem->FreeDirectory(level0DirPtr);
621 }
622
623 //_____________________________________________________________________________
624 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
625         const AliCDBId& queryId, TList* result) {
626 // multiple request (AliCDBStorage::GetAll)
627
628         TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
629
630         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
631         if (!level1DirPtr) {
632                 AliDebug(2,Form("Can't open level1 directory <%s>!",
633                         level1Dir.Data()));
634                 return;
635         }
636
637         const char* level2;
638         Long_t flag=0;
639         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
640
641                 TString level2Str(level2);
642                 if (level2Str == "." || level2Str == "..") {
643                         continue;
644                 }
645
646                 TString fullPath = Form("%s/%s",level1Dir.Data(), level2); 
647
648                 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
649                 
650                 if(res){
651                         AliDebug(2, Form("Error reading entry %s !",level2Str.Data()));
652                         continue;
653                 }
654                 if(!(flag&2)) continue; // bit 1 of flag = directory!
655                 
656                 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
657
658                         AliCDBPath entryPath(level0, level1, level2);
659                         AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
660                                 queryId.GetVersion(), queryId.GetSubVersion());
661
662                         AliCDBEntry* anEntry = GetEntry(entryId);
663                         if (anEntry) {
664                                 result->Add(anEntry);
665                         }
666                 }
667         }
668
669         gSystem->FreeDirectory(level1DirPtr);
670 }
671
672 //_____________________________________________________________________________
673 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
674 // multiple request (AliCDBStorage::GetAll)
675         
676         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
677         if (!storageDirPtr) {
678                 AliDebug(2,Form("Can't open storage directory <%s>",
679                         fBaseDirectory.Data()));
680                 return NULL;
681         }
682
683         TList* result = new TList();
684         result->SetOwner();
685
686         const char* level0;
687         Long_t flag=0;
688         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
689
690                 TString level0Str(level0);
691                 if (level0Str == "." || level0Str == "..") {
692                         continue;
693                 }
694                 
695                 TString fullPath = Form("%s/%s",fBaseDirectory.Data(), level0); 
696
697                 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
698                 
699                 if(res){
700                         AliDebug(2, Form("Error reading entry %s !",level0Str.Data()));
701                         continue;
702                 }
703                 
704                 if(!(flag&2)) continue; // bit 1 of flag = directory!                           
705
706                 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
707                         GetEntriesForLevel0(level0, queryId, result);
708                 }
709         }
710
711         gSystem->FreeDirectory(storageDirPtr);
712
713         return result;  
714 }
715
716 //_____________________________________________________________________________
717 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
718 // put an AliCDBEntry object into the database
719
720         AliCDBId& id = entry->GetId();
721
722         // set version and subVersion for the entry to be stored
723         if (!PrepareId(id)) return kFALSE;
724
725         
726         // build filename from entry's id
727         TString filename="";
728         if (!IdToFilename(id, filename)) {
729
730                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
731                 return kFALSE;
732         }
733
734         // open file
735         TFile file(filename, "CREATE");
736         if (!file.IsOpen()) {
737                 AliError(Form("Can't open file <%s>!", filename.Data()));
738                 return kFALSE;
739         }
740         
741         //SetTreeToFile(entry, &file);
742
743         entry->SetVersion(id.GetVersion());
744         entry->SetSubVersion(id.GetSubVersion());
745
746         // write object (key name: "AliCDBEntry")
747         Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
748         if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
749
750         file.Close();
751         if(result) {
752                 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
753                         AliInfo(Form("CDB object stored into file %s",filename.Data()));
754         }
755
756         return result;
757 }
758
759 //_____________________________________________________________________________
760 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
761
762         TString fullFileName(fileName);
763         fullFileName.Prepend(fBaseDirectory+'/');
764         TFile *file = TFile::Open(fullFileName);
765         if (!file) {
766                 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
767                 return NULL;
768         }
769         file->cd();
770
771         TList *list = new TList();
772         list->SetOwner();
773         int i=0;
774         TString keycycle;
775         
776         AliCDBId *id;
777         while(1){
778                 i++;
779                 keycycle = "AliCDBId;";
780                 keycycle+=i;
781                 
782                 id = (AliCDBId*) file->Get(keycycle);
783                 if(!id) break;
784                 list->AddFirst(id);
785         }
786         file->Close(); delete file; file=0;     
787         return list;
788 }
789
790 //_____________________________________________________________________________
791 Bool_t AliCDBLocal::Contains(const char* path) const{
792 // check for path in storage's fBaseDirectory
793
794         TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
795         Bool_t result=kFALSE;
796
797         void* dirPtr = gSystem->OpenDirectory(dirName); 
798         if (dirPtr) result=kTRUE;
799         gSystem->FreeDirectory(dirPtr);
800
801         return result;
802 }
803
804 //_____________________________________________________________________________
805 void AliCDBLocal::QueryValidFiles()
806 {
807 // Query the CDB for files valid for AliCDBStorage::fRun
808 // fills list fValidFileIds with AliCDBId objects created from file name
809
810         if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
811         if(fMetaDataFilter) {
812                 AliWarning ("CDB meta data parameters are not used by local storage query!");
813                 delete fMetaDataFilter; fMetaDataFilter=0;
814         }
815
816         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
817
818         const char* level0;
819         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
820
821                 TString level0Str(level0);
822                 if (level0Str == "." || level0Str == "..") {
823                         continue;
824                 }
825
826                 if (fPathFilter.Level0Comprises(level0)) {
827                         TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
828                         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
829                         const char* level1;
830                         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
831
832                                 TString level1Str(level1);
833                                 if (level1Str == "." || level1Str == "..") {
834                                         continue;
835                                 }
836
837                                 if (fPathFilter.Level1Comprises(level1)) {
838                                         TString level1Dir = Form("%s/%s/%s",
839                                                         fBaseDirectory.Data(),level0,level1);
840
841                                         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
842                                         const char* level2;
843                                         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
844
845                                                 TString level2Str(level2);
846                                                 if (level2Str == "." || level2Str == "..") {
847                                                         continue;
848                                                 }
849
850                                                 if (fPathFilter.Level2Comprises(level2)) {
851                                                         TString dirName = Form("%s/%s/%s/%s",
852                                                                         fBaseDirectory.Data(),level0,level1,level2);
853
854                                                         void* dirPtr = gSystem->OpenDirectory(dirName);
855
856                                                         const char* filename;
857
858                                                         AliCDBRunRange aRunRange; // the runRange got from filename
859                                                         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
860
861                                                         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
862
863                                                                 TString aString(filename);
864                                                                 if (aString == "." || aString == "..") continue;
865
866                                                                 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
867                                                                 AliCDBRunRange runrg(fRun,fRun);
868                                                                 if (!aRunRange.Comprises(runrg)) continue;
869                                                                 // aRunRange contains requested run!
870                                                                 AliCDBPath validPath(level0,level1,level2);
871                                                                 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
872                                                                 fValidFileIds.AddLast(validId.Clone());
873                                                         }
874                                                         gSystem->FreeDirectory(dirPtr);
875                                                 }
876                                         }
877                                         gSystem->FreeDirectory(level1DirPtr);
878                                 }
879                         }
880                         gSystem->FreeDirectory(level0DirPtr);
881                 }
882         }
883         gSystem->FreeDirectory(storageDirPtr);
884
885 }
886
887 //_____________________________________________________________________________
888 Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
889 // get last version found in the database valid for run and path
890
891         AliCDBPath aCDBPath(path);
892         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
893                 AliError(Form("Invalid path in request: %s", path));
894                 return -1;
895         }
896
897         AliCDBId query(path, run, run, -1, -1);
898         AliCDBId* dataId = GetId(query);
899
900         if(!dataId) return -1;
901
902         Int_t version = dataId->GetVersion();
903         delete dataId;
904
905         return version;
906
907 }
908
909 //_____________________________________________________________________________
910 Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
911 // get last version found in the database valid for run and path
912
913         AliCDBPath aCDBPath(path);
914         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
915                 AliError(Form("Invalid path in request: %s", path));
916                 return -1;
917         }
918
919         AliCDBId query(path, run, run, version, -1);
920         AliCDBId *dataId = GetId(query);
921
922         if(!dataId) return -1;
923
924         Int_t subVersion = dataId->GetSubVersion();
925
926         delete dataId;
927         return subVersion;
928
929 }
930
931 /////////////////////////////////////////////////////////////////////////////////////////////////
932 //                                                                                             //
933 // AliCDBLocal factory                                                                         //
934 //                                                                                             //
935 /////////////////////////////////////////////////////////////////////////////////////////////////
936
937 ClassImp(AliCDBLocalFactory)
938
939 //_____________________________________________________________________________
940 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
941 // check if the string is valid local URI
942
943         TRegexp dbPattern("^local://.+$");
944
945         return TString(dbString).Contains(dbPattern);
946 }
947
948 //_____________________________________________________________________________
949 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
950 // create AliCDBLocalParam class from the URI string
951
952         if (!Validate(dbString)) {
953                 return NULL;
954         }
955
956         TString pathname(dbString + sizeof("local://") - 1);
957         
958         gSystem->ExpandPathName(pathname);
959
960         if (pathname[0] != '/') {
961                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
962         }
963
964         return new AliCDBLocalParam(pathname);
965 }
966
967 //_____________________________________________________________________________
968 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
969 // create AliCDBLocal storage instance from parameters
970         
971         if (AliCDBLocalParam::Class() == param->IsA()) {
972                 
973                 const AliCDBLocalParam* localParam = 
974                         (const AliCDBLocalParam*) param;
975                 
976                 return new AliCDBLocal(localParam->GetPath());
977         }
978
979         return NULL;
980 }
981
982 /////////////////////////////////////////////////////////////////////////////////////////////////
983 //                                                                                             //
984 // AliCDBLocal Parameter class                                                                 //                                          //
985 //                                                                                             //
986 /////////////////////////////////////////////////////////////////////////////////////////////////
987
988 ClassImp(AliCDBLocalParam)
989
990 //_____________________________________________________________________________
991 AliCDBLocalParam::AliCDBLocalParam():
992  AliCDBParam(),
993  fDBPath()
994  {
995 // default constructor
996
997 }
998
999 //_____________________________________________________________________________
1000 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
1001  AliCDBParam(),
1002  fDBPath(dbPath)
1003 {
1004 // constructor
1005
1006         SetType("local");
1007         SetURI(TString("local://") + dbPath);
1008 }
1009
1010 //_____________________________________________________________________________
1011 AliCDBLocalParam::~AliCDBLocalParam() {
1012 // destructor
1013
1014 }
1015
1016 //_____________________________________________________________________________
1017 AliCDBParam* AliCDBLocalParam::CloneParam() const {
1018 // clone parameter
1019
1020         return new AliCDBLocalParam(fDBPath);
1021 }
1022
1023 //_____________________________________________________________________________
1024 ULong_t AliCDBLocalParam::Hash() const {
1025 // return Hash function
1026
1027        return fDBPath.Hash();
1028 }
1029
1030 //_____________________________________________________________________________
1031 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
1032 // check if this object is equal to AliCDBParam obj
1033
1034         if (this == obj) {
1035                 return kTRUE;
1036         }
1037
1038         if (AliCDBLocalParam::Class() != obj->IsA()) {
1039                 return kFALSE;
1040         }
1041
1042         AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
1043
1044         return fDBPath == other->fDBPath;
1045 }
1046