initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OSspecific / POSIX / fileStat.C
blobc71274d2f7f269ba3b5b56fbb4862ba87e539cac
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "fileStat.H"
28 #include "IOstreams.H"
29 #include "timer.H"
31 #include <signal.h>
32 #include <unistd.h>
33 #include <sys/sysmacros.h>
35 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
37 Foam::fileStat::fileStat()
39     isValid_(false)
43 Foam::fileStat::fileStat(const fileName& fName, const unsigned int maxTime)
45     // Work on volatile
46     volatile bool locIsValid = false;
48     timer myTimer(maxTime);
50     if (!timedOut(myTimer))
51     {
52         if (::stat(fName.c_str(), &status_) != 0)
53         {
54             locIsValid = false;
55         }
56         else
57         {
58             locIsValid = true;
59         }
60     }
62     // Copy into (non-volatile, possible register based) member var
63     isValid_ = locIsValid;
67 Foam::fileStat::fileStat(Istream& is)
69     is >> *this;
73 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
75 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
77     return
78         isValid_
79      && (
80             major(status_.st_dev) == major(stat2.status().st_dev)
81          && minor(status_.st_dev) == minor(stat2.status().st_dev)
82         );
86 bool Foam::fileStat::sameINode(const fileStat& stat2) const
88     return isValid_ && (status_.st_ino == stat2.status().st_ino);
92 bool Foam::fileStat::sameINode(const label iNode) const
94     return isValid_ && (status_.st_ino == ino_t(iNode));
98 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
100 Foam::Istream& Foam::operator>>(Istream& is, fileStat& fStat)
102     // Read beginning of machine info list
103     is.readBegin("fileStat");
105     label
106         devMaj, devMin,
107         ino, mode, uid, gid,
108         rdevMaj, rdevMin,
109         size, atime, mtime, ctime;
111     is  >> fStat.isValid_
112         >> devMaj
113         >> devMin
114         >> ino
115         >> mode
116         >> uid
117         >> gid
118         >> rdevMaj
119         >> rdevMin
120         >> size
121         >> atime
122         >> mtime
123         >> ctime;
125     dev_t st_dev = makedev(devMaj, devMin);
126     fStat.status_.st_dev = st_dev;
128     fStat.status_.st_ino = ino;
129     fStat.status_.st_mode = mode;
130     fStat.status_.st_uid = uid;
131     fStat.status_.st_gid = gid;
133     dev_t st_rdev = makedev(rdevMaj, rdevMin);
134     fStat.status_.st_rdev = st_rdev;
136     fStat.status_.st_size = size;
137     fStat.status_.st_atime = atime;
138     fStat.status_.st_mtime = mtime;
139     fStat.status_.st_ctime = ctime;
141     // Read end of machine info list
142     is.readEnd("fileStat");
144     // Check state of Istream
145     is.check("Istream& operator>>(Istream&, fileStat&)");
147     return is;
151 Foam::Ostream& Foam::operator<<(Ostream& os, const fileStat& fStat)
153     // Set precision so 32bit unsigned int can be printed
154     // int oldPrecision = os.precision();
155     int oldPrecision = 0;
156     os.precision(10);
158     os  << token::BEGIN_LIST << fStat.isValid_
159         << token::SPACE << label(major(fStat.status_.st_dev))
160         << token::SPACE << label(minor(fStat.status_.st_dev))
161         << token::SPACE << label(fStat.status_.st_ino)
162         << token::SPACE << label(fStat.status_.st_mode)
163         << token::SPACE << label(fStat.status_.st_uid)
164         << token::SPACE << label(fStat.status_.st_gid)
165         << token::SPACE << label(major(fStat.status_.st_rdev))
166         << token::SPACE << label(minor(fStat.status_.st_rdev))
167         << token::SPACE << label(fStat.status_.st_size)
168         << token::SPACE << label(fStat.status_.st_atime)
169         << token::SPACE << label(fStat.status_.st_mtime)
170         << token::SPACE << label(fStat.status_.st_ctime)
171         << token::END_LIST;
173     os.precision(oldPrecision);
174     return os;
178 // ************************************************************************* //