All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fileMonitor.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::fileMonitor
26 
27 Description
28  Checking for changes to files.
29 
30 Note
31  The default is to use stat to get the timestamp.
32 
33  Compile with FOAM_USE_INOTIFY to use the inotify
34  (Linux specific, since 2.6.13) framework. The problem is that inotify does
35  not work on nfs3 mounted directories!!
36 
37 SourceFiles
38  fileMonitor.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef fileMonitor_H
43 #define fileMonitor_H
44 
45 #include <sys/types.h>
46 #include "NamedEnum.H"
47 #include "className.H"
48 #include "DynamicList.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 class fileMonitor;
56 class fileMonitorWatcher;
57 
58 /*---------------------------------------------------------------------------*\
59  Class fileMonitor Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class fileMonitor
63 {
64 
65 public:
66 
67  // Public data types
68 
69  //- Enumeration defining the file state.
70  enum fileState
71  {
73  MODIFIED = 1,
74  DELETED = 2
75  };
76 
78 
79 
80 private:
81 
82  // Private Data
83 
84  //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
85  const bool useInotify_;
86 
87  //- State for all watchFds based on local files
88  mutable DynamicList<fileState> localState_;
89 
90  //- State for all watchFds - synchronised
91  mutable DynamicList<fileState> state_;
92 
93  //- Filename for all watchFds
94  DynamicList<fileName> watchFile_;
95 
96  //- Free watchFds
97  DynamicList<label> freeWatchFds_;
98 
99  //- Watch mechanism (stat or inotify)
100  mutable autoPtr<fileMonitorWatcher> watcher_;
101 
102 
103  // Private Member Functions
104 
105  //- Update localState_ from any events.
106  void checkFiles() const;
107 
108 
109 public:
110 
111  // Declare name of the class and its debug switch
112  ClassName("fileMonitor");
113 
114  // Constructors
115 
116  //- Construct null
117  fileMonitor(const bool useInotify);
118 
119  //- Disallow default bitwise copy construction
120  fileMonitor(const fileMonitor&) = delete;
121 
122 
123  //- Destructor
124  ~fileMonitor();
125 
126 
127  // Member Functions
128 
129  //- Add file to watch. Return watch descriptor
130  label addWatch(const fileName&);
131 
132  //- Remove file to watch. Return true if successful
133  bool removeWatch(const label watchFd);
134 
135  //- Get name of file being watched
136  const fileName& getFile(const label watchFd) const;
137 
138  //- Check state using handle
139  fileState getState(const label watchFd) const;
140 
141  //- Check state of all files. Updates state_.
142  void updateStates
143  (
144  const bool masterOnly,
145  const bool syncPar
146  ) const;
147 
148  //- Reset state (e.g. after having read it) using handle
149  void setUnmodified(const label watchFd);
150 
151 
152  // Member Operators
153 
154  //- Disallow default bitwise assignment
155  void operator=(const fileMonitor&) = delete;
156 };
157 
158 
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160 
161 } // End namespace Foam
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 #endif
166 
167 // ************************************************************************* //
fileMonitor(const bool useInotify)
Construct null.
Definition: fileMonitor.C:429
static const NamedEnum< fileState, 3 > fileStateNames_
Definition: fileMonitor.H:76
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:79
void operator=(const fileMonitor &)=delete
Disallow default bitwise assignment.
Checking for changes to files.
Definition: fileMonitor.H:61
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:508
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:69
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
Definition: fileMonitor.C:611
ClassName("fileMonitor")
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
Definition: fileMonitor.C:489
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Definition: fileMonitor.C:516
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:502
~fileMonitor()
Destructor.
Definition: fileMonitor.C:442
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
Definition: fileMonitor.C:450
Macro definitions for declaring ClassName(), NamespaceName(), etc.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Namespace for OpenFOAM.