~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/catalog/engine.cc

  • Committer: Brian Aker
  • Date: 2011-01-04 02:03:24 UTC
  • Revision ID: brian@tangent.org-20110104020324-043om9xrux4obl96
Fix additional member of trans services to handle identifier.

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
 
void Engine::prime(drizzled::message::catalog::vector &messages)
96
 
{
97
 
  bool found_local= false;
98
 
  drizzled::CachedDirectory directory(drizzled::getFullDataHome().file_string(), drizzled::CachedDirectory::DIRECTORY, true);
99
 
  drizzled::CachedDirectory::Entries files= directory.getEntries();
100
 
 
101
 
 
102
 
  for (drizzled::CachedDirectory::Entries::iterator fileIter= files.begin();
103
 
       fileIter != files.end(); fileIter++)
104
 
  {
105
 
    drizzled::CachedDirectory::Entry *entry= *fileIter;
106
 
    drizzled::message::catalog::shared_ptr message;
107
 
 
108
 
    if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
109
 
      continue;
110
 
 
111
 
    drizzled::identifier::Catalog identifier(entry->filename);
112
 
 
113
 
    if (readFile(identifier, message))
114
 
    {
115
 
      messages.push_back(message);
116
 
 
117
 
      if (drizzled::catalog::local_identifier() == identifier)
118
 
        found_local= true;
119
 
    }
120
 
  }
121
 
 
122
 
  if (not found_local)
123
 
  {
124
 
    messages.push_back(drizzled::catalog::local()->message());
125
 
  }
126
 
}
127
 
 
128
 
bool Engine::writeFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
129
 
{
130
 
  char file_tmp[FN_REFLEN];
131
 
  std::string file(identifier.getPath());
132
 
 
133
 
 
134
 
  file.append(1, FN_LIBCHAR);
135
 
  file.append(CATALOG_OPT_EXT);
136
 
 
137
 
  snprintf(file_tmp, FN_REFLEN, "%sXXXXXX", file.c_str());
138
 
 
139
 
  int fd= mkstemp(file_tmp);
140
 
 
141
 
  if (fd == -1)
142
 
  {
143
 
    perror(file_tmp);
144
 
 
145
 
    return false;
146
 
  }
147
 
 
148
 
  bool success;
149
 
 
150
 
  try {
151
 
    success= message->SerializeToFileDescriptor(fd);
152
 
  }
153
 
  catch (...)
154
 
  {
155
 
    success= false;
156
 
  }
157
 
 
158
 
  if (not success)
159
 
  {
160
 
    drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), file.c_str(),
161
 
                       message->InitializationErrorString().empty() ? "unknown" :  message->InitializationErrorString().c_str());
162
 
 
163
 
    if (close(fd) == -1)
164
 
      perror(file_tmp);
165
 
 
166
 
    if (unlink(file_tmp))
167
 
      perror(file_tmp);
168
 
 
169
 
    return false;
170
 
  }
171
 
 
172
 
  if (close(fd) == -1)
173
 
  {
174
 
    perror(file_tmp);
175
 
 
176
 
    if (unlink(file_tmp))
177
 
      perror(file_tmp);
178
 
 
179
 
    return false;
180
 
  }
181
 
 
182
 
  if (rename(file_tmp, file.c_str()) == -1)
183
 
  {
184
 
    if (unlink(file_tmp))
185
 
      perror(file_tmp);
186
 
 
187
 
    return false;
188
 
  }
189
 
 
190
 
  return true;
191
 
}
192
 
 
193
 
 
194
 
bool Engine::readFile(const drizzled::identifier::Catalog &identifier, drizzled::message::catalog::shared_ptr &message)
195
 
{
196
 
  std::string path(identifier.getPath());
197
 
 
198
 
  /*
199
 
    Pass an empty file name, and the database options file name as extension
200
 
    to avoid table name to file name encoding.
201
 
  */
202
 
  path.append(1, FN_LIBCHAR);
203
 
  path.append(CATALOG_OPT_EXT);
204
 
 
205
 
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
206
 
 
207
 
  if (input.good())
208
 
  {
209
 
    message= drizzled::message::catalog::make_shared(identifier);
210
 
    if (message->ParseFromIstream(&input))
211
 
    {
212
 
      return true;
213
 
    }
214
 
 
215
 
    drizzled::my_error(drizzled::ER_CORRUPT_CATALOG_DEFINITION, MYF(0), path.c_str(),
216
 
                       message->InitializationErrorString().empty() ? "unknown" :  message->InitializationErrorString().c_str());
217
 
  }
218
 
  else
219
 
  {
220
 
    perror(path.c_str());
221
 
  }
222
 
 
223
 
  return false;
224
 
}
225
 
 
226
 
} /* namespace catalog */
227
 
} /* namespace plugin */