MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
discover.cc
Go to the documentation of this file.
1 /* Copyright (c) 2004, 2010, 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 "discover.h"
27 #include <my_dir.h>
28 
48 int readfrm(const char *name, uchar **frmdata, size_t *len)
49 {
50  int error;
51  char index_file[FN_REFLEN];
52  File file;
53  size_t read_len;
54  uchar *read_data;
55  MY_STAT state;
56  DBUG_ENTER("readfrm");
57  DBUG_PRINT("enter",("name: '%s'",name));
58 
59  *frmdata= NULL; // In case of errors
60  *len= 0;
61  error= 1;
62  if ((file= mysql_file_open(key_file_frm,
63  fn_format(index_file, name, "", reg_ext,
64  MY_UNPACK_FILENAME|MY_APPEND_EXT),
65  O_RDONLY | O_SHARE,
66  MYF(0))) < 0)
67  goto err_end;
68 
69  // Get length of file
70  error= 2;
71  if (mysql_file_fstat(file, &state, MYF(0)))
72  goto err;
73  read_len= state.st_size;
74 
75  // Read whole frm file
76  error= 3;
77  read_data= 0; // Nothing to free
78  if (read_string(file, &read_data, read_len))
79  goto err;
80 
81  // Setup return data
82  *frmdata= (uchar*) read_data;
83  *len= read_len;
84  error= 0;
85 
86  err:
87  if (file > 0)
88  (void) mysql_file_close(file, MYF(MY_WME));
89 
90  err_end: /* Here when no file */
91  DBUG_RETURN (error);
92 } /* readfrm */
93 
94 
95 /*
96  Write the content of a frm data pointer
97  to a frm file.
98 
99  @param name path to table-file "db/name"
100  @param frmdata frm data
101  @param len length of the frmdata
102 
103  @retval
104  0 ok
105  @retval
106  2 Could not write file
107 */
108 
109 int writefrm(const char *name, const uchar *frmdata, size_t len)
110 {
111  File file;
112  char index_file[FN_REFLEN];
113  int error;
114  DBUG_ENTER("writefrm");
115  DBUG_PRINT("enter",("name: '%s' len: %lu ",name, (ulong) len));
116 
117  error= 0;
118  if ((file= mysql_file_create(key_file_frm,
119  fn_format(index_file, name, "", reg_ext,
120  MY_UNPACK_FILENAME | MY_APPEND_EXT),
121  CREATE_MODE, O_RDWR | O_TRUNC,
122  MYF(MY_WME))) >= 0)
123  {
124  if (mysql_file_write(file, frmdata, len, MYF(MY_WME | MY_NABP)))
125  error= 2;
126  (void) mysql_file_close(file, MYF(0));
127  }
128  DBUG_RETURN(error);
129 } /* writefrm */
130 
131 
132 
133 
134