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) 2011-2015 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 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
36 :
37  isValid_(false)
38 {}
39 
40 
41 Foam::fileStat::fileStat(const fileName& fName, const unsigned int maxTime)
42 {
43  // Work on volatile
44  volatile bool locIsValid = false;
45 
46  timer myTimer(maxTime);
47 
48  if (!timedOut(myTimer))
49  {
50  if (::stat(fName.c_str(), &status_) != 0)
51  {
52  locIsValid = false;
53  }
54  else
55  {
56  locIsValid = true;
57  }
58  }
59 
60  // Copy into (non-volatile, possible register based) member var
61  isValid_ = locIsValid;
62 }
63 
64 
66 {
67  is >> *this;
68 }
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
73 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
74 {
75  return
76  isValid_
77  && (
78  major(status_.st_dev) == major(stat2.status().st_dev)
79  && minor(status_.st_dev) == minor(stat2.status().st_dev)
80  );
81 }
82 
83 
84 bool Foam::fileStat::sameINode(const fileStat& stat2) const
85 {
86  return isValid_ && (status_.st_ino == stat2.status().st_ino);
87 }
88 
89 
90 bool Foam::fileStat::sameINode(const label iNode) const
91 {
92  return isValid_ && (status_.st_ino == ino_t(iNode));
93 }
94 
95 
96 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
97 
99 {
100  FixedList<label, 13> stat(is);
101 
102  fStat.isValid_ = stat[0];
103 
104  dev_t st_dev = makedev(stat[1], stat[2]);
105  fStat.status_.st_dev = st_dev;
106 
107  fStat.status_.st_ino = stat[3];
108  fStat.status_.st_mode = stat[4];
109  fStat.status_.st_uid = stat[5];
110  fStat.status_.st_gid = stat[6];
111 
112  dev_t st_rdev = makedev(stat[7], stat[8]);
113  fStat.status_.st_rdev = st_rdev;
114 
115  fStat.status_.st_size = stat[9];
116  fStat.status_.st_atime = stat[10];
117  fStat.status_.st_mtime = stat[11];
118  fStat.status_.st_ctime = stat[12];
119 
120  // Check state of Istream
121  is.check("Istream& operator>>(Istream&, fileStat&)");
122 
123  return is;
124 }
125 
126 
128 {
130 
131  stat[0] = label(fStat.isValid_);
132  stat[1] = label(major(fStat.status_.st_dev));
133  stat[2] = label(minor(fStat.status_.st_dev));
134  stat[3] = label(fStat.status_.st_ino);
135  stat[4] = label(fStat.status_.st_mode);
136  stat[5] = label(fStat.status_.st_uid);
137  stat[6] = label(fStat.status_.st_gid);
138  stat[7] = label(major(fStat.status_.st_rdev));
139  stat[8] = label(minor(fStat.status_.st_rdev));
140  stat[9] = label(fStat.status_.st_size);
141  stat[10] = label(fStat.status_.st_atime);
142  stat[11] = label(fStat.status_.st_mtime);
143  stat[12] = label(fStat.status_.st_ctime);
144 
145  return os << stat;
146 }
147 
148 
149 // ************************************************************************* //
bool sameINode(const fileStat &stat2) const
Compare two fileStats for same Inode.
Definition: fileStat.C:84
bool sameDevice(const fileStat &stat2) const
Compare two fileStats for same device.
Definition: fileStat.C:73
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:69
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
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:35
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
Wrapper for stat() system call.
Definition: fileStat.H:65
#define timedOut(x)
Check it a timeout has occured.
Definition: timer.H:71
Ostream & operator<<(Ostream &, const ensightPart &)
const struct stat & status() const
Raw status.
Definition: fileStat.H:93