FreeFOAM The Cross-Platform CFD Toolkit
fileStat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "fileStat.H"
27 #include <OpenFOAM/IOstreams.H>
28 #include "timer.H"
29 
30 #include <signal.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
37 :
38  isValid_(false)
39 {}
40 
41 
42 Foam::fileStat::fileStat(const fileName& fName, const unsigned int maxTime)
43 {
44  // Work on volatile
45  volatile bool locIsValid = false;
46 
47  timer myTimer(maxTime);
48 
49  if (!timedOut(myTimer))
50  {
51  if (::stat(fName.c_str(), &status_) != 0)
52  {
53  locIsValid = false;
54  }
55  else
56  {
57  locIsValid = true;
58  }
59  }
60 
61  // Copy into (non-volatile, possible register based) member var
62  isValid_ = locIsValid;
63 }
64 
65 
67 {
68  is >> *this;
69 }
70 
71 
72 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
73 
74 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
75 {
76  return
77  isValid_
78  && (
79  major(status_.st_dev) == major(stat2.status().st_dev)
80  && minor(status_.st_dev) == minor(stat2.status().st_dev)
81  );
82 }
83 
84 
85 bool Foam::fileStat::sameINode(const fileStat& stat2) const
86 {
87  return isValid_ && (status_.st_ino == stat2.status().st_ino);
88 }
89 
90 
91 bool Foam::fileStat::sameINode(const label iNode) const
92 {
93  return isValid_ && (status_.st_ino == ino_t(iNode));
94 }
95 
96 
97 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
98 
100 {
101  // Read beginning of machine info list
102  is.readBegin("fileStat");
103 
104  label
105  devMaj, devMin,
106  ino, mode, uid, gid,
107  rdevMaj, rdevMin,
108  size, atime, mtime, ctime;
109 
110  is >> fStat.isValid_
111  >> devMaj
112  >> devMin
113  >> ino
114  >> mode
115  >> uid
116  >> gid
117  >> rdevMaj
118  >> rdevMin
119  >> size
120  >> atime
121  >> mtime
122  >> ctime;
123 
124  dev_t st_dev = makedev(devMaj, devMin);
125  fStat.status_.st_dev = st_dev;
126 
127  fStat.status_.st_ino = ino;
128  fStat.status_.st_mode = mode;
129  fStat.status_.st_uid = uid;
130  fStat.status_.st_gid = gid;
131 
132  dev_t st_rdev = makedev(rdevMaj, rdevMin);
133  fStat.status_.st_rdev = st_rdev;
134 
135  fStat.status_.st_size = size;
136  fStat.status_.st_atime = atime;
137  fStat.status_.st_mtime = mtime;
138  fStat.status_.st_ctime = ctime;
139 
140  // Read end of machine info list
141  is.readEnd("fileStat");
142 
143  // Check state of Istream
144  is.check("Istream& operator>>(Istream&, fileStat&)");
145 
146  return is;
147 }
148 
149 
151 {
152  // Set precision so 32bit unsigned int can be printed
153  // int oldPrecision = os.precision();
154  int oldPrecision = 0;
155  os.precision(10);
156 
157  os << token::BEGIN_LIST << fStat.isValid_
158  << token::SPACE << label(major(fStat.status_.st_dev))
159  << token::SPACE << label(minor(fStat.status_.st_dev))
160  << token::SPACE << label(fStat.status_.st_ino)
161  << token::SPACE << label(fStat.status_.st_mode)
162  << token::SPACE << label(fStat.status_.st_uid)
163  << token::SPACE << label(fStat.status_.st_gid)
164  << token::SPACE << label(major(fStat.status_.st_rdev))
165  << token::SPACE << label(minor(fStat.status_.st_rdev))
166  << token::SPACE << label(fStat.status_.st_size)
167  << token::SPACE << label(fStat.status_.st_atime)
168  << token::SPACE << label(fStat.status_.st_mtime)
169  << token::SPACE << label(fStat.status_.st_ctime)
170  << token::END_LIST;
171 
172  os.precision(oldPrecision);
173  return os;
174 }
175 
176 
177 // ************************ vim: set sw=4 sts=4 et: ************************ //