~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/discover.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

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
/**
 
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;
 
53
  MY_STAT state;  
 
54
  DBUG_ENTER("readfrm");
 
55
  DBUG_PRINT("enter",("name: '%s'",name));
 
56
  
 
57
  *frmdata= NULL;      // In case of errors
 
58
  *len= 0;
 
59
  error= 1;
 
60
  if ((file=my_open(fn_format(index_file,name,"",reg_ext,
 
61
                              MY_UNPACK_FILENAME|MY_APPEND_EXT),
 
62
                    O_RDONLY | O_SHARE,
 
63
                    MYF(0))) < 0)  
 
64
    goto err_end; 
 
65
  
 
66
  // Get length of file
 
67
  error= 2;
 
68
  if (my_fstat(file, &state, MYF(0)))
 
69
    goto err;
 
70
  read_len= state.st_size;  
 
71
 
 
72
  // Read whole frm file
 
73
  error= 3;
 
74
  read_data= 0;                                 // Nothing to free
 
75
  if (read_string(file, &read_data, read_len))
 
76
    goto err;
 
77
 
 
78
  // Setup return data
 
79
  *frmdata= (uchar*) read_data;
 
80
  *len= read_len;
 
81
  error= 0;
 
82
  
 
83
 err:
 
84
  if (file > 0)
 
85
    VOID(my_close(file,MYF(MY_WME)));
 
86
  
 
87
 err_end:                     /* Here when no file */
 
88
  DBUG_RETURN (error);
 
89
} /* readfrm */
 
90
 
 
91
 
 
92
/*
 
93
  Write the content of a frm data pointer 
 
94
  to a frm file.
 
95
 
 
96
  @param name           path to table-file "db/name"
 
97
  @param frmdata        frm data
 
98
  @param len            length of the frmdata
 
99
 
 
100
  @retval
 
101
    0   ok
 
102
  @retval
 
103
    2    Could not write file
 
104
*/
 
105
 
 
106
int writefrm(const char *name, const uchar *frmdata, size_t len)
 
107
{
 
108
  File file;
 
109
  char   index_file[FN_REFLEN];
 
110
  int error;
 
111
  DBUG_ENTER("writefrm");
 
112
  DBUG_PRINT("enter",("name: '%s' len: %lu ",name, (ulong) len));
 
113
 
 
114
  error= 0;
 
115
  if ((file=my_create(fn_format(index_file,name,"",reg_ext,
 
116
                      MY_UNPACK_FILENAME|MY_APPEND_EXT),
 
117
                      CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0)
 
118
  {
 
119
    if (my_write(file, frmdata, len,MYF(MY_WME | MY_NABP)))
 
120
      error= 2;
 
121
    VOID(my_close(file,MYF(0)));
 
122
  }
 
123
  DBUG_RETURN(error);
 
124
} /* writefrm */
 
125
 
 
126
 
 
127
 
 
128
 
 
129