drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
engine.cc
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 <drizzled/display.h>
28#include <google/protobuf/io/zero_copy_stream.h>
29#include <google/protobuf/io/zero_copy_stream_impl.h>
30
31#include <iostream>
32#include <fstream>
33#include <string>
34
35#include <drizzled/data_home.h>
37#include <drizzled/catalog/local.h>
38#include <plugin/catalog/module.h>
39
40namespace plugin {
41namespace catalog {
42
43static std::string CATALOG_OPT_EXT(".cat");
44
45bool Engine::create(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
46{
47 if (::mkdir(identifier.getPath().c_str(), 0777) == -1)
48 {
49 return false;
50 }
51
52 if (not writeFile(identifier, message))
53 {
54 ::rmdir(identifier.getPath().c_str());
55
56 return false;
57 }
58
59 return true;
60}
61
62bool Engine::drop(const drizzled::identifier::Catalog &identifier)
63{
64 std::string file(identifier.getPath());
65 file.append(1, FN_LIBCHAR);
66 file.append(CATALOG_OPT_EXT);
67
68 // No catalog file, no love from us.
69 if (::access(file.c_str(), F_OK))
70 {
71 drizzled::sql_perror("access()", file);
72 return false;
73 }
74
75 if (::unlink(file.c_str()))
76 {
77 drizzled::sql_perror("unlink()", file);
78 return false;
79 }
80
81 if (::rmdir(identifier.getPath().c_str()))
82 {
83 drizzled::sql_perror("rmdir()", identifier.getPath());
84 //@todo If this happens, we want a report of it. For the moment I dump to
85 //stderr so I can catch it in Hudson.
86 drizzled::CachedDirectory dir(identifier.getPath());
87 }
88
89 return true;
90}
91
92void Engine::getMessages(drizzled::message::catalog::vector &messages)
93{
94 prime(messages);
95}
96
97drizzled::message::catalog::shared_ptr Engine::getMessage(const drizzled::identifier::Catalog& identifier)
98{
99 if (drizzled::catalog::local_identifier() == identifier)
100 {
101 return drizzled::message::catalog::make_shared(identifier);
102 }
103
104 drizzled::message::catalog::shared_ptr message;
105 if ((message= readFile(identifier)))
106 {
107 assert(message);
108 return message;
109 }
110
111 return drizzled::message::catalog::shared_ptr();
112}
113
114void Engine::prime(drizzled::message::catalog::vector &messages)
115{
116 bool found_local= false;
117 drizzled::CachedDirectory directory(drizzled::getFullDataHome().string(), drizzled::CachedDirectory::DIRECTORY, true);
118 drizzled::CachedDirectory::Entries files= directory.getEntries();
119
120
121 for (drizzled::CachedDirectory::Entries::reference entry : files)
122 {
123 drizzled::message::catalog::shared_ptr message;
124
125 if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
126 {
127 continue;
128 }
129
130 drizzled::identifier::Catalog identifier(entry->filename);
131
132 if ((message= readFile(identifier)))
133 {
134 messages.push_back(message);
135
136 if (drizzled::catalog::local_identifier() == identifier)
137 {
138 found_local= true;
139 }
140 }
141 }
142
143 if (found_local == false)
144 {
145 messages.push_back(drizzled::catalog::local()->message());
146 }
147}
148
149bool Engine::writeFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
150{
151 char file_tmp[FN_REFLEN];
152 std::string file(identifier.getPath());
153
154 file.append(1, FN_LIBCHAR);
155 file.append(CATALOG_OPT_EXT);
156
157 snprintf(file_tmp, FN_REFLEN, "%sXXXXXX", file.c_str());
158
159 int fd= mkstemp(file_tmp);
160
161 if (fd == -1)
162 {
163 drizzled::sql_perror("mkstemp()", file_tmp);
164
165 return false;
166 }
167
168 bool success;
169
170 try {
171 success= message->SerializeToFileDescriptor(fd);
172 }
173 catch (...)
174 {
175 success= false;
176 }
177
178 if (success == false)
179 {
180 drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), file.c_str(),
181 message->InitializationErrorString().empty() ? "unknown" : message->InitializationErrorString().c_str());
182
183 if (::close(fd) == -1)
184 {
185 drizzled::sql_perror("close()", file_tmp);
186 }
187
188 if (::unlink(file_tmp))
189 {
190 drizzled::sql_perror("unlink()", file_tmp);
191 }
192
193 return false;
194 }
195
196 if (::close(fd) == -1)
197 {
198 drizzled::sql_perror("close()", file_tmp);
199
200 if (::unlink(file_tmp))
201 {
202 drizzled::sql_perror("unlink()", file_tmp);
203 }
204
205 return false;
206 }
207
208 if (::rename(file_tmp, file.c_str()) == -1)
209 {
210 drizzled::sql_perror("rename()", file_tmp);
211 if (::unlink(file_tmp))
212 {
213 drizzled::sql_perror("unlink()", file_tmp);
214 }
215
216 return false;
217 }
218
219 return true;
220}
221
222
223drizzled::message::catalog::shared_ptr Engine::readFile(const drizzled::identifier::Catalog& identifier)
224{
225 std::string path(identifier.getPath());
226
227 /*
228 Pass an empty file name, and the database options file name as extension
229 to avoid table name to file name encoding.
230 */
231 path.append(1, FN_LIBCHAR);
232 path.append(CATALOG_OPT_EXT);
233
234 std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
235
236 if (input.good())
237 {
238 drizzled::message::catalog::shared_ptr message= drizzled::message::catalog::make_shared(identifier);
239
240 if (not message)
241 return drizzled::message::catalog::shared_ptr();
242
243
244 if (message->ParseFromIstream(&input))
245 {
246 return message;
247 }
248
249 drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), path.c_str(),
250 message->InitializationErrorString().empty() ? "unknown" : message->InitializationErrorString().c_str());
251 }
252 else
253 {
254 drizzled::sql_perror("std::fstream::good()", path);
255 }
256
257 return drizzled::message::catalog::shared_ptr();
258}
259
260} /* namespace catalog */
261} /* namespace plugin */
Defines the interface to the CachedDirectory class.