mirror of
https://github.com/ksherlock/mpw.git
synced 2026-04-19 18:16:29 +00:00
FDEntry::close
This commit is contained in:
+12
-18
@@ -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())
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user