-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TDirectoryFile.cxx
2136 lines (1873 loc) · 66.9 KB
/
TDirectoryFile.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/io:$Id$
// Author: Rene Brun 22/01/2007
/*************************************************************************
* Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/**
\class TDirectoryFile
\ingroup IO
A ROOT file is structured in Directories (like a file system).
Each Directory has a list of Keys (see TKeys) and a list of objects
in memory. A Key is a small object that describes the type and location
of a persistent object in a file. The persistent object may be a directory.
Begin_Macro
../../../tutorials/io/fildir.C
End_Macro
The structure of a file is shown in TFile::TFile
*/
#include "Riostream.h"
#include "Strlen.h"
#include "TDirectoryFile.h"
#include "TFile.h"
#include "TBufferFile.h"
#include "TBufferJSON.h"
#include "TMapFile.h"
#include "TClassTable.h"
#include "TInterpreter.h"
#include "THashList.h"
#include "TBrowser.h"
#include "TFree.h"
#include "TKey.h"
#include "TStreamerInfo.h"
#include "TROOT.h"
#include "TError.h"
#include "Bytes.h"
#include "TClass.h"
#include "TRegexp.h"
#include "TSystem.h"
#include "TStreamerElement.h"
#include "TProcessUUID.h"
#include "TVirtualMutex.h"
#include "TEmulatedCollectionProxy.h"
const UInt_t kIsBigFile = BIT(16);
const Int_t kMaxLen = 2048;
ClassImp(TDirectoryFile);
////////////////////////////////////////////////////////////////////////////////
/// Default Constructor
TDirectoryFile::TDirectoryFile() : TDirectory()
, fModified(kFALSE), fWritable(kFALSE), fNbytesKeys(0), fNbytesName(0)
, fBufferSize(0), fSeekDir(0), fSeekParent(0), fSeekKeys(0)
, fFile(0), fKeys(0)
{
}
////////////////////////////////////////////////////////////////////////////////
/// Create a new TDirectoryFile
///
/// A new directory with a name and a title is created in the current directory.
/// The directory header information is immediatly saved on the file
/// A new key is added in the parent directory.
/// When this constructor is called from a class directly derived
/// from TDirectoryFile, the third argument, classname, MUST be specified.
/// In this case, classname must be the name of the derived class.
///
/// Note that the directory name cannot contain slashes.
TDirectoryFile::TDirectoryFile(const char *name, const char *title, Option_t *classname, TDirectory* initMotherDir)
: TDirectory()
, fModified(kFALSE), fWritable(kFALSE), fNbytesKeys(0), fNbytesName(0)
, fBufferSize(0), fSeekDir(0), fSeekParent(0), fSeekKeys(0)
, fFile(0), fKeys(0)
{
// We must not publish this objects to the list of RecursiveRemove (indirectly done
// by 'Appending' this object to it's mother) before the object is completely
// initialized.
// However a better option would be to delay the publishing until the very end,
// but it is currently done in the middle of the initialization (by Build which
// is a public interface) ....
R__LOCKGUARD(gROOTMutex);
fName = name;
fTitle = title;
if (initMotherDir==0) initMotherDir = gDirectory;
if (strchr(name,'/')) {
::Error("TDirectoryFile","directory name (%s) cannot contain a slash", name);
gDirectory = 0;
return;
}
if (strlen(GetName()) == 0) {
::Error("TDirectoryFile","directory name cannot be \"\"");
gDirectory = 0;
return;
}
Build(initMotherDir ? initMotherDir->GetFile() : 0, initMotherDir);
TDirectory* motherdir = GetMotherDir();
TFile* f = GetFile();
if ((motherdir==0) || (f==0)) return;
if (!f->IsWritable()) return; //*-* in case of a directory in memory
if (motherdir->GetKey(name)) {
Error("TDirectoryFile","An object with name %s exists already", name);
return;
}
TClass *cl = 0;
if (classname[0]) {
cl = TClass::GetClass(classname);
if (!cl) {
Error("TDirectoryFile","Invalid class name: %s",classname);
return;
}
} else {
cl = IsA();
}
fBufferSize = 0;
fWritable = kTRUE;
Init(cl);
fModified = kFALSE;
// Temporarily redundant, see comment on lock early in the function.
// R__LOCKGUARD(gROOTMutex);
gROOT->GetUUIDs()->AddUUID(fUUID,this);
// We should really be doing this now rather than in Build, see
// comment at the start of the function.
// if (initMotherDir && strlen(GetName()) != 0) initMotherDir->Append(this);
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize the key associated with this directory (and the related
/// data members.
void TDirectoryFile::Init(TClass *cl)
{
TFile* f = GetFile();
if (f->IsBinary()) {
if (cl==0) {
cl = IsA();
}
TDirectory* motherdir = GetMotherDir();
fSeekParent = f->GetSeekDir();
Int_t nbytes = TDirectoryFile::Sizeof();
TKey *key = new TKey(fName,fTitle,cl,nbytes,motherdir);
fNbytesName = key->GetKeylen();
fSeekDir = key->GetSeekKey();
if (fSeekDir == 0) return;
char *buffer = key->GetBuffer();
TDirectoryFile::FillBuffer(buffer);
Int_t cycle = motherdir ? motherdir->AppendKey(key) : 0;
key->WriteFile(cycle);
} else {
fSeekParent = 0;
fNbytesName = 0;
fSeekDir = f->DirCreateEntry(this);
if (fSeekDir == 0) return;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Copy constructor.
TDirectoryFile::TDirectoryFile(const TDirectoryFile & directory) : TDirectory(directory)
, fModified(kFALSE), fWritable(kFALSE), fNbytesKeys(0), fNbytesName(0)
, fBufferSize(0), fSeekDir(0), fSeekParent(0), fSeekKeys(0)
, fFile(0), fKeys(0)
{
((TDirectoryFile&)directory).Copy(*this);
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TDirectoryFile::~TDirectoryFile()
{
if (fKeys) {
fKeys->Delete("slow");
SafeDelete(fKeys);
}
CleanTargets();
// Delete our content before we become somewhat invalid
// since some those objects (TTree for example) needs information
// from this object. Note that on some platform after the end
// of the body (i.e. thus during ~TDirectory which is also
// contains this code) the exeuction of 'this->GetFile()' fails
// to return the 'proper' value (because it uses the wrong
// virtual function).
if (fList) {
fList->Delete("slow");
SafeDelete(fList);
}
if (gDebug) {
Info("~TDirectoryFile", "dtor called for %s", GetName());
}
}
////////////////////////////////////////////////////////////////////////////////
/// Append object to this directory.
///
/// If replace is true:
/// remove any existing objects with the same same (if the name is not ""
void TDirectoryFile::Append(TObject *obj, Bool_t replace /* = kFALSE */)
{
if (obj == 0 || fList == 0) return;
TDirectory::Append(obj,replace);
if (!fMother) return;
if (fMother->IsA() == TMapFile::Class()) {
TMapFile *mfile = (TMapFile*)fMother;
mfile->Add(obj);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Insert key in the linked list of keys of this directory.
Int_t TDirectoryFile::AppendKey(TKey *key)
{
if (!fKeys) {
Error("AppendKey","TDirectoryFile not initialized yet.");
return 0;
}
fModified = kTRUE;
key->SetMotherDir(this);
// This is a fast hash lookup in case the key does not already exist
TKey *oldkey = (TKey*)fKeys->FindObject(key->GetName());
if (!oldkey) {
fKeys->Add(key);
return 1;
}
// If the key name already exists we have to make a scan for it
// and insert the new key ahead of the current one
TObjLink *lnk = fKeys->FirstLink();
while (lnk) {
oldkey = (TKey*)lnk->GetObject();
if (!strcmp(oldkey->GetName(), key->GetName()))
break;
lnk = lnk->Next();
}
fKeys->AddBefore(lnk, key);
return oldkey->GetCycle() + 1;
}
////////////////////////////////////////////////////////////////////////////////
/// Browse the content of the directory.
void TDirectoryFile::Browse(TBrowser *b)
{
TString name;
if (b) {
TObject *obj = 0;
TIter nextin(fList);
TKey *key = 0, *keyo = 0;
TIter next(fKeys);
cd();
//Add objects that are only in memory
while ((obj = nextin())) {
if (fKeys->FindObject(obj->GetName())) continue;
b->Add(obj, obj->GetName());
}
//Add keys
while ((key = (TKey *) next())) {
int skip = 0;
if (!keyo || (keyo && strcmp(keyo->GetName(), key->GetName()))) {
skip = 0;
obj = fList->FindObject(key->GetName());
if (obj) {
b->Add(obj, obj->GetName());
if (obj->IsFolder() && !obj->InheritsFrom("TTree"))
skip = 1;
}
}
if (!skip) {
name.Form("%s;%d", key->GetName(), key->GetCycle());
b->Add(key, name);
}
keyo = key;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Initialise directory to defaults.
void TDirectoryFile::Build(TFile* motherFile, TDirectory* motherDir)
{
// If directory is created via default ctor (when dir is read from file)
// don't add it here to the directory since its name is not yet known.
// It will be added to the directory in TKey::ReadObj().
if (motherDir && strlen(GetName()) != 0) motherDir->Append(this);
fModified = kTRUE;
fWritable = kFALSE;
fDatimeC.Set();
fDatimeM.Set();
fNbytesKeys = 0;
fSeekDir = 0;
fSeekParent = 0;
fSeekKeys = 0;
fList = new THashList(100,50);
fKeys = new THashList(100,50);
fList->UseRWLock();
fMother = motherDir;
fFile = motherFile ? motherFile : TFile::CurrentFile();
SetBit(kCanDelete);
}
////////////////////////////////////////////////////////////////////////////////
/// Change current directory to "this" directory.
/// Using path one can
/// change the current directory to "path". The absolute path syntax is:
///
/// file.root:/dir1/dir2
///
/// where file.root is the file and /dir1/dir2 the desired subdirectory
/// in the file. Relative syntax is relative to "this" directory. E.g:
/// ../aa. Returns kTRUE in case of success.
Bool_t TDirectoryFile::cd(const char *path)
{
Bool_t ok = TDirectory::cd(path);
if (ok) TFile::CurrentFile() = fFile;
return ok;
}
////////////////////////////////////////////////////////////////////////////////
/// Clean the pointers to this object (gDirectory, TContext, etc.)
void TDirectoryFile::CleanTargets()
{
// After CleanTargets either gFile was changed appropriately
// by a cd() or needs to be set to zero.
if (gFile == this) {
gFile = 0;
}
TDirectory::CleanTargets();
}
////////////////////////////////////////////////////////////////////////////////
/// Make a clone of an object using the Streamer facility.
///
/// If the object derives from TNamed, this function is called
/// by TNamed::Clone. TNamed::Clone uses the optional argument newname to set
/// a new name to the newly created object.
///
/// If autoadd is true and if the object class has a
/// DirectoryAutoAdd function, it will be called at the end of the
/// function with the parameter gDirectory. This usually means that
/// the object will be appended to the current ROOT directory.
TObject *TDirectoryFile::CloneObject(const TObject *obj, Bool_t autoadd /* = kTRUE */)
{
// if no default ctor return immediately (error issued by New())
char *pobj = (char*)obj->IsA()->New();
if (!pobj) return 0;
Int_t baseOffset = obj->IsA()->GetBaseClassOffset(TObject::Class());
if (baseOffset==-1) {
// cl does not inherit from TObject.
// Since this is not supported in this function, the only reason we could reach this code
// is because something is screwed up in the ROOT code.
Fatal("CloneObject","Incorrect detection of the inheritance from TObject for class %s.\n",
obj->IsA()->GetName());
}
TObject *newobj = (TObject*)(pobj+baseOffset);
//create a buffer where the object will be streamed
{
// NOTE: do we still need to make this change to gFile?
// NOTE: This can not be 'gDirectory=0' as at least roofit expect gDirectory to not be null
// during the streaming ....
TFile *filsav = gFile;
gFile = 0;
const Int_t bufsize = 10000;
TBufferFile buffer(TBuffer::kWrite,bufsize);
buffer.MapObject(obj); //register obj in map to handle self reference
{
Bool_t isRef = obj->TestBit(kIsReferenced);
((TObject*)obj)->ResetBit(kIsReferenced);
((TObject*)obj)->Streamer(buffer);
if (isRef) ((TObject*)obj)->SetBit(kIsReferenced);
}
// read new object from buffer
buffer.SetReadMode();
buffer.ResetMap();
buffer.SetBufferOffset(0);
buffer.MapObject(newobj); //register obj in map to handle self reference
newobj->Streamer(buffer);
newobj->ResetBit(kIsReferenced);
newobj->ResetBit(kCanDelete);
gFile = filsav;
}
if (autoadd) {
ROOT::DirAutoAdd_t func = obj->IsA()->GetDirectoryAutoAdd();
if (func) {
func(newobj,this);
}
}
return newobj;
}
////////////////////////////////////////////////////////////////////////////////
/// Scan the memory lists of all files for an object with name
TObject *TDirectoryFile::FindObjectAnyFile(const char *name) const
{
TFile *f;
R__LOCKGUARD(gROOTMutex);
TIter next(gROOT->GetListOfFiles());
while ((f = (TFile*)next())) {
TObject *obj = f->GetList()->FindObject(name);
if (obj) return obj;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Find a directory named "apath".
///
/// It apath is null or empty, returns "this" directory.
/// Otherwise use the name "apath" to find a directory.
/// The absolute path syntax is:
///
/// file.root:/dir1/dir2
///
/// where file.root is the file and /dir1/dir2 the desired subdirectory
/// in the file. Relative syntax is relative to "this" directory. E.g:
/// ../aa.
/// Returns 0 in case path does not exist.
/// If printError is true, use Error with 'funcname' to issue an error message.
TDirectory *TDirectoryFile::GetDirectory(const char *apath,
Bool_t printError, const char *funcname)
{
Int_t nch = 0;
if (apath) nch = strlen(apath);
if (!nch) {
return this;
}
if (funcname==0 || strlen(funcname)==0) funcname = "GetDirectory";
TDirectory *result = this;
char *path = new char[nch+1]; path[0] = 0;
if (nch) strlcpy(path,apath,nch+1);
char *s = (char*)strchr(path, ':');
if (s) {
*s = '\0';
R__LOCKGUARD(gROOTMutex);
TDirectory *f = (TDirectory *)gROOT->GetListOfFiles()->FindObject(path);
if (!f && !strcmp(gROOT->GetName(), path)) f = gROOT;
if (s) *s = ':';
if (f) {
result = f;
if (s && *(s+1)) result = f->GetDirectory(s+1,printError,funcname);
delete [] path; return result;
} else {
if (printError) Error(funcname, "No such file %s", path);
delete [] path; return 0;
}
}
// path starts with a slash (assumes current file)
if (path[0] == '/') {
TDirectory *td = fFile;
if (!fFile) td = gROOT;
result = td->GetDirectory(path+1,printError,funcname);
delete [] path; return result;
}
TDirectoryFile *obj;
char *slash = (char*)strchr(path,'/');
if (!slash) { // we are at the lowest level
if (!strcmp(path, "..")) {
result = GetMotherDir();
delete [] path; return result;
}
GetObject(path,obj);
if (!obj) {
if (printError) Error(funcname,"Unknown directory %s", path);
delete [] path; return 0;
}
delete [] path; return obj;
}
TString subdir(path);
slash = (char*)strchr(subdir.Data(),'/');
*slash = 0;
//Get object with path from current directory/file
if (!strcmp(subdir, "..")) {
TDirectory* mom = GetMotherDir();
if (mom)
result = mom->GetDirectory(slash+1,printError,funcname);
delete [] path; return result;
}
GetObject(subdir,obj);
if (!obj) {
if (printError) Error(funcname,"Unknown directory %s", subdir.Data());
delete [] path; return 0;
}
result = ((TDirectory*)obj)->GetDirectory(slash+1,printError,funcname);
delete [] path; return result;
}
////////////////////////////////////////////////////////////////////////////////
/// Delete all objects from memory and directory structure itself.
void TDirectoryFile::Close(Option_t *option)
{
if (!fList || !fSeekDir) {
return;
}
// Save the directory key list and header
Save();
Bool_t nodelete = option ? (!strcmp(option, "nodelete") ? kTRUE : kFALSE) : kFALSE;
if (!nodelete) {
Bool_t fast = kTRUE;
TObjLink *lnk = fList->FirstLink();
while (lnk) {
if (lnk->GetObject()->IsA() == TDirectoryFile::Class()) {fast = kFALSE;break;}
lnk = lnk->Next();
}
// Delete objects from directory list, this in turn, recursively closes all
// sub-directories (that were allocated on the heap)
// if this dir contains subdirs, we must use the slow option for Delete!
// we must avoid "slow" as much as possible, in particular Delete("slow")
// with a large number of objects (eg >10^5) would take for ever.
{
if (fast) fList->Delete();
else fList->Delete("slow");
}
}
// Delete keys from key list (but don't delete the list header)
if (fKeys) {
fKeys->Delete("slow");
}
CleanTargets();
}
////////////////////////////////////////////////////////////////////////////////
/// Delete Objects or/and keys in a directory
///
/// Properties of the namecycle string:
/// - namecycle has the format name;cycle
/// - namecycle = "" is same as namecycle ="T*"
/// - name = * means all
/// - cycle = * means all cycles (memory and keys)
/// - cycle = "" or cycle = 9999 ==> apply to a memory object
/// When name=* use T* to delete subdirectories also
///
/// To delete one directory, you must specify the directory cycle,
/// eg. file.Delete("dir1;1");
///
/// Examples:
/// | Pattern | Description |
/// |---------|-------------|
/// | foo | delete object named foo in memory |
/// | foo* | delete all objects with a name starting with foo |
/// | foo;1 | delete cycle 1 of foo on file |
/// | foo;* | delete all cycles of foo on file and also from memory |
/// | *;2 | delete all objects on file having the cycle 2 |
/// | *;* | delete all objects from memory and file |
/// | T*;* | delete all objects from memory and file and all subdirectories |
///
/// ## WARNING
/// If the key to be deleted contains special characters ("+","^","?", etc
/// that have a special meaning for the regular expression parser (see TRegexp)
/// then you must specify 2 backslash characters to escape the regular expression.
/// For example, if the key to be deleted is namecycle = "C++", you must call
///
/// mydir.Delete("C\\+\\+"));
///
void TDirectoryFile::Delete(const char *namecycle)
{
if (gDebug)
Info("Delete","Call for this = %s namecycle = %s",
GetName(), (namecycle ? namecycle : "null"));
TDirectory::TContext ctxt(this);
Short_t cycle;
char name[kMaxLen];
const char *nmcy = (namecycle) ? namecycle : "";
DecodeNameCycle(nmcy, name, cycle, kMaxLen);
Int_t deleteall = 0;
Int_t deletetree = 0;
if(strcmp(name,"*") == 0) deleteall = 1;
if(strcmp(name,"*T") == 0){ deleteall = 1; deletetree = 1;}
if(strcmp(name,"T*") == 0){ deleteall = 1; deletetree = 1;}
if(namecycle==0 || !namecycle[0]){ deleteall = 1; deletetree = 1;}
TRegexp re(name,kTRUE);
TString s;
Int_t deleteOK = 0;
//*-*---------------------Case of Object in memory---------------------
// ========================
if (cycle >= 9999 ) {
TNamed *idcur;
TIter next(fList);
while ((idcur = (TNamed *) next())) {
deleteOK = 0;
s = idcur->GetName();
if (deleteall || s.Index(re) != kNPOS) {
deleteOK = 1;
if (idcur->IsA() == TDirectoryFile::Class()) {
deleteOK = 2;
if (!deletetree && deleteall) deleteOK = 0;
}
}
if (deleteOK != 0) {
fList->Remove(idcur);
if (deleteOK==2) {
// read subdirectories to correctly delete them
if (deletetree)
((TDirectory*) idcur)->ReadAll("dirs");
idcur->Delete(deletetree ? "T*;*" : "*");
delete idcur;
} else
idcur->Delete(name);
}
}
// if (deleteOK == 2) {
// Info("Delete","Dir:%lx %s", fList->FindObject(name), name);
// delete fList->FindObject(name); //deleting a TDirectory
// }
}
//*-*---------------------Case of Key---------------------
// ===========
if (cycle != 9999 ) {
if (IsWritable()) {
TKey *key;
TIter nextkey(GetListOfKeys());
while ((key = (TKey *) nextkey())) {
deleteOK = 0;
s = key->GetName();
if (deleteall || s.Index(re) != kNPOS) {
if (cycle == key->GetCycle()) deleteOK = 1;
if (cycle > 9999) deleteOK = 1;
//if (!strcmp(key->GetClassName(),"TDirectory")) {
if (strstr(key->GetClassName(),"TDirectory")) {
deleteOK = 2;
if (!deletetree && deleteall) deleteOK = 0;
if (cycle == key->GetCycle()) deleteOK = 2;
}
}
if (deleteOK) {
if (deleteOK==2) {
// read directory with subdirectories to correctly delete and free key structure
TDirectory* dir = GetDirectory(key->GetName(), kTRUE, "Delete");
if (dir!=0) {
dir->Delete("T*;*");
fList->Remove(dir);
delete dir;
}
}
key->Delete();
fKeys->Remove(key);
fModified = kTRUE;
delete key;
}
}
TFile* f = GetFile();
if (fModified && (f!=0)) {
WriteKeys(); //*-* Write new keys structure
WriteDirHeader(); //*-* Write new directory header
f->WriteFree(); //*-* Write new free segments list
f->WriteHeader(); //*-* Write new file header
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Encode directory header into output buffer
void TDirectoryFile::FillBuffer(char *&buffer)
{
Version_t version = TDirectoryFile::Class_Version();
if (fSeekDir > TFile::kStartBigFile ||
fSeekParent > TFile::kStartBigFile ||
fSeekKeys > TFile::kStartBigFile )
{
// One of the address is larger than 2GB we need to use longer onfile
// integer, thus we increase the verison number.
// Note that fSeekDir and fSeekKey are not necessarily correlated, if
// some object are 'removed' from the file and the holes are reused.
version += 1000;
}
tobuf(buffer, version);
fDatimeC.FillBuffer(buffer);
fDatimeM.FillBuffer(buffer);
tobuf(buffer, fNbytesKeys);
tobuf(buffer, fNbytesName);
if (version > 1000) {
tobuf(buffer, fSeekDir);
tobuf(buffer, fSeekParent);
tobuf(buffer, fSeekKeys);
} else {
tobuf(buffer, (Int_t)fSeekDir);
tobuf(buffer, (Int_t)fSeekParent);
tobuf(buffer, (Int_t)fSeekKeys);
}
fUUID.FillBuffer(buffer);
if (fFile && fFile->GetVersion() < 40000) return;
if (version <=1000) for (Int_t i=0;i<3;i++) tobuf(buffer,Int_t(0));
}
////////////////////////////////////////////////////////////////////////////////
/// Find key with name keyname in the current directory
TKey *TDirectoryFile::FindKey(const char *keyname) const
{
Short_t cycle;
char name[kMaxLen];
DecodeNameCycle(keyname, name, cycle, kMaxLen);
return GetKey(name,cycle);
}
////////////////////////////////////////////////////////////////////////////////
/// Find key with name keyname in the current directory or
/// its subdirectories.
///
/// NOTE: that If a key is found, the directory containing the key becomes
/// the current directory
TKey *TDirectoryFile::FindKeyAny(const char *keyname) const
{
TDirectory *dirsav = gDirectory;
Short_t cycle;
char name[kMaxLen];
DecodeNameCycle(keyname, name, cycle, kMaxLen);
TIter next(GetListOfKeys());
TKey *key;
while ((key = (TKey *) next())) {
if (!strcmp(name, key->GetName()))
if ((cycle == 9999) || (cycle >= key->GetCycle())) {
((TDirectory*)this)->cd(); // may be we should not make cd ???
return key;
}
}
//try with subdirectories
next.Reset();
while ((key = (TKey *) next())) {
//if (!strcmp(key->GetClassName(),"TDirectory")) {
if (strstr(key->GetClassName(),"TDirectory")) {
TDirectory* subdir =
((TDirectory*)this)->GetDirectory(key->GetName(), kTRUE, "FindKeyAny");
TKey *k = (subdir!=0) ? subdir->FindKeyAny(keyname) : 0;
if (k) return k;
}
}
if (dirsav) dirsav->cd();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Find object by name in the list of memory objects of the current
/// directory or its sub-directories.
///
/// After this call the current directory is not changed.
/// To automatically set the current directory where the object is found,
/// use FindKeyAny(aname)->ReadObj().
TObject *TDirectoryFile::FindObjectAny(const char *aname) const
{
//object may be already in the list of objects in memory
TObject *obj = TDirectory::FindObjectAny(aname);
if (obj) return obj;
TDirectory *dirsav = gDirectory;
Short_t cycle;
char name[kMaxLen];
DecodeNameCycle(aname, name, cycle, kMaxLen);
TIter next(GetListOfKeys());
TKey *key;
//may be a key in the current directory
while ((key = (TKey *) next())) {
if (!strcmp(name, key->GetName())) {
if (cycle == 9999) return key->ReadObj();
if (cycle >= key->GetCycle()) return key->ReadObj();
}
}
//try with subdirectories
next.Reset();
while ((key = (TKey *) next())) {
//if (!strcmp(key->GetClassName(),"TDirectory")) {
if (strstr(key->GetClassName(),"TDirectory")) {
TDirectory* subdir =
((TDirectory*)this)->GetDirectory(key->GetName(), kTRUE, "FindKeyAny");
TKey *k = subdir==0 ? 0 : subdir->FindKeyAny(aname);
if (k) { if (dirsav) dirsav->cd(); return k->ReadObj();}
}
}
if (dirsav) dirsav->cd();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Return pointer to object identified by namecycle.
///
/// Properties:
/// - namecycle has the format name;cycle
/// - name = * is illegal, cycle = * is illegal
/// - cycle = "" or cycle = 9999 ==> apply to a memory object
///
/// Examples:
/// | Pattern | Explanation |
/// |---------|-------------|
/// | foo | get object named foo in memory if object is not in memory, try with highest cycle from file |
/// | foo;1 | get cycle 1 of foo on file |
///
/// The retrieved object should in principle derive from TObject.
/// If not, the function TDirectoryFile::GetObject should be called.
/// However, this function will still work for a non-TObject, provided that
/// the calling application cast the return type to the correct type (which
/// is the actual type of the object).
///
/// ### The GetObjectMethod
/// The method GetObject offers better protection and avoids the need
/// for any cast:
/// ~~~{.cpp}
/// MyClass *obj;
/// directory->GetObject("some object",obj);
/// if (obj) { ... the object exist and inherits from MyClass ... }
/// ~~~
///
/// ### Very important note about inheritance
/// In case the class of this object derives from TObject but not
/// as a first inheritance, one must use dynamic_cast<>().
///
/// #### Example 1 - Normal case:
///
/// class MyClass : public TObject, public AnotherClass
///
/// then on return, one can adopt a C style cast:
///
/// auto objPtr = (MyClass*)directory->Get("some object of MyClass");
///
/// #### Example 2 - Special case:
///
/// class MyClass : public AnotherClass, public TObject
///
/// then on return, one must do:
///
/// auto objPtr = dynamic_cast<MyClass*>(directory->Get("some object of MyClass"));
///
/// Of course, dynamic_cast<> can also be used in the example 1.
///
TObject *TDirectoryFile::Get(const char *namecycle)
{
Short_t cycle;
char name[kMaxLen];
DecodeNameCycle(namecycle, name, cycle, kMaxLen);
Int_t nch = strlen(name);
for (Int_t i = nch-1; i > 0; i--) {
if (name[i] == '/') {
name[i] = 0;
TDirectory* dirToSearch=GetDirectory(name);
const char *subnamecycle = namecycle + i + 1;
name[i] = '/';
return dirToSearch?dirToSearch->Get(subnamecycle):0;
}
}
const char *namobj = name;
//*-*---------------------Case of Object in memory---------------------
// ========================
TObject *idcur = fList ? fList->FindObject(namobj) : nullptr;
if (idcur) {
if (idcur==this && strlen(namobj)!=0) {
// The object has the same name has the directory and
// that's what we picked-up! We just need to ignore
// it ...
idcur = 0;
} else if (cycle == 9999) {
return idcur;
} else {
if (idcur->InheritsFrom(TCollection::Class()))
idcur->Delete(); // delete also list elements
delete idcur;
idcur = 0;
}
}
//*-*---------------------Case of Key---------------------
// ===========
TKey *key;
TIter nextkey(GetListOfKeys());
while ((key = (TKey *) nextkey())) {
if (strcmp(namobj,key->GetName()) == 0) {
if ((cycle == 9999) || (cycle == key->GetCycle())) {
TDirectory::TContext ctxt(this);
idcur = key->ReadObj();
break;
}
}
}
return idcur;
}
////////////////////////////////////////////////////////////////////////////////
/// Return pointer to object identified by namecycle.
///
/// The returned object may or may not derive from TObject.
///
/// - namecycle has the format name;cycle
/// - name = * is illegal, cycle = * is illegal
/// - cycle = "" or cycle = 9999 ==> apply to a memory object
///
/// ## Very important note
/// The calling application must cast the returned object to
/// the final type, e.g.
///
/// auto objPtr = (MyClass*)directory->GetObject("some object of MyClass");
void *TDirectoryFile::GetObjectUnchecked(const char *namecycle)
{
return GetObjectChecked(namecycle,(TClass*)0);
}
////////////////////////////////////////////////////////////////////////////////
/// See documentation of TDirectoryFile::GetObjectCheck(const char *namecycle, const TClass *cl)
void *TDirectoryFile::GetObjectChecked(const char *namecycle, const char* classname)
{
return GetObjectChecked(namecycle,TClass::GetClass(classname));
}
////////////////////////////////////////////////////////////////////////////////
/// Return pointer to object identified by namecycle if and only if the actual
/// object is a type suitable to be stored as a pointer to a "expectedClass"
/// If expectedClass is null, no check is performed.
///
/// - namecycle has the format name;cycle
/// - name = * is illegal, cycle = * is illegal
/// - cycle = "" or cycle = 9999 ==> apply to a memory object
///
/// ### Very important note
/// The calling application must cast the returned pointer to