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-2018 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 <sys/types.h>
49 #include "Enum.H"
50 #include "className.H"
51 #include "DynamicList.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward Declarations
59 class fileMonitor;
60 class fileMonitorWatcher;
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 autoPtr<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  // Declare name of the class and its debug switch
120  ClassName("fileMonitor");
121 
122  // Constructors
123 
124  //- Construct null
125  explicit fileMonitor(const bool useInotify);
126 
127 
128  //- Destructor
129  ~fileMonitor();
130 
131 
132  // Member Functions
133 
134  //- Add file to watch. Return watch descriptor
135  label addWatch(const fileName&);
136 
137  //- Remove file to watch. Return true if successful
138  bool removeWatch(const label watchFd);
139 
140  //- Get name of file being watched
141  const fileName& getFile(const label watchFd) const;
142 
143  //- Check state using handle
144  fileState getState(const label watchFd) const;
145 
146  //- Check state of all files. Updates state_.
147  void updateStates
148  (
149  const bool masterOnly,
150  const bool syncPar
151  ) const;
152 
153  //- Reset state (e.g. after having read it) using handle
154  void setUnmodified(const label watchFd);
155 };
156 
157 
158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
159 
160 } // End namespace Foam
161 
162 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
163 
164 #endif
165 
166 // ************************************************************************* //
Foam::Enum< fileState >
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::fileMonitor::UNMODIFIED
Definition: fileMonitor.H:74
Foam::DynamicList< fileState >
Foam::fileMonitor::getFile
const fileName & getFile(const label watchFd) const
Get name of file being watched.
Definition: fileMonitor.C:509
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:72
Foam::fileMonitor::~fileMonitor
~fileMonitor()
Destructor.
Definition: fileMonitor.C:444
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:62
Foam::fileMonitor::fileStateNames_
static const Enum< fileState > fileStateNames_
Definition: fileMonitor.H:79
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Foam::fileMonitor
Checking for changes to files.
Definition: fileMonitor.H:65
Foam::fileMonitor::MODIFIED
Definition: fileMonitor.H:75
Foam::fileMonitor::ClassName
ClassName("fileMonitor")
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::fileMonitor::getState
fileState getState(const label watchFd) const
Check state using handle.
Definition: fileMonitor.C:515
Foam::fileMonitor::setUnmodified
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
Definition: fileMonitor.C:619
Foam::fileMonitor::updateStates
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Definition: fileMonitor.C:523
DynamicList.H
Foam::fileMonitor::DELETED
Definition: fileMonitor.H:76
Enum.H