1 /* Copyright (c) 2004, 2015, 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 "discover.h"
32 #include "mysqld.h"
33 #include "table.h"
34 #include <my_dir.h>
35 #include "my_sys.h"
36 
37 #include "pfs_file_provider.h"
38 #include "mysql/psi/mysql_file.h"
39 
40 /**
41   Read the contents of a .frm file.
42 
43   frmdata and len are set to 0 on error.
44 
45   @param name           path to table-file "db/name"
46   @param frmdata        frm data
47   @param len            length of the read frmdata
48 
49   @retval
50     0	ok
51   @retval
52     1	Could not open file
53   @retval
54     2    Could not stat file
55   @retval
56     3    Could not allocate data for read.  Could not read file
57 */
58 
readfrm(const char * name,uchar ** frmdata,size_t * len)59 int readfrm(const char *name, uchar **frmdata, size_t *len)
60 {
61   int    error;
62   char	 index_file[FN_REFLEN];
63   File	 file;
64   size_t read_len;
65   uchar *read_data;
66   MY_STAT state;
67   DBUG_ENTER("readfrm");
68   DBUG_PRINT("enter",("name: '%s'",name));
69 
70   *frmdata= NULL;      // In case of errors
71   *len= 0;
72   error= 1;
73   if ((file= mysql_file_open(key_file_frm,
74                              fn_format(index_file, name, "", reg_ext,
75                                MY_UNPACK_FILENAME|MY_APPEND_EXT),
76                              O_RDONLY | O_SHARE,
77                              MYF(0))) < 0)
78     goto err_end;
79 
80   // Get length of file
81   error= 2;
82   if (mysql_file_fstat(file, &state, MYF(0)))
83     goto err;
84   read_len= static_cast<size_t>(state.st_size);
85 
86   // Read whole frm file
87   error= 3;
88   read_data= 0;                                 // Nothing to free
89   if (read_string(file, &read_data, read_len))
90     goto err;
91 
92   // Setup return data
93   *frmdata= read_data;
94   *len= read_len;
95   error= 0;
96 
97  err:
98   if (file > 0)
99     (void) mysql_file_close(file, MYF(MY_WME));
100 
101  err_end:		      /* Here when no file */
102   DBUG_RETURN (error);
103 } /* readfrm */
104 
105 
106 /*
107   Write the content of a frm data pointer
108   to a frm file.
109 
110   @param name           path to table-file "db/name"
111   @param frmdata        frm data
112   @param len            length of the frmdata
113 
114   @retval
115     0	ok
116   @retval
117     2    Could not write file
118 */
119 
writefrm(const char * name,const uchar * frmdata,size_t len)120 int writefrm(const char *name, const uchar *frmdata, size_t len)
121 {
122   File file;
123   char	 index_file[FN_REFLEN];
124   int error;
125   DBUG_ENTER("writefrm");
126   DBUG_PRINT("enter",("name: '%s' len: %lu ",name, (ulong) len));
127 
128   error= 0;
129   if ((file= mysql_file_create(key_file_frm,
130                                fn_format(index_file, name, "", reg_ext,
131                                          MY_UNPACK_FILENAME | MY_APPEND_EXT),
132                                CREATE_MODE, O_RDWR | O_TRUNC,
133                                MYF(MY_WME))) >= 0)
134   {
135     if (mysql_file_write(file, frmdata, len, MYF(MY_WME | MY_NABP)))
136       error= 2;
137     (void) mysql_file_close(file, MYF(0));
138   }
139   DBUG_RETURN(error);
140 } /* writefrm */
141 
142 
143 
144 
145 
146