~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/discover.cc

Merge of Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
*/
22
 
#include <drizzled/server_includes.h>
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
 
*/
42
 
int readfrm(const char *name, uchar **frmdata, size_t *len)
43
 
{
44
 
  int    error;
45
 
  char   index_file[FN_REFLEN];
46
 
  File   file;
47
 
  size_t read_len;
48
 
  uchar *read_data;
49
 
  struct stat state;  
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;
62
 
  if (fstat(file, &state))
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
73
 
  *frmdata= (uchar*) read_data;
74
 
  *len= read_len;
75
 
  error= 0;
76
 
  
77
 
 err:
78
 
  if (file > 0)
79
 
    VOID(my_close(file,MYF(MY_WME)));
80
 
  
81
 
 err_end:                     /* Here when no file */
82
 
  return(error);
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
 
*/
99
 
int writefrm(const char *name, const uchar *frmdata, size_t len)
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;
112
 
    VOID(my_close(file,MYF(0)));
113
 
  }
114
 
  return(error);
115
 
} /* writefrm */