fileStat.H
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-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 Class
28  Foam::fileStat
29 
30 Description
31  Wrapper for stat() and lstat() system calls.
32 
33 Warning
34  On Linux (an maybe on others) a stat() of an nfs mounted (remote)
35  file does never timeout and cannot be interrupted!
36  So e.g. Foam::ping first and hope nfs is running.
37 
38 SourceFiles
39  fileStat.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef Foam_fileStat_H
44 #define Foam_fileStat_H
45 
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 
49 #include "label.H"
50 #include "fileName.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward Declarations
58 class fileStat;
59 
60 Istream& operator>>(Istream& is, fileStat& fs);
61 Ostream& operator<<(Ostream& os, const fileStat& fs);
62 
63 
64 /*---------------------------------------------------------------------------*\
65  Class fileStat Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class fileStat
69 {
70  // Private Data
71 
72  struct stat status_;
73 
74  bool valid_;
75 
76 
77 public:
78 
79  // Constructors
80 
81  //- Default construct
82  fileStat();
83 
84  //- Construct from components.
85  //
86  // \param fName The file name or directory name to stat.
87  // \param followLink If it is a link, get the status of the source
88  // file/directory.
89  // \param maxTime The timeout value.
90  //
91  // \note An empty filename is a no-op.
92  explicit fileStat
93  (
94  const char* fName,
95  const bool followLink = true,
96  const unsigned int maxTime = 0
97  );
98 
99  //- Construct from components.
100  //
101  // \param fName The file name or directory name to stat.
102  // \param followLink If it is a link, get the status of the source
103  // file/directory.
104  // \param maxTime The timeout value.
105  //
106  // \note An empty filename is a no-op.
107  explicit fileStat
108  (
109  const fileName& fName,
110  const bool followLink = true,
111  const unsigned int maxTime = 0
112  );
113 
114  //- Construct from Istream
115  explicit fileStat(Istream& is);
116 
117 
118  // Member Functions
119 
120  // Access
121 
122  //- True if file-stat was successful
123  bool good() const noexcept
124  {
125  return valid_;
126  }
127 
128  //- True if file-stat was successful
129  explicit operator bool() const noexcept
130  {
131  return valid_;
132  }
133 
134  //- The raw status
135  const struct stat& status() const noexcept
136  {
137  return status_;
138  }
139 
140  //- Size in bytes, 0 for an invalid file-stat.
141  label size() const;
142 
143  //- The modification time in seconds,
144  //- 0 for an invalid file-stat.
145  time_t modTime() const;
147  //- The modification time in seconds (nanosecond resolution),
148  //- 0 for an invalid file-stat.
149  double dmodTime() const;
150 
151 
152  // Check
153 
154  //- Compare two fileStats for same device
155  bool sameDevice(const fileStat& other) const;
156 
157  //- Compare two fileStats for same Inode
158  bool sameINode(const fileStat& other) const;
159 
160  //- Compare state against inode
161  bool sameINode(const label iNode) const;
162 
163 
164  // IOstream Operators
165 
166  friend Istream& operator>>(Istream& is, fileStat& fs);
167  friend Ostream& operator<<(Ostream& os, const fileStat& fs);
168 
169 
170  // Housekeeping
171 
172  //- True if file-stat was successful
173  bool valid() const noexcept { return good(); }
174 };
175 
176 
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
178 
179 } // End namespace Foam
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 #endif
184 
185 // ************************************************************************* //
friend Ostream & operator<<(Ostream &os, const fileStat &fs)
A class for handling file names.
Definition: fileName.H:72
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
fileStat()
Default construct.
Definition: fileStat.C:33
bool good() const noexcept
True if file-stat was successful.
Definition: fileStat.H:130
const struct stat & status() const noexcept
The raw status.
Definition: fileStat.H:146
double dmodTime() const
The modification time in seconds (nanosecond resolution), 0 for an invalid file-stat.
Definition: fileStat.C:106
Istream & operator>>(Istream &, directionInfo &)
label size() const
Size in bytes, 0 for an invalid file-stat.
Definition: fileStat.C:94
time_t modTime() const
The modification time in seconds, 0 for an invalid file-stat.
Definition: fileStat.C:100
bool sameINode(const fileStat &other) const
Compare two fileStats for same Inode.
Definition: fileStat.C:133
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
Wrapper for stat() and lstat() system calls.
Definition: fileStat.H:63
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool sameDevice(const fileStat &other) const
Compare two fileStats for same device.
Definition: fileStat.C:122
friend Istream & operator>>(Istream &is, fileStat &fs)
bool valid() const noexcept
True if file-stat was successful.
Definition: fileStat.H:198
Namespace for OpenFOAM.