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 -------------------------------------------------------------------------------
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::fileMonitor
29 
30 Description
31  Checking for changes to files.
32 
33 Note
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 
40 SourceFiles
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 
56 namespace Foam
57 {
58 
59 // Forward Declarations
60 class fileMonitor;
61 class fileMonitorWatcher;
62 
63 /*---------------------------------------------------------------------------*\
64  Class fileMonitor Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class fileMonitor
68 {
69 public:
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 
81  static const Enum<fileState> fileStateNames_;
82 
83 private:
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 
118 public:
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 // ************************************************************************* //
Foam::Enum< fileState >
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::fileMonitor::UNMODIFIED
Definition: fileMonitor.H:75
Foam::DynamicList< fileState >
Foam::fileMonitor::getFile
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:507
Foam::fileMonitor::removeWatch
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
Definition: fileMonitor.C:493
Foam::fileMonitor::addWatch
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
Definition: fileMonitor.C:452
Foam::fileMonitor::fileState
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:73
Foam::fileMonitor::~fileMonitor
~fileMonitor()
Destructor.
Definition: fileMonitor.C:444
Foam::fileMonitor::fileStateNames_
static const Enum< fileState > fileStateNames_
Definition: fileMonitor.H:80
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Foam::fileMonitor
Checking for changes to files.
Definition: fileMonitor.H:66
Foam::fileMonitor::MODIFIED
Definition: fileMonitor.H:76
Foam::fileMonitor::ClassName
ClassName("fileMonitor")
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::fileMonitor::getState
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:513
Foam::fileMonitor::setUnmodified
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
Definition: fileMonitor.C:617
Foam::fileMonitor::updateStates
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Definition: fileMonitor.C:521
DynamicList.H
Foam::fileMonitor::DELETED
Definition: fileMonitor.H:77
Enum.H