|
Packit |
83fed9 |
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* Libmemcached library
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
|
|
Packit |
83fed9 |
* All rights reserved.
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
83fed9 |
* modification, are permitted provided that the following conditions are
|
|
Packit |
83fed9 |
* met:
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* * Redistributions of source code must retain the above copyright
|
|
Packit |
83fed9 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* * Redistributions in binary form must reproduce the above
|
|
Packit |
83fed9 |
* copyright notice, this list of conditions and the following disclaimer
|
|
Packit |
83fed9 |
* in the documentation and/or other materials provided with the
|
|
Packit |
83fed9 |
* distribution.
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* * The names of its contributors may not be used to endorse or
|
|
Packit |
83fed9 |
* promote products derived from this software without specific prior
|
|
Packit |
83fed9 |
* written permission.
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
83fed9 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
83fed9 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
83fed9 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
83fed9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
83fed9 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
83fed9 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
83fed9 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
83fed9 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
83fed9 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
83fed9 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
83fed9 |
*
|
|
Packit |
83fed9 |
*/
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
#include <libmemcached/common.h>
|
|
Packit |
83fed9 |
#include <libmemcached/options.hpp>
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
#include <libmemcached/csl/context.h>
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
const char *memcached_parse_filename(memcached_st *memc)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
assert_msg(memc, "Invalid memcached_st");
|
|
Packit |
83fed9 |
return memcached_array_string(memc->configure.filename);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
size_t memcached_parse_filename_length(memcached_st *memc)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return memcached_array_size(memc->configure.filename);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
static memcached_return_t _parse_file_options(memcached_st& self, memcached_array_st *real_name)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
FILE *fp= fopen(memcached_array_string(real_name), "r");
|
|
Packit |
83fed9 |
if (not fp)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
memcached_string_t error_message= memcached_array_to_string(real_name);
|
|
Packit |
83fed9 |
memcached_return_t rc= memcached_set_errno(self, errno, MEMCACHED_AT, error_message);
|
|
Packit |
83fed9 |
return rc;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
char buffer[BUFSIZ];
|
|
Packit |
83fed9 |
memcached_return_t rc= MEMCACHED_INVALID_ARGUMENTS;
|
|
Packit |
83fed9 |
while (fgets(buffer, sizeof(buffer), fp))
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
size_t length= strlen(buffer);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (length == 1 and buffer[0] == '\n')
|
|
Packit |
83fed9 |
continue;
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (memcached_failed(rc= memcached_parse_configuration(&self, buffer, length)))
|
|
Packit |
83fed9 |
break;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
fclose(fp);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
return rc;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t libmemcached_check_configuration(const char *option_string, size_t length, char *error_buffer, size_t error_buffer_size)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
memcached_st memc, *memc_ptr;
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (option_string == NULL or length == 0)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return MEMCACHED_INVALID_ARGUMENTS;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (error_buffer and error_buffer_size)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
error_buffer[0]= 0;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (not (memc_ptr= memcached_create(&memc)))
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t rc= memcached_parse_configuration(memc_ptr, option_string, length);
|
|
Packit |
83fed9 |
if (memcached_failed(rc) and error_buffer and error_buffer_size)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size);
|
|
Packit |
83fed9 |
error_buffer[error_buffer_size -1]= 0;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
bool has_filename= memcached_behavior_get(memc_ptr, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE);
|
|
Packit |
83fed9 |
if (memcached_success(rc) and has_filename)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
assert_msg(memcached_parse_filename(memc_ptr), "Invalid configuration file");
|
|
Packit |
83fed9 |
assert_msg(memcached_parse_filename_length(memc_ptr), "Invalid configuration file");
|
|
Packit |
83fed9 |
rc= _parse_file_options(*memc_ptr, memc_ptr->configure.filename);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (memcached_failed(rc) and error_buffer and error_buffer_size)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size);
|
|
Packit |
83fed9 |
error_buffer[error_buffer_size -1]= 0;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_free(memc_ptr);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
return rc;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t memcached_parse_configuration(memcached_st *self, char const *option_string, size_t length)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
WATCHPOINT_ASSERT(self);
|
|
Packit |
83fed9 |
if (not self)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return MEMCACHED_INVALID_ARGUMENTS;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t rc;
|
|
Packit |
83fed9 |
Context context(option_string, length, self, rc);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
context.start();
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
return rc;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
void memcached_set_configuration_file(memcached_st *self, const char *filename, size_t filename_length)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
assert_msg(filename, "Invalid filename");
|
|
Packit |
83fed9 |
assert_msg(filename_length, "Invalid filename_length");
|
|
Packit |
83fed9 |
memcached_array_free(self->configure.filename);
|
|
Packit |
83fed9 |
self->configure.filename= memcached_strcpy(self, filename, filename_length);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t memcached_parse_configure_file(memcached_st& self, const char *filename, size_t length)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
if (not filename)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
WATCHPOINT_ASSERT(self);
|
|
Packit |
83fed9 |
if (not length)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_array_st *tmp_array= memcached_strcpy(&self, filename, length);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
if (not tmp_array)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return memcached_set_error(self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t rc= memcached_parse_configure_file(self, *tmp_array);
|
|
Packit |
83fed9 |
memcached_array_free(tmp_array);
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
return rc;
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
memcached_return_t memcached_parse_configure_file(memcached_st& self, memcached_array_st& filename)
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
WATCHPOINT_ASSERT(memcached_array_size(&filename));
|
|
Packit |
83fed9 |
if (not memcached_array_size(&filename))
|
|
Packit |
83fed9 |
{
|
|
Packit |
83fed9 |
return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT);
|
|
Packit |
83fed9 |
}
|
|
Packit |
83fed9 |
|
|
Packit |
83fed9 |
return _parse_file_options(self, &filename);
|
|
Packit |
83fed9 |
}
|