FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "fileStat.H"
00027 #include <OpenFOAM/IOstreams.H>
00028 #include "timer.H"
00029 
00030 #include <signal.h>
00031 #include <unistd.h>
00032 #include <sys/types.h>
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 Foam::fileStat::fileStat()
00037 :
00038     isValid_(false)
00039 {}
00040 
00041 
00042 Foam::fileStat::fileStat(const fileName& fName, const unsigned int maxTime)
00043 {
00044     // Work on volatile
00045     volatile bool locIsValid = false;
00046 
00047     timer myTimer(maxTime);
00048 
00049     if (!timedOut(myTimer))
00050     {
00051         if (::stat(fName.c_str(), &status_) != 0)
00052         {
00053             locIsValid = false;
00054         }
00055         else
00056         {
00057             locIsValid = true;
00058         }
00059     }
00060 
00061     // Copy into (non-volatile, possible register based) member var
00062     isValid_ = locIsValid;
00063 }
00064 
00065 
00066 Foam::fileStat::fileStat(Istream& is)
00067 {
00068     is >> *this;
00069 }
00070 
00071 
00072 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00073 
00074 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
00075 {
00076     return
00077         isValid_
00078      && (
00079             major(status_.st_dev) == major(stat2.status().st_dev)
00080          && minor(status_.st_dev) == minor(stat2.status().st_dev)
00081         );
00082 }
00083 
00084 
00085 bool Foam::fileStat::sameINode(const fileStat& stat2) const
00086 {
00087     return isValid_ && (status_.st_ino == stat2.status().st_ino);
00088 }
00089 
00090 
00091 bool Foam::fileStat::sameINode(const label iNode) const
00092 {
00093     return isValid_ && (status_.st_ino == ino_t(iNode));
00094 }
00095 
00096 
00097 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00098 
00099 Foam::Istream& Foam::operator>>(Istream& is, fileStat& fStat)
00100 {
00101     // Read beginning of machine info list
00102     is.readBegin("fileStat");
00103 
00104     label
00105         devMaj, devMin,
00106         ino, mode, uid, gid,
00107         rdevMaj, rdevMin,
00108         size, atime, mtime, ctime;
00109 
00110     is  >> fStat.isValid_
00111         >> devMaj
00112         >> devMin
00113         >> ino
00114         >> mode
00115         >> uid
00116         >> gid
00117         >> rdevMaj
00118         >> rdevMin
00119         >> size
00120         >> atime
00121         >> mtime
00122         >> ctime;
00123 
00124     dev_t st_dev = makedev(devMaj, devMin);
00125     fStat.status_.st_dev = st_dev;
00126 
00127     fStat.status_.st_ino = ino;
00128     fStat.status_.st_mode = mode;
00129     fStat.status_.st_uid = uid;
00130     fStat.status_.st_gid = gid;
00131 
00132     dev_t st_rdev = makedev(rdevMaj, rdevMin);
00133     fStat.status_.st_rdev = st_rdev;
00134 
00135     fStat.status_.st_size = size;
00136     fStat.status_.st_atime = atime;
00137     fStat.status_.st_mtime = mtime;
00138     fStat.status_.st_ctime = ctime;
00139 
00140     // Read end of machine info list
00141     is.readEnd("fileStat");
00142 
00143     // Check state of Istream
00144     is.check("Istream& operator>>(Istream&, fileStat&)");
00145 
00146     return is;
00147 }
00148 
00149 
00150 Foam::Ostream& Foam::operator<<(Ostream& os, const fileStat& fStat)
00151 {
00152     // Set precision so 32bit unsigned int can be printed
00153     // int oldPrecision = os.precision();
00154     int oldPrecision = 0;
00155     os.precision(10);
00156 
00157     os  << token::BEGIN_LIST << fStat.isValid_
00158         << token::SPACE << label(major(fStat.status_.st_dev))
00159         << token::SPACE << label(minor(fStat.status_.st_dev))
00160         << token::SPACE << label(fStat.status_.st_ino)
00161         << token::SPACE << label(fStat.status_.st_mode)
00162         << token::SPACE << label(fStat.status_.st_uid)
00163         << token::SPACE << label(fStat.status_.st_gid)
00164         << token::SPACE << label(major(fStat.status_.st_rdev))
00165         << token::SPACE << label(minor(fStat.status_.st_rdev))
00166         << token::SPACE << label(fStat.status_.st_size)
00167         << token::SPACE << label(fStat.status_.st_atime)
00168         << token::SPACE << label(fStat.status_.st_mtime)
00169         << token::SPACE << label(fStat.status_.st_ctime)
00170         << token::END_LIST;
00171 
00172     os.precision(oldPrecision);
00173     return os;
00174 }
00175 
00176 
00177 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines