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
87 
88  //- State for all watchFds - synchronised
90 
91  //- Filename for all watchFds
93 
94  //- Free watchFds
96 
97  //- Watch mechanism (stat or inotify)
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 // ************************************************************************* //
Foam::fileMonitor::MODIFIED
@ MODIFIED
Definition: fileMonitor.H:72
Foam::fileMonitor::localState_
DynamicList< fileState > localState_
State for all watchFds based on local files.
Definition: fileMonitor.H:85
Foam::fileMonitor::DELETED
@ DELETED
Definition: fileMonitor.H:73
Foam::fileMonitor::fileMonitor
fileMonitor(const fileMonitor &)
Disallow default bitwise copy construct.
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::DynamicList< fileState >
Foam::fileMonitor::UNMODIFIED
@ UNMODIFIED
Definition: fileMonitor.H:71
Foam::fileMonitor::getFile
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:502
Foam::fileMonitor::removeWatch
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
Definition: fileMonitor.C:489
NamedEnum.H
Foam::fileMonitor::state_
DynamicList< fileState > state_
State for all watchFds - synchronised.
Definition: fileMonitor.H:88
Foam::fileMonitor::addWatch
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
Definition: fileMonitor.C:450
Foam::fileMonitor::fileState
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:69
Foam::fileMonitor::operator=
void operator=(const fileMonitor &)
Disallow default bitwise assignment.
Foam::fileMonitor::~fileMonitor
~fileMonitor()
Destructor.
Definition: fileMonitor.C:442
Foam::label
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
Foam::fileMonitor::watcher_
autoPtr< fileMonitorWatcher > watcher_
Watch mechanism (stat or inotify)
Definition: fileMonitor.H:97
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Foam::fileMonitor::freeWatchFds_
DynamicList< label > freeWatchFds_
Free watchFds.
Definition: fileMonitor.H:94
Foam::fileMonitor
Checking for changes to files.
Definition: fileMonitor.H:61
Foam::fileMonitor::ClassName
ClassName("fileMonitor")
Foam::fileMonitor::fileStateNames_
static const NamedEnum< fileState, 3 > fileStateNames_
Definition: fileMonitor.H:76
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::fileMonitor::useInotify_
const bool useInotify_
Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
Definition: fileMonitor.H:82
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::fileMonitor::getState
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:508
Foam::fileMonitor::setUnmodified
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
Definition: fileMonitor.C:611
Foam::fileMonitor::watchFile_
DynamicList< fileName > watchFile_
Filename for all watchFds.
Definition: fileMonitor.H:91
Foam::fileMonitor::updateStates
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Definition: fileMonitor.C:516
Foam::fileMonitor::checkFiles
void checkFiles() const
Update localState_ from any events.
Definition: fileMonitor.C:294
DynamicList.H
Foam::NamedEnum< fileState, 3 >