ROOT logo
// @(#)root/io:$Id: TFile.h 23122 2008-04-10 14:56:30Z rdm $
// 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.             *
 *************************************************************************/

#ifndef ROOT_TFile
#define ROOT_TFile


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TFile                                                                //
//                                                                      //
// ROOT file.                                                           //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TDirectoryFile
#include "TDirectoryFile.h"
#endif
#ifndef ROOT_TUrl
#include "TUrl.h"
#endif

class TFree;
class TArrayC;
class TArchiveFile;
class TFileOpenHandle;
class TFileCacheRead;
class TFileCacheWrite;
class TProcessID;
class TStopwatch;


class TFile : public TDirectoryFile {
  friend class TDirectoryFile;

public:
   // Asynchronous open request status
   enum EAsyncOpenStatus { kAOSNotAsync = -1,  kAOSFailure = 0,
                           kAOSInProgress = 1, kAOSSuccess = 2 };
   // Open timeout constants
   enum EOpenTimeOut { kInstantTimeout = 0, kEternalTimeout = 999999999 };

protected:
   Double_t         fSumBuffer;      //Sum of buffer sizes of objects written so far
   Double_t         fSum2Buffer;     //Sum of squares of buffer sizes of objects written so far
   Long64_t         fBytesWrite;     //Number of bytes written to this file
   Long64_t         fBytesRead;      //Number of bytes read from this file
   Long64_t         fBEGIN;          //First used byte in file
   Long64_t         fEND;            //Last used byte in file
   Long64_t         fSeekFree;       //Location on disk of free segments structure
   Long64_t         fSeekInfo;       //Location on disk of StreamerInfo record
   Int_t            fD;              //File descriptor
   Int_t            fVersion;        //File format version
   Int_t            fCompress;       //Compression level from 0(not compressed) to 9 (max compression)
   Int_t            fNbytesFree;     //Number of bytes for free segments structure
   Int_t            fNbytesInfo;     //Number of bytes for StreamerInfo record
   Int_t            fWritten;        //Number of objects written so far
   Int_t            fNProcessIDs;    //Number of TProcessID written to this file
   Int_t            fReadCalls;      //Number of read calls ( not counting the cache calls )
   TString          fRealName;       //Effective real file name (not original url)
   TString          fOption;         //File options
   Char_t           fUnits;          //Number of bytes for file pointers
   TList           *fFree;           //Free segments linked list table
   TArrayC         *fClassIndex;     //!Index of TStreamerInfo classes written to this file
   TObjArray       *fProcessIDs;     //!Array of pointers to TProcessIDs
   Long64_t         fOffset;         //!Seek offset used by remote file classes
   TArchiveFile    *fArchive;        //!Archive file from which we read this file
   TFileCacheRead  *fCacheRead;      //!Pointer to the read cache (if any)
   TFileCacheWrite *fCacheWrite;     //!Pointer to the write cache (if any)
   Long64_t         fArchiveOffset;  //!Offset at which file starts in archive
   Bool_t           fIsArchive;      //!True if this is a pure archive file
   Bool_t           fNoAnchorInName; //!True if we don't want to force the anchor to be appended to the file name
   Bool_t           fIsRootFile;     //!True is this is a ROOT file, raw file otherwise
   Bool_t           fInitDone;       //!True if the file has been initialized
   Bool_t           fMustFlush;      //!True if the file buffers must be flushed
   TFileOpenHandle *fAsyncHandle;    //!For proper automatic cleanup
   EAsyncOpenStatus fAsyncOpenStatus; //!Status of an asynchronous open request
   TUrl             fUrl;            //!URL of file

   TList           *fInfoCache;      //!Cached list of the streamer infos in this file
   TList           *fOpenPhases;     //!Time info about open phases

   static TList    *fgAsyncOpenRequests; //List of handles for pending open requests

   static TString   fgCacheFileDir;          //Directory where to locally stage files
   static Bool_t    fgCacheFileDisconnected; //Indicates, we trust in the files in the cache dir without stat on the cached file
   static Bool_t    fgCacheFileForce;        //Indicates, to force all READ to CACHEREAD
   static UInt_t    fgOpenTimeout; //Timeout for open operations in ms  - 0 corresponds to blocking i/o
   static Bool_t    fgOnlyStaged ; //Before the file is opened, it is checked, that the file is staged, if not, the open fails
   static Long64_t  fgBytesWrite;  //Number of bytes written by all TFile objects
   static Long64_t  fgBytesRead;   //Number of bytes read by all TFile objects
   static Long64_t  fgFileCounter; //Counter for all opened files
   static Int_t     fgReadCalls;   //Number of bytes read from all TFile objects
   static Bool_t    fgReadInfo;    //if true (default) ReadStreamerInfo is called when opening a file

   virtual EAsyncOpenStatus GetAsyncOpenStatus() { return fAsyncOpenStatus; }
   Long64_t      GetRelOffset() const { return fOffset - fArchiveOffset; }
   virtual void  Init(Bool_t create);
   Bool_t        FlushWriteCache();
   Int_t         ReadBufferViaCache(char *buf, Int_t len);
   Int_t         WriteBufferViaCache(const char *buf, Int_t len);

   // Interface to basic system I/O routines
   virtual Int_t    SysOpen(const char *pathname, Int_t flags, UInt_t mode);
   virtual Int_t    SysClose(Int_t fd);
   virtual Int_t    SysRead(Int_t fd, void *buf, Int_t len);
   virtual Int_t    SysWrite(Int_t fd, const void *buf, Int_t len);
   virtual Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence);
   virtual Int_t    SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime);
   virtual Int_t    SysSync(Int_t fd);

   // Interface for text-based TDirectory I/O
   virtual Long64_t DirCreateEntry(TDirectory*) { return 0; }
   virtual Int_t    DirReadKeys(TDirectory*) { return 0; }
   virtual void     DirWriteKeys(TDirectory*) {}
   virtual void     DirWriteHeader(TDirectory*) {}

private:
   TFile(const TFile &);            //Files cannot be copied
   void operator=(const TFile &);

   static void   CpProgress(Long64_t bytesread, Long64_t size, TStopwatch &watch);
   static TFile *OpenFromCache(const char *name, Option_t *option = "",
                               const char *ftitle = "", Int_t compress = 1,
                               Int_t netopt = 0);

public:
   // TFile status bits
   enum EStatusBits {
      kRecovered     = BIT(10),
      kHasReferences = BIT(11),
      kDevNull       = BIT(12),
      kWriteError    = BIT(14), // BIT(13) is taken up by TObject
      kBinaryFile    = BIT(15),
      kRedirected    = BIT(16)
   };
   enum ERelativeTo { kBeg = 0, kCur = 1, kEnd = 2 };
   enum { kStartBigFile  = 2000000000 };
   // File type
   enum EFileType { kDefault = 0, kLocal = 1, kNet = 2, kWeb = 3, kFile = 4};

   TFile();
   TFile(const char *fname, Option_t *option="", const char *ftitle="", Int_t compress=1);
   virtual ~TFile();
   virtual void        Close(Option_t *option=""); // *MENU*
   virtual void        Copy(TObject &) const { MayNotUse("Copy(TObject &)"); }
   virtual TKey*       CreateKey(TDirectory* mother, const TObject* obj, const char* name, Int_t bufsize);
   virtual TKey*       CreateKey(TDirectory* mother, const void* obj, const TClass* cl,
                                 const char* name, Int_t bufsize);
   virtual void        Delete(const char *namecycle="");
   virtual void        Draw(Option_t *option="");
   virtual void        DrawMap(const char *keys="*",Option_t *option=""); // *MENU*
   virtual void        FillBuffer(char *&buffer);
   virtual void        Flush();
   TArchiveFile       *GetArchive() const { return fArchive; }
   Int_t               GetBestBuffer() const;
   virtual Int_t       GetBytesToPrefetch() const;
   TFileCacheRead     *GetCacheRead() const;
   TFileCacheWrite    *GetCacheWrite() const;
   TArrayC            *GetClassIndex() const { return fClassIndex; }
   Int_t               GetCompressionLevel() const { return fCompress; }
   Float_t             GetCompressionFactor();
   virtual Long64_t    GetEND() const { return fEND; }
   virtual Int_t       GetErrno() const;
   virtual void        ResetErrno() const;
   Int_t               GetFd() const { return fD; }
   virtual const TUrl *GetEndpointUrl() const { return &fUrl; }
   TObjArray          *GetListOfProcessIDs() const {return fProcessIDs;}
   TList              *GetListOfFree() const { return fFree; }
   virtual Int_t       GetNfree() const { return fFree->GetSize(); }
   virtual Int_t       GetNProcessIDs() const { return fNProcessIDs; }
   Option_t           *GetOption() const { return fOption.Data(); }
   virtual Long64_t    GetBytesRead() const { return fBytesRead; }
   virtual Long64_t    GetBytesWritten() const;
   virtual Int_t       GetReadCalls() const { return fReadCalls; }
   Int_t               GetVersion() const { return fVersion; }
   Int_t               GetRecordHeader(char *buf, Long64_t first, Int_t maxbytes,
                                       Int_t &nbytes, Int_t &objlen, Int_t &keylen);
   virtual Int_t       GetNbytesInfo() const {return fNbytesInfo;}
   virtual Int_t       GetNbytesFree() const {return fNbytesFree;}
   virtual Long64_t    GetSeekFree() const {return fSeekFree;}
   virtual Long64_t    GetSeekInfo() const {return fSeekInfo;}
   virtual Long64_t    GetSize() const;
   virtual TList      *GetStreamerInfoList();
   const   TList      *GetStreamerInfoCache();
   virtual void        IncrementProcessIDs() { fNProcessIDs++; }
   virtual Bool_t      IsArchive() const { return fIsArchive; }
           Bool_t      IsBinary() const { return TestBit(kBinaryFile); }
           Bool_t      IsRaw() const { return !fIsRootFile; }
   virtual Bool_t      IsOpen() const;
   virtual void        ls(Option_t *option="") const;
   virtual void        MakeFree(Long64_t first, Long64_t last);
   virtual void        MakeProject(const char *dirname, const char *classes="*",
                                   Option_t *option="new"); // *MENU*
   virtual void        Map(); // *MENU*
   virtual Bool_t      Matches(const char *name);
   virtual Bool_t      MustFlush() const {return fMustFlush;}
   virtual void        Paint(Option_t *option="");
   virtual void        Print(Option_t *option="") const;
   virtual Bool_t      ReadBufferAsync(Long64_t offs, Int_t len);
   virtual Bool_t      ReadBuffer(char *buf, Int_t len);
   virtual Bool_t      ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf);
   virtual void        ReadFree();
   virtual TProcessID *ReadProcessID(UShort_t pidf);
   virtual void        ReadStreamerInfo();
   virtual Int_t       Recover();
   virtual Int_t       ReOpen(Option_t *mode);
   virtual void        Seek(Long64_t offset, ERelativeTo pos = kBeg);
   virtual void        SetCacheRead(TFileCacheRead *cache);
   virtual void        SetCacheWrite(TFileCacheWrite *cache);
   virtual void        SetCompressionLevel(Int_t level=1);
   virtual void        SetEND(Long64_t last) { fEND = last; }
   virtual void        SetOption(Option_t *option=">") { fOption = option; }
   virtual void        SetReadCalls(Int_t readcalls = 0) { fReadCalls = readcalls; }
   virtual void        ShowStreamerInfo();
   virtual Int_t       Sizeof() const;
   void                SumBuffer(Int_t bufsize);
   virtual void        UseCache(Int_t maxCacheSize = 10, Int_t pageSize = 0);
   virtual Bool_t      WriteBuffer(const char *buf, Int_t len);
   virtual Int_t       Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0);
   virtual Int_t       Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0) const;
   virtual void        WriteFree();
   virtual void        WriteHeader();
   virtual UShort_t    WriteProcessID(TProcessID *pid);
   virtual void        WriteStreamerInfo();

   static TFileOpenHandle
                      *AsyncOpen(const char *name, Option_t *option = "",
                                 const char *ftitle = "", Int_t compress = 1,
                                 Int_t netopt = 0);
   static TFile       *Open(const char *name, Option_t *option = "",
                            const char *ftitle = "", Int_t compress = 1,
                            Int_t netopt = 0);
   static TFile       *Open(TFileOpenHandle *handle);

   static EFileType    GetType(const char *name, Option_t *option = "", TString *prefix = 0);

   static EAsyncOpenStatus GetAsyncOpenStatus(const char *name);
   static EAsyncOpenStatus GetAsyncOpenStatus(TFileOpenHandle *handle);
   static const TUrl  *GetEndpointUrl(const char *name);

   static Long64_t     GetFileBytesRead();
   static Long64_t     GetFileBytesWritten();
   static Int_t        GetFileReadCalls();

   static void         SetFileBytesRead(Long64_t bytes = 0);
   static void         SetFileBytesWritten(Long64_t bytes = 0);
   static void         SetFileReadCalls(Int_t readcalls = 0);
   static void         SetReadStreamerInfo(Bool_t readinfo=kTRUE);

   static Long64_t     GetFileCounter();
   static void         IncrementFileCounter();

   static Bool_t       SetCacheFileDir(const char *cacheDir, Bool_t operateDisconnected = kTRUE,
                                       Bool_t forceCacheread = kFALSE);
   static const char  *GetCacheFileDir();
   static Bool_t       ShrinkCacheFileDir(Long64_t shrinkSize, Long_t cleanupInteval = 0);
   static Bool_t       Cp(const char *src, const char *dst, Bool_t progressbar = kTRUE,
                          UInt_t buffersize = 1000000);

   static UInt_t       SetOpenTimeout(UInt_t timeout);  // in ms
   static UInt_t       GetOpenTimeout(); // in ms
   static Bool_t       SetOnlyStaged(Bool_t onlystaged);
   static Bool_t       GetOnlyStaged();

   ClassDef(TFile,7)  //ROOT file
};


//
// Class holding info about the file being opened
//
class TFileOpenHandle : public TNamed {

friend class TFile;
friend class TAlienFile;

private:
   TString  fOpt;        // Options
   Int_t    fCompress;   // Compression factor
   Int_t    fNetOpt;     // Network options
   TFile   *fFile;       // TFile instance of the file being opened

   TFileOpenHandle(TFile *f) : TNamed("",""), fOpt(""), fCompress(1),
                               fNetOpt(0), fFile(f) { }
   TFileOpenHandle(const char *n, const char *o, const char *t, Int_t cmp,
                   Int_t no) : TNamed(n,t), fOpt(o), fCompress(cmp),
                               fNetOpt(no), fFile(0) { }
   TFileOpenHandle(const TFileOpenHandle&);
   TFileOpenHandle& operator=(const TFileOpenHandle&);

   TFile      *GetFile() const { return fFile; }

public:
   ~TFileOpenHandle() { }

   Bool_t      Matches(const char *name);

   const char *GetOpt() const { return fOpt; }
   Int_t       GetCompress() const { return fCompress; }
   Int_t       GetNetOpt() const { return fNetOpt; }
};

R__EXTERN TFile   *gFile;

#endif
 TFile.h:1
 TFile.h:2
 TFile.h:3
 TFile.h:4
 TFile.h:5
 TFile.h:6
 TFile.h:7
 TFile.h:8
 TFile.h:9
 TFile.h:10
 TFile.h:11
 TFile.h:12
 TFile.h:13
 TFile.h:14
 TFile.h:15
 TFile.h:16
 TFile.h:17
 TFile.h:18
 TFile.h:19
 TFile.h:20
 TFile.h:21
 TFile.h:22
 TFile.h:23
 TFile.h:24
 TFile.h:25
 TFile.h:26
 TFile.h:27
 TFile.h:28
 TFile.h:29
 TFile.h:30
 TFile.h:31
 TFile.h:32
 TFile.h:33
 TFile.h:34
 TFile.h:35
 TFile.h:36
 TFile.h:37
 TFile.h:38
 TFile.h:39
 TFile.h:40
 TFile.h:41
 TFile.h:42
 TFile.h:43
 TFile.h:44
 TFile.h:45
 TFile.h:46
 TFile.h:47
 TFile.h:48
 TFile.h:49
 TFile.h:50
 TFile.h:51
 TFile.h:52
 TFile.h:53
 TFile.h:54
 TFile.h:55
 TFile.h:56
 TFile.h:57
 TFile.h:58
 TFile.h:59
 TFile.h:60
 TFile.h:61
 TFile.h:62
 TFile.h:63
 TFile.h:64
 TFile.h:65
 TFile.h:66
 TFile.h:67
 TFile.h:68
 TFile.h:69
 TFile.h:70
 TFile.h:71
 TFile.h:72
 TFile.h:73
 TFile.h:74
 TFile.h:75
 TFile.h:76
 TFile.h:77
 TFile.h:78
 TFile.h:79
 TFile.h:80
 TFile.h:81
 TFile.h:82
 TFile.h:83
 TFile.h:84
 TFile.h:85
 TFile.h:86
 TFile.h:87
 TFile.h:88
 TFile.h:89
 TFile.h:90
 TFile.h:91
 TFile.h:92
 TFile.h:93
 TFile.h:94
 TFile.h:95
 TFile.h:96
 TFile.h:97
 TFile.h:98
 TFile.h:99
 TFile.h:100
 TFile.h:101
 TFile.h:102
 TFile.h:103
 TFile.h:104
 TFile.h:105
 TFile.h:106
 TFile.h:107
 TFile.h:108
 TFile.h:109
 TFile.h:110
 TFile.h:111
 TFile.h:112
 TFile.h:113
 TFile.h:114
 TFile.h:115
 TFile.h:116
 TFile.h:117
 TFile.h:118
 TFile.h:119
 TFile.h:120
 TFile.h:121
 TFile.h:122
 TFile.h:123
 TFile.h:124
 TFile.h:125
 TFile.h:126
 TFile.h:127
 TFile.h:128
 TFile.h:129
 TFile.h:130
 TFile.h:131
 TFile.h:132
 TFile.h:133
 TFile.h:134
 TFile.h:135
 TFile.h:136
 TFile.h:137
 TFile.h:138
 TFile.h:139
 TFile.h:140
 TFile.h:141
 TFile.h:142
 TFile.h:143
 TFile.h:144
 TFile.h:145
 TFile.h:146
 TFile.h:147
 TFile.h:148
 TFile.h:149
 TFile.h:150
 TFile.h:151
 TFile.h:152
 TFile.h:153
 TFile.h:154
 TFile.h:155
 TFile.h:156
 TFile.h:157
 TFile.h:158
 TFile.h:159
 TFile.h:160
 TFile.h:161
 TFile.h:162
 TFile.h:163
 TFile.h:164
 TFile.h:165
 TFile.h:166
 TFile.h:167
 TFile.h:168
 TFile.h:169
 TFile.h:170
 TFile.h:171
 TFile.h:172
 TFile.h:173
 TFile.h:174
 TFile.h:175
 TFile.h:176
 TFile.h:177
 TFile.h:178
 TFile.h:179
 TFile.h:180
 TFile.h:181
 TFile.h:182
 TFile.h:183
 TFile.h:184
 TFile.h:185
 TFile.h:186
 TFile.h:187
 TFile.h:188
 TFile.h:189
 TFile.h:190
 TFile.h:191
 TFile.h:192
 TFile.h:193
 TFile.h:194
 TFile.h:195
 TFile.h:196
 TFile.h:197
 TFile.h:198
 TFile.h:199
 TFile.h:200
 TFile.h:201
 TFile.h:202
 TFile.h:203
 TFile.h:204
 TFile.h:205
 TFile.h:206
 TFile.h:207
 TFile.h:208
 TFile.h:209
 TFile.h:210
 TFile.h:211
 TFile.h:212
 TFile.h:213
 TFile.h:214
 TFile.h:215
 TFile.h:216
 TFile.h:217
 TFile.h:218
 TFile.h:219
 TFile.h:220
 TFile.h:221
 TFile.h:222
 TFile.h:223
 TFile.h:224
 TFile.h:225
 TFile.h:226
 TFile.h:227
 TFile.h:228
 TFile.h:229
 TFile.h:230
 TFile.h:231
 TFile.h:232
 TFile.h:233
 TFile.h:234
 TFile.h:235
 TFile.h:236
 TFile.h:237
 TFile.h:238
 TFile.h:239
 TFile.h:240
 TFile.h:241
 TFile.h:242
 TFile.h:243
 TFile.h:244
 TFile.h:245
 TFile.h:246
 TFile.h:247
 TFile.h:248
 TFile.h:249
 TFile.h:250
 TFile.h:251
 TFile.h:252
 TFile.h:253
 TFile.h:254
 TFile.h:255
 TFile.h:256
 TFile.h:257
 TFile.h:258
 TFile.h:259
 TFile.h:260
 TFile.h:261
 TFile.h:262
 TFile.h:263
 TFile.h:264
 TFile.h:265
 TFile.h:266
 TFile.h:267
 TFile.h:268
 TFile.h:269
 TFile.h:270
 TFile.h:271
 TFile.h:272
 TFile.h:273
 TFile.h:274
 TFile.h:275
 TFile.h:276
 TFile.h:277
 TFile.h:278
 TFile.h:279
 TFile.h:280
 TFile.h:281
 TFile.h:282
 TFile.h:283
 TFile.h:284
 TFile.h:285
 TFile.h:286
 TFile.h:287
 TFile.h:288
 TFile.h:289
 TFile.h:290
 TFile.h:291
 TFile.h:292
 TFile.h:293
 TFile.h:294
 TFile.h:295
 TFile.h:296
 TFile.h:297
 TFile.h:298
 TFile.h:299
 TFile.h:300
 TFile.h:301
 TFile.h:302
 TFile.h:303
 TFile.h:304
 TFile.h:305
 TFile.h:306
 TFile.h:307
 TFile.h:308
 TFile.h:309
 TFile.h:310
 TFile.h:311
 TFile.h:312
 TFile.h:313
 TFile.h:314