From 1a6db8cdfa4e504b23741566bee1a561902393d1 Mon Sep 17 00:00:00 2001 From: mattijs Date: Sun, 29 Aug 2010 02:42:20 +0100 Subject: [PATCH] ENH: fileMonitor : monitor directory, not file Some editors (emacs) rename file at startup to the backup version. Hence watching the original file tells one nothing. --- src/OSspecific/POSIX/fileMonitor.C | 229 +++++++++++++++++------------ src/OSspecific/POSIX/fileMonitor.H | 17 +-- src/OpenFOAM/db/Time/Time.C | 5 + src/OpenFOAM/db/Time/TimeIO.C | 1 + 4 files changed, 152 insertions(+), 100 deletions(-) diff --git a/src/OSspecific/POSIX/fileMonitor.C b/src/OSspecific/POSIX/fileMonitor.C index af6d29ddd0..48807d2e68 100644 --- a/src/OSspecific/POSIX/fileMonitor.C +++ b/src/OSspecific/POSIX/fileMonitor.C @@ -31,6 +31,7 @@ Class #include "Pstream.H" #include "PackedList.H" #include "PstreamReduceOps.H" +#include "OSspecific.H" #ifdef FOAM_USE_STAT # include "OSspecific.H" @@ -112,50 +113,104 @@ namespace Foam #ifdef FOAM_USE_STAT //- From watch descriptor to modified time - HashTable lastMod; + DynamicList lastMod_; //- initialize HashTable size inline fileMonitorWatcher(const label sz = 20) : - lastMod(sz) + lastMod_(sz) {} - inline label addWatch(const fileName& fName) + inline bool addWatch(const label watchFd, const fileName& fName) { - const label watchFd = lastMod.size(); - lastMod.insert(watchFd, lastModified(fName)); - return watchFd; + if (watchFd < lastMod_.size() && lastMod_[watchFd] != 0) + { + // Reuse of watchFd : should have lastMod set to 0. + FatalErrorIn("addWatch(const label, const fileName&)") + << "Problem adding watch " << watchFd + << " to file " << fName + << abort(FatalError); + } + + lastMod_(watchFd) = lastModified(fName); + return true; } inline bool removeWatch(const label watchFd) { - return lastMod.erase(watchFd); + lastMod_[watchFd] = 0; + return true; } #else //- File descriptor for the inotify instance int fd; - //- initialize inotify - inline fileMonitorWatcher(const label dummy = 0) + //- Current watchIDs and corresponding directory id + DynamicList