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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2016-2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "fileStat.H"
30#include "IOstreams.H"
31#include "timer.H"
32
33#include <unistd.h>
34
35#undef major
36#undef minor
37#undef makedev
38
39#define major(dev) int(((dev) >> 8) & 0xff)
40#define minor(dev) int((dev) & 0xff)
41#define makedev(majNum, minNum) (((unsigned(majNum)) << 8) | (unsigned(minNum)))
42
43
44// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
45
47:
48 valid_(false)
49{}
50
51
53(
54 const char* fName,
55 const bool followLink,
56 const unsigned int maxTime
57)
58:
59 valid_(false)
60{
61 if (!fName || !fName[0])
62 {
63 return;
64 }
65
66 // Work on volatile
67 volatile bool locIsValid = false;
68
69 timer myTimer(maxTime);
70
71 if (!timedOut(myTimer))
72 {
73 #ifdef _WIN32
74 locIsValid = (::stat(fName, &status_) == 0);
75 #else
76 if (followLink)
77 {
78 locIsValid = (::stat(fName, &status_) == 0);
79 }
80 else
81 {
82 locIsValid = (::lstat(fName, &status_) == 0);
83 }
84 #endif
85 }
86
87 // Copy into (non-volatile, possible register based) member var
88 valid_ = locIsValid;
89}
90
91
93(
94 const fileName& fName,
95 const bool followLink,
96 const unsigned int maxTime
97)
98:
99 fileStat(fName.c_str(), followLink, maxTime)
100{}
101
102
104{
105 is >> *this;
106}
107
108
109// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
110
111Foam::label Foam::fileStat::size() const
112{
113 return valid_ ? label(status_.st_size) : 0;
114}
115
116
118{
119 return valid_ ? status_.st_mtime : 0;
120}
121
122
124{
125 return
126 (
127 valid_
128 ?
129 #ifdef __APPLE__
130 (status_.st_mtime + 1e-9*status_.st_mtimespec.tv_nsec)
131 #elif defined (_WIN32)
132 (status_.st_mtime)
133 #else
134 (status_.st_mtime + 1e-9*status_.st_mtim.tv_nsec)
135 #endif
136 : 0
137 );
138}
139
140
141bool Foam::fileStat::sameDevice(const fileStat& other) const
142{
143 return
144 valid_
145 && (
146 major(status_.st_dev) == major(other.status_.st_dev)
147 && minor(status_.st_dev) == minor(other.status_.st_dev)
148 );
149}
150
151
152bool Foam::fileStat::sameINode(const fileStat& other) const
153{
154 return valid_ && (status_.st_ino == other.status_.st_ino);
155}
156
157
158bool Foam::fileStat::sameINode(const label iNode) const
159{
160 return valid_ && (status_.st_ino == ino_t(iNode));
161}
162
163
164// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
165
167{
168 FixedList<label, 13> list(is);
169
170 fs.valid_ = list[0];
171
172 dev_t st_dev = makedev(list[1], list[2]);
173 fs.status_.st_dev = st_dev;
174
175 fs.status_.st_ino = list[3];
176 fs.status_.st_mode = list[4];
177 fs.status_.st_uid = list[5];
178 fs.status_.st_gid = list[6];
179
180 dev_t st_rdev = makedev(list[7], list[8]);
181 fs.status_.st_rdev = st_rdev;
182
183 fs.status_.st_size = list[9];
184 fs.status_.st_atime = list[10];
185 fs.status_.st_mtime = list[11];
186 fs.status_.st_ctime = list[12];
187
189 return is;
190}
191
192
194{
196
197 list[0] = label(fs.valid_);
198 list[1] = label(major(fs.status_.st_dev));
199 list[2] = label(minor(fs.status_.st_dev));
200 list[3] = label(fs.status_.st_ino);
201 list[4] = label(fs.status_.st_mode);
202 list[5] = label(fs.status_.st_uid);
203 list[6] = label(fs.status_.st_gid);
204 list[7] = label(major(fs.status_.st_rdev));
205 list[8] = label(minor(fs.status_.st_rdev));
206 list[9] = label(fs.status_.st_size);
207 list[10] = label(fs.status_.st_atime);
208 list[11] = label(fs.status_.st_mtime);
209 list[12] = label(fs.status_.st_ctime);
210
211 return os << list;
212}
213
214
215// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
#define minor(dev)
Definition: fileStat.C:40
#define major(dev)
Definition: fileStat.C:39
#define makedev(majNum, minNum)
Definition: fileStat.C:41
#define timedOut(x)
Check if timeout has occurred.
Definition: timer.H:73
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A class for handling file names.
Definition: fileName.H:76
Wrapper for stat() and lstat() system calls.
Definition: fileStat.H:68
time_t modTime() const
Return the modification time in seconds.
Definition: fileStat.C:117
label size() const
Size in bytes. Zero for an invalid file-stat.
Definition: fileStat.C:111
double dmodTime() const
Return the modification time in seconds (nanosecond resolution)
Definition: fileStat.C:123
fileStat()
Empty constructor.
Definition: fileStat.C:46
bool sameINode(const fileStat &other) const
Compare two fileStats for same Inode.
Definition: fileStat.C:152
bool sameDevice(const fileStat &other) const
Compare two fileStats for same device.
Definition: fileStat.C:141
Implements a timeout mechanism via sigalarm.
Definition: timer.H:84
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
volScalarField & e
Definition: createFields.H:11