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() noexcept
30 lttng::file_descriptor::file_descriptor(int raw_fd
) noexcept
: _raw_fd
{ raw_fd
}
32 LTTNG_ASSERT(is_valid_fd(_raw_fd
));
35 lttng::file_descriptor::file_descriptor(lttng::file_descriptor
&& other
) noexcept
37 std::swap(_raw_fd
, other
._raw_fd
);
40 lttng::file_descriptor
& lttng::file_descriptor::operator=(lttng::file_descriptor
&& other
) noexcept
43 std::swap(_raw_fd
, other
._raw_fd
);
47 lttng::file_descriptor::~file_descriptor() noexcept
52 int lttng::file_descriptor::fd() const noexcept
54 LTTNG_ASSERT(is_valid_fd(_raw_fd
));
58 void lttng::file_descriptor::_cleanup() noexcept
60 if (!is_valid_fd(_raw_fd
)) {
64 const auto ret
= ::close(_raw_fd
);
68 PERROR("Failed to close file descriptor: fd=%i", _raw_fd
);
72 void lttng::file_descriptor::write(const void *buffer
, std::size_t size
)
75 * This is a limitation of the internal helper that is not a problem in practice for the
78 using lttng_write_return_type
= decltype(lttng_write(
79 std::declval
<int>(), std::declval
<const void *>(), std::declval
<size_t>()));
80 constexpr auto max_supported_write_size
=
81 std::numeric_limits
<lttng_write_return_type
>::max();
83 if (size
> max_supported_write_size
) {
84 LTTNG_THROW_UNSUPPORTED_ERROR(fmt::format(
85 "Write size exceeds the maximal supported value of lttng_write: write_size={}, maximal_write_size={}",
87 max_supported_write_size
));
90 const auto write_ret
= lttng_write(fd(), buffer
, size
);
91 if (write_ret
< 0 || static_cast<std::size_t>(write_ret
) != size
) {
92 LTTNG_THROW_POSIX(fmt::format("Failed to write to file descriptor: fd={}", fd()),
97 void lttng::file_descriptor::read(void *buffer
, std::size_t size
)
100 * This is a limitation of the internal helper that is not a problem in practice for the
103 using lttng_read_return_type
= decltype(lttng_read(
104 std::declval
<int>(), std::declval
<void *>(), std::declval
<size_t>()));
105 constexpr auto max_supported_read_size
= std::numeric_limits
<lttng_read_return_type
>::max();
107 if (size
> max_supported_read_size
) {
108 LTTNG_THROW_UNSUPPORTED_ERROR(fmt::format(
109 "Read size exceeds the maximal supported value of lttng_read: read_size={}, maximal_read_size={}",
111 max_supported_read_size
));
114 const auto read_ret
= lttng_read(fd(), buffer
, size
);
115 if (read_ret
< 0 || static_cast<std::size_t>(read_ret
) != size
) {
116 LTTNG_THROW_POSIX(fmt::format("Failed to read from file descriptor: fd={}", fd()),
This page took 0.031897 seconds and 4 git commands to generate.