~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/catalog/engine.cc

Merge Stewart's dead code removal

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