OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fileStat.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "fileStat.H"
00028 #include "IOstreams.H"
00029 #include "timer.H"
00030 
00031 #include <signal.h>
00032 #include <unistd.h>
00033 #include <sys/sysmacros.h>
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 Foam::fileStat::fileStat()
00038 :
00039     isValid_(false)
00040 {}
00041 
00042 
00043 Foam::fileStat::fileStat(const fileName& fName, const unsigned int maxTime)
00044 {
00045     // Work on volatile
00046     volatile bool locIsValid = false;
00047 
00048     timer myTimer(maxTime);
00049 
00050     if (!timedOut(myTimer))
00051     {
00052         if (::stat(fName.c_str(), &status_) != 0)
00053         {
00054             locIsValid = false;
00055         }
00056         else
00057         {
00058             locIsValid = true;
00059         }
00060     }
00061 
00062     // Copy into (non-volatile, possible register based) member var
00063     isValid_ = locIsValid;
00064 }
00065 
00066 
00067 Foam::fileStat::fileStat(Istream& is)
00068 {
00069     is >> *this;
00070 }
00071 
00072 
00073 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00074 
00075 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
00076 {
00077     return
00078         isValid_
00079      && (
00080             major(status_.st_dev) == major(stat2.status().st_dev)
00081          && minor(status_.st_dev) == minor(stat2.status().st_dev)
00082         );
00083 }
00084 
00085 
00086 bool Foam::fileStat::sameINode(const fileStat& stat2) const
00087 {
00088     return isValid_ && (status_.st_ino == stat2.status().st_ino);
00089 }
00090 
00091 
00092 bool Foam::fileStat::sameINode(const label iNode) const
00093 {
00094     return isValid_ && (status_.st_ino == ino_t(iNode));
00095 }
00096 
00097 
00098 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00099 
00100 Foam::Istream& Foam::operator>>(Istream& is, fileStat& fStat)
00101 {
00102     // Read beginning of machine info list
00103     is.readBegin("fileStat");
00104 
00105     label
00106         devMaj, devMin,
00107         ino, mode, uid, gid,
00108         rdevMaj, rdevMin,
00109         size, atime, mtime, ctime;
00110 
00111     is  >> fStat.isValid_
00112         >> devMaj
00113         >> devMin
00114         >> ino
00115         >> mode
00116         >> uid
00117         >> gid
00118         >> rdevMaj
00119         >> rdevMin
00120         >> size
00121         >> atime
00122         >> mtime
00123         >> ctime;
00124 
00125     dev_t st_dev = makedev(devMaj, devMin);
00126     fStat.status_.st_dev = st_dev;
00127 
00128     fStat.status_.st_ino = ino;
00129     fStat.status_.st_mode = mode;
00130     fStat.status_.st_uid = uid;
00131     fStat.status_.st_gid = gid;
00132 
00133     dev_t st_rdev = makedev(rdevMaj, rdevMin);
00134     fStat.status_.st_rdev = st_rdev;
00135 
00136     fStat.status_.st_size = size;
00137     fStat.status_.st_atime = atime;
00138     fStat.status_.st_mtime = mtime;
00139     fStat.status_.st_ctime = ctime;
00140 
00141     // Read end of machine info list
00142     is.readEnd("fileStat");
00143 
00144     // Check state of Istream
00145     is.check("Istream& operator>>(Istream&, fileStat&)");
00146 
00147     return is;
00148 }
00149 
00150 
00151 Foam::Ostream& Foam::operator<<(Ostream& os, const fileStat& fStat)
00152 {
00153     // Set precision so 32bit unsigned int can be printed
00154     // int oldPrecision = os.precision();
00155     int oldPrecision = 0;
00156     os.precision(10);
00157 
00158     os  << token::BEGIN_LIST << fStat.isValid_
00159         << token::SPACE << label(major(fStat.status_.st_dev))
00160         << token::SPACE << label(minor(fStat.status_.st_dev))
00161         << token::SPACE << label(fStat.status_.st_ino)
00162         << token::SPACE << label(fStat.status_.st_mode)
00163         << token::SPACE << label(fStat.status_.st_uid)
00164         << token::SPACE << label(fStat.status_.st_gid)
00165         << token::SPACE << label(major(fStat.status_.st_rdev))
00166         << token::SPACE << label(minor(fStat.status_.st_rdev))
00167         << token::SPACE << label(fStat.status_.st_size)
00168         << token::SPACE << label(fStat.status_.st_atime)
00169         << token::SPACE << label(fStat.status_.st_mtime)
00170         << token::SPACE << label(fStat.status_.st_ctime)
00171         << token::END_LIST;
00172 
00173     os.precision(oldPrecision);
00174     return os;
00175 }
00176 
00177 
00178 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd