47
47
static const char hexchars[]= "0123456789abcdef";
49
static bool tablename_to_filename(const char *from, char *to, size_t to_length);
49
static bool tablename_to_filename(const string &from, string &to);
52
52
Translate a cursor name to a table name (WL #1324).
203
203
path length on success, 0 on failure
206
size_t TableIdentifier::build_table_filename(std::string &path, const char *db, const char *table_name, bool is_tmp)
206
size_t TableIdentifier::build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp)
208
char dbbuff[FN_REFLEN];
209
char tbbuff[FN_REFLEN];
210
208
bool conversion_error= false;
212
memset(tbbuff, 0, sizeof(tbbuff));
210
conversion_error= tablename_to_filename(db, path);
211
if (conversion_error)
213
errmsg_printf(ERRMSG_LVL_ERROR,
214
_("Schema name cannot be encoded and fit within filesystem "
215
"name length restrictions."));
219
path.append(FN_ROOTDIR);
213
221
if (is_tmp) // It a conversion tmp
215
strncpy(tbbuff, table_name, sizeof(tbbuff));
223
path.append(table_name);
219
conversion_error= tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
227
conversion_error= tablename_to_filename(table_name, path);
220
228
if (conversion_error)
222
230
errmsg_printf(ERRMSG_LVL_ERROR,
228
memset(dbbuff, 0, sizeof(dbbuff));
229
conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
230
if (conversion_error)
232
errmsg_printf(ERRMSG_LVL_ERROR,
233
_("Schema name cannot be encoded and fit within filesystem "
234
"name length restrictions."));
239
path.append(FN_ROOTDIR);
242
237
return path.length();
256
251
true if errors happen. false on success.
258
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
253
static bool tablename_to_filename(const string &from, string &to)
262
for (; *from && length < to_length; length++, from++)
256
string::const_iterator iter= from.begin();
257
for (; iter != from.end(); ++iter)
264
if ((*from >= '0' && *from <= '9') ||
265
(*from >= 'a' && *from <= 'z') ||
266
/* OSX defines an extra set of high-bit and multi-byte characters
267
that cannot be used on the filesystem. Instead of trying to sort
268
those out, we'll just escape encode all high-bit-set chars on OSX.
269
It won't really hurt anything - it'll just make some filenames ugly. */
270
#if !defined(TARGET_OS_OSX)
271
((unsigned char)*from >= 128) ||
277
to[length]= tolower(*from);
261
if ((isdigit(*iter)) ||
281
if ((*from >= 'A' && *from <= 'Z'))
283
to[length]= tolower(*from);
273
to.push_back(tolower(*iter));
287
if (length + 3 >= to_length)
290
278
/* We need to escape this char in a way that can be reversed */
292
to[length++]= hexchars[(*from >> 4) & 15];
293
to[length]= hexchars[(*from) & 15];
280
to.push_back(hexchars[(*iter >> 4) & 15]);
281
to.push_back(hexchars[(*iter) & 15]);
296
if (internal::check_if_legal_tablename(to) &&
297
length + 4 < to_length)
284
if (internal::check_if_legal_tablename(to.c_str()))
299
memcpy(to + length, "@@@", 4);
319
305
case message::Table::FUNCTION:
320
306
case message::Table::STANDARD:
321
307
assert(path.size() == 0);
322
build_table_filename(path, getSchemaName().c_str(), table_name.c_str(), false);
308
build_table_filename(path, getSchemaName(), table_name, false);
324
310
case message::Table::INTERNAL:
325
311
assert(path.size() == 0);
326
build_table_filename(path, getSchemaName().c_str(), table_name.c_str(), true);
312
build_table_filename(path, getSchemaName(), table_name, true);
328
314
case message::Table::TEMPORARY:
329
315
if (path.empty())
337
323
hash_value= hasher(path);
339
325
key.resize(getKeySize());
340
size_t key_length= TableIdentifier::createKey(&key[0], *this);
342
assert(key_length == getKeySize()); // If this is off, then we have a memory issue.
327
std::copy(getSchemaName().begin(), getSchemaName().end(), key.begin());
328
std::copy(getTableName().begin(), getTableName().end(), key.begin() + getSchemaName().length() + 1);