2 * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "file-descriptor.hpp"
10 #include <common/error.hpp>
11 #include <common/exception.hpp>
12 #include <common/format.hpp>
13 #include <common/readwrite.hpp>
20 bool is_valid_fd(int fd
)
24 } // anonymous namespace
26 lttng::file_descriptor::file_descriptor(int raw_fd
) noexcept
: _raw_fd
{ raw_fd
}
28 LTTNG_ASSERT(is_valid_fd(_raw_fd
));
31 lttng::file_descriptor::file_descriptor(lttng::file_descriptor
&& other
) noexcept
33 std::swap(_raw_fd
, other
._raw_fd
);
36 lttng::file_descriptor
& lttng::file_descriptor::operator=(lttng::file_descriptor
&& other
) noexcept
39 std::swap(_raw_fd
, other
._raw_fd
);
43 lttng::file_descriptor::~file_descriptor() noexcept
48 int lttng::file_descriptor::fd() const noexcept
50 LTTNG_ASSERT(is_valid_fd(_raw_fd
));
54 void lttng::file_descriptor::_cleanup() noexcept
56 if (!is_valid_fd(_raw_fd
)) {
60 const auto ret
= ::close(_raw_fd
);
64 PERROR("Failed to close file descriptor: fd=%i", _raw_fd
);
68 void lttng::file_descriptor::write(const void *buffer
, std::size_t size
)
71 * This is a limitation of the internal helper that is not a problem in practice for the
74 using lttng_write_return_type
= decltype(lttng_write(
75 std::declval
<int>(), std::declval
<const void *>(), std::declval
<size_t>()));
76 constexpr auto max_supported_write_size
=
77 std::numeric_limits
<lttng_write_return_type
>::max();
79 if (size
> max_supported_write_size
) {
80 LTTNG_THROW_UNSUPPORTED_ERROR(lttng::format(
81 "Write size exceeds the maximal supported value of lttng_write: write_size={}, maximal_write_size={}",
83 max_supported_write_size
));
86 const auto write_ret
= lttng_write(fd(), buffer
, size
);
87 if (write_ret
< 0 || static_cast<std::size_t>(write_ret
) != size
) {
88 LTTNG_THROW_POSIX(lttng::format("Failed to write to file descriptor: fd={}", fd()),
93 void lttng::file_descriptor::read(void *buffer
, std::size_t size
)
96 * This is a limitation of the internal helper that is not a problem in practice for the
99 using lttng_read_return_type
= decltype(lttng_read(
100 std::declval
<int>(), std::declval
<void *>(), std::declval
<size_t>()));
101 constexpr auto max_supported_read_size
= std::numeric_limits
<lttng_read_return_type
>::max();
103 if (size
> max_supported_read_size
) {
104 LTTNG_THROW_UNSUPPORTED_ERROR(lttng::format(
105 "Read size exceeds the maximal supported value of lttng_read: read_size={}, maximal_read_size={}",
107 max_supported_read_size
));
110 const auto read_ret
= lttng_read(fd(), buffer
, size
);
111 if (read_ret
< 0 || static_cast<std::size_t>(read_ret
) != size
) {
112 LTTNG_THROW_POSIX(lttng::format("Failed to read from file descriptor: fd={}", fd()),
This page took 0.032946 seconds and 4 git commands to generate.