1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
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.
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.
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
25
#include <sys/types.h>
27
#include "drizzled/display.h"
28
#include <google/protobuf/io/zero_copy_stream.h>
29
#include <google/protobuf/io/zero_copy_stream_impl.h>
35
#include "drizzled/data_home.h"
36
#include "drizzled/cached_directory.h"
37
#include "plugin/catalog/module.h"
42
static std::string CATALOG_OPT_EXT(".cat");
44
bool Engine::create(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
46
if (mkdir(identifier.getPath().c_str(), 0777) == -1)
49
if (not writeFile(identifier, message))
51
rmdir(identifier.getPath().c_str());
58
bool Engine::drop(const drizzled::identifier::Catalog &identifier)
60
std::string file(identifier.getPath());
61
file.append(1, FN_LIBCHAR);
62
file.append(CATALOG_OPT_EXT);
64
// No catalog file, no love from us.
65
if (access(file.c_str(), F_OK))
71
if (unlink(file.c_str()))
77
if (rmdir(identifier.getPath().c_str()))
79
perror(identifier.getPath().c_str());
80
//@todo If this happens, we want a report of it. For the moment I dump
81
//to stderr so I can catch it in Hudson.
82
drizzled::CachedDirectory dir(identifier.getPath());
88
void Engine::getMessages(drizzled::message::catalog::vector &messages)
93
void Engine::prime(drizzled::message::catalog::vector &messages)
95
bool found_local= false;
96
static drizzled::identifier::Catalog LOCAL_IDENTIFIER("local");
97
drizzled::CachedDirectory directory(drizzled::getFullDataHome().file_string(), drizzled::CachedDirectory::DIRECTORY, true);
98
drizzled::CachedDirectory::Entries files= directory.getEntries();
101
for (drizzled::CachedDirectory::Entries::iterator fileIter= files.begin();
102
fileIter != files.end(); fileIter++)
104
drizzled::CachedDirectory::Entry *entry= *fileIter;
105
drizzled::message::catalog::shared_ptr message;
107
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
110
drizzled::identifier::Catalog identifier(entry->filename);
112
if (readFile(identifier, message))
114
messages.push_back(message);
116
if (LOCAL_IDENTIFIER == identifier)
123
messages.push_back(drizzled::message::catalog::make_shared(LOCAL_IDENTIFIER));
127
bool Engine::writeFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
129
char file_tmp[FN_REFLEN];
130
std::string file(identifier.getPath());
133
file.append(1, FN_LIBCHAR);
134
file.append(CATALOG_OPT_EXT);
136
snprintf(file_tmp, FN_REFLEN, "%sXXXXXX", file.c_str());
138
int fd= mkstemp(file_tmp);
150
success= message->SerializeToFileDescriptor(fd);
159
drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), file.c_str(),
160
message->InitializationErrorString().empty() ? "unknown" : message->InitializationErrorString().c_str());
165
if (unlink(file_tmp))
175
if (unlink(file_tmp))
181
if (rename(file_tmp, file.c_str()) == -1)
183
if (unlink(file_tmp))
193
bool Engine::readFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
195
std::string path(identifier.getPath());
198
Pass an empty file name, and the database options file name as extension
199
to avoid table name to file name encoding.
201
path.append(1, FN_LIBCHAR);
202
path.append(CATALOG_OPT_EXT);
204
std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
208
message= drizzled::message::catalog::make_shared(identifier);
209
if (message->ParseFromIstream(&input))
214
drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), path.c_str(),
215
message->InitializationErrorString().empty() ? "unknown" : message->InitializationErrorString().c_str());
219
perror(path.c_str());
225
} /* namespace catalog */
226
} /* namespace plugin */