/src/mysql-server/mysys/my_open.cc
Line | Count | Source |
1 | | /* Copyright (c) 2000, 2025, Oracle and/or its affiliates. |
2 | | |
3 | | This program is free software; you can redistribute it and/or modify |
4 | | it under the terms of the GNU General Public License, version 2.0, |
5 | | as published by the Free Software Foundation. |
6 | | |
7 | | This program is designed to work with certain software (including |
8 | | but not limited to OpenSSL) that is licensed under separate terms, |
9 | | as designated in a particular file or component or in included license |
10 | | documentation. The authors of MySQL hereby grant you an additional |
11 | | permission to link the program and your derivative works with the |
12 | | separately licensed software that they have either included with |
13 | | the program or referenced in the documentation. |
14 | | |
15 | | Without limiting anything contained in the foregoing, this file, |
16 | | which is part of C Driver for MySQL (Connector/C), is also subject to the |
17 | | Universal FOSS Exception, version 1.0, a copy of which can be found at |
18 | | http://oss.oracle.com/licenses/universal-foss-exception. |
19 | | |
20 | | This program is distributed in the hope that it will be useful, |
21 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | | GNU General Public License, version 2.0, for more details. |
24 | | |
25 | | You should have received a copy of the GNU General Public License |
26 | | along with this program; if not, write to the Free Software |
27 | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
28 | | |
29 | | /** |
30 | | @file mysys/my_open.cc |
31 | | */ |
32 | | |
33 | | #include "my_config.h" |
34 | | |
35 | | #include <fcntl.h> |
36 | | #include <cerrno> |
37 | | #include <string> |
38 | | #ifdef HAVE_UNISTD_H |
39 | | #include <unistd.h> |
40 | | #endif |
41 | | |
42 | | #include "my_dbug.h" |
43 | | #include "my_inttypes.h" |
44 | | #include "my_sys.h" |
45 | | #include "my_thread_local.h" |
46 | | #include "mysys/mysys_priv.h" |
47 | | #include "mysys_err.h" |
48 | | |
49 | | /** |
50 | | Open a file. |
51 | | |
52 | | @param filename Fully qualified file name |
53 | | @param Flags Read | write |
54 | | @param MyFlags Special flags |
55 | | |
56 | | @retval File descriptor if successful |
57 | | @retval -1 in case of errors |
58 | | */ |
59 | | |
60 | 0 | File my_open(const char *filename, int Flags, myf MyFlags) { |
61 | 0 | DBUG_TRACE; |
62 | |
|
63 | 0 | File fd = -1; |
64 | | #if defined(_WIN32) |
65 | | fd = my_win_open(filename, Flags); |
66 | | #else |
67 | 0 | fd = mysys_priv::RetryOnEintr( |
68 | 0 | [&]() { return open(filename, Flags, my_umask); }, -1); |
69 | 0 | #endif |
70 | |
|
71 | 0 | if (fd < 0) { |
72 | 0 | set_my_errno(errno); |
73 | 0 | DBUG_PRINT("error", ("Got error %d on open", my_errno())); |
74 | 0 | if (MyFlags & (MY_FAE | MY_WME)) { |
75 | 0 | MyOsError(my_errno(), EE_FILENOTFOUND, MYF(0), filename); |
76 | 0 | } |
77 | 0 | return fd; |
78 | 0 | } |
79 | 0 | RegisterFilename(fd, filename, file_info::OpenType::FILE_BY_OPEN); |
80 | 0 | return fd; |
81 | 0 | } |
82 | | |
83 | | /** |
84 | | Close a file. |
85 | | |
86 | | @param fd File sescriptor |
87 | | @param MyFlags Special Flags |
88 | | |
89 | | @retval 0 if successful |
90 | | @retval -1 in case of errors |
91 | | */ |
92 | | |
93 | 0 | int my_close(File fd, myf MyFlags) { |
94 | 0 | DBUG_TRACE; |
95 | | |
96 | | // Save the filename before unregistering, in case we need to report |
97 | | // error from close() |
98 | 0 | const std::string fname = my_filename(fd); |
99 | | |
100 | | // Need to remove file_info entry first to avoid race with another |
101 | | // thread reusing this fd after it has been closed. |
102 | 0 | file_info::UnregisterFilename(fd); |
103 | |
|
104 | 0 | int err = -1; |
105 | 0 | #ifndef _WIN32 |
106 | 0 | err = mysys_priv::RetryOnEintr([&fd]() { return close(fd); }, -1); |
107 | | #else |
108 | | err = my_win_close(fd); |
109 | | #endif |
110 | 0 | if (err == -1) { |
111 | 0 | DBUG_PRINT("error", ("Got error %d on close", err)); |
112 | 0 | set_my_errno(errno); |
113 | 0 | if (MyFlags & (MY_FAE | MY_WME)) { |
114 | 0 | MyOsError(my_errno(), EE_BADCLOSE, MYF(0), fname.c_str()); |
115 | 0 | } |
116 | 0 | } |
117 | 0 | return err; |
118 | 0 | } |