PerFile Class Reference

#include <PerFile.h>

List of all members.

Public Member Functions

 PerFile (string fullfilepathname, Per::EAccessMode accessmode)
virtual ~PerFile ()
string GetFullFilePathName () const
UInt_t GetNumClient () const
Per::EAccessMode GetAccessMode () const
TFile * GetTFile () const
Per::EErrorCode GetErrorCode () const
bool HasFileEndKey () const
bool IsRemote () const
bool IsOpen () const
std::ostream & Print (std::ostream &s) const
bool AddNewClient (Per::EAccessMode accessmode)
UInt_t Close (bool kForce=false)
void ChangeFile (std::string fullfilepathname, TFile *fileptr)

Private Attributes

string fFullFilePathName
Per::EAccessMode fAccessMode
TFile * fTFile
UInt_t fNumClient
bool fRemote
Per::EErrorCode fErrorCode

Detailed Description

Definition at line 24 of file PerFile.h.


Constructor & Destructor Documentation

PerFile::PerFile ( string  fullfilepathname,
Per::EAccessMode  accessmode 
)

Definition at line 149 of file PerFile.cxx.

References bfld::AsString(), fErrorCode, fNumClient, fRemote, fTFile, GetErrorCode(), gSystem(), HasFileEndKey(), Per::kErrFileError, Per::kErrFileExists, Per::kErrFileNoSpace, Per::kErrFileNotFound, Msg::kInfo, Per::kNew, Per::kRead, and MSG.

00149                                                                   :
00150   fFullFilePathName(fullfilepathname),fAccessMode(accessmode),fTFile(0),
00151   fNumClient(0),fRemote(false),fErrorCode(Per::kErrSuccess) {
00152   //
00153   //  Purpose:  PerFile constructor.  Opens ROOT TFile with requested 
00154   //            filename and accessmode.  Uses the static TFile::Open
00155   //            method to open both local and remote files transparently.
00156   //            User should use PerFile::IsOpen() to check to see if
00157   //            file was opened successfully.
00158   //
00159   //  Arguments: 
00160   //    fullfilepathname:  full file path name to be opened.  If file is
00161   //                       remote, must follow protocol as defined in
00162   //                       the TFile::Open method.  Specifically for remote
00163   //                       files served by the rootd server daemon, the
00164   //                       file name is prefaced by "root:" or "roots:" 
00165   //                       and the node name as in:
00166   //                       "root://urheim.hep.umn.edu/~minos/filename.dat"
00167   //                       See the TNetFile class description or  
00168   //                       http://root.cern.ch/root/NetFile.html for more 
00169   //                       information on the remote file name format.
00170   //    accessmode: enumerated access mode of type EAccessMode (defined in
00171   //                Per.h) in which file is to be opened:  
00172   //                kRead = open file in read only mode.
00173   //                kNew = open new file in read/write. If file exists 
00174   //                       previously, do not overwrite.
00175   //                kRecreate = same as kNew, but if file exists previously
00176   //                            overwrite.
00177   //                kUpdate = open existing file in read/write mode.
00178   //
00179   //  Return: n/a.
00180   //
00181   //  Notes: Authorization for remote access follows the protocol
00182   //         discussed in http://root.cern.ch/root/NetFile.html with
00183   //         useage dependent on how the rootd server was configured on
00184   //         the remote site.  
00185   //         One approach, when not using secure authentication, is
00186   //         to store the username&password required for remote access in
00187   //         a .netrc file located in the user's home directory.  The format 
00188   //         for a line in the .netrc file is:
00189   //         machine <machine name> login <username> password <password>
00190   //         e.g.
00191   //         machine urheim.hep.umn.edu login myusername password mypassword
00192   //                     
00193   //         If the constructor fails to open the file successfully, 
00194   //         PerFile::GetErrorCode can be used to determine the reason
00195   //         for the failure.
00196   // 
00197   //  Contact:   S. Kasahara
00198   // 
00199 
00200   gSystem -> ResetErrno();
00201   
00202   // Open File assuming default compression mode.  May want to specify
00203   // eventually both the compression mode and a job related title.
00204   // Determine if file is remote
00205   if ( fullfilepathname.find("root:") == 0 
00206     || fullfilepathname.find("roots:") == 0 ) {
00207     fRemote = true;
00208     // Assumes that user has correctly set up authorization according to
00209     // instructions in http://root.cern.ch/root/NetFile.html
00210     // Use of TNetFile is enforced instead of relying on TFile::Open, because
00211     // TFile::Open defaults to TFile even if the user has specified "root:"
00212     // if it determines that access is local and parses the file string to
00213     // determine the local file path.  This does not result in 
00214     // a correct file path if rootd has been set up for anonymous useage.
00215     // Update: 9/25/2005, SK/GMI  The TEnv configuration variable
00216     // "TFile.ForceRemote" is now set true in the PerFileManager, so
00217     // that TFile::Open can be used in all cases. The old line:
00218     // fTFile =new TNetFile(fullfilepathname.c_str(),Per::AsString(accessmode),
00219     //                      "Persistency file");
00220     // has been replaced by:
00221     fTFile = TFile::Open(fullfilepathname.c_str(),Per::AsString(accessmode),
00222                          "Persistency file");
00223   }
00224   else {
00225     fTFile = TFile::Open(fullfilepathname.c_str(),Per::AsString(accessmode),
00226                          "Persistency file");
00227   }
00228 
00229 
00230   if (fTFile == (TFile*)0 || fTFile -> IsZombie() || !fTFile ->IsOpen()) {
00231     // File did not open successfully
00232     MSG("Per", Msg::kInfo) << "Unable to open " << fullfilepathname 
00233         << " with accessmode " << Per::AsString(accessmode) << endl;
00234     fErrorCode = Per::kErrFileError;
00235     if ( fTFile && fRemote ) {
00236       TNetFile* netfile = dynamic_cast<TNetFile*>(fTFile);
00237       if (netfile) {
00238         switch ( netfile -> GetErrorCode() ) {
00239         case kErrFileExists:
00240           fErrorCode = Per::kErrFileExists; break;
00241         case kErrNoFile:
00242           fErrorCode = Per::kErrFileNotFound; break;
00243         case kErrNoSpace:
00244           fErrorCode = Per::kErrFileNoSpace; break;
00245         default:
00246           break;
00247         }
00248       }
00249     }
00250     else if (fTFile) {
00251       // Local file, determine why file failed to open through system calls
00252       if (accessmode == Per::kNew &&
00253           !(gSystem -> AccessPathName(fullfilepathname.c_str(),kFileExists))) {
00254         // New file failed to open due to existing file of same name
00255         fErrorCode = Per::kErrFileExists;
00256       }
00257       else if (accessmode == Per::kRead &&
00258          (gSystem -> AccessPathName(fullfilepathname.c_str(),kFileExists))) {
00259         // File failed to open because it does not exist
00260         fErrorCode = Per::kErrFileNotFound;
00261       }
00262       else {
00263         switch (fTFile -> GetErrno()) {
00264           // TFile::TFile runs its own error checking so all file open errors 
00265           // won't appear in errno
00266         case ENOSPC:
00267           fErrorCode = Per::kErrFileNoSpace; break;
00268         default:
00269           break;
00270         }
00271       }
00272     }
00273     if(fTFile) delete fTFile; fTFile = 0;
00274   }
00275   else {
00276     fNumClient++;
00277     // fRemote is used to describe rootd served files only
00278     // rootd files that are not closed will not UseCache because
00279     // of dispatcher service concerns
00280     if ( gROOT -> GetVersionInt() <= 53000 ) {
00281       if ( !fRemote || HasFileEndKey() ) fTFile->UseCache();
00282     }
00283   }
00284   
00285 }

PerFile::~PerFile (  )  [virtual]

Definition at line 287 of file PerFile.cxx.

References Close(), fNumClient, fTFile, and IsOpen().

00287                   {
00288   //
00289   //  Purpose:  PerFile destructor.  Closes ROOT TFile and retrieves
00290   //            allocated memory.
00291   //
00292   //  Contact:   S. Kasahara
00293   // 
00294 
00295   if (IsOpen()) {
00296     fTFile -> Close();
00297     delete fTFile; fTFile = 0;
00298   }
00299   fNumClient = 0;
00300 
00301 }


Member Function Documentation

bool PerFile::AddNewClient ( Per::EAccessMode  accessmode  ) 

Definition at line 45 of file PerFile.cxx.

References Per::kNew, and Per::kRecreate.

00045                                                     {
00046   //
00047   //  Purpose:  Add new client to the previously opened file if new requested 
00048   //            accessmode is compatible with the accessmode in which the file
00049   //            was originally opened. If successful fNumClient is incremented.
00050   //
00051   //  Arguments: 
00052   //    accessmode: enumerated file access mode of type EAccessMode (defined
00053   //                in Per.h).
00054   //
00055   //  Return:    true means client was successfully added to file.
00056   //             false means new accessmode was incompatible with original
00057   //             accessmode.
00058   //
00059   //  Contact:   S. Kasahara
00060   // 
00061   //  Notes: access modes kNew & kRecreate are considered compatible, 
00062   //         all other mode combinations are considered incompatible.
00063 
00064   bool accessok = false;
00065 
00066   if(fAccessMode == accessmode) {
00067     accessok = true;
00068   }
00069   else {
00070     if((fAccessMode == Per::kNew || fAccessMode == Per::kRecreate) &&
00071        (accessmode == Per::kNew || accessmode == Per::kRecreate)) {
00072         accessok = true;
00073     }
00074   }
00075 
00076   if(accessok) fNumClient++;
00077   return accessok;  
00078 }

void PerFile::ChangeFile ( std::string  fullfilepathname,
TFile *  fileptr 
) [inline]

Definition at line 48 of file PerFile.h.

References fFullFilePathName, and fTFile.

00049     { fFullFilePathName = fullfilepathname; fTFile = fileptr; }

UInt_t PerFile::Close ( bool  kForce = false  ) 

Definition at line 80 of file PerFile.cxx.

References cd(), fNumClient, fTFile, and IsOpen().

Referenced by ~PerFile().

00080                                  {
00081   //
00082   //  Purpose:  Decrement the number of clients to open file, and close
00083   //            file if number of clients reaches zero.  Argument kForce set
00084   //            true forces closure of file regardless of number of clients
00085   //            (default is kForce==false).
00086   //
00087   //  Arguments: 
00088   //    kForce:  bool to force file closure regardless of number of clients.
00089   //             (by default kForce is set false).
00090   //
00091   //  Return:  number of clients of file remaining after method completes.
00092   //
00093   //  Contact:   S. Kasahara
00094   // 
00095 
00096   if (!kForce) {
00097     if(fNumClient > 0) fNumClient--; // decrement number of clients by one
00098   }
00099   else {
00100     fNumClient=0; // user requested forced file closure.
00101   }
00102 
00103   if( !fNumClient ) {
00104     // No clients left, close file
00105     if (IsOpen()) {
00106       if (fTFile -> IsWritable()) {
00107         // Write out Key with name "FileEnd" as final object to file.  
00108         // This marker is used by a reader of an open file to determine 
00109         // when a file has actually been closed by the writer.
00110         TDirectory *savedir = gDirectory;
00111         fTFile -> cd();  // change to file directory
00112         TNamed *isend = new TNamed("FileEnd","PerFile End Key");
00113         isend -> Write(NULL,TObject::kOverwrite);
00114         delete isend;
00115         savedir -> cd(); // change back to users directory
00116       }
00117       fTFile -> Close();
00118       delete fTFile;
00119       fTFile = 0;
00120     }
00121   }
00122 
00123   return fNumClient;
00124 }

Per::EAccessMode PerFile::GetAccessMode (  )  const [inline]

Definition at line 35 of file PerFile.h.

References fAccessMode.

00035 {return fAccessMode;}

Per::EErrorCode PerFile::GetErrorCode (  )  const [inline]

Definition at line 37 of file PerFile.h.

References fErrorCode.

Referenced by PerFile().

00037 {return fErrorCode;}

string PerFile::GetFullFilePathName (  )  const [inline]

Definition at line 33 of file PerFile.h.

References fFullFilePathName.

00033 {return fFullFilePathName;}

UInt_t PerFile::GetNumClient (  )  const [inline]

Definition at line 34 of file PerFile.h.

References fNumClient.

00034 {return fNumClient;}

TFile* PerFile::GetTFile (  )  const [inline]

Definition at line 36 of file PerFile.h.

References fTFile.

00036 {return fTFile;}

bool PerFile::HasFileEndKey (  )  const

Definition at line 126 of file PerFile.cxx.

References fTFile, and IsOpen().

Referenced by PerFile().

00126                                   {
00127   //
00128   //  Purpose:  Determine if file has been closed by the writer.  This
00129   //            is determined by the presence or absence of a "FileEnd"
00130   //            Key.
00131   //
00132   //  Arguments: none.
00133   //
00134   //  Return:  true if "FileEnd" Key is present, else false.
00135   //
00136   //  Contact:   S. Kasahara
00137   // 
00138   //  Notes:  This method is used by a reader of an open file to determine 
00139   //          when that file has actually been closed by the writer.  
00140   //
00141 
00142   if ( IsOpen() && (fTFile -> Get("FileEnd")) )
00143     return true;
00144   else 
00145     return false;
00146 
00147 }

bool PerFile::IsOpen (  )  const [inline]

Definition at line 40 of file PerFile.h.

References fTFile.

Referenced by Close(), HasFileEndKey(), Print(), and ~PerFile().

00040 {return (fTFile != (TFile*)0) ? true : false;}

bool PerFile::IsRemote (  )  const [inline]

Definition at line 39 of file PerFile.h.

References fRemote.

Referenced by Print().

00039 {return fRemote;}

std::ostream & PerFile::Print ( std::ostream &  s  )  const

Definition at line 303 of file PerFile.cxx.

References bfld::AsString(), fAccessMode, fFullFilePathName, fNumClient, IsOpen(), and IsRemote().

Referenced by operator<<().

00303                                                {
00304   //
00305   //  Purpose:  Print status of file on ostream.
00306   //
00307   //  Arguments: ms ostream to display on.
00308   //
00309   //  Return:  ostream reference.
00310   //
00311   //  Contact:   S. Kasahara
00312   // 
00313 
00314 
00315   ms << (IsRemote() ? "Remote" : "Local") << " File " << fFullFilePathName;
00316  
00317   if (IsOpen()) {
00318     ms << " is open with accessmode " << Per::AsString(fAccessMode) 
00319        << " and " << fNumClient << " client(s)."<< endl;
00320   }
00321   else {
00322     ms << " was not opened successfully with accessmode " 
00323        << Per::AsString(fAccessMode) << "."<<endl;
00324   }
00325 
00326   return ms;
00327 
00328 }


Member Data Documentation

Definition at line 55 of file PerFile.h.

Referenced by GetAccessMode(), and Print().

Definition at line 59 of file PerFile.h.

Referenced by GetErrorCode(), and PerFile().

string PerFile::fFullFilePathName [private]

Definition at line 54 of file PerFile.h.

Referenced by ChangeFile(), GetFullFilePathName(), and Print().

UInt_t PerFile::fNumClient [private]

Definition at line 57 of file PerFile.h.

Referenced by Close(), GetNumClient(), PerFile(), Print(), and ~PerFile().

bool PerFile::fRemote [private]

Definition at line 58 of file PerFile.h.

Referenced by IsRemote(), and PerFile().

TFile* PerFile::fTFile [private]

Definition at line 56 of file PerFile.h.

Referenced by ChangeFile(), Close(), GetTFile(), HasFileEndKey(), IsOpen(), PerFile(), and ~PerFile().


The documentation for this class was generated from the following files:

Generated on 2 Nov 2017 for loon by  doxygen 1.6.1