1/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include "mysys_priv.h"
17#include "mysys_err.h"
18#include <m_string.h>
19#include <errno.h>
20
21CREATE_NOSYMLINK_FUNCTION(
22 open_nosymlinks(const char *pathname, int flags, int mode),
23 openat(dfd, filename, O_NOFOLLOW | flags, mode),
24 open(pathname, O_NOFOLLOW | flags, mode)
25);
26
27/*
28 Open a file
29
30 SYNOPSIS
31 my_open()
32 FileName Fully qualified file name
33 Flags Read | write
34 MyFlags Special flags
35
36 RETURN VALUE
37 File descriptor
38*/
39
40File my_open(const char *FileName, int Flags, myf MyFlags)
41 /* Path-name of file */
42 /* Read | write .. */
43 /* Special flags */
44{
45 File fd;
46 DBUG_ENTER("my_open");
47 DBUG_PRINT("my",("Name: '%s' Flags: %d MyFlags: %lu",
48 FileName, Flags, MyFlags));
49 if (!(MyFlags & (MY_WME | MY_FAE | MY_FFNF)))
50 MyFlags|= my_global_flags;
51#if defined(_WIN32)
52 fd= my_win_open(FileName, Flags);
53#else
54 if (MyFlags & MY_NOSYMLINKS)
55 fd = open_nosymlinks(FileName, Flags | O_CLOEXEC, my_umask);
56 else
57 fd = open(FileName, Flags | O_CLOEXEC, my_umask);
58#endif
59
60 fd= my_register_filename(fd, FileName, FILE_BY_OPEN,
61 EE_FILENOTFOUND, MyFlags);
62 DBUG_RETURN(fd);
63} /* my_open */
64
65
66/*
67 Close a file
68
69 SYNOPSIS
70 my_close()
71 fd File sescriptor
72 myf Special Flags
73
74*/
75
76int my_close(File fd, myf MyFlags)
77{
78 int err;
79 char *name= NULL;
80 DBUG_ENTER("my_close");
81 DBUG_PRINT("my",("fd: %d MyFlags: %lu",fd, MyFlags));
82 if (!(MyFlags & (MY_WME | MY_FAE)))
83 MyFlags|= my_global_flags;
84
85 if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
86 {
87 name= my_file_info[fd].name;
88 my_file_info[fd].name= NULL;
89 my_file_info[fd].type= UNOPEN;
90 }
91#ifndef _WIN32
92 do
93 {
94 err= close(fd);
95 } while (err == -1 && errno == EINTR);
96#else
97 err= my_win_close(fd);
98#endif
99 if (err)
100 {
101 DBUG_PRINT("error",("Got error %d on close",err));
102 my_errno=errno;
103 if (MyFlags & (MY_FAE | MY_WME))
104 my_error(EE_BADCLOSE, MYF(ME_BELL | ME_WAITTANG | (MyFlags & (ME_JUST_INFO | ME_NOREFRESH))),
105 name,errno);
106 }
107 if (name)
108 {
109 my_free(name);
110 }
111 statistic_decrement(my_file_opened, &THR_LOCK_open);
112 DBUG_RETURN(err);
113} /* my_close */
114
115
116/*
117 Register file in my_file_info[]
118
119 SYNOPSIS
120 my_register_filename()
121 fd File number opened, -1 if error on open
122 FileName File name
123 type_file_type How file was created
124 error_message_number Error message number if caller got error (fd == -1)
125 MyFlags Flags for my_close()
126
127 RETURN
128 -1 error
129 # Filenumber
130
131*/
132
133File my_register_filename(File fd, const char *FileName, enum file_type
134 type_of_file, uint error_message_number, myf MyFlags)
135{
136 DBUG_ENTER("my_register_filename");
137 if ((int) fd >= MY_FILE_MIN)
138 {
139 if ((uint) fd >= my_file_limit)
140 {
141 statistic_increment(my_file_opened,&THR_LOCK_open);
142 DBUG_RETURN(fd); /* safeguard */
143 }
144 my_file_info[fd].name = (char*) my_strdup(FileName, MyFlags);
145 statistic_increment(my_file_opened,&THR_LOCK_open);
146 statistic_increment(my_file_total_opened,&THR_LOCK_open);
147 my_file_info[fd].type = type_of_file;
148 DBUG_PRINT("exit",("fd: %d",fd));
149 DBUG_RETURN(fd);
150 }
151 my_errno= errno;
152
153 DBUG_PRINT("error",("Got error %d on open", my_errno));
154 if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
155 {
156 if (my_errno == EMFILE)
157 error_message_number= EE_OUT_OF_FILERESOURCES;
158 my_error(error_message_number,
159 MYF(ME_BELL | ME_WAITTANG | (MyFlags & (ME_JUST_INFO | ME_NOREFRESH))),
160 FileName, my_errno);
161 }
162 DBUG_RETURN(-1);
163}
164