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#ifndef __APPLE__
35 #include <sys/sysmacros.h>
36#endif
37
38// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39
41:
42 valid_(false)
43{}
44
45
47(
48 const char* fName,
49 const bool followLink,
50 const unsigned int maxTime
51)
52:
53 valid_(false)
54{
55 if (!fName || !fName[0])
56 {
57 return;
58 }
59
60 // Work on volatile
61 volatile bool locIsValid = false;
62
63 timer myTimer(maxTime);
64
65 if (!timedOut(myTimer))
66 {
67 if (followLink)
68 {
69 locIsValid = (::stat(fName, &status_) == 0);
70 }
71 else
72 {
73 locIsValid = (::lstat(fName, &status_) == 0);
74 }
75 }
76
77 // Copy into (non-volatile, possible register based) member var
78 valid_ = locIsValid;
79}
80
81
83(
84 const fileName& fName,
85 const bool followLink,
86 const unsigned int maxTime
87)
88:
89 fileStat(fName.c_str(), followLink, maxTime)
90{}
91
92
93Foam::fileStat::fileStat(Istream& is)
94{
95 is >> *this;
96}
97
98
99// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100
101Foam::label Foam::fileStat::size() const
102{
103 return valid_ ? label(status_.st_size) : 0;
104}
105
106
107time_t Foam::fileStat::modTime() const
108{
109 return valid_ ? status_.st_mtime : 0;
110}
111
112
113double Foam::fileStat::dmodTime() const
114{
115 return
116 (
117 valid_
118 ?
119 #ifdef __APPLE__
120 (status_.st_mtime + 1e-9*status_.st_mtimespec.tv_nsec)
121 #else
122 (status_.st_mtime + 1e-9*status_.st_mtim.tv_nsec)
123 #endif
124 : 0
125 );
126}
127
128
129bool Foam::fileStat::sameDevice(const fileStat& other) const
130{
131 return
132 valid_
133 && (
134 major(status_.st_dev) == major(other.status_.st_dev)
135 && minor(status_.st_dev) == minor(other.status_.st_dev)
136 );
137}
138
139
140bool Foam::fileStat::sameINode(const fileStat& other) const
141{
142 return valid_ && (status_.st_ino == other.status_.st_ino);
143}
144
145
146bool Foam::fileStat::sameINode(const label iNode) const
147{
148 return valid_ && (status_.st_ino == ino_t(iNode));
149}
150
151
152// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
153
154Foam::Istream& Foam::operator>>(Istream& is, fileStat& fs)
155{
156 FixedList<label, 13> list(is);
157
158 fs.valid_ = list[0];
159
160 dev_t st_dev = makedev(list[1], list[2]);
161 fs.status_.st_dev = st_dev;
162
163 fs.status_.st_ino = list[3];
164 fs.status_.st_mode = list[4];
165 fs.status_.st_uid = list[5];
166 fs.status_.st_gid = list[6];
167
168 dev_t st_rdev = makedev(list[7], list[8]);
169 fs.status_.st_rdev = st_rdev;
170
171 fs.status_.st_size = list[9];
172 fs.status_.st_atime = list[10];
173 fs.status_.st_mtime = list[11];
174 fs.status_.st_ctime = list[12];
175
176 is.check(FUNCTION_NAME);
177 return is;
178}
179
180
181Foam::Ostream& Foam::operator<<(Ostream& os, const fileStat& fs)
182{
183 FixedList<label, 13> list;
184
185 list[0] = label(fs.valid_);
186 list[1] = label(major(fs.status_.st_dev));
187 list[2] = label(minor(fs.status_.st_dev));
188 list[3] = label(fs.status_.st_ino);
189 list[4] = label(fs.status_.st_mode);
190 list[5] = label(fs.status_.st_uid);
191 list[6] = label(fs.status_.st_gid);
192 list[7] = label(major(fs.status_.st_rdev));
193 list[8] = label(minor(fs.status_.st_rdev));
194 list[9] = label(fs.status_.st_size);
195 list[10] = label(fs.status_.st_atime);
196 list[11] = label(fs.status_.st_mtime);
197 list[12] = label(fs.status_.st_ctime);
198
199 return os << list;
200}
201
202
203// ************************************************************************* //
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
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
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
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