• R/O
  • SSH

posixpp: 提交

The main posixpp library and associated tests.


Commit MetaInfo

修订版c017ac4845af251ed83c143162cf99baf3fac13e (tree)
时间2021-02-17 11:51:25
作者Eric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Several small changes.

1. Use noexcept in more places.
2. Make close call return expected<void>
3. Add dup and dup2.
4. Drop lambda for int to fd coversion and use inline static instead.

更改概述

差异

diff -r 0bdb02fe5844 -r c017ac4845af pubincludes/posixpp/expected.h
--- a/pubincludes/posixpp/expected.h Tue Feb 16 15:07:26 2021 -0800
+++ b/pubincludes/posixpp/expected.h Tue Feb 16 18:51:25 2021 -0800
@@ -151,4 +151,18 @@
151151 }
152152 }
153153
154+//! Throw away return value, but cascade error upward.
155+template <typename T>
156+expected<void> error_cascade_void(expected<T> &&result)
157+{
158+ using outresult_t = expected<void>;
159+ using errtag = typename outresult_t::err_tag;
160+ if (result.has_error()) {
161+ return outresult_t{errtag{}, result.error()};
162+ } else {
163+ return outresult_t{};
164+ }
165+}
166+
167+
154168 } // namespace posixpp
diff -r 0bdb02fe5844 -r c017ac4845af pubincludes/posixpp/fd.h
--- a/pubincludes/posixpp/fd.h Tue Feb 16 15:07:26 2021 -0800
+++ b/pubincludes/posixpp/fd.h Tue Feb 16 18:51:25 2021 -0800
@@ -10,15 +10,23 @@
1010 //! A file descriptor and the associated functions
1111 class fd {
1212 public:
13- // Usually, you shouldn't use this.
13+ //! Avoid using this to create a file descriptor directly from an integer
1414 explicit constexpr fd(int fdval) noexcept : fd_(fdval) {}
15+
16+ //! \brief Will call `close` on a seemingly valid file descriptor and ignore
17+ //! the return value.
18+ //!
19+ //! If you care about the return value of `close`, use the `close` method of
20+ //! this class.
1521 ~fd() noexcept {
1622 using ::syscalls::linux::close;
1723 if (fd_ >= 0) {
1824 close(fd_); // Ignore any error return.
1925 }
2026 }
27+
2128 fd(fd &&other) noexcept : fd_(other.fd_) { other.fd_ = -1; }
29+
2230 fd &operator =(fd &&other) noexcept {
2331 using ::syscalls::linux::close;
2432 if (fd_ >= 0) {
@@ -31,6 +39,25 @@
3139
3240 [[nodiscard]] constexpr int as_fd() const noexcept { return fd_; }
3341
42+ [[nodiscard]] expected<fd> dup() noexcept {
43+ using ::syscalls::linux::dup;
44+ return error_cascade(dup(fd_), int_to_fd);
45+ }
46+
47+ [[nodiscard]] expected<void> dup2(fd &newfd) noexcept {
48+ using ::syscalls::linux::dup2;
49+ return error_cascade_void(dup2(fd_, newfd.as_fd()));
50+ }
51+
52+ [[nodiscard]] expected<void> close() noexcept {
53+ using ::syscalls::linux::close;
54+ auto const tmpfd = fd_;
55+ fd_ = -1;
56+ return close(tmpfd);
57+ }
58+
59+
60+
3461 ///@{
3562 [[nodiscard]] static expected<fd>
3663 open(char const *pathname, openflags flags) noexcept {
@@ -58,8 +85,9 @@
5885 {
5986 using ::syscalls::linux::openat;
6087 using posixpp::error_cascade;
61- return error_cascade(openat(dirfd.as_fd(), pathname, flags.getbits(), 0),
62- [](auto r) { return fd{static_cast<int>(r)}; });
88+ auto const dfd = dirfd.as_fd();
89+ return error_cascade(openat(dfd, pathname, flags.getbits(), 0),
90+ int_to_fd);
6391 }
6492 [[nodiscard]] static expected<fd>
6593 openat(fd const &dirfd, char const *pathname, fdflags flags) noexcept
@@ -77,7 +105,7 @@
77105 pathname,
78106 flags.getbits(),
79107 mode.getbits()),
80- [](auto r) { return fd{static_cast<int>(r)}; });
108+ int_to_fd);
81109 }
82110 [[nodiscard]] static expected<fd>
83111 openat(fd const &dirfd, char const *pathname,
@@ -101,6 +129,11 @@
101129 [](auto r) { return static_cast<::std::size_t>(r);});
102130 }
103131
132+ protected:
133+ static fd int_to_fd(int fdes) noexcept {
134+ return fd{fdes};
135+ }
136+
104137 private:
105138 int fd_;
106139 };
diff -r 0bdb02fe5844 -r c017ac4845af pubincludes/syscalls/linux/simple_io.h
--- a/pubincludes/syscalls/linux/simple_io.h Tue Feb 16 15:07:26 2021 -0800
+++ b/pubincludes/syscalls/linux/simple_io.h Tue Feb 16 18:51:25 2021 -0800
@@ -6,29 +6,39 @@
66 namespace syscalls {
77 namespace linux {
88
9-inline expected_t open(char const *path, int flags, int mode)
9+inline expected_t open(char const *path, int flags, int mode) noexcept
1010 {
1111 return syscall_expected(call_id::open, path, flags, mode);
1212 }
1313
14-inline expected_t openat(int fd, char const *path, int flags, int mode)
14+inline expected_t openat(int fd, char const *path, int flags, int mode) noexcept
1515 {
1616 return syscall_expected(call_id::openat, fd, path, flags, mode);
1717 }
1818
19-inline expected_t read(int fd, char *data, ::std::int64_t size)
19+inline expected_t read(int fd, char *data, ::std::int64_t size) noexcept
2020 {
2121 return syscall_expected(call_id::read, fd, data, size);
2222 }
2323
24-inline expected_t write(int fd, char const *data, ::std::int64_t size)
24+inline expected_t write(int fd, char const *data, ::std::int64_t size) noexcept
2525 {
2626 return syscall_expected(call_id::write, fd, data, size);
2727 }
2828
29-inline expected_t close(int fd)
29+inline ::posixpp::expected<void> close(int fd) noexcept
3030 {
31- return syscall_expected(call_id::close, fd);
31+ return error_cascade_void(syscall_expected(call_id::close, fd));
32+}
33+
34+inline expected_t dup(int oldfd) noexcept
35+{
36+ return syscall_expected(call_id::dup, oldfd);
37+}
38+
39+inline expected_t dup2(int oldfd, int newfd) noexcept
40+{
41+ return syscall_expected(call_id::dup2, oldfd, newfd);
3242 }
3343
3444 }
diff -r 0bdb02fe5844 -r c017ac4845af pubincludes/syscalls/linux/x86_64/syscall.h
--- a/pubincludes/syscalls/linux/x86_64/syscall.h Tue Feb 16 15:07:26 2021 -0800
+++ b/pubincludes/syscalls/linux/x86_64/syscall.h Tue Feb 16 18:51:25 2021 -0800
@@ -12,17 +12,19 @@
1212 using val_t = ::std::int64_t;
1313
1414 struct syscall_param {
15- syscall_param(val_t v) : value(v) { }
16- syscall_param(void *v) : value(reinterpret_cast<val_t>(v)) {
15+ syscall_param(val_t v) noexcept : value(v) { } // NOLINT
16+ // NOLINTNEXTLINE
17+ syscall_param(void *v) noexcept : value(reinterpret_cast<val_t>(v)) {
1718 static_assert(sizeof(void *) == sizeof(val_t));
1819 }
19- syscall_param(void const *v) : value(reinterpret_cast<val_t>(v)) {
20+ // NOLINTNEXTLINE
21+ syscall_param(void const *v) noexcept : value(reinterpret_cast<val_t>(v)) {
2022 static_assert(sizeof(void *) == sizeof(val_t));
2123 }
2224 val_t value;
2325 };
2426
25-inline val_t do_syscall(call_id callnum)
27+inline val_t do_syscall(call_id callnum) noexcept
2628 {
2729 val_t retval;
2830 asm volatile (
@@ -35,7 +37,7 @@
3537 }
3638
3739 inline val_t do_syscall(call_id callnum,
38- syscall_param const &p1)
40+ syscall_param const &p1) noexcept
3941 {
4042 val_t retval;
4143 asm volatile (
@@ -49,7 +51,7 @@
4951
5052 inline val_t do_syscall(call_id callnum,
5153 syscall_param const &p1,
52- syscall_param const &p2)
54+ syscall_param const &p2) noexcept
5355 {
5456 val_t retval;
5557 asm volatile (
@@ -64,7 +66,7 @@
6466 inline val_t do_syscall(call_id callnum,
6567 syscall_param const &p1,
6668 syscall_param const &p2,
67- syscall_param const &p3)
69+ syscall_param const &p3) noexcept
6870 {
6971 val_t retval;
7072 asm volatile (
@@ -80,7 +82,7 @@
8082 syscall_param const &p1,
8183 syscall_param const &p2,
8284 syscall_param const &p3,
83- syscall_param const &p4)
85+ syscall_param const &p4) noexcept
8486 {
8587 val_t retval;
8688 register val_t rp4 asm ("r10") = p4.value;
@@ -98,7 +100,7 @@
98100 syscall_param const &p2,
99101 syscall_param const &p3,
100102 syscall_param const &p4,
101- syscall_param const &p5)
103+ syscall_param const &p5) noexcept
102104 {
103105 val_t retval;
104106 register volatile val_t rp4 asm ("r10") = p4.value;
@@ -118,7 +120,7 @@
118120 syscall_param const &p3,
119121 syscall_param const &p4,
120122 syscall_param const &p5,
121- syscall_param const &p6)
123+ syscall_param const &p6) noexcept
122124 {
123125 val_t retval;
124126 register volatile val_t rp4 asm ("r10") = p4.value;
@@ -138,7 +140,7 @@
138140
139141 template <typename... T>
140142 expected_t
141-syscall_expected(call_id callnum, T &&... args)
143+syscall_expected(call_id callnum, T &&... args) noexcept
142144 {
143145 val_t result = do_syscall(callnum, ::std::forward<T>(args)...);
144146
Show on old repository browser