fileMonitor.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) 2017-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::fileMonitor
29
30Description
31 Checking for changes to files.
32
33Note
34 The default is to use stat to get the timestamp.
35
36 Compile with FOAM_USE_INOTIFY to use the inotify
37 (Linux specific, since 2.6.13) framework. The problem is that inotify does
38 not work on nfs3 mounted directories!!
39
40SourceFiles
41 fileMonitor.C
42
43\*---------------------------------------------------------------------------*/
44
45#ifndef fileMonitor_H
46#define fileMonitor_H
47
48#include "Enum.H"
49#include "className.H"
50#include "DynamicList.H"
51#include <memory>
52#include <sys/types.h>
53
54// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55
56namespace Foam
57{
58
59// Forward Declarations
60class fileMonitor;
61class fileMonitorWatcher;
62
63/*---------------------------------------------------------------------------*\
64 Class fileMonitor Declaration
65\*---------------------------------------------------------------------------*/
66
67class fileMonitor
68{
69public:
70
71 // Public Data Types
72
73 //- Enumeration defining the file state.
74 enum fileState
75 {
76 UNMODIFIED = 0,
77 MODIFIED = 1,
78 DELETED = 2
79 };
80
82
83private:
84
85 // Private Data
86
87 //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
88 const bool useInotify_;
89
90 //- State for all watchFds based on local files
91 mutable DynamicList<fileState> localState_;
92
93 //- State for all watchFds - synchronised
94 mutable DynamicList<fileState> state_;
95
96 //- Filename for all watchFds
97 DynamicList<fileName> watchFile_;
98
99 //- Free watchFds
100 DynamicList<label> freeWatchFds_;
101
102 //- Watch mechanism (stat or inotify)
103 mutable std::unique_ptr<fileMonitorWatcher> watcher_;
104
105
106 // Private Member Functions
107
108 //- Update localState_ from any events.
109 void checkFiles() const;
110
111 //- No copy construct
112 fileMonitor(const fileMonitor&) = delete;
113
114 //- No copy assignment
115 void operator=(const fileMonitor&) = delete;
116
117
118public:
119
120 // Declare name of the class and its debug switch
121 ClassName("fileMonitor");
122
123 // Constructors
124
125 //- Construct null
126 explicit fileMonitor(const bool useInotify);
127
128
129 //- Destructor
130 ~fileMonitor();
131
132
133 // Member Functions
134
135 //- Add file to watch. Return watch descriptor
136 label addWatch(const fileName&);
137
138 //- Remove file to watch. Return true if successful
139 bool removeWatch(const label watchFd);
140
141 //- Get name of file being watched
142 const fileName& getFile(const label watchFd) const;
143
144 //- Check state using handle
145 fileState getState(const label watchFd) const;
146
147 //- Check state of all files. Updates state_.
148 void updateStates
149 (
150 const bool masterOnly,
151 const bool syncPar
152 ) const;
153
154 //- Reset state (e.g. after having read it) using handle
155 void setUnmodified(const label watchFd);
156};
157
158
159// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160
161} // End namespace Foam
162
163// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164
165#endif
166
167// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
Checking for changes to files.
Definition: fileMonitor.H:67
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:513
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
Definition: fileMonitor.C:452
static const Enum< fileState > fileStateNames_
Definition: fileMonitor.H:80
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
~fileMonitor()
Destructor.
const fileName & getFile(const label watchFd) const
Get name of file being watched.
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:507
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:74
fileMonitor(const bool useInotify)
Construct null.
fileState getState(const label watchFd) const
Check state using handle.
ClassName("fileMonitor")
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
A class for handling file names.
Definition: fileName.H:76
Macro definitions for declaring ClassName(), NamespaceName(), etc.
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:67
Namespace for OpenFOAM.