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 Foam_fileMonitor_H
46 #define Foam_fileMonitor_H
47 
48 #include "DynamicList.H"
49 #include <memory>
50 #include <sys/types.h>
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward Declarations
58 class fileMonitor;
59 class fileMonitorWatcher;
60 template<class EnumType> class Enum;
61 
62 /*---------------------------------------------------------------------------*\
63  Class fileMonitor Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 class fileMonitor
67 {
68 public:
69 
70  // Public Data Types
71 
72  //- Enumeration defining the file state.
73  enum fileState
74  {
75  UNMODIFIED = 0,
76  MODIFIED = 1,
77  DELETED = 2
78  };
79 
80  static const Enum<fileState> fileStateNames_;
81 
82 private:
83 
84  // Private Data
85 
86  //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
87  const bool useInotify_;
88 
89  //- State for all watchFds based on local files
90  mutable DynamicList<fileState> localState_;
91 
92  //- State for all watchFds - synchronised
93  mutable DynamicList<fileState> state_;
94 
95  //- Filename for all watchFds
96  DynamicList<fileName> watchFile_;
97 
98  //- Free watchFds
99  DynamicList<label> freeWatchFds_;
100 
101  //- Watch mechanism (stat or inotify)
102  mutable std::unique_ptr<fileMonitorWatcher> watcher_;
103 
104 
105  // Private Member Functions
106 
107  //- Update localState_ from any events.
108  void checkFiles() const;
109 
110  //- No copy construct
111  fileMonitor(const fileMonitor&) = delete;
112 
113  //- No copy assignment
114  void operator=(const fileMonitor&) = delete;
115 
116 
117 public:
118 
119  //- Named/registered debug switch: 'fileMonitor'
120  static int debug;
121 
122 
123  // Constructors
124 
125  //- Construct with specified inotify use
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&);
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 // ************************************************************************* //
static const Enum< fileState > fileStateNames_
Definition: fileMonitor.H:77
A class for handling file names.
Definition: fileName.H:72
Checking for changes to files.
Definition: fileMonitor.H:61
static int debug
Named/registered debug switch: &#39;fileMonitor&#39;.
Definition: fileMonitor.H:137
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:519
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:70
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
Definition: fileMonitor.C:623
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
Definition: fileMonitor.C:499
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Definition: fileMonitor.C:527
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:513
~fileMonitor()
Destructor.
Definition: fileMonitor.C:453
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
Definition: fileMonitor.C:461
Namespace for OpenFOAM.