* Refactoring.
This commit is contained in:
+30
-1
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user