aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2004-06-15 13:49:42 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2004-06-15 13:49:42 +0000
commit0b70231b9d9f2dfa5a9447fcd01fdb5c8f1fa0ea (patch)
tree5c37a0080ddd82f409a2695a2bf830608b706d40 /src/libutil
parent1bc6afefac7af8d4c917e898d6670d9b9d7f29f1 (diff)
* Refactoring.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc31
-rw-r--r--src/libutil/util.hh9
2 files changed, 39 insertions, 1 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 399233983..676404ecf 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -365,11 +365,16 @@ AutoCloseFD::AutoCloseFD(int fd)
AutoCloseFD::~AutoCloseFD()
{
- if (fd != -1) close(fd);
+ try {
+ close();
+ } catch (Error & e) {
+ printMsg(lvlError, format("error (ignored): %1%") % e.msg());
+ }
}
void AutoCloseFD::operator =(int fd)
{
+ if (this->fd != fd) close();
this->fd = fd;
}
@@ -378,6 +383,30 @@ AutoCloseFD::operator int()
return fd;
}
+void AutoCloseFD::close()
+{
+ if (fd != -1) {
+ if (::close(fd) == -1)
+ /* This should never happen. */
+ throw SysError("closing file descriptor");
+ fd = -1;
+ }
+}
+
+bool AutoCloseFD::isOpen()
+{
+ return fd != -1;
+}
+
+
+void Pipe::create()
+{
+ int fds[2];
+ if (pipe(fds) != 0) throw SysError("creating pipe");
+ readSide = fds[0];
+ writeSide = fds[1];
+}
+
AutoCloseDir::AutoCloseDir()
{
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index d9ca1dac2..dcd0bf766 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -181,6 +181,15 @@ public:
~AutoCloseFD();
void operator =(int fd);
operator int();
+ void close();
+ bool isOpen();
+};
+
+class Pipe
+{
+public:
+ AutoCloseFD readSide, writeSide;
+ void create();
};
class AutoCloseDir