Drizzled Public API Documentation

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