chiark / gitweb /
Use right exception classes.
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 5 Mar 2011 11:10:31 +0000 (11:10 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 5 Mar 2011 11:10:31 +0000 (11:10 +0000)
(Though maybe there are too many anyway...)
Some Linux build fixes.

src/InotifyWatcher.cc
src/PollingWatcher.cc
src/PollingWatcher.h
src/Watcher.h
src/WatcherImplementation.cc
src/timeval.h

index cda5d80..53c242b 100644 (file)
@@ -35,17 +35,17 @@ InotifyWatcher::InotifyWatcher(const std::string &path_arg,
   dir_wd(-1) {
   // Get an inotify FD
   if((ifd = inotify_init()) < 0)
-    throw SystemError("inotify_init", errno);
+    throw Watcher::SystemError("inotify_init", errno);
   // Watch the containing directory
   if((dir_wd = inotify_add_watch(ifd, dir.c_str(),
                                  IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO)) < 0)
-    throw SystemError("inotify_add_watch for " + dir, errno);
+    throw Watcher::SystemError("inotify_add_watch for " + dir, errno);
   // Try to open the file.  This will add file_wd if it exists.
   openFile();
   // Discard initial content
   if(fp) {
     if(fseek(fp, 0, SEEK_END) < 0)
-      throw IOError("seeking " + path, errno);
+      throw Watcher::IOError("seeking " + path, errno);
   }
 }
 
@@ -64,7 +64,7 @@ void InotifyWatcher::openFile() {
     // wait for it to come into existence.
     if(!(fp = fopen(path.c_str(), "r"))) {
       if(errno != ENOENT)
-        throw IOError("opening " + path, errno);
+        throw Watcher::IOError("opening " + path, errno);
     } else {
       // If we did open it then detect changes to it.
       // (But see below concerning IN_*_SELF.)
@@ -72,7 +72,7 @@ void InotifyWatcher::openFile() {
                                       IN_MODIFY
                                       |IN_MOVE_SELF
                                       |IN_DELETE_SELF)) < 0)
-        throw SystemError("inotify_add_watch for " + path, errno);
+        throw Watcher::SystemError("inotify_add_watch for " + path, errno);
     }
   }
 }
@@ -92,7 +92,7 @@ void InotifyWatcher::closeFile() {
         if(errno == EINVAL)
           warn("inotify_rm_watch: %s", strerror(errno));
         else
-          throw SystemError("inotify_rm_watch for " + path, errno);
+          throw Watcher::SystemError("inotify_rm_watch for " + path, errno);
       }
       file_wd = -1;
     }
@@ -106,7 +106,7 @@ void InotifyWatcher::work() {
   int n = read(ifd, buffer, sizeof buffer);
   if(n < 0) {
     if(errno != EINTR && errno != EAGAIN)
-      throw SystemError("read from inotify fd", errno);
+      throw Watcher::SystemError("read from inotify fd", errno);
     return;
   }
   // We might have more than one event.
@@ -114,11 +114,11 @@ void InotifyWatcher::work() {
   while(n > 0) {
     // Make sure we have a whole event
     if((size_t)n < sizeof (struct inotify_event))
-      throw SystemError("short read from inotify fd");
+      throw Watcher::SystemError("short read from inotify fd");
     const struct inotify_event &event = *(const struct inotify_event *)ptr;
     const size_t total = sizeof event + event.len;
     if((size_t)n < total)
-      throw SystemError("short read from inotify fd");
+      throw Watcher::SystemError("short read from inotify fd");
     
     // See if the file contents changed.
     if(event.wd == file_wd) {
index d71d150..fd66ae9 100644 (file)
@@ -17,9 +17,7 @@
 #include <config.h>
 #include "Watcher.h"
 #include "PollingWatcher.h"
-#include "IOError.h"
 #include <cerrno>
-#include <sys/stat.h>
 
 PollingWatcher::PollingWatcher(const std::string &path,
                                Watcher *watcher):
@@ -28,10 +26,7 @@ PollingWatcher::PollingWatcher(const std::string &path,
   dev(-1),
   ino(-1) {
   // Try opening the file
-  if((fp = fopen(path.c_str(), "r"))) {
-    if(fseek(fp, 0, SEEK_END) < 0)
-      throw IOError("seeking " + path, errno);
-  }
+  openFile();
 }
 
 PollingWatcher::~PollingWatcher() {
@@ -50,11 +45,11 @@ void PollingWatcher::openFile() {
   if(!fp) {
     if(!(fp = fopen(path.c_str(), "r"))) {
       if(errno != ENOENT)
-        throw IOError("opening " + path, errno);
+        throw Watcher::IOError("opening " + path, errno);
       return;
     }
     if(fstat(fileno(fp), &sb) < 0)
-      throw IOError("fstat " + path, errno);
+      throw Watcher::IOError("fstat " + path, errno);
     dev = sb.st_dev;
     ino = sb.st_ino;
   }
index 7b94433..f9e0c5a 100644 (file)
@@ -18,6 +18,7 @@
 #define POLLINGWATCHER_H
 
 #include "Watcher.h"
+#include <sys/stat.h>
 
 class PollingWatcher: public WatcherImplementation {
 public:
index 0d82fa3..5f75d87 100644 (file)
@@ -20,6 +20,7 @@
 #include <string>
 #include <stdexcept>
 #include <cstring>
+#include <cstdio>
 
 #if HAVE_SYS_INOTIFY_H
 # define WATCHER InotifyWatcher
index c96f58c..fab0af4 100644 (file)
@@ -16,7 +16,6 @@
 //
 #include <config.h>
 #include "Watcher.h"
-#include "IOError.h"
 #include <cerrno>
 
 WatcherImplementation::~WatcherImplementation() {
@@ -58,7 +57,7 @@ void WatcherImplementation::readLines() {
       // and throw.
       if(ferror(fp)) {
         closeFile();
-        throw IOError("reading " + path, errno);
+        throw Watcher::IOError("reading " + path, errno);
       }
       // Otherwise it must be EOF.
       // Make sure future reads, after the file is extended, will succeed.
index 32157c1..8877c3e 100644 (file)
@@ -18,6 +18,7 @@
 #define TIMEVAL_H
 
 #include <sys/time.h>
+#include <limits.h>
 
 #if SIZEOF_TIME_T == SIZEOF_INT
 # define TIME_MAX INT_MAX