libcruft-util/posix/fd.cpp

185 lines
3.7 KiB
C++
Raw Normal View History

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#include "fd.hpp"
#include "except.hpp"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
using util::posix::fd;
///////////////////////////////////////////////////////////////////////////////
fd::fd (const std::experimental::filesystem::path &path, int flags):
fd (path, flags, 0666)
{ ; }
//-----------------------------------------------------------------------------
fd::fd (const std::experimental::filesystem::path &path, int flags, mode_t mode):
m_fd (error::try_value (::open (path.c_str (), flags, mode)))
{ ; }
2016-10-02 16:34:40 +11:00
///////////////////////////////////////////////////////////////////////////////
fd::fd (fd &&rhs):
m_fd (-1)
{
std::swap (m_fd, rhs.m_fd);
}
2017-12-26 17:26:53 +11:00
//-----------------------------------------------------------------------------
fd&
fd::operator= (fd &&rhs)
{
close ();
std::swap (m_fd, rhs.m_fd);
return *this;
}
//-----------------------------------------------------------------------------
fd&
fd::operator= (int rhs)
{
reset (rhs);
return *this;
}
2016-10-02 16:34:40 +11:00
///////////////////////////////////////////////////////////////////////////////
fd::fd (int _fd):
m_fd (_fd)
2016-10-02 16:34:40 +11:00
{ ; }
///////////////////////////////////////////////////////////////////////////////
fd
fd::dup (void)
{
return dup (m_fd);
}
//-----------------------------------------------------------------------------
fd
fd::dup (int _fd)
{
return fd {
error::try_value (::dup (_fd))
};
}
///////////////////////////////////////////////////////////////////////////////
fd::~fd ()
{
if (m_fd < 0)
return;
2017-12-26 17:26:53 +11:00
close ();
}
///////////////////////////////////////////////////////////////////////////////
struct ::stat
fd::stat (void) const
{
struct stat buf;
2017-12-26 17:26:53 +11:00
error::try_value (fstat (m_fd, &buf));
return buf;
}
///////////////////////////////////////////////////////////////////////////////
2017-12-26 17:26:53 +11:00
void
fd::close (void)
{
2017-12-26 17:25:58 +11:00
error::try_value (::close (m_fd));
m_fd = -1;
}
//-----------------------------------------------------------------------------
2017-12-26 17:25:58 +11:00
void
fd::reset (void)
{
if (m_fd >= 0) {
close ();
m_fd = -1;
}
}
//-----------------------------------------------------------------------------
void
fd::reset (int rhs)
{
if (m_fd >= 0)
close ();
m_fd = rhs;
}
//-----------------------------------------------------------------------------
int
fd::release (void)
{
int tmp = m_fd;
m_fd = -1;
return tmp;
}
///////////////////////////////////////////////////////////////////////////////
ssize_t
2017-12-26 17:26:53 +11:00
fd::read (void *buffer, size_t count)
{
2017-12-26 17:26:53 +11:00
return error::try_value (
::read (m_fd, buffer, count)
);
}
///////////////////////////////////////////////////////////////////////////////
ssize_t
fd::write (const void *buffer, size_t count)
{
return error::try_value (
::write (m_fd, buffer, count)
);
}
2016-10-11 20:58:31 +11:00
///////////////////////////////////////////////////////////////////////////////
off_t
fd::lseek (off_t offset, int whence)
{
return error::try_value (
::lseek (m_fd, offset, whence)
);
2016-10-11 20:58:31 +11:00
}
///////////////////////////////////////////////////////////////////////////////
fd::operator int (void) const
{
return m_fd;
}