FDEntry::close

This commit is contained in:
Kelvin Sherlock
2013-02-25 20:40:28 -05:00
parent e4dff8bc81
commit 95f41785eb
3 changed files with 37 additions and 18 deletions
+12 -18
View File
@@ -51,25 +51,19 @@ namespace MPW
int fd = f.cookie;
int rv = OS::Internal::FDEntry::close(fd);
if (rv < 0)
{
f.error = OS::notOpenErr;
d0 = kEINVAL;
}
else
{
f.error = 0;
d0 = 0;
}
d0 = OS::Internal::FDEntry::action(fd,
// success callback.
[&f](int fd, OS::Internal::FDEntry &e)
{
f.error = 0;
if (--e.refcount == 0)
{
Log(" close(%02x)\n", fd);
::close(fd);
}
return 0;
},
// error callback.
[&f](int fd){
f.error = OS::notOpenErr;
return kEINVAL;
}
);
#if 0
if (fd < 0 || fd >= OS::Internal::FDTable.size())
+23
View File
@@ -48,6 +48,29 @@ namespace OS { namespace Internal {
}
int FDEntry::close(int fd, bool force)
{
if (fd < 0 || fd >= FDTable.size())
{
errno = EBADF;
return -1;
}
auto &e = FDTable[fd];
if (!e.refcount)
{
errno = EBADF;
return -1;
}
if (--e.refcount == 0 || force)
{
e.refcount = 0;
return ::close(fd);
}
return 0;
}
ssize_t FDEntry::read(int fd, void *buffer, size_t count)
{
if (fd < 0 || fd >= FDTable.size())
+2
View File
@@ -30,6 +30,8 @@ namespace OS { namespace Internal {
static ssize_t read(int fd, void *buffer, size_t count);
static ssize_t write(int fd, const void *buffer, size_t count);
static int close(int fd, bool force = false);
template<class F1, class F2>
static int32_t action(int fd, F1 good, F2 bad)