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