1 /*
2    Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
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, version 2.0, 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 MY_IO_INCLUDED
25 #define MY_IO_INCLUDED 1
26 
27 /**
28   @file include/my_io.h
29   Common \#defines and includes for file and socket I/O.
30 */
31 
32 #include <mysql/components/services/my_io_bits.h>
33 
34 #ifdef _WIN32
35 
36 /* Define missing access() modes. */
37 #define F_OK 0
38 #define W_OK 2
39 #define R_OK 4 /* Test for read permission.  */
40 
41 /* Define missing file locking constants. */
42 #define F_RDLCK 1
43 #define F_WRLCK 2
44 #define F_UNLCK 3
45 
46 #define O_NONBLOCK 1 /* For emulation of fcntl() */
47 
48 /*
49   SHUT_RDWR is called SD_BOTH in windows and
50   is defined to 2 in winsock2.h
51   #define SD_BOTH 0x02
52 */
53 #define SHUT_RDWR 0x02
54 
55 #endif  // _WIN32
56 
57 /* file create flags */
58 
59 #ifdef _WIN32
60 /* Only for my_fopen() - _O_BINARY is set by default for my_open() */
61 #define MY_FOPEN_BINARY _O_BINARY
62 #else
63 #define MY_FOPEN_BINARY 0 /* Ignore on non-Windows */
64 #endif
65 
66 #ifdef _WIN32
67 #define O_NOFOLLOW 0 /* Ignore on Windows */
68 #endif
69 
70 /* additional file share flags for win32 */
71 #ifdef _WIN32
72 #define _SH_DENYRWD 0x110 /* deny read/write mode & delete */
73 #define _SH_DENYWRD 0x120 /* deny write mode & delete      */
74 #define _SH_DENYRDD 0x130 /* deny read mode & delete       */
75 #define _SH_DENYDEL 0x140 /* deny delete only              */
76 #endif                    /* _WIN32 */
77 
78 /* General constants */
79 #define FN_LEN 256        /* Max file name len */
80 #define FN_HEADLEN 253    /* Max length of filepart of file name */
81 #define FN_EXTLEN 20      /* Max length of extension (part of FN_LEN) */
82 #define FN_REFLEN 512     /* Max length of full path-name */
83 #define FN_REFLEN_SE 4000 /* Max length of full path-name in SE */
84 #define FN_EXTCHAR '.'
85 #define FN_HOMELIB '~'    /* ~/ is used as abbrev for home dir */
86 #define FN_CURLIB '.'     /* ./ is used as abbrev for current dir */
87 #define FN_PARENTDIR ".." /* Parent directory; Must be a string */
88 
89 #ifdef _WIN32
90 #define FN_LIBCHAR '\\'
91 #define FN_LIBCHAR2 '/'
92 #define FN_DIRSEP "/\\" /* Valid directory separators */
93 #define FN_EXEEXT ".exe"
94 #define FN_SOEXT ".dll"
95 #define FN_ROOTDIR "\\"
96 #define FN_DEVCHAR ':'
97 #define FN_NETWORK_DRIVES /* Uses \\ to indicate network drives */
98 #else
99 #define FN_LIBCHAR '/'
100 /*
101   FN_LIBCHAR2 is not defined on !Windows. Use is_directory_separator().
102 */
103 #define FN_DIRSEP "/" /* Valid directory separators */
104 #define FN_EXEEXT ""
105 #define FN_SOEXT ".so"
106 #define FN_ROOTDIR "/"
107 #endif
108 
is_directory_separator(char c)109 static inline int is_directory_separator(char c) {
110 #ifdef _WIN32
111   return c == FN_LIBCHAR || c == FN_LIBCHAR2;
112 #else
113   return c == FN_LIBCHAR;
114 #endif
115 }
116 
117 /*
118   MY_FILE_MIN is  Windows speciality and is used to quickly detect
119   the mismatch of CRT and mysys file IO usage on Windows at runtime.
120   CRT file descriptors can be in the range 0-2047, whereas descriptors
121   returned by my_open() will start with 2048. If a file descriptor with value
122   less then MY_FILE_MIN is passed to mysys IO function, chances are it stemms
123   from open()/fileno() and not my_open()/my_fileno.
124 
125   For Posix,  mysys functions are light wrappers around libc, and MY_FILE_MIN
126   is logically 0.
127 */
128 
129 #ifdef _WIN32
130 #define MY_FILE_MIN 2048
131 #else
132 #define MY_FILE_MIN 0
133 #endif
134 
135 /*
136   MY_NFILE is the default size of my_file_info array.
137 
138   It is larger on Windows, because it all file handles are stored in
139   my_file_info Default size is 16384 and this should be enough for most cases.If
140   it is not enough, --max-open-files with larger value can be used.
141 
142   For Posix , my_file_info array is only used to store filenames for
143   error reporting and its size is not a limitation for number of open files.
144 */
145 #ifdef _WIN32
146 #define MY_NFILE (16384 + MY_FILE_MIN)
147 #else
148 #define MY_NFILE 64
149 #endif
150 
151 #define OS_FILE_LIMIT UINT_MAX
152 
153 /*
154   Io buffer size; Must be a power of 2 and a multiple of 512. May be
155   smaller what the disk page size. This influences the speed of the
156   isam btree library. eg to big to slow.
157 */
158 #define IO_SIZE 4096
159 
160 #if defined(_WIN32)
161 #define socket_errno WSAGetLastError()
162 #define SOCKET_EINTR WSAEINTR
163 #define SOCKET_EAGAIN WSAEINPROGRESS
164 #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
165 #define SOCKET_EADDRINUSE WSAEADDRINUSE
166 #define SOCKET_ETIMEDOUT WSAETIMEDOUT
167 #define SOCKET_ECONNRESET WSAECONNRESET
168 #define SOCKET_ENFILE ENFILE
169 #define SOCKET_EMFILE EMFILE
170 #else /* Unix */
171 #define socket_errno errno
172 #define closesocket(A) close(A)
173 #define SOCKET_EINTR EINTR
174 #define SOCKET_EAGAIN EAGAIN
175 #define SOCKET_EWOULDBLOCK EWOULDBLOCK
176 #define SOCKET_EADDRINUSE EADDRINUSE
177 #define SOCKET_ETIMEDOUT ETIMEDOUT
178 #define SOCKET_ECONNRESET ECONNRESET
179 #define SOCKET_ENFILE ENFILE
180 #define SOCKET_EMFILE EMFILE
181 #endif
182 
183 #ifndef _WIN32
184 #define INVALID_SOCKET -1
185 #endif /* _WIN32 */
186 
187 /* File permissions */
188 #define USER_READ (1L << 0)
189 #define USER_WRITE (1L << 1)
190 #define USER_EXECUTE (1L << 2)
191 #define GROUP_READ (1L << 3)
192 #define GROUP_WRITE (1L << 4)
193 #define GROUP_EXECUTE (1L << 5)
194 #define OTHERS_READ (1L << 6)
195 #define OTHERS_WRITE (1L << 7)
196 #define OTHERS_EXECUTE (1L << 8)
197 #define USER_RWX USER_READ | USER_WRITE | USER_EXECUTE
198 #define GROUP_RWX GROUP_READ | GROUP_WRITE | GROUP_EXECUTE
199 #define OTHERS_RWX OTHERS_READ | OTHERS_WRITE | OTHERS_EXECUTE
200 
201 #endif  // MY_IO_INCLUDED
202