MySQL 8.3.0
Source Code Documentation
file_handle.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24#ifndef MYSQL_HARNESS_STDX_FILE_HANDLE_INCLUDED
25#define MYSQL_HARNESS_STDX_FILE_HANDLE_INCLUDED
26
27#include <string>
28#include <system_error>
29#include <utility> // exchange
30
31#ifndef _WIN32
32#include <sys/types.h> // ino_t
33#endif
34
38
39// (Partial) Implementation of P1883r1
40//
41// see: http://wg21.link/p1883
42//
43// barely enough implementation to replace all uses of
44//
45// - open()
46// - close()
47
48namespace stdx {
49namespace io {
50
51enum class creation {
52 open_existing = 0,
56 // always_new
57};
58
59enum class mode {
60 unchanged = 0,
61 read = 6,
62 write = 7,
63 append = 9,
64};
65
66enum class caching {
67 unchanged = 0,
68 none = 1,
69 all = 6,
70 temporary = 8,
71};
72
73class flag {
74 public:
75 using value_type = uint64_t;
76
77 static constexpr value_type none{0};
78 static constexpr value_type unlink_on_first_close{1 << 0};
79
80 constexpr flag(value_type v) : value_{v} {}
81
82 constexpr value_type value() const noexcept { return value_; }
83
84 constexpr value_type operator&(const value_type &other) {
85 return value() & other;
86 }
87
88 private:
90};
91
93 public:
94 path_handle() = default;
95};
96
98 public:
101
103#ifdef _WIN32
104 using dev_t = ::_dev_t;
105 using ino_t = ::_ino_t;
106#else
107 using dev_t = ::dev_t;
108 using ino_t = ::ino_t;
109#endif
110
111 static constexpr const native_handle_type invalid_handle{-1};
112
115 : handle_{h},
116 devid_{devid},
117 inode_{inode},
118 caching_{caching},
119 flags_{flags} {}
120
121 file_handle(const file_handle &) = delete;
123
125 : handle_{std::exchange(rhs.handle_, invalid_handle)},
126 devid_{std::move(rhs.devid_)},
127 inode_{std::move(rhs.inode_)},
128 caching_{std::move(rhs.caching_)},
129 flags_{std::move(rhs.flags_)} {}
130
132 if (handle_ != invalid_handle) {
133 close();
134 }
135 }
136
139 mode _mode = mode::read, creation _creation = creation::open_existing,
140 caching _caching = caching::all, flag flags = flag::none) noexcept;
141
142 static stdx::expected<file_handle, std::error_code> uniquely_named_file(
143 const path_handle &base, mode _mode = mode::write,
144 caching _caching = caching::temporary, flag flags = flag::none) noexcept;
145
147
149
150 stdx::expected<size_t, std::error_code> write(const char *data,
151 const size_t len);
152
153 native_handle_type release() noexcept {
154 return std::exchange(handle_, invalid_handle);
155 }
156
157 caching kernel_caching() const noexcept { return caching_; }
158 flag flags() const noexcept { return flags_; }
159
160 native_handle_type native_handle() const noexcept { return handle_; }
161
162 dev_t st_dev() const noexcept { return devid_; }
163 ino_t st_ino() const noexcept { return inode_; }
164
165 // get path of the current file_handle
167
168 private:
169 native_handle_type handle_{invalid_handle};
170
175};
176
177} // namespace io
178} // namespace stdx
179
180#endif
Definition: expected.h:943
Definition: filesystem.h:37
a type-safe flags type.
Definition: flags.h:114
Definition: file_handle.h:97
native_handle_type native_handle() const noexcept
Definition: file_handle.h:160
file_handle(const file_handle &)=delete
int native_handle_type
Definition: file_handle.h:102
::ino_t ino_t
Definition: file_handle.h:108
file_handle(file_handle &&rhs)
Definition: file_handle.h:124
caching caching_
Definition: file_handle.h:173
::dev_t dev_t
Definition: file_handle.h:107
ino_t st_ino() const noexcept
Definition: file_handle.h:163
dev_t devid_
Definition: file_handle.h:171
caching kernel_caching() const noexcept
Definition: file_handle.h:157
dev_t st_dev() const noexcept
Definition: file_handle.h:162
file_handle & operator=(const file_handle &)=delete
file_handle(native_handle_type h, dev_t devid, ino_t inode, caching caching=caching::none, flag flags=flag::none) noexcept
Definition: file_handle.h:113
flag flags() const noexcept
Definition: file_handle.h:158
~file_handle()
Definition: file_handle.h:131
flag flags_
Definition: file_handle.h:174
ino_t inode_
Definition: file_handle.h:172
Definition: file_handle.h:73
value_type value_
Definition: file_handle.h:89
constexpr flag(value_type v)
Definition: file_handle.h:80
constexpr value_type operator&(const value_type &other)
Definition: file_handle.h:84
uint64_t value_type
Definition: file_handle.h:75
static constexpr value_type none
Definition: file_handle.h:77
static constexpr value_type unlink_on_first_close
Definition: file_handle.h:78
constexpr value_type value() const noexcept
Definition: file_handle.h:82
Definition: file_handle.h:92
static int flags[50]
Definition: hp_test1.cc:39
static char * path
Definition: mysqldump.cc:148
Definition: os0file.h:88
stdx::expected< void, std::error_code > close(file_handle_type native_handle)
close file handle.
Definition: file.h:238
Definition: varlen_sort.h:174
stdx::expected< void, std::error_code > unlink(const char *path_name)
Definition: filesystem.cc:61
HARNESS_STDX_EXPORT path current_path()
get current path.
Definition: filesystem.cc:138
creation
Definition: file_handle.h:51
mode
Definition: file_handle.h:59
caching
Definition: file_handle.h:66
Definition: bit.h:33
#define HARNESS_STDX_EXPORT
Definition: stdx_export.h:15