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