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 | Copyright (C) 2011-2015 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 private:
80  // Private data
81 
82  //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
83  const bool useInotify_;
84 
85  //- State for all watchFds based on local files
86  mutable DynamicList<fileState> localState_;
87 
88  //- State for all watchFds - synchronised
89  mutable DynamicList<fileState> state_;
90 
91  //- Filename for all watchFds
92  DynamicList<fileName> watchFile_;
93 
94  //- Free watchFds
95  DynamicList<label> freeWatchFds_;
96 
97  //- Watch mechanism (stat or inotify)
98  mutable autoPtr<fileMonitorWatcher> watcher_;
99 
100 
101  // Private Member Functions
102 
103  //- Update localState_ from any events.
104  void checkFiles() const;
105 
106  //- Disallow default bitwise copy construct
107  fileMonitor(const fileMonitor&);
108 
109  //- Disallow default bitwise assignment
110  void operator=(const fileMonitor&);
111 
112 
113 public:
114 
115  // Declare name of the class and its debug switch
116  ClassName("fileMonitor");
117 
118  // Constructors
119 
120  //- Construct null
121  fileMonitor(const bool useInotify);
122 
123 
124  //- Destructor
125  ~fileMonitor();
126 
127 
128  // Member Functions
129 
130  //- Add file to watch. Return watch descriptor
131  label addWatch(const fileName&);
132 
133  //- Remove file to watch. Return true if successful
134  bool removeWatch(const label watchFd);
135 
136  //- Get name of file being watched
137  const fileName& getFile(const label watchFd) const;
138 
139  //- Check state using handle
140  fileState getState(const label watchFd) const;
141 
142  //- Check state of all files. Updates state_.
143  void updateStates
144  (
145  const bool masterOnly,
146  const bool syncPar
147  ) const;
148 
149  //- Reset state (e.g. after having read it) using handle
150  void setUnmodified(const label watchFd);
151 };
152 
153 
154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
155 
156 } // End namespace Foam
157 
158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
159 
160 #endif
161 
162 // ************************************************************************* //
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:69
Checking for changes to files.
Definition: fileMonitor.H:61
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")
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:508
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
Definition: fileMonitor.C:489
~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:53
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:502
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Definition: fileMonitor.C:516
Namespace for OpenFOAM.