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