MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
derror.cc
Go to the documentation of this file.
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 Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 
24 #include "sql_priv.h"
25 #include "unireg.h"
26 #include "derror.h"
27 #include "mysys_err.h"
28 #include "mysqld.h" // lc_messages_dir
29 #include "derror.h" // read_texts
30 #include "sql_class.h" // THD
31 
32 static void init_myfunc_errs(void);
33 
34 
35 C_MODE_START
36 static const char **get_server_errmsgs()
37 {
38  if (!current_thd)
39  return DEFAULT_ERRMSGS;
40  return CURRENT_THD_ERRMSGS;
41 }
42 C_MODE_END
43 
58 bool init_errmessage(void)
59 {
60  const char **errmsgs, **ptr;
61  DBUG_ENTER("init_errmessage");
62 
63  /*
64  Get a pointer to the old error messages pointer array.
65  read_texts() tries to free it.
66  */
67  errmsgs= my_error_unregister(ER_ERROR_FIRST, ER_ERROR_LAST);
68 
69  /* Read messages from file. */
70  if (read_texts(ERRMSG_FILE, my_default_lc_messages->errmsgs->language,
71  &errmsgs, ER_ERROR_LAST - ER_ERROR_FIRST + 1) &&
72  !errmsgs)
73  {
74  if (!(errmsgs= (const char**) my_malloc((ER_ERROR_LAST-ER_ERROR_FIRST+1)*
75  sizeof(char*), MYF(0))))
76  DBUG_RETURN(TRUE);
77  for (ptr= errmsgs; ptr < errmsgs + ER_ERROR_LAST - ER_ERROR_FIRST; ptr++)
78  *ptr= "";
79  }
80 
81  /* Register messages for use with my_error(). */
82  if (my_error_register(get_server_errmsgs, ER_ERROR_FIRST, ER_ERROR_LAST))
83  {
84  my_free(errmsgs);
85  DBUG_RETURN(TRUE);
86  }
87 
88  DEFAULT_ERRMSGS= errmsgs; /* Init global variable */
89  init_myfunc_errs(); /* Init myfunc messages */
90  DBUG_RETURN(FALSE);
91 }
92 
93 
100 bool read_texts(const char *file_name, const char *language,
101  const char ***point, uint error_messages)
102 {
103  register uint i;
104  uint count,funktpos,textcount;
105  size_t length;
106  File file;
107  char name[FN_REFLEN];
108  char lang_path[FN_REFLEN];
109  uchar *buff;
110  uchar head[32],*pos;
111  DBUG_ENTER("read_texts");
112 
113  LINT_INIT(buff);
114  funktpos=0;
115  convert_dirname(lang_path, language, NullS);
116  (void) my_load_path(lang_path, lang_path, lc_messages_dir);
117  if ((file= mysql_file_open(key_file_ERRMSG,
118  fn_format(name, file_name, lang_path, "", 4),
119  O_RDONLY | O_SHARE | O_BINARY,
120  MYF(0))) < 0)
121  {
122  /*
123  Trying pre-5.4 sematics of the --language parameter.
124  It included the language-specific part, e.g.:
125 
126  --language=/path/to/english/
127  */
128  if ((file= mysql_file_open(key_file_ERRMSG,
129  fn_format(name, file_name, lc_messages_dir, "", 4),
130  O_RDONLY | O_SHARE | O_BINARY,
131  MYF(0))) < 0)
132  goto err;
133 
134  sql_print_warning("Using pre 5.5 semantics to load error messages from %s.",
135  lc_messages_dir);
136 
137  sql_print_warning("If this is not intended, refer to the documentation for "
138  "valid usage of --lc-messages-dir and --language "
139  "parameters.");
140  }
141 
142  funktpos=1;
143  if (mysql_file_read(file, (uchar*) head, 32, MYF(MY_NABP)))
144  goto err;
145  if (head[0] != (uchar) 254 || head[1] != (uchar) 254 ||
146  head[2] != 3 || head[3] != 1)
147  goto err; /* purecov: inspected */
148  textcount=head[4];
149 
150  error_message_charset_info= system_charset_info;
151  length=uint4korr(head+6); count=uint4korr(head+10);
152 
153  if (count < error_messages)
154  {
155  sql_print_error("\
156 Error message file '%s' had only %d error messages,\n\
157 but it should contain at least %d error messages.\n\
158 Check that the above file is the right version for this program!",
159  name,count,error_messages);
160  (void) mysql_file_close(file, MYF(MY_WME));
161  DBUG_RETURN(1);
162  }
163 
164  /* Free old language */
165  my_free(*point);
166  if (!(*point= (const char**)
167  my_malloc((size_t) (length+count*sizeof(char*)),MYF(0))))
168  {
169  funktpos=2; /* purecov: inspected */
170  goto err; /* purecov: inspected */
171  }
172  buff= (uchar*) (*point + count);
173 
174  if (mysql_file_read(file, buff, (size_t) count*4, MYF(MY_NABP)))
175  goto err;
176  for (i=0, pos= buff ; i< count ; i++)
177  {
178  (*point)[i]= (char*) buff+uint4korr(pos);
179  pos+=4;
180  }
181  if (mysql_file_read(file, buff, length, MYF(MY_NABP)))
182  goto err;
183 
184  for (i=1 ; i < textcount ; i++)
185  {
186  point[i]= *point +uint2korr(head+10+i+i);
187  }
188  (void) mysql_file_close(file, MYF(0));
189  DBUG_RETURN(0);
190 
191 err:
192  sql_print_error((funktpos == 2) ? "Not enough memory for messagefile '%s'" :
193  ((funktpos == 1) ? "Can't read from messagefile '%s'" :
194  "Can't find messagefile '%s'"), name);
195  if (file != FERR)
196  (void) mysql_file_close(file, MYF(MY_WME));
197  DBUG_RETURN(1);
198 } /* read_texts */
199 
200 
205 static void init_myfunc_errs()
206 {
207  init_glob_errs(); /* Initiate english errors */
208  if (!(specialflag & SPECIAL_ENGLISH))
209  {
210  EE(EE_FILENOTFOUND) = ER(ER_FILE_NOT_FOUND);
211  EE(EE_CANTCREATEFILE) = ER(ER_CANT_CREATE_FILE);
212  EE(EE_READ) = ER(ER_ERROR_ON_READ);
213  EE(EE_WRITE) = ER(ER_ERROR_ON_WRITE);
214  EE(EE_BADCLOSE) = ER(ER_ERROR_ON_CLOSE);
215  EE(EE_OUTOFMEMORY) = ER(ER_OUTOFMEMORY);
216  EE(EE_DELETE) = ER(ER_CANT_DELETE_FILE);
217  EE(EE_LINK) = ER(ER_ERROR_ON_RENAME);
218  EE(EE_EOFERR) = ER(ER_UNEXPECTED_EOF);
219  EE(EE_CANTLOCK) = ER(ER_CANT_LOCK);
220  EE(EE_DIR) = ER(ER_CANT_READ_DIR);
221  EE(EE_STAT) = ER(ER_CANT_GET_STAT);
222  EE(EE_GETWD) = ER(ER_CANT_GET_WD);
223  EE(EE_SETWD) = ER(ER_CANT_SET_WD);
224  EE(EE_DISK_FULL) = ER(ER_DISK_FULL);
225  }
226 }