fileStat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
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 "IOstreams.H"
28 #include "timer.H"
29 
30 #include <signal.h>
31 #include <unistd.h>
32 #include <sys/sysmacros.h>
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
37 
38 const char* Foam::fileStat::variantExts_[] = {"gz", "orig"};
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
44 :
45  isValid_(false)
46 {}
47 
48 
50 (
51  const fileName& fName,
52  const bool checkVariants,
53  const bool followLink,
54  const unsigned int maxTime
55 )
56 {
57  // Work on volatile
58  volatile bool locIsValid = false;
59 
60  timer myTimer(maxTime);
61 
62  if (!timedOut(myTimer))
63  {
64  int (*getFileStatus)(const char *, struct stat *) =
65  followLink ? ::stat : ::lstat;
66 
67  if (getFileStatus(fName.c_str(), &status_) == 0)
68  {
69  locIsValid = true;
70  }
71  else if (checkVariants)
72  {
73  for (label i = 0; !locIsValid && i < nVariants_; ++ i)
74  {
75  const fileName fNameVar = fName + "." + variantExts_[i];
76  if (getFileStatus(fNameVar.c_str(), &status_) == 0)
77  {
78  locIsValid = true;
79  }
80  }
81  }
82  }
83 
84  // Copy into (non-volatile, possible register based) member var
85  isValid_ = locIsValid;
86 }
87 
88 
90 {
91  is >> *this;
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
97 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
98 {
99  return
100  isValid_
101  && (
102  major(status_.st_dev) == major(stat2.status().st_dev)
103  && minor(status_.st_dev) == minor(stat2.status().st_dev)
104  );
105 }
106 
107 
108 bool Foam::fileStat::sameINode(const fileStat& stat2) const
109 {
110  return isValid_ && (status_.st_ino == stat2.status().st_ino);
111 }
112 
113 
114 bool Foam::fileStat::sameINode(const label iNode) const
115 {
116  return isValid_ && (status_.st_ino == ino_t(iNode));
117 }
118 
119 
120 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
121 
123 {
124  FixedList<label, 13> stat(is);
125 
126  fStat.isValid_ = stat[0];
127 
128  dev_t st_dev = makedev(stat[1], stat[2]);
129  fStat.status_.st_dev = st_dev;
130 
131  fStat.status_.st_ino = stat[3];
132  fStat.status_.st_mode = stat[4];
133  fStat.status_.st_uid = stat[5];
134  fStat.status_.st_gid = stat[6];
135 
136  dev_t st_rdev = makedev(stat[7], stat[8]);
137  fStat.status_.st_rdev = st_rdev;
138 
139  fStat.status_.st_size = stat[9];
140  fStat.status_.st_atime = stat[10];
141  fStat.status_.st_mtime = stat[11];
142  fStat.status_.st_ctime = stat[12];
143 
144  // Check state of Istream
145  is.check("Istream& operator>>(Istream&, fileStat&)");
146 
147  return is;
148 }
149 
150 
152 {
154 
155  stat[0] = label(fStat.isValid_);
156  stat[1] = label(major(fStat.status_.st_dev));
157  stat[2] = label(minor(fStat.status_.st_dev));
158  stat[3] = label(fStat.status_.st_ino);
159  stat[4] = label(fStat.status_.st_mode);
160  stat[5] = label(fStat.status_.st_uid);
161  stat[6] = label(fStat.status_.st_gid);
162  stat[7] = label(major(fStat.status_.st_rdev));
163  stat[8] = label(minor(fStat.status_.st_rdev));
164  stat[9] = label(fStat.status_.st_size);
165  stat[10] = label(fStat.status_.st_atime);
166  stat[11] = label(fStat.status_.st_mtime);
167  stat[12] = label(fStat.status_.st_ctime);
168 
169  return os << stat;
170 }
171 
172 
173 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:79
static const label nVariants_
Number of file variants.
Definition: fileStat.H:79
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:54
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
fileStat()
Empty constructor.
Definition: fileStat.C:43
bool sameINode(const fileStat &stat2) const
Compare two fileStats for same Inode.
Definition: fileStat.C:108
const struct stat & status() const
Raw status.
Definition: fileStat.H:110
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool sameDevice(const fileStat &stat2) const
Compare two fileStats for same device.
Definition: fileStat.C:97
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
Wrapper for stat() system call.
Definition: fileStat.H:65
#define timedOut(x)
Check it a timeout has occurred.
Definition: timer.H:71
Ostream & operator<<(Ostream &, const ensightPart &)
static const char * variantExts_[]
Extensions of the file variants.
Definition: fileStat.H:82