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