-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TFile.cxx
5075 lines (4530 loc) · 173 KB
/
TFile.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: 3a19890259ad6443ee313e090166614971ad4296 $
// Author: Rene Brun 28/11/94
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/**
\defgroup IO Input/Output Library
The library collecting the ROOT classes dedicated to data input and output.
*/
/**
\file TFile.cxx
\class TFile
\ingroup IO
A ROOT file is a suite of consecutive data records (TKey instances) with
a well defined format.
If the key is located past the 32 bit file limit (> 2 GB) then some fields will
be 8 instead of 4 bytes:
Byte Range | Member Name | Description
----------------|-----------|--------------
1->4 | Nbytes | Length of compressed object (in bytes)
5->6 | Version | TKey version identifier
7->10 | ObjLen | Length of uncompressed object
11->14 | Datime | Date and time when object was written to file
15->16 | KeyLen | Length of the key structure (in bytes)
17->18 | Cycle | Cycle of key
19->22 [19->26] | SeekKey | Pointer to record itself (consistency check)
23->26 [27->34] | SeekPdir | Pointer to directory header
27->27 [35->35] | lname | Number of bytes in the class name
28->.. [36->..] | ClassName | Object Class Name
..->.. | lname | Number of bytes in the object name
..->.. | Name | lName bytes with the name of the object
..->.. | lTitle | Number of bytes in the object title
..->.. | Title | Title of the object
-----> | DATA | Data bytes associated to the object
The first data record starts at byte fBEGIN (currently set to kBEGIN).
Bytes 1->kBEGIN contain the file description, when fVersion >= 1000000
it is a large file (> 2 GB) and the offsets will be 8 bytes long and
fUnits will be set to 8:
Byte Range | Record Name | Description
----------------|-------------|------------
1->4 | "root" | Root file identifier
5->8 | fVersion | File format version
9->12 | fBEGIN | Pointer to first data record
13->16 [13->20] | fEND | Pointer to first free word at the EOF
17->20 [21->28] | fSeekFree | Pointer to FREE data record
21->24 [29->32] | fNbytesFree | Number of bytes in FREE data record
25->28 [33->36] | nfree | Number of free data records
29->32 [37->40] | fNbytesName | Number of bytes in TNamed at creation time
33->33 [41->41] | fUnits | Number of bytes for file pointers
34->37 [42->45] | fCompress | Compression level and algorithm
38->41 [46->53] | fSeekInfo | Pointer to TStreamerInfo record
42->45 [54->57] | fNbytesInfo | Number of bytes in TStreamerInfo record
46->63 [58->75] | fUUID | Universal Unique ID
Begin_Macro
../../../tutorials/io/file.C
End_Macro
The structure of a directory is shown in TDirectoryFile::TDirectoryFile
*/
#include "RConfig.h"
#ifdef R__LINUX
// for posix_fadvise
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#endif
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#ifndef WIN32
# include <unistd.h>
#else
# define ssize_t int
# include <io.h>
# include <sys/types.h>
#endif
#include "Bytes.h"
#include "Compression.h"
#include "Riostream.h"
#include "RConfigure.h"
#include "Strlen.h"
#include "TArrayC.h"
#include "TClass.h"
#include "TClassEdit.h"
#include "TClassTable.h"
#include "TDatime.h"
#include "TError.h"
#include "TFile.h"
#include "TFileCacheRead.h"
#include "TFileCacheWrite.h"
#include "TFree.h"
#include "TInterpreter.h"
#include "TKey.h"
#include "TMakeProject.h"
#include "TPluginManager.h"
#include "TProcessUUID.h"
#include "TRegexp.h"
#include "TPRegexp.h"
#include "TROOT.h"
#include "TStreamerInfo.h"
#include "TStreamerElement.h"
#include "TSystem.h"
#include "TTimeStamp.h"
#include "TVirtualPerfStats.h"
#include "TArchiveFile.h"
#include "TEnv.h"
#include "TVirtualMonitoring.h"
#include "TVirtualMutex.h"
#include "TMathBase.h"
#include "TObjString.h"
#include "TStopwatch.h"
#include "compiledata.h"
#include <cmath>
#include <set>
#include "TSchemaRule.h"
#include "TSchemaRuleSet.h"
#include "TThreadSlots.h"
#include "TGlobal.h"
#include "TMath.h"
using std::sqrt;
std::atomic<Long64_t> TFile::fgBytesRead{0};
std::atomic<Long64_t> TFile::fgBytesWrite{0};
std::atomic<Long64_t> TFile::fgFileCounter{0};
std::atomic<Int_t> TFile::fgReadCalls{0};
Int_t TFile::fgReadaheadSize = 256000;
Bool_t TFile::fgReadInfo = kTRUE;
TList *TFile::fgAsyncOpenRequests = 0;
TString TFile::fgCacheFileDir;
Bool_t TFile::fgCacheFileForce = kFALSE;
Bool_t TFile::fgCacheFileDisconnected = kTRUE;
UInt_t TFile::fgOpenTimeout = TFile::kEternalTimeout;
Bool_t TFile::fgOnlyStaged = 0;
#ifdef R__USE_IMT
ROOT::TRWSpinLock TFile::fgRwLock;
#endif
const Int_t kBEGIN = 100;
ClassImp(TFile);
//*-*x17 macros/layout_file
// Needed to add the "fake" global gFile to the list of globals.
namespace {
static struct AddPseudoGlobals {
AddPseudoGlobals() {
// User "gCling" as synonym for "libCore static initialization has happened".
// This code here must not trigger it.
TGlobalMappedFunction::Add(new TGlobalMappedFunction("gFile", "TFile*",
(TGlobalMappedFunction::GlobalFunc_t)&TFile::CurrentFile));
}
} gAddPseudoGlobals;
}
////////////////////////////////////////////////////////////////////////////////
/// File default Constructor.
TFile::TFile() : TDirectoryFile(), fInfoCache(0)
{
fD = -1;
fFree = 0;
fWritten = 0;
fSumBuffer = 0;
fSum2Buffer = 0;
fClassIndex = 0;
fCompress = 0;
fProcessIDs = 0;
fNProcessIDs = 0;
fOffset = 0;
fArchive = 0;
fCacheRead = 0;
fCacheReadMap = new TMap();
fCacheWrite = 0;
fArchiveOffset = 0;
fReadCalls = 0;
fInfoCache = 0;
fOpenPhases = 0;
fNoAnchorInName = kFALSE;
fIsRootFile = kTRUE;
fIsArchive = kFALSE;
fInitDone = kFALSE;
fMustFlush = kTRUE;
fIsPcmFile = kFALSE;
fAsyncHandle = 0;
fAsyncOpenStatus = kAOSNotAsync;
SetBit(kBinaryFile, kTRUE);
fBEGIN = 0;
fEND = 0;
fBytesRead = 0;
fBytesReadExtra = 0;
fBytesWrite = 0;
fCompress = 0;
fNbytesFree = 0;
fNbytesInfo = 0;
fSeekFree = 0;
fSeekInfo = 0;
fUnits = 0;
fVersion = 0;
if (gDebug)
Info("TFile", "default ctor");
}
////////////////////////////////////////////////////////////////////////////////
/// Opens or creates a local ROOT file.
///
/// \param[in] fname1 The name of the file
/// \param[in] option Specifies the mode in which the file is opened
/// \param[in] ftitle The title of the file
/// \param[in] compress Specifies the compression algorithm and level
///
/// It is recommended to specify fname1 as "<file>.root". The suffix ".root"
/// will be used by object browsers to automatically identify the file as
/// a ROOT file. If the constructor fails in any way IsZombie() will
/// return true. Use IsOpen() to check if the file is (still) open.
/// To open non-local files use the static TFile::Open() method, that
/// will take care of opening the files using the correct remote file
/// access plugin.
///
/// Option | Description
/// -------|------------
/// NEW or CREATE | Create a new file and open it for writing, if the file already exists the file is not opened.
/// RECREATE | Create a new file, if the file already exists it will be overwritten.
/// UPDATE | Open an existing file for writing. If no file exists, it is created.
/// READ | Open an existing file for reading (default).
/// NET | Used by derived remote file access classes, not a user callable option.
/// WEB | Used by derived remote http access class, not a user callable option.
///
/// If option = "" (default), READ is assumed.
/// The file can be specified as a URL of the form:
///
/// file:///user/rdm/bla.root or file:/user/rdm/bla.root
///
/// The file can also be a member of an archive, in which case it is
/// specified as:
///
/// multi.zip#file.root or multi.zip#0
///
/// which will open file.root which is a member of the file multi.zip
/// archive or member 1 from the archive. For more on archive file
/// support see the TArchiveFile class.
/// TFile and its remote access plugins can also be used to open any
/// file, i.e. also non ROOT files, using:
///
/// file.tar?filetype=raw
///
/// This is convenient because the many remote file access plugins allow
/// easy access to/from the many different mass storage systems.
/// The title of the file (ftitle) will be shown by the ROOT browsers.
/// A ROOT file (like a Unix file system) may contain objects and
/// directories. There are no restrictions for the number of levels
/// of directories.
/// A ROOT file is designed such that one can write in the file in pure
/// sequential mode (case of BATCH jobs). In this case, the file may be
/// read sequentially again without using the file index written
/// at the end of the file. In case of a job crash, all the information
/// on the file is therefore protected.
/// A ROOT file can be used interactively. In this case, one has the
/// possibility to delete existing objects and add new ones.
/// When an object is deleted from the file, the freed space is added
/// into the FREE linked list (fFree). The FREE list consists of a chain
/// of consecutive free segments on the file. At the same time, the first
/// 4 bytes of the freed record on the file are overwritten by GAPSIZE
/// where GAPSIZE = -(Number of bytes occupied by the record).
/// Option compress is used to specify the compression level and algorithm:
///
/// compress = 100 * algorithm + level
///
/// Level | Explanation
/// ------|-------------
/// 0 | objects written to this file will not be compressed.
/// 1 | minimal compression level but fast.
/// ... | ....
/// 9 | maximal compression level but slower and might use more memory.
/// (For the currently supported algorithms, the maximum level is 9)
/// If compress is negative it indicates the compression level is not set yet.
/// The enumeration ROOT::ECompressionAlgorithm associates each
/// algorithm with a number. There is a utility function to help
/// to set the value of compress. For example,
/// ROOT::CompressionSettings(ROOT::kLZMA, 1)
/// will build an integer which will set the compression to use
/// the LZMA algorithm and compression level 1. These are defined
/// in the header file <em>Compression.h</em>.
/// Note that the compression settings may be changed at any time.
/// The new compression settings will only apply to branches created
/// or attached after the setting is changed and other objects written
/// after the setting is changed.
/// In case the file does not exist or is not a valid ROOT file,
/// it is made a Zombie. One can detect this situation with a code like:
/// ~~~{.cpp}
/// TFile f("file.root");
/// if (f.IsZombie()) {
/// std::cout << "Error opening file" << std::endl;
/// exit(-1);
/// }
/// ~~~
/// When opening the file, the system checks the validity of this directory.
/// If something wrong is detected, an automatic Recovery is performed. In
/// this case, the file is scanned sequentially reading all logical blocks
/// and attempting to rebuild a correct directory (see TFile::Recover).
/// One can disable the automatic recovery procedure when reading one
/// or more files by setting the environment variable "TFile.Recover: 0"
/// in the system.rootrc file.
///
TFile::TFile(const char *fname1, Option_t *option, const char *ftitle, Int_t compress)
: TDirectoryFile(), fUrl(fname1,kTRUE), fInfoCache(0), fOpenPhases(0)
{
if (!gROOT)
::Fatal("TFile::TFile", "ROOT system not initialized");
// store name without the options as name and title
TString sfname1 = fname1;
fNoAnchorInName = kFALSE;
if (sfname1.Index("?") != kNPOS) {
TString s = sfname1(0, sfname1.Index("?"));
SetName(s);
fNoAnchorInName = kTRUE;
} else
SetName(fname1);
SetTitle(ftitle);
// accept also URL like "file:..." syntax
fname1 = fUrl.GetFile();
// if option contains filetype=raw then go into raw file mode
fIsRootFile = kTRUE;
if (strstr(fUrl.GetOptions(), "filetype=raw"))
fIsRootFile = kFALSE;
// if option contains filetype=pcm then go into ROOT PCM file mode
fIsPcmFile = kFALSE;
if (strstr(fUrl.GetOptions(), "filetype=pcm"))
fIsPcmFile = kTRUE;
// Init initialization control flag
fInitDone = kFALSE;
fMustFlush = kTRUE;
// We are opening synchronously
fAsyncHandle = 0;
fAsyncOpenStatus = kAOSNotAsync;
TDirectoryFile::Build(this, 0);
fD = -1;
fFree = 0;
fVersion = gROOT->GetVersionInt(); //ROOT version in integer format
fUnits = 4;
fOption = option;
fCompress = compress;
fWritten = 0;
fSumBuffer = 0;
fSum2Buffer = 0;
fBytesRead = 0;
fBytesReadExtra = 0;
fBytesWrite = 0;
fClassIndex = 0;
fSeekInfo = 0;
fNbytesInfo = 0;
fProcessIDs = 0;
fNProcessIDs = 0;
fOffset = 0;
fCacheRead = 0;
fCacheReadMap = new TMap();
fCacheWrite = 0;
fReadCalls = 0;
SetBit(kBinaryFile, kTRUE);
fOption.ToUpper();
fArchiveOffset = 0;
fIsArchive = kFALSE;
fArchive = 0;
if (fIsRootFile && !fIsPcmFile && fOption != "NEW" && fOption != "CREATE"
&& fOption != "RECREATE") {
// If !gPluginMgr then we are at startup and cannot handle plugins
// as TArchiveFile yet.
fArchive = gPluginMgr ? TArchiveFile::Open(fUrl.GetUrl(), this) : 0;
if (fArchive) {
fname1 = fArchive->GetArchiveName();
// if no archive member is specified then this TFile is just used
// to read the archive contents
if (!strlen(fArchive->GetMemberName()))
fIsArchive = kTRUE;
}
}
if (fOption == "NET")
return;
if (fOption == "WEB") {
fOption = "READ";
fWritable = kFALSE;
return;
}
if (fOption == "NEW")
fOption = "CREATE";
Bool_t create = (fOption == "CREATE") ? kTRUE : kFALSE;
Bool_t recreate = (fOption == "RECREATE") ? kTRUE : kFALSE;
Bool_t update = (fOption == "UPDATE") ? kTRUE : kFALSE;
Bool_t read = (fOption == "READ") ? kTRUE : kFALSE;
if (!create && !recreate && !update && !read) {
read = kTRUE;
fOption = "READ";
}
Bool_t devnull = kFALSE;
if (!fname1 || !fname1[0]) {
Error("TFile", "file name is not specified");
goto zombie;
}
// support dumping to /dev/null on UNIX
if (!strcmp(fname1, "/dev/null") &&
!gSystem->AccessPathName(fname1, kWritePermission)) {
devnull = kTRUE;
create = kTRUE;
recreate = kFALSE;
update = kFALSE;
read = kFALSE;
fOption = "CREATE";
SetBit(kDevNull);
}
const char *fname;
if ((fname = gSystem->ExpandPathName(fname1))) {
SetName(fname);
delete [] fname;
fRealName = GetName();
fname = fRealName.Data();
} else {
Error("TFile", "error expanding path %s", fname1);
goto zombie;
}
if (recreate) {
if (!gSystem->AccessPathName(fname, kFileExists)) {
if (gSystem->Unlink(fname) != 0) {
SysError("TFile", "could not delete %s (errno: %d)",
fname, gSystem->GetErrno());
goto zombie;
}
}
recreate = kFALSE;
create = kTRUE;
fOption = "CREATE";
}
if (create && !devnull && !gSystem->AccessPathName(fname, kFileExists)) {
Error("TFile", "file %s already exists", fname);
goto zombie;
}
if (update) {
if (gSystem->AccessPathName(fname, kFileExists)) {
update = kFALSE;
create = kTRUE;
}
if (update && gSystem->AccessPathName(fname, kWritePermission)) {
Error("TFile", "no write permission, could not open file %s", fname);
goto zombie;
}
}
if (read) {
if (gSystem->AccessPathName(fname, kFileExists)) {
Error("TFile", "file %s does not exist", fname);
goto zombie;
}
if (gSystem->AccessPathName(fname, kReadPermission)) {
Error("TFile", "no read permission, could not open file %s", fname);
goto zombie;
}
}
// Connect to file system stream
if (create || update) {
#ifndef WIN32
fD = SysOpen(fname, O_RDWR | O_CREAT, 0644);
#else
fD = SysOpen(fname, O_RDWR | O_CREAT | O_BINARY, S_IREAD | S_IWRITE);
#endif
if (fD == -1) {
SysError("TFile", "file %s can not be opened", fname);
goto zombie;
}
fWritable = kTRUE;
} else {
#ifndef WIN32
fD = SysOpen(fname, O_RDONLY, 0644);
#else
fD = SysOpen(fname, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
#endif
if (fD == -1) {
SysError("TFile", "file %s can not be opened for reading", fname);
goto zombie;
}
fWritable = kFALSE;
}
Init(create);
return;
zombie:
// error in file opening occurred, make this object a zombie
{
R__LOCKGUARD(gROOTMutex);
gROOT->GetListOfClosedObjects()->Add(this);
}
MakeZombie();
gDirectory = gROOT;
}
////////////////////////////////////////////////////////////////////////////////
/// TFile objects can not be copied.
TFile::TFile(const TFile &) : TDirectoryFile(), fInfoCache(0)
{
MayNotUse("TFile::TFile(const TFile &)");
}
////////////////////////////////////////////////////////////////////////////////
/// File destructor.
TFile::~TFile()
{
Close();
// In case where the TFile is still open at 'tear-down' time the order of operation will be
// call Close("nodelete")
// then later call delete TFile
// which means that at this point we might still have object held and those
// might requires a 'valid' TFile object in their desctructor (for example,
// TTree call's GetReadCache which expects a non-null fCacheReadMap).
// So delete the objects (if any) now.
if (fList)
fList->Delete("slow");
SafeDelete(fAsyncHandle);
SafeDelete(fCacheRead);
SafeDelete(fCacheReadMap);
SafeDelete(fCacheWrite);
SafeDelete(fProcessIDs);
SafeDelete(fFree);
SafeDelete(fArchive);
SafeDelete(fInfoCache);
SafeDelete(fOpenPhases);
{
R__LOCKGUARD(gROOTMutex);
gROOT->GetListOfClosedObjects()->Remove(this);
gROOT->GetUUIDs()->RemoveUUID(GetUniqueID());
}
if (IsOnHeap()) {
// Delete object from CINT symbol table so it can not be used anymore.
// CINT object are always on the heap.
gInterpreter->ResetGlobalVar(this);
}
if (gDebug)
Info("~TFile", "dtor called for %s [%lx]", GetName(),(Long_t)this);
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize a TFile object.
///
/// \param[in] create Create a new file.
///
/// TFile implementations providing asynchronous open functionality need to
/// override this method to run the appropriate checks before calling this
/// standard initialization part. See TXNetFile::Init for an example.
void TFile::Init(Bool_t create)
{
if (fInitDone)
// Already called once
return;
fInitDone = kTRUE;
if (!fIsRootFile) {
gDirectory = gROOT;
return;
}
if (fArchive) {
if (fOption != "READ") {
Error("Init", "archive %s can only be opened in read mode", GetName());
delete fArchive;
fArchive = 0;
fIsArchive = kFALSE;
goto zombie;
}
fArchive->OpenArchive();
if (fIsArchive) return;
// Make sure the anchor is in the name
if (!fNoAnchorInName)
if (!strchr(GetName(),'#'))
SetName(TString::Format("%s#%s", GetName(), fArchive->GetMemberName()));
if (fArchive->SetCurrentMember() != -1)
fArchiveOffset = fArchive->GetMemberFilePosition();
else {
Error("Init", "member %s not found in archive %s",
fArchive->GetMemberName(), fArchive->GetArchiveName());
delete fArchive;
fArchive = 0;
fIsArchive = kFALSE;
goto zombie;
}
}
Int_t nfree;
fBEGIN = (Long64_t)kBEGIN; //First used word in file following the file header
// make newly opened file the current file and directory
cd();
if (create) {
//*-*---------------NEW file
fFree = new TList;
fEND = fBEGIN; //Pointer to end of file
new TFree(fFree, fBEGIN, Long64_t(kStartBigFile)); //Create new free list
//*-* Write Directory info
Int_t namelen= TNamed::Sizeof();
Int_t nbytes = namelen + TDirectoryFile::Sizeof();
TKey *key = new TKey(fName, fTitle, IsA(), nbytes, this);
fNbytesName = key->GetKeylen() + namelen;
fSeekDir = key->GetSeekKey();
fSeekFree = 0;
fNbytesFree = 0;
WriteHeader();
char *buffer = key->GetBuffer();
TNamed::FillBuffer(buffer);
TDirectoryFile::FillBuffer(buffer);
key->WriteFile();
delete key;
} else {
//*-*----------------UPDATE
//char *header = new char[kBEGIN];
char *header = new char[kBEGIN+200];
Seek(0);
//ReadBuffer(header, kBEGIN);
if (ReadBuffer(header, kBEGIN+200)) {
// ReadBuffer returns kTRUE in case of failure.
Error("Init","%s failed to read the file type data.",
GetName());
delete [] header;
goto zombie;
}
// make sure this is a ROOT file
if (strncmp(header, "root", 4)) {
Error("Init", "%s not a ROOT file", GetName());
delete [] header;
goto zombie;
}
char *buffer = header + 4; // skip the "root" file identifier
frombuf(buffer, &fVersion);
Int_t headerLength;
frombuf(buffer, &headerLength);
fBEGIN = (Long64_t)headerLength;
if (fVersion < 1000000) { //small file
Int_t send,sfree,sinfo;
frombuf(buffer, &send); fEND = (Long64_t)send;
frombuf(buffer, &sfree); fSeekFree= (Long64_t)sfree;
frombuf(buffer, &fNbytesFree);
frombuf(buffer, &nfree);
frombuf(buffer, &fNbytesName);
frombuf(buffer, &fUnits );
frombuf(buffer, &fCompress);
frombuf(buffer, &sinfo); fSeekInfo = (Long64_t)sinfo;
frombuf(buffer, &fNbytesInfo);
} else { // new format to support large files
frombuf(buffer, &fEND);
frombuf(buffer, &fSeekFree);
frombuf(buffer, &fNbytesFree);
frombuf(buffer, &nfree);
frombuf(buffer, &fNbytesName);
frombuf(buffer, &fUnits );
frombuf(buffer, &fCompress);
frombuf(buffer, &fSeekInfo);
frombuf(buffer, &fNbytesInfo);
}
if (fBEGIN < 0 || fBEGIN > fEND) {
// humm fBEGIN is wrong ....
Error("Init","file %s has an incorrect header length (%lld) or incorrect end of file length (%lld)",
GetName(),fBEGIN,fEND);
goto zombie;
}
fSeekDir = fBEGIN;
//*-*-------------Read Free segments structure if file is writable
if (fWritable) {
fFree = new TList;
if (fSeekFree > fBEGIN) {
ReadFree();
} else {
Warning("Init","file %s probably not closed, cannot read free segments",GetName());
}
}
//*-*-------------Read directory info
// buffer_keyloc is the start of the key record.
char *buffer_keyloc = 0;
Int_t nbytes = fNbytesName + TDirectoryFile::Sizeof();
if ( (nbytes + fBEGIN) > fEND) {
// humm fBEGIN is wrong ....
Error("Init","file %s has an incorrect header length (%lld) or incorrect end of file length (%lld)",
GetName(),fBEGIN+nbytes,fEND);
goto zombie;
}
if (nbytes+fBEGIN > kBEGIN+200) {
delete [] header;
header = new char[nbytes];
buffer = header;
Seek(fBEGIN);
if (ReadBuffer(buffer,nbytes)) {
// ReadBuffer returns kTRUE in case of failure.
Error("Init","%s failed to read the file header information at %lld (size=%d)",
GetName(),fBEGIN,nbytes);
delete [] header;
goto zombie;
}
buffer = header+fNbytesName;
buffer_keyloc = header;
} else {
buffer = header+fBEGIN+fNbytesName;
buffer_keyloc = header+fBEGIN;
}
Version_t version,versiondir;
frombuf(buffer,&version); versiondir = version%1000;
fDatimeC.ReadBuffer(buffer);
fDatimeM.ReadBuffer(buffer);
frombuf(buffer, &fNbytesKeys);
frombuf(buffer, &fNbytesName);
if (version > 1000) {
frombuf(buffer, &fSeekDir);
frombuf(buffer, &fSeekParent);
frombuf(buffer, &fSeekKeys);
} else {
Int_t sdir,sparent,skeys;
frombuf(buffer, &sdir); fSeekDir = (Long64_t)sdir;
frombuf(buffer, &sparent); fSeekParent = (Long64_t)sparent;
frombuf(buffer, &skeys); fSeekKeys = (Long64_t)skeys;
}
if (versiondir > 1) fUUID.ReadBuffer(buffer);
//*-*---------read TKey::FillBuffer info
buffer_keyloc += sizeof(Int_t); // Skip NBytes;
Version_t keyversion;
frombuf(buffer_keyloc, &keyversion);
// Skip ObjLen, DateTime, KeyLen, Cycle, SeekKey, SeekPdir
if (keyversion > 1000) {
// Large files
buffer_keyloc += 2*sizeof(Int_t)+2*sizeof(Short_t)+2*sizeof(Long64_t);
} else {
buffer_keyloc += 2*sizeof(Int_t)+2*sizeof(Short_t)+2*sizeof(Int_t);
}
TString cname;
cname.ReadBuffer(buffer_keyloc);
cname.ReadBuffer(buffer_keyloc); // fName.ReadBuffer(buffer); file may have been renamed
fTitle.ReadBuffer(buffer_keyloc);
delete [] header;
if (fNbytesName < 10 || fNbytesName > 10000) {
Error("Init","cannot read directory info of file %s", GetName());
goto zombie;
}
//*-* -------------Check if file is truncated
Long64_t size;
if ((size = GetSize()) == -1) {
Error("Init", "cannot stat the file %s", GetName());
goto zombie;
}
//*-* -------------Check if, in case of inconsistencies, we are requested to
//*-* -------------attempt recovering the file
Bool_t tryrecover = (gEnv->GetValue("TFile.Recover", 1) == 1) ? kTRUE : kFALSE;
//*-* -------------Read keys of the top directory
if (fSeekKeys > fBEGIN && fEND <= size) {
//normal case. Recover only if file has no keys
TDirectoryFile::ReadKeys(kFALSE);
gDirectory = this;
if (!GetNkeys()) {
if (tryrecover) {
Recover();
} else {
Error("Init", "file %s has no keys", GetName());
goto zombie;
}
}
} else if ((fBEGIN+nbytes == fEND) && (fEND == size)) {
//the file might be open by another process and nothing written to the file yet
Warning("Init","file %s has no keys", GetName());
gDirectory = this;
} else {
//something had been written to the file. Trailer is missing, must recover
if (fEND > size) {
if (tryrecover) {
Error("Init","file %s is truncated at %lld bytes: should be %lld, "
"trying to recover", GetName(), size, fEND);
} else {
Error("Init","file %s is truncated at %lld bytes: should be %lld",
GetName(), size, fEND);
goto zombie;
}
} else {
if (tryrecover) {
Warning("Init","file %s probably not closed, "
"trying to recover", GetName());
} else {
Warning("Init","file %s probably not closed", GetName());
goto zombie;
}
}
Int_t nrecov = Recover();
if (nrecov) {
Warning("Init", "successfully recovered %d keys", nrecov);
} else {
Warning("Init", "no keys recovered, file has been made a Zombie");
goto zombie;
}
}
}
{
R__LOCKGUARD(gROOTMutex);
gROOT->GetListOfFiles()->Add(this);
gROOT->GetUUIDs()->AddUUID(fUUID,this);
}
// Create StreamerInfo index
{
Int_t lenIndex = gROOT->GetListOfStreamerInfo()->GetSize()+1;
if (lenIndex < 5000) lenIndex = 5000;
fClassIndex = new TArrayC(lenIndex);
if (fgReadInfo) {
if (fSeekInfo > fBEGIN) {
ReadStreamerInfo();
if (IsZombie()) {
R__LOCKGUARD(gROOTMutex);
gROOT->GetListOfFiles()->Remove(this);
goto zombie;
}
} else if (fVersion != gROOT->GetVersionInt() && fVersion > 30000) {
// Don't complain about missing streamer info for empty files.
if (fKeys->GetSize()) {
Warning("Init","no StreamerInfo found in %s therefore preventing schema evolution when reading this file.",GetName());
}
}
}
}
// Count number of TProcessIDs in this file
{
TIter next(fKeys);
TKey *key;
while ((key = (TKey*)next())) {
if (!strcmp(key->GetClassName(),"TProcessID")) fNProcessIDs++;
}
fProcessIDs = new TObjArray(fNProcessIDs+1);
}
return;
zombie:
{
R__LOCKGUARD(gROOTMutex);
gROOT->GetListOfClosedObjects()->Add(this);
}
// error in file opening occurred, make this object a zombie
fWritable = kFALSE;
MakeZombie();
gDirectory = gROOT;
}
////////////////////////////////////////////////////////////////////////////////
/// Close a file.
///
/// \param[in] option If option == "R", all TProcessIDs referenced by this file are deleted.
///
/// Calling TFile::Close("R") might be necessary in case one reads a long list
/// of files having TRef, writing some of the referenced objects or TRef
/// to a new file. If the TRef or referenced objects of the file being closed
/// will not be referenced again, it is possible to minimize the size
/// of the TProcessID data structures in memory by forcing a delete of
/// the unused TProcessID.
void TFile::Close(Option_t *option)
{
TString opt = option;
opt.ToLower();
if (!IsOpen()) return;
if (fIsArchive || !fIsRootFile) {
FlushWriteCache();
SysClose(fD);
fD = -1;
if (gMonitoringWriter)
gMonitoringWriter->SendFileCloseEvent(this);
return;
}
if (IsWritable()) {
WriteStreamerInfo();
}
// Finish any concurrent I/O operations before we close the file handles.
if (fCacheRead) fCacheRead->Close();
{
TIter iter(fCacheReadMap);
TObject *key = 0;
while ((key = iter()) != 0) {
TFileCacheRead *cache = dynamic_cast<TFileCacheRead *>(fCacheReadMap->GetValue(key));
cache->Close();
}
}
// Delete all supported directories structures from memory
// If gDirectory points to this object or any of the nested
// TDirectoryFile, TDirectoryFile::Close will induce the proper cd.
fMustFlush = kFALSE; // Make sure there is only one Flush.
TDirectoryFile::Close(option);
if (IsWritable()) {
TFree *f1 = (TFree*)fFree->First();
if (f1) {
WriteFree(); //*-*- Write free segments linked list
WriteHeader(); //*-*- Now write file header ; this forces a Flush/fsync
} else {
Flush();
}
}
fMustFlush = kTRUE;
FlushWriteCache();
if (gMonitoringWriter)
gMonitoringWriter->SendFileCloseEvent(this);
delete fClassIndex;
fClassIndex = 0;
// Delete free segments from free list (but don't delete list header)
if (fFree) {
fFree->Delete();
}
if (IsOpen()) {
SysClose(fD);
fD = -1;
}
fWritable = kFALSE;
// delete the TProcessIDs
TList pidDeleted;
TIter next(fProcessIDs);
TProcessID *pid;
while ((pid = (TProcessID*)next())) {
if (!pid->DecrementCount()) {
if (pid != TProcessID::GetSessionProcessID()) pidDeleted.Add(pid);
} else if(opt.Contains("r")) {
pid->Clear();
}
}
pidDeleted.Delete();
if (!IsZombie()) {
R__LOCKGUARD(gROOTMutex);