2016-10-02 15:50:13 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2016-10-02 15:50:13 +11:00
|
|
|
*
|
|
|
|
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "fd.hpp"
|
2016-10-02 15:50:13 +11:00
|
|
|
|
2017-12-18 15:46:52 +11:00
|
|
|
#include "except.hpp"
|
2018-08-13 14:51:33 +10:00
|
|
|
#include "cast.hpp"
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::posix::fd;
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-05 19:12:03 +11:00
|
|
|
fd::fd (const std::filesystem::path &path, int flags):
|
2016-10-08 17:18:04 +11:00
|
|
|
fd (path, flags, 0666)
|
|
|
|
{ ; }
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-12-05 19:12:03 +11:00
|
|
|
fd::fd (const std::filesystem::path &path, int flags, mode_t mode):
|
2018-08-13 14:51:33 +10:00
|
|
|
m_fd (error::try_value (::open (path.u8string ().c_str (), flags, mode)))
|
2018-08-27 14:17:50 +10:00
|
|
|
{
|
|
|
|
// You always want binary mode. Always.
|
|
|
|
//
|
|
|
|
// But we want the user to have considered this platform issue regardless
|
|
|
|
// so we won't forcibly set the flag, but we will abort the program when
|
|
|
|
// debugging if they've failed to do so.
|
|
|
|
CHECK_EQ (flags & O_BINARY, O_BINARY);
|
|
|
|
}
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
|
2016-10-02 16:34:40 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-07-24 15:48:50 +10:00
|
|
|
fd::fd (fd &&rhs) noexcept:
|
2016-10-02 15:50:13 +11:00
|
|
|
m_fd (-1)
|
|
|
|
{
|
|
|
|
std::swap (m_fd, rhs.m_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-26 17:26:53 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fd&
|
2018-07-24 15:48:50 +10:00
|
|
|
fd::operator= (fd &&rhs) noexcept
|
2017-12-26 17:26:53 +11:00
|
|
|
{
|
|
|
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-10-02 16:13:31 +11:00
|
|
|
fd::fd (int _fd):
|
|
|
|
m_fd (_fd)
|
2016-10-02 16:34:40 +11:00
|
|
|
{ ; }
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
|
2016-10-02 16:13:31 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
fd
|
2018-08-22 21:32:43 +10:00
|
|
|
fd::dup (void) const
|
2016-10-02 16:13:31 +11:00
|
|
|
{
|
|
|
|
return dup (m_fd);
|
|
|
|
}
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-02 16:13:31 +11:00
|
|
|
fd
|
|
|
|
fd::dup (int _fd)
|
|
|
|
{
|
2017-12-18 15:57:43 +11:00
|
|
|
return fd {
|
|
|
|
error::try_value (::dup (_fd))
|
|
|
|
};
|
2016-10-02 16:13:31 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-10-02 15:50:13 +11:00
|
|
|
fd::~fd ()
|
|
|
|
{
|
|
|
|
if (m_fd < 0)
|
|
|
|
return;
|
2017-12-26 17:26:53 +11:00
|
|
|
close ();
|
2016-10-02 15:50:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
struct ::stat
|
|
|
|
fd::stat (void) const
|
|
|
|
{
|
|
|
|
struct stat buf;
|
2017-12-26 17:26:53 +11:00
|
|
|
error::try_value (fstat (m_fd, &buf));
|
2016-10-02 15:50:13 +11:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 20:58:19 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-12-26 17:26:53 +11:00
|
|
|
void
|
|
|
|
fd::close (void)
|
2016-10-11 20:58:19 +11:00
|
|
|
{
|
2017-12-26 17:25:58 +11:00
|
|
|
error::try_value (::close (m_fd));
|
|
|
|
m_fd = -1;
|
2016-10-11 20:58:19 +11:00
|
|
|
}
|
|
|
|
|
2017-12-19 18:12:01 +11:00
|
|
|
|
2016-10-11 20:58:19 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-10-11 20:58:19 +11:00
|
|
|
ssize_t
|
2018-08-13 14:51:33 +10:00
|
|
|
fd::read (void *buffer, std::size_t count)
|
2017-12-19 18:12:01 +11:00
|
|
|
{
|
2017-12-26 17:26:53 +11:00
|
|
|
return error::try_value (
|
2018-08-13 14:51:33 +10:00
|
|
|
::read (m_fd, buffer, cruft::cast::narrow<unsigned> (count))
|
2017-12-26 17:26:53 +11:00
|
|
|
);
|
2017-12-19 18:12:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
ssize_t
|
2016-10-11 20:58:19 +11:00
|
|
|
fd::write (const void *buffer, size_t count)
|
|
|
|
{
|
2017-12-18 15:57:43 +11:00
|
|
|
return error::try_value (
|
2018-08-13 14:51:33 +10:00
|
|
|
::write (m_fd, buffer, cruft::cast::narrow<unsigned> (count))
|
2017-12-18 15:57:43 +11:00
|
|
|
);
|
2016-10-11 20:58:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 20:58:31 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
off_t
|
|
|
|
fd::lseek (off_t offset, int whence)
|
|
|
|
{
|
2017-12-18 15:57:43 +11:00
|
|
|
return error::try_value (
|
|
|
|
::lseek (m_fd, offset, whence)
|
|
|
|
);
|
2016-10-11 20:58:31 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-02 15:50:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
fd::operator int (void) const
|
|
|
|
{
|
|
|
|
return m_fd;
|
|
|
|
}
|