~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2004 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @file
19
20
  @brief
21
  Functions for discover of frm file from handler
22
*/
23
24
#include "mysql_priv.h"
25
26
/**
27
  Read the contents of a .frm file.
28
29
  frmdata and len are set to 0 on error.
30
31
  @param name           path to table-file "db/name"
32
  @param frmdata        frm data
33
  @param len            length of the read frmdata
34
35
  @retval
36
    0	ok
37
  @retval
38
    1	Could not open file
39
  @retval
40
    2    Could not stat file
41
  @retval
42
    3    Could not allocate data for read.  Could not read file
43
*/
44
45
int readfrm(const char *name, uchar **frmdata, size_t *len)
46
{
47
  int    error;
48
  char	 index_file[FN_REFLEN];
49
  File	 file;
50
  size_t read_len;
51
  uchar *read_data;
15 by brian
Fix for stat, NETWARE removal
52
  struct stat state;  
1 by brian
clean slate
53
  
54
  *frmdata= NULL;      // In case of errors
55
  *len= 0;
56
  error= 1;
57
  if ((file=my_open(fn_format(index_file,name,"",reg_ext,
58
                              MY_UNPACK_FILENAME|MY_APPEND_EXT),
59
		    O_RDONLY | O_SHARE,
60
		    MYF(0))) < 0)  
61
    goto err_end; 
62
  
63
  // Get length of file
64
  error= 2;
15 by brian
Fix for stat, NETWARE removal
65
  if (fstat(file, &state))
1 by brian
clean slate
66
    goto err;
67
  read_len= state.st_size;  
68
69
  // Read whole frm file
70
  error= 3;
71
  read_data= 0;                                 // Nothing to free
72
  if (read_string(file, &read_data, read_len))
73
    goto err;
74
75
  // Setup return data
76
  *frmdata= (uchar*) read_data;
77
  *len= read_len;
78
  error= 0;
79
  
80
 err:
81
  if (file > 0)
82
    VOID(my_close(file,MYF(MY_WME)));
83
  
84
 err_end:		      /* Here when no file */
51.1.79 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols, and removed custom dbug funcs for decimal (these should go away anyway...)
85
  return(error);
1 by brian
clean slate
86
} /* readfrm */
87
88
89
/*
90
  Write the content of a frm data pointer 
91
  to a frm file.
92
93
  @param name           path to table-file "db/name"
94
  @param frmdata        frm data
95
  @param len            length of the frmdata
96
97
  @retval
98
    0	ok
99
  @retval
100
    2    Could not write file
101
*/
102
103
int writefrm(const char *name, const uchar *frmdata, size_t len)
104
{
105
  File file;
106
  char	 index_file[FN_REFLEN];
107
  int error;
108
109
  error= 0;
110
  if ((file=my_create(fn_format(index_file,name,"",reg_ext,
111
                      MY_UNPACK_FILENAME|MY_APPEND_EXT),
112
		      CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0)
113
  {
114
    if (my_write(file, frmdata, len,MYF(MY_WME | MY_NABP)))
115
      error= 2;
116
    VOID(my_close(file,MYF(0)));
117
  }
51.1.78 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
118
  return(error);
1 by brian
clean slate
119
} /* writefrm */
120
121
122
123
124