MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_open.c
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 <my_dir.h>
19 #include <errno.h>
20 
21 
22 /*
23  Open a file
24 
25  SYNOPSIS
26  my_open()
27  FileName Fully qualified file name
28  Flags Read | write
29  MyFlags Special flags
30 
31  RETURN VALUE
32  File descriptor
33 */
34 
35 File my_open(const char *FileName, int Flags, myf MyFlags)
36  /* Path-name of file */
37  /* Read | write .. */
38  /* Special flags */
39 {
40  File fd;
41  DBUG_ENTER("my_open");
42  DBUG_PRINT("my",("Name: '%s' Flags: %d MyFlags: %d",
43  FileName, Flags, MyFlags));
44 #if defined(_WIN32)
45  fd= my_win_open(FileName, Flags);
46 #elif !defined(NO_OPEN_3)
47  fd = open(FileName, Flags, my_umask); /* Normal unix */
48 #else
49  fd = open((char *) FileName, Flags);
50 #endif
51 
52  fd= my_register_filename(fd, FileName, FILE_BY_OPEN, EE_FILENOTFOUND, MyFlags);
53  DBUG_RETURN(fd);
54 } /* my_open */
55 
56 
57 /*
58  Close a file
59 
60  SYNOPSIS
61  my_close()
62  fd File sescriptor
63  myf Special Flags
64 
65 */
66 
67 int my_close(File fd, myf MyFlags)
68 {
69  int err;
70  DBUG_ENTER("my_close");
71  DBUG_PRINT("my",("fd: %d MyFlags: %d",fd, MyFlags));
72 
73  mysql_mutex_lock(&THR_LOCK_open);
74 #ifndef _WIN32
75  do
76  {
77  err= close(fd);
78  } while (err == -1 && errno == EINTR);
79 #else
80  err= my_win_close(fd);
81 #endif
82  if (err)
83  {
84  DBUG_PRINT("error",("Got error %d on close",err));
85  my_errno=errno;
86  if (MyFlags & (MY_FAE | MY_WME))
87  {
88  char errbuf[MYSYS_STRERROR_SIZE];
89  my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG), my_filename(fd),
90  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
91  }
92  }
93  if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
94  {
95  my_free(my_file_info[fd].name);
96 #if !defined(HAVE_PREAD) && !defined(_WIN32)
97  mysql_mutex_destroy(&my_file_info[fd].mutex);
98 #endif
99  my_file_info[fd].type = UNOPEN;
100  }
101  my_file_opened--;
102  mysql_mutex_unlock(&THR_LOCK_open);
103  DBUG_RETURN(err);
104 } /* my_close */
105 
106 
107 /*
108  Register file in my_file_info[]
109 
110  SYNOPSIS
111  my_register_filename()
112  fd File number opened, -1 if error on open
113  FileName File name
114  type_file_type How file was created
115  error_message_number Error message number if caller got error (fd == -1)
116  MyFlags Flags for my_close()
117 
118  RETURN
119  -1 error
120  # Filenumber
121 
122 */
123 
124 File my_register_filename(File fd, const char *FileName, enum file_type
125  type_of_file, uint error_message_number, myf MyFlags)
126 {
127  DBUG_ENTER("my_register_filename");
128  if ((int) fd >= MY_FILE_MIN)
129  {
130  if ((uint) fd >= my_file_limit)
131  {
132 #if !defined(HAVE_PREAD)
133  my_errno= EMFILE;
134 #else
135  thread_safe_increment(my_file_opened,&THR_LOCK_open);
136  DBUG_RETURN(fd); /* safeguard */
137 #endif
138  }
139  else
140  {
141  mysql_mutex_lock(&THR_LOCK_open);
142  if ((my_file_info[fd].name = (char*) my_strdup(FileName,MyFlags)))
143  {
144  my_file_opened++;
145  my_file_total_opened++;
146  my_file_info[fd].type = type_of_file;
147 #if !defined(HAVE_PREAD) && !defined(_WIN32)
148  mysql_mutex_init(key_my_file_info_mutex, &my_file_info[fd].mutex,
149  MY_MUTEX_INIT_FAST);
150 #endif
151  mysql_mutex_unlock(&THR_LOCK_open);
152  DBUG_PRINT("exit",("fd: %d",fd));
153  DBUG_RETURN(fd);
154  }
155  mysql_mutex_unlock(&THR_LOCK_open);
156  my_errno= ENOMEM;
157  }
158  (void) my_close(fd, MyFlags);
159  }
160  else
161  my_errno= errno;
162 
163  DBUG_PRINT("error",("Got error %d on open", my_errno));
164  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
165  {
166  char errbuf[MYSYS_STRERROR_SIZE];
167  if (my_errno == EMFILE)
168  error_message_number= EE_OUT_OF_FILERESOURCES;
169  DBUG_PRINT("error",("print err: %d",error_message_number));
170  my_error(error_message_number, MYF(ME_BELL+ME_WAITTANG), FileName,
171  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
172  }
173  DBUG_RETURN(-1);
174 }
175 
176 
177 
178 
179 #ifdef EXTRA_DEBUG
180 
181 void my_print_open_files(void)
182 {
183  if (my_file_opened | my_stream_opened)
184  {
185  uint i;
186  for (i= 0 ; i < my_file_limit ; i++)
187  {
188  if (my_file_info[i].type != UNOPEN)
189  {
190  fprintf(stderr, EE(EE_FILE_NOT_CLOSED), my_file_info[i].name, i);
191  fputc('\n', stderr);
192  }
193  }
194  }
195 }
196 
197 #endif