~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/catalog/engine.cc

  • Committer: Brian Aker
  • Date: 2010-12-03 04:10:23 UTC
  • mto: (2017.3.1 catalogs)
  • mto: This revision was merged to the branch mainline in revision 2073.
  • Revision ID: brian@tangent.org-20101203041023-hd0cwx8jgwcrivck
Big hunk of burning create/drop work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2010 Brian Aker
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <fcntl.h>
 
24
#include <sys/stat.h>
 
25
#include <sys/types.h>
 
26
 
 
27
#include <google/protobuf/io/zero_copy_stream.h>
 
28
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
29
 
 
30
#include <iostream>
 
31
#include <fstream>
 
32
#include <string>
 
33
 
 
34
#include "plugin/catalog/module.h"
 
35
 
 
36
namespace plugin {
 
37
namespace catalog {
 
38
 
 
39
static std::string CATALOG_OPT_EXT(".cat");
 
40
 
 
41
bool Engine::create(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
 
42
{
 
43
  if (mkdir(identifier.getPath().c_str(), 0777) == -1)
 
44
    return false;
 
45
 
 
46
  if (not writeFile(identifier, message))
 
47
  {
 
48
    rmdir(identifier.getPath().c_str());
 
49
 
 
50
    return false;
 
51
  }
 
52
  return false;
 
53
}
 
54
 
 
55
bool Engine::drop(const drizzled::identifier::Catalog &identifier)
 
56
{
 
57
  std::string file(identifier.getPath());
 
58
  file.append(1, FN_LIBCHAR);
 
59
  file.append(CATALOG_OPT_EXT);
 
60
 
 
61
  // No catalog file, no love from us.
 
62
  if (access(file.c_str(), F_OK))
 
63
  {
 
64
    perror(file.c_str());
 
65
    return false;
 
66
  }
 
67
 
 
68
  if (unlink(file.c_str()))
 
69
  {
 
70
    perror(file.c_str());
 
71
    return false;
 
72
  }
 
73
 
 
74
  if (rmdir(identifier.getPath().c_str()))
 
75
  {
 
76
    perror(identifier.getPath().c_str());
 
77
    //@todo If this happens, we want a report of it. For the moment I dump
 
78
    //to stderr so I can catch it in Hudson.
 
79
    drizzled::CachedDirectory dir(identifier.getPath());
 
80
  }
 
81
 
 
82
  return true;
 
83
}
 
84
 
 
85
bool Engine::writeFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
 
86
{
 
87
  char file_tmp[FN_REFLEN];
 
88
  std::string file(identifier.getPath());
 
89
 
 
90
 
 
91
  file.append(1, FN_LIBCHAR);
 
92
  file.append(CATALOG_OPT_EXT);
 
93
 
 
94
  snprintf(file_tmp, FN_REFLEN, "%sXXXXXX", file.c_str());
 
95
 
 
96
  int fd= mkstemp(file_tmp);
 
97
 
 
98
  if (fd == -1)
 
99
  {
 
100
    perror(file_tmp);
 
101
 
 
102
    return false;
 
103
  }
 
104
 
 
105
  bool success;
 
106
 
 
107
  try {
 
108
    success= message->SerializeToFileDescriptor(fd);
 
109
  }
 
110
  catch (...)
 
111
  {
 
112
    success= false;
 
113
  }
 
114
 
 
115
  if (not success)
 
116
  {
 
117
    drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), file.c_str(),
 
118
                       message->InitializationErrorString().empty() ? "unknown" :  message->InitializationErrorString().c_str());
 
119
 
 
120
    if (close(fd) == -1)
 
121
      perror(file_tmp);
 
122
 
 
123
    if (unlink(file_tmp))
 
124
      perror(file_tmp);
 
125
 
 
126
    return false;
 
127
  }
 
128
 
 
129
  if (close(fd) == -1)
 
130
  {
 
131
    perror(file_tmp);
 
132
 
 
133
    if (unlink(file_tmp))
 
134
      perror(file_tmp);
 
135
 
 
136
    return false;
 
137
  }
 
138
 
 
139
  if (rename(file_tmp, file.c_str()) == -1)
 
140
  {
 
141
    if (unlink(file_tmp))
 
142
      perror(file_tmp);
 
143
 
 
144
    return false;
 
145
  }
 
146
 
 
147
  return true;
 
148
}
 
149
 
 
150
 
 
151
bool Engine::readFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
 
152
{
 
153
  std::string path(identifier.getPath());
 
154
 
 
155
  /*
 
156
    Pass an empty file name, and the database options file name as extension
 
157
    to avoid table name to file name encoding.
 
158
  */
 
159
  path.append(1, FN_LIBCHAR);
 
160
  path.append(CATALOG_OPT_EXT);
 
161
 
 
162
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
 
163
 
 
164
  if (input.good())
 
165
  {
 
166
    if (message->ParseFromIstream(&input))
 
167
    {
 
168
      return true;
 
169
    }
 
170
 
 
171
    drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), path.c_str(),
 
172
                       message->InitializationErrorString().empty() ? "unknown" :  message->InitializationErrorString().c_str());
 
173
  }
 
174
  else
 
175
  {
 
176
    perror(path.c_str());
 
177
  }
 
178
 
 
179
  return false;
 
180
}
 
181
 
 
182
} /* namespace catalog */
 
183
} /* namespace plugin */