25 #include <sys/types.h>
27 #include <boost/foreach.hpp>
28 #include <drizzled/display.h>
29 #include <google/protobuf/io/zero_copy_stream.h>
30 #include <google/protobuf/io/zero_copy_stream_impl.h>
36 #include <drizzled/data_home.h>
38 #include <drizzled/catalog/local.h>
39 #include <plugin/catalog/module.h>
44 static std::string CATALOG_OPT_EXT(
".cat");
48 if (::mkdir(identifier.getPath().c_str(), 0777) == -1)
53 if (not writeFile(identifier, message))
55 ::rmdir(identifier.getPath().c_str());
65 std::string file(identifier.getPath());
66 file.append(1, FN_LIBCHAR);
67 file.append(CATALOG_OPT_EXT);
70 if (::access(file.c_str(), F_OK))
72 drizzled::sql_perror(
"access()", file);
76 if (::unlink(file.c_str()))
78 drizzled::sql_perror(
"unlink()", file);
82 if (::rmdir(identifier.getPath().c_str()))
84 drizzled::sql_perror(
"rmdir()", identifier.getPath());
93 void Engine::getMessages(drizzled::message::catalog::vector &messages)
100 if (drizzled::catalog::local_identifier() == identifier)
102 return drizzled::message::catalog::make_shared(identifier);
105 drizzled::message::catalog::shared_ptr message;
106 if ((message= readFile(identifier)))
112 return drizzled::message::catalog::shared_ptr();
115 void Engine::prime(drizzled::message::catalog::vector &messages)
117 bool found_local=
false;
118 drizzled::CachedDirectory directory(drizzled::getFullDataHome().file_string(), drizzled::CachedDirectory::DIRECTORY,
true);
119 drizzled::CachedDirectory::Entries files= directory.getEntries();
122 BOOST_FOREACH(drizzled::CachedDirectory::Entries::reference entry, files)
124 drizzled::message::catalog::shared_ptr message;
126 if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
133 if (message= readFile(identifier))
135 messages.push_back(message);
137 if (drizzled::catalog::local_identifier() == identifier)
144 if (found_local ==
false)
146 messages.push_back(drizzled::catalog::local()->message());
152 char file_tmp[FN_REFLEN];
153 std::string file(identifier.getPath());
155 file.append(1, FN_LIBCHAR);
156 file.append(CATALOG_OPT_EXT);
158 snprintf(file_tmp, FN_REFLEN,
"%sXXXXXX", file.c_str());
160 int fd= mkstemp(file_tmp);
164 drizzled::sql_perror(
"mkstemp()", file_tmp);
172 success= message->SerializeToFileDescriptor(fd);
179 if (success ==
false)
181 drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), file.c_str(),
182 message->InitializationErrorString().empty() ?
"unknown" : message->InitializationErrorString().c_str());
184 if (::close(fd) == -1)
186 drizzled::sql_perror(
"close()", file_tmp);
189 if (::unlink(file_tmp))
191 drizzled::sql_perror(
"unlink()", file_tmp);
197 if (::close(fd) == -1)
199 drizzled::sql_perror(
"close()", file_tmp);
201 if (::unlink(file_tmp))
203 drizzled::sql_perror(
"unlink()", file_tmp);
209 if (::rename(file_tmp, file.c_str()) == -1)
211 drizzled::sql_perror(
"rename()", file_tmp);
212 if (::unlink(file_tmp))
214 drizzled::sql_perror(
"unlink()", file_tmp);
226 std::string path(identifier.getPath());
232 path.append(1, FN_LIBCHAR);
233 path.append(CATALOG_OPT_EXT);
235 std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
239 drizzled::message::catalog::shared_ptr message= drizzled::message::catalog::make_shared(identifier);
242 return drizzled::message::catalog::shared_ptr();
245 if (message->ParseFromIstream(&input))
250 drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), path.c_str(),
251 message->InitializationErrorString().empty() ?
"unknown" : message->InitializationErrorString().c_str());
255 drizzled::sql_perror(
"std::fstream::good()", path);
258 return drizzled::message::catalog::shared_ptr();