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-2019 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 fileStat_H
44 #define 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  //- Empty constructor
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  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  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  //- Raw status
123  const struct stat& status() const
124  {
125  return status_;
126  }
127 
128  //- Was file-stat successful?
129  bool valid() const
130  {
131  return valid_;
132  }
133 
134  //- Size in bytes. Zero for an invalid file-stat.
135  label size() const;
136 
137  //- Return the modification time in seconds.
138  // Zero for an invalid file-stat.
139  time_t modTime() const;
140 
141  //- Return the modification time in seconds (nanosecond resolution)
142  // Zero for an invalid file-stat.
143  double dmodTime() const;
144 
145 
146  // Check
147 
148  //- Compare two fileStats for same device
149  bool sameDevice(const fileStat& other) const;
150 
151  //- Compare two fileStats for same Inode
152  bool sameINode(const fileStat& other) const;
153 
154  //- Compare state against inode
155  bool sameINode(const label iNode) const;
156 
157 
158  // IOstream Operators
159 
160  friend Istream& operator>>(Istream& is, fileStat& fs);
161  friend Ostream& operator<<(Ostream& os, const fileStat& fs);
162 };
163 
164 
165 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
166 
167 } // End namespace Foam
168 
169 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170 
171 #endif
172 
173 // ************************************************************************* //
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::fileStat::sameDevice
bool sameDevice(const fileStat &other) const
Compare two fileStats for same device.
Definition: fileStat.C:141
Foam::fileStat::operator>>
friend Istream & operator>>(Istream &is, fileStat &fs)
Foam::fileStat
Wrapper for stat() and lstat() system calls.
Definition: fileStat.H:67
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::fileStat::operator<<
friend Ostream & operator<<(Ostream &os, const fileStat &fs)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
fileName.H
Foam::fileStat::valid
bool valid() const
Was file-stat successful?
Definition: fileStat.H:128
Foam::fileStat::fileStat
fileStat()
Empty constructor.
Definition: fileStat.C:46
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::fileStat::status
const struct stat & status() const
Raw status.
Definition: fileStat.H:122
Foam::fileStat::dmodTime
double dmodTime() const
Return the modification time in seconds (nanosecond resolution)
Definition: fileStat.C:123
label.H
Foam::fileStat::size
label size() const
Size in bytes. Zero for an invalid file-stat.
Definition: fileStat.C:111
Foam::fileStat::modTime
time_t modTime() const
Return the modification time in seconds.
Definition: fileStat.C:117
Foam::fileStat::sameINode
bool sameINode(const fileStat &other) const
Compare two fileStats for same Inode.
Definition: fileStat.C:152
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56