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
|
|
|
*
|
2018-02-26 10:54:26 +11:00
|
|
|
* Copyright 2017-2018 Danny Robson <danny@nerdcruft.net>
|
2016-10-02 15:50:13 +11:00
|
|
|
*/
|
|
|
|
|
2018-02-26 10:54:26 +11:00
|
|
|
#ifndef CRUFT_UTIL_POSIX_FD_HPP
|
|
|
|
#define CRUFT_UTIL_POSIX_FD_HPP
|
2016-10-02 15:50:13 +11:00
|
|
|
|
2018-02-26 10:54:26 +11:00
|
|
|
#include "../debug.hpp"
|
2017-12-19 18:12:01 +11:00
|
|
|
#include "../view.hpp"
|
|
|
|
|
2018-08-31 13:57:03 +10:00
|
|
|
#include <experimental/filesystem>
|
|
|
|
|
2016-10-02 15:50:13 +11:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2018-08-31 13:57:03 +10:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#if !defined(O_BINARY)
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2016-10-08 17:18:04 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::posix {
|
2016-10-02 15:50:13 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// A simple RAII wrapper for file descriptors
|
|
|
|
class fd {
|
|
|
|
public:
|
2016-10-02 16:34:40 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2018-08-27 14:17:50 +10:00
|
|
|
// If you are using a system which delineates between binary/text mode
|
|
|
|
// for descriptors the behaviour is undefined if you don't set the
|
|
|
|
// necessary flags for binary mode.
|
2016-10-08 17:18:04 +11:00
|
|
|
fd (const std::experimental::filesystem::path &path, int flags);
|
|
|
|
fd (const std::experimental::filesystem::path &path, int flags, mode_t);
|
2016-10-02 15:50:13 +11:00
|
|
|
|
2018-07-24 15:48:50 +10:00
|
|
|
fd (fd &&) noexcept;
|
|
|
|
fd& operator= (fd &&) noexcept;
|
2017-12-26 17:26:53 +11:00
|
|
|
fd& operator= (int);
|
2016-10-02 16:34:40 +11:00
|
|
|
|
|
|
|
// The int constructor steals the fd. So don't pass in something that
|
|
|
|
// you don't want closed at destruct time. This should really only be
|
|
|
|
// used when interfacing with results of syscalls that we don't wrap.
|
2016-10-02 15:50:13 +11:00
|
|
|
explicit fd (int);
|
|
|
|
|
2016-10-02 16:34:40 +11:00
|
|
|
// copy constructors are removed in favour of explicit calls to dup.
|
|
|
|
// This should reduce unexpected or expensive copies as much as
|
|
|
|
// possible; one should not be doing this unless it is absolutely
|
|
|
|
// required.
|
2016-10-02 16:13:31 +11:00
|
|
|
fd (const fd&) = delete;
|
2018-08-22 21:32:43 +10:00
|
|
|
fd dup (void) const;
|
2016-10-02 16:13:31 +11:00
|
|
|
static fd dup (int);
|
|
|
|
|
2016-10-02 15:50:13 +11:00
|
|
|
~fd ();
|
|
|
|
|
2016-10-02 16:34:40 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2016-10-02 15:50:13 +11:00
|
|
|
struct ::stat stat (void) const;
|
|
|
|
|
2017-12-26 17:25:58 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
void close (void);
|
|
|
|
void reset (int);
|
|
|
|
void reset (void);
|
|
|
|
int release (void);
|
|
|
|
|
2016-10-11 20:58:19 +11:00
|
|
|
//---------------------------------------------------------------------
|
2018-02-01 13:47:42 +11:00
|
|
|
[[nodiscard]] ssize_t read (void *buf, size_t count);
|
|
|
|
[[nodiscard]] ssize_t write (const void *buf, size_t count);
|
2017-12-26 17:26:53 +11:00
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2018-01-31 19:30:48 +11:00
|
|
|
template <typename ValueT>
|
|
|
|
[[nodiscard]]
|
|
|
|
ValueT*
|
2018-08-05 14:42:02 +10:00
|
|
|
read (cruft::view<ValueT*> dst)
|
2017-12-26 17:26:53 +11:00
|
|
|
{
|
2018-01-31 19:30:48 +11:00
|
|
|
return dst.begin () + read (
|
|
|
|
&*std::data (dst), std::size (dst) * sizeof (ValueT)
|
|
|
|
);
|
2017-12-26 17:26:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2018-01-31 19:30:48 +11:00
|
|
|
template <typename ValueT>
|
|
|
|
[[nodiscard]]
|
|
|
|
auto
|
2018-08-05 14:42:02 +10:00
|
|
|
write (const cruft::view<ValueT*> &src)
|
2017-12-26 17:26:53 +11:00
|
|
|
{
|
2018-01-31 19:30:48 +11:00
|
|
|
return const_cast<ValueT*> (
|
|
|
|
write (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<const ValueT*> (src)
|
2018-01-31 19:30:48 +11:00
|
|
|
)
|
|
|
|
);
|
2017-12-26 17:26:53 +11:00
|
|
|
}
|
|
|
|
|
2016-10-11 20:58:19 +11:00
|
|
|
|
2016-10-11 20:58:31 +11:00
|
|
|
//---------------------------------------------------------------------
|
2018-01-31 19:30:48 +11:00
|
|
|
template <typename ValueT>
|
|
|
|
[[nodiscard]]
|
|
|
|
const ValueT*
|
2018-08-05 14:42:02 +10:00
|
|
|
write (const cruft::view<const ValueT*> &src)
|
2018-01-31 19:30:48 +11:00
|
|
|
{
|
2018-02-26 10:54:26 +11:00
|
|
|
const auto written = write (
|
2018-01-31 19:30:48 +11:00
|
|
|
&*std::data (src), std::size (src) * sizeof (ValueT)
|
|
|
|
);
|
2018-02-26 10:54:26 +11:00
|
|
|
|
|
|
|
CHECK_MOD (written, sizeof (ValueT));
|
|
|
|
return src.begin () + written / sizeof (ValueT);
|
2018-01-31 19:30:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
[[nodiscard]] off_t lseek (off_t offset, int whence);
|
2016-10-11 20:58:31 +11:00
|
|
|
|
2017-12-26 17:26:53 +11:00
|
|
|
|
2016-10-02 16:34:40 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2016-10-02 15:50:13 +11:00
|
|
|
operator int (void) const;
|
2018-02-01 13:47:14 +11:00
|
|
|
int native (void) const { return m_fd; }
|
2016-10-02 15:50:13 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fd;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|