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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 
24 /**
25   @file
26 
27   @brief
28   Functions for discover of frm file from handler
29 */
30 
31 #include "sql_priv.h"
32 #include "unireg.h"
33 #include "discover.h"
34 #include <my_dir.h>
35 
36 /**
37   Read the contents of a .frm file.
38 
39   frmdata and len are set to 0 on error.
40 
41   @param name           path to table-file "db/name"
42   @param frmdata        frm data
43   @param len            length of the read frmdata
44 
45   @retval
46     0	ok
47   @retval
48     1	Could not open file
49   @retval
50     2    Could not stat file
51   @retval
52     3    Could not allocate data for read.  Could not read file
53 */
54 
readfrm(const char * name,uchar ** frmdata,size_t * len)55 int readfrm(const char *name, uchar **frmdata, size_t *len)
56 {
57   int    error;
58   char	 index_file[FN_REFLEN];
59   File	 file;
60   size_t read_len;
61   uchar *read_data;
62   MY_STAT state;
63   DBUG_ENTER("readfrm");
64   DBUG_PRINT("enter",("name: '%s'",name));
65 
66   *frmdata= NULL;      // In case of errors
67   *len= 0;
68   error= 1;
69   if ((file= mysql_file_open(key_file_frm,
70                              fn_format(index_file, name, "", reg_ext,
71                                MY_UNPACK_FILENAME|MY_APPEND_EXT),
72                              O_RDONLY | O_SHARE,
73                              MYF(0))) < 0)
74     goto err_end;
75 
76   // Get length of file
77   error= 2;
78   if (mysql_file_fstat(file, &state, MYF(0)))
79     goto err;
80   read_len= state.st_size;
81 
82   // Read whole frm file
83   error= 3;
84   read_data= 0;                                 // Nothing to free
85   if (read_string(file, &read_data, read_len))
86     goto err;
87 
88   // Setup return data
89   *frmdata= (uchar*) read_data;
90   *len= read_len;
91   error= 0;
92 
93  err:
94   if (file > 0)
95     (void) mysql_file_close(file, MYF(MY_WME));
96 
97  err_end:		      /* Here when no file */
98   DBUG_RETURN (error);
99 } /* readfrm */
100 
101 
102 /*
103   Write the content of a frm data pointer
104   to a frm file.
105 
106   @param name           path to table-file "db/name"
107   @param frmdata        frm data
108   @param len            length of the frmdata
109 
110   @retval
111     0	ok
112   @retval
113     2    Could not write file
114 */
115 
writefrm(const char * name,const uchar * frmdata,size_t len)116 int writefrm(const char *name, const uchar *frmdata, size_t len)
117 {
118   File file;
119   char	 index_file[FN_REFLEN];
120   int error;
121   DBUG_ENTER("writefrm");
122   DBUG_PRINT("enter",("name: '%s' len: %lu ",name, (ulong) len));
123 
124   error= 0;
125   if ((file= mysql_file_create(key_file_frm,
126                                fn_format(index_file, name, "", reg_ext,
127                                          MY_UNPACK_FILENAME | MY_APPEND_EXT),
128                                CREATE_MODE, O_RDWR | O_TRUNC,
129                                MYF(MY_WME))) >= 0)
130   {
131     if (mysql_file_write(file, frmdata, len, MYF(MY_WME | MY_NABP)))
132       error= 2;
133     (void) mysql_file_close(file, MYF(0));
134   }
135   DBUG_RETURN(error);
136 } /* writefrm */
137 
138 
139 
140 
141 
142