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-2017 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
37 :
38  isValid_(false)
39 {}
40 
41 
43 (
44  const fileName& fName,
45  const bool followLink,
46  const unsigned int maxTime
47 )
48 {
49  // Work on volatile
50  volatile bool locIsValid = false;
51 
52  timer myTimer(maxTime);
53 
54  if (!timedOut(myTimer))
55  {
56  if (followLink)
57  {
58  if (::stat(fName.c_str(), &status_) != 0)
59  {
60  locIsValid = false;
61  }
62  else
63  {
64  locIsValid = true;
65  }
66  }
67  else
68  {
69  if (::lstat(fName.c_str(), &status_) != 0)
70  {
71  locIsValid = false;
72  }
73  else
74  {
75  locIsValid = true;
76  }
77  }
78  }
79 
80  // Copy into (non-volatile, possible register based) member var
81  isValid_ = locIsValid;
82 }
83 
84 
86 {
87  is >> *this;
88 }
89 
90 
91 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
92 
93 bool Foam::fileStat::sameDevice(const fileStat& stat2) const
94 {
95  return
96  isValid_
97  && (
98  major(status_.st_dev) == major(stat2.status().st_dev)
99  && minor(status_.st_dev) == minor(stat2.status().st_dev)
100  );
101 }
102 
103 
104 bool Foam::fileStat::sameINode(const fileStat& stat2) const
105 {
106  return isValid_ && (status_.st_ino == stat2.status().st_ino);
107 }
108 
109 
110 bool Foam::fileStat::sameINode(const label iNode) const
111 {
112  return isValid_ && (status_.st_ino == ino_t(iNode));
113 }
114 
115 
116 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
117 
119 {
120  FixedList<label, 13> stat(is);
121 
122  fStat.isValid_ = stat[0];
123 
124  dev_t st_dev = makedev(stat[1], stat[2]);
125  fStat.status_.st_dev = st_dev;
126 
127  fStat.status_.st_ino = stat[3];
128  fStat.status_.st_mode = stat[4];
129  fStat.status_.st_uid = stat[5];
130  fStat.status_.st_gid = stat[6];
131 
132  dev_t st_rdev = makedev(stat[7], stat[8]);
133  fStat.status_.st_rdev = st_rdev;
134 
135  fStat.status_.st_size = stat[9];
136  fStat.status_.st_atime = stat[10];
137  fStat.status_.st_mtime = stat[11];
138  fStat.status_.st_ctime = stat[12];
139 
140  // Check state of Istream
141  is.check("Istream& operator>>(Istream&, fileStat&)");
142 
143  return is;
144 }
145 
146 
148 {
150 
151  stat[0] = label(fStat.isValid_);
152  stat[1] = label(major(fStat.status_.st_dev));
153  stat[2] = label(minor(fStat.status_.st_dev));
154  stat[3] = label(fStat.status_.st_ino);
155  stat[4] = label(fStat.status_.st_mode);
156  stat[5] = label(fStat.status_.st_uid);
157  stat[6] = label(fStat.status_.st_gid);
158  stat[7] = label(major(fStat.status_.st_rdev));
159  stat[8] = label(minor(fStat.status_.st_rdev));
160  stat[9] = label(fStat.status_.st_size);
161  stat[10] = label(fStat.status_.st_atime);
162  stat[11] = label(fStat.status_.st_mtime);
163  stat[12] = label(fStat.status_.st_ctime);
164 
165  return os << stat;
166 }
167 
168 
169 // ************************************************************************* //
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: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:36
bool sameINode(const fileStat &stat2) const
Compare two fileStats for same Inode.
Definition: fileStat.C:104
const struct stat & status() const
Raw status.
Definition: fileStat.H:100
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:93
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 &)