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