MySQL 8.0.37
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 <string>
29#include <system_error>
30#include <utility> // exchange
31
32#ifndef _WIN32
33#include <sys/types.h> // ino_t
34#endif
35
39
40// (Partial) Implementation of P1883r1
41//
42// see: http://wg21.link/p1883
43//
44// barely enough implementation to replace all uses of
45//
46// - open()
47// - close()
48
49namespace stdx {
50namespace io {
51
52enum class creation {
53 open_existing = 0,
57 // always_new
58};
59
60enum class mode {
61 unchanged = 0,
62 read = 6,
63 write = 7,
64 append = 9,
65};
66
67enum class caching {
68 unchanged = 0,
69 none = 1,
70 all = 6,
71 temporary = 8,
72};
73
74class flag {
75 public:
76 using value_type = uint64_t;
77
78 static constexpr value_type none{0};
79 static constexpr value_type unlink_on_first_close{1 << 0};
80
81 constexpr flag(value_type v) : value_{v} {}
82
83 constexpr value_type value() const noexcept { return value_; }
84
85 constexpr value_type operator&(const value_type &other) {
86 return value() & other;
87 }
88
89 private:
91};
92
94 public:
95 path_handle() = default;
96};
97
99 public:
102
104#ifdef _WIN32
105 using dev_t = ::_dev_t;
106 using ino_t = ::_ino_t;
107#else
108 using dev_t = ::dev_t;
109 using ino_t = ::ino_t;
110#endif
111
112 static constexpr const native_handle_type invalid_handle{-1};
113
116 : handle_{h},
117 devid_{devid},
118 inode_{inode},
119 caching_{caching},
120 flags_{flags} {}
121
122 file_handle(const file_handle &) = delete;
124
126 : handle_{std::exchange(rhs.handle_, invalid_handle)},
127 devid_{std::move(rhs.devid_)},
128 inode_{std::move(rhs.inode_)},
129 caching_{std::move(rhs.caching_)},
130 flags_{std::move(rhs.flags_)} {}
131
133 if (handle_ != invalid_handle) {
134 close();
135 }
136 }
137
140 mode _mode = mode::read, creation _creation = creation::open_existing,
141 caching _caching = caching::all, flag flags = flag::none) noexcept;
142
143 static stdx::expected<file_handle, std::error_code> uniquely_named_file(
144 const path_handle &base, mode _mode = mode::write,
145 caching _caching = caching::temporary, flag flags = flag::none) noexcept;
146
148
150
151 stdx::expected<size_t, std::error_code> write(const char *data,
152 const size_t len);
153
154 native_handle_type release() noexcept {
155 return std::exchange(handle_, invalid_handle);
156 }
157
158 caching kernel_caching() const noexcept { return caching_; }
159 flag flags() const noexcept { return flags_; }
160
161 native_handle_type native_handle() const noexcept { return handle_; }
162
163 dev_t st_dev() const noexcept { return devid_; }
164 ino_t st_ino() const noexcept { return inode_; }
165
166 // get path of the current file_handle
168
169 private:
170 native_handle_type handle_{invalid_handle};
171
176};
177
178} // namespace io
179} // namespace stdx
180
181#endif
Definition: expected.h:944
Definition: filesystem.h:38
a type-safe flags type.
Definition: flags.h:115
Definition: file_handle.h:98
native_handle_type native_handle() const noexcept
Definition: file_handle.h:161
file_handle(const file_handle &)=delete
int native_handle_type
Definition: file_handle.h:103
::ino_t ino_t
Definition: file_handle.h:109
file_handle(file_handle &&rhs)
Definition: file_handle.h:125
caching caching_
Definition: file_handle.h:174
::dev_t dev_t
Definition: file_handle.h:108
ino_t st_ino() const noexcept
Definition: file_handle.h:164
dev_t devid_
Definition: file_handle.h:172
caching kernel_caching() const noexcept
Definition: file_handle.h:158
dev_t st_dev() const noexcept
Definition: file_handle.h:163
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:114
flag flags() const noexcept
Definition: file_handle.h:159
~file_handle()
Definition: file_handle.h:132
flag flags_
Definition: file_handle.h:175
ino_t inode_
Definition: file_handle.h:173
Definition: file_handle.h:74
value_type value_
Definition: file_handle.h:90
constexpr flag(value_type v)
Definition: file_handle.h:81
constexpr value_type operator&(const value_type &other)
Definition: file_handle.h:85
uint64_t value_type
Definition: file_handle.h:76
static constexpr value_type none
Definition: file_handle.h:78
static constexpr value_type unlink_on_first_close
Definition: file_handle.h:79
constexpr value_type value() const noexcept
Definition: file_handle.h:83
Definition: file_handle.h:93
static int flags[50]
Definition: hp_test1.cc:40
static char * path
Definition: mysqldump.cc:137
Definition: os0file.h:86
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:52
mode
Definition: file_handle.h:60
caching
Definition: file_handle.h:67
Definition: bit.h:34
#define HARNESS_STDX_EXPORT
Definition: stdx_export.h:15