From: Brian Aker Date: Fri, 28 Sep 2007 20:52:44 +0000 (-0700) Subject: Bunch of fixes related to portability. X-Git-Tag: 0.3~17 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=ccd00450d57624afba24bfbb6e76d1d69d0ad0f8;p=awesomized%2Flibmemcached Bunch of fixes related to portability. Also reworked code to use send/recv over write/read. More logic in get_test3test case --- diff --git a/ChangeLog b/ChangeLog index 6982c9b3..505ac0ec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 0.3 * Jeff Fisher provided a spec file + * Added support for Solaris + * Fixed read to be recv and write to be send 0.2 Thu Sep 27 03:46:57 PDT 2007 * First public version diff --git a/configure.in b/configure.in index 9704a42f..b3b0bc21 100644 --- a/configure.in +++ b/configure.in @@ -1,14 +1,25 @@ -AC_INIT(src/memcat.c) -AC_CONFIG_AUX_DIR(config) -AM_CONFIG_HEADER(config.h) -AM_INIT_AUTOMAKE("libmemcached", 0.2 ) - -AC_PROG_CC -AC_PROG_LIBTOOL -LIBTOOL="$LIBTOOL --preserve-dup-deps" -AC_SUBST(LIBTOOL)dnl - -AC_C_CONST -AC_TYPE_SIZE_T -AC_CHECK_HEADERS(limits.h syslimits.h) -AC_OUTPUT(Makefile src/Makefile tests/Makefile docs/Makefile lib/Makefile include/Makefile support/Makefile) +AC_INIT(src/memcat.c) +AC_CONFIG_AUX_DIR(config) +AM_CONFIG_HEADER(config.h) +AM_INIT_AUTOMAKE("libmemcached", 0.2 ) + +AC_PROG_CC +AC_PROG_LIBTOOL +LIBTOOL="$LIBTOOL --preserve-dup-deps" +AC_SUBST(LIBTOOL)dnl + +# We only support GCC and Sun's forte at the moment +if test "$GCC" = "yes" +then + CFLAGS="-Wall" + CXXFLAGS="$CXXFLAGS -fno-implicit-templates -fno-exceptions -fno-rtti" +else + CFLAGS="-Xa -xstrconst -mt -D_FORTEC_ -fast -m64" + LDFLAGS="-lsocket -lnsl" + DTRACEFLAGS="-64" +fi + +AC_C_CONST +AC_TYPE_SIZE_T +AC_CHECK_HEADERS(limits.h syslimits.h) +AC_OUTPUT(Makefile src/Makefile tests/Makefile docs/Makefile lib/Makefile include/Makefile support/Makefile) diff --git a/lib/Makefile.am b/lib/Makefile.am index f6597186..0b4bbc6b 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,18 +1,18 @@ -INCLUDES = -I$(top_builddir)/include -Wall -LIBS = - -lib_LTLIBRARIES = libmemcached.la -libmemcached_la_SOURCES = memcached.c \ - memcached_strerror.c \ - memcached_connect.c \ - memcached_response.c \ - memcached_get.c \ - memcached_storage.c \ - memcached_delete.c \ - memcached_hash.c \ - memcached_auto.c \ - memcached_verbosity.c \ - memcached_quit.c \ - memcached_flush.c \ - memcached_string.c \ - memcached_stats.c +INCLUDES = -I$(top_builddir)/include +LIBS = + +lib_LTLIBRARIES = libmemcached.la +libmemcached_la_SOURCES = memcached.c \ + memcached_strerror.c \ + memcached_connect.c \ + memcached_response.c \ + memcached_get.c \ + memcached_storage.c \ + memcached_delete.c \ + memcached_hash.c \ + memcached_auto.c \ + memcached_verbosity.c \ + memcached_quit.c \ + memcached_flush.c \ + memcached_string.c \ + memcached_stats.c diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index acb554b1..aa7ce5df 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -24,7 +24,7 @@ static memcached_return memcached_auto(memcached_st *ptr, offset); if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= write(ptr->hosts[server_key].fd, buffer, send_length); + sent_length= send(ptr->hosts[server_key].fd, buffer, send_length, 0); if (sent_length == -1 || sent_length != send_length) return MEMCACHED_WRITE_FAILURE; diff --git a/lib/memcached_delete.c b/lib/memcached_delete.c index b50aaef0..8af15424 100644 --- a/lib/memcached_delete.c +++ b/lib/memcached_delete.c @@ -26,7 +26,7 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= write(ptr->hosts[server_key].fd, buffer, send_length); + sent_length= send(ptr->hosts[server_key].fd, buffer, send_length, 0); if (sent_length == -1 || sent_length != send_length) return MEMCACHED_WRITE_FAILURE; diff --git a/lib/memcached_flush.c b/lib/memcached_flush.c index a822f4ad..209a7e0f 100644 --- a/lib/memcached_flush.c +++ b/lib/memcached_flush.c @@ -25,7 +25,7 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= write(ptr->hosts[x].fd, buffer, send_length); + sent_length= send(ptr->hosts[x].fd, buffer, send_length, 0); if (sent_length == -1 || sent_length != send_length) return MEMCACHED_WRITE_FAILURE; diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 06b406e3..0b775d3d 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -49,7 +49,6 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len string_ptr++; if (end_ptr == string_ptr) goto read_error; - for (next_ptr= string_ptr; end_ptr == string_ptr || *string_ptr != ' '; string_ptr++); *flags= (uint16_t)strtol(next_ptr, &string_ptr, 10); @@ -76,10 +75,14 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len if (*value_length) { size_t read_length; + size_t partial_length; + size_t to_read; char *value; + char *value_ptr; /* We add two bytes so that we can walk the \r\n */ value= (char *)malloc(((*value_length) +2) * sizeof(char)); + memset(value, 0, ((*value_length) +2) * sizeof(char)); if (!value) { @@ -88,7 +91,18 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len return NULL; } - read_length= read(ptr->hosts[server_key].fd, value, (*value_length)+2); + value_ptr= value; + read_length= 0; + to_read= (*value_length) + 2; + /* This is overkill */ + while ((partial_length= recv(ptr->hosts[server_key].fd, value_ptr, to_read, 0)) > 0) + { + value_ptr+= partial_length; + read_length+= partial_length; + to_read-= partial_length; + if (read_length == (size_t)(*value_length + 2)) + break; + } if (read_length != (size_t)(*value_length + 2)) { @@ -128,7 +142,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, if (*error != MEMCACHED_SUCCESS) return NULL; - if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1)) + if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) { *error= MEMCACHED_WRITE_FAILURE; return NULL; @@ -198,8 +212,8 @@ memcached_return memcached_mget(memcached_st *ptr, memcached_string_st *string= cursor_key_exec[x]; memcached_string_append(ptr, string, "\r\n", 2); - if ((write(ptr->hosts[x].fd, string->string, - memcached_string_length(ptr, string)) == -1)) + if ((send(ptr->hosts[x].fd, string->string, + memcached_string_length(ptr, string), 0) == -1)) { memcached_quit(ptr); rc= MEMCACHED_SOME_ERRORS; diff --git a/lib/memcached_response.c b/lib/memcached_response.c index 91ae3fe6..ce129aee 100644 --- a/lib/memcached_response.c +++ b/lib/memcached_response.c @@ -21,7 +21,7 @@ memcached_return memcached_response(memcached_st *ptr, while (1) { unsigned int read_length; - read_length= read(ptr->hosts[server_key].fd, buffer_ptr, 1); + read_length= recv(ptr->hosts[server_key].fd, buffer_ptr, 1, 0); if (read_length != 1) return MEMCACHED_UNKNOWN_READ_FAILURE; @@ -72,7 +72,6 @@ memcached_return memcached_response(memcached_st *ptr, default: return MEMCACHED_UNKNOWN_READ_FAILURE; - return MEMCACHED_READ_FAILURE; } return MEMCACHED_SUCCESS; diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index ed2c0e20..717ca455 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -240,7 +240,7 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= write(ptr->hosts[server_key].fd, buffer, send_length); + sent_length= send(ptr->hosts[server_key].fd, buffer, send_length, 0); if (sent_length == -1 || sent_length != send_length) return MEMCACHED_WRITE_FAILURE; diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 1ee6c4ba..499fb7c8 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -37,19 +37,19 @@ static memcached_return memcached_send(memcached_st *ptr, (unsigned long long)expiration, value_length); if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - if ((sent_length= write(ptr->hosts[server_key].fd, buffer, write_length)) == -1) + if ((sent_length= send(ptr->hosts[server_key].fd, buffer, write_length, 0)) == -1) return MEMCACHED_WRITE_FAILURE; assert(write_length == sent_length); - if ((sent_length= write(ptr->hosts[server_key].fd, value, value_length)) == -1) + if ((sent_length= send(ptr->hosts[server_key].fd, value, value_length, 0)) == -1) return MEMCACHED_WRITE_FAILURE; assert(value_length == sent_length); - if ((sent_length= write(ptr->hosts[server_key].fd, "\r\n", 2)) == -1) + if ((sent_length= send(ptr->hosts[server_key].fd, "\r\n", 2, 0)) == -1) return MEMCACHED_WRITE_FAILURE; assert(2 == sent_length); - sent_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + sent_length= recv(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 0); if (sent_length && buffer[0] == 'S') /* STORED */ return MEMCACHED_SUCCESS; diff --git a/lib/memcached_strerror.c b/lib/memcached_strerror.c index c11af89f..854df8b9 100644 --- a/lib/memcached_strerror.c +++ b/lib/memcached_strerror.c @@ -49,6 +49,4 @@ char *memcached_strerror(memcached_st *ptr, memcached_return rc) default: return "Gibberish returned!"; }; - - return "COMPLETELY UNKNOWN ERROR, SOMEONE FORGOT TO UPDATE ERROR MESSAGES"; } diff --git a/lib/memcached_verbosity.c b/lib/memcached_verbosity.c index b68fbbd4..c60e7b22 100644 --- a/lib/memcached_verbosity.c +++ b/lib/memcached_verbosity.c @@ -21,7 +21,7 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) { memcached_return rc; - if ((write(ptr->hosts[x].fd, buffer, send_length) == -1)) + if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1)) { continue; return MEMCACHED_SOME_ERRORS; diff --git a/src/Makefile.am b/src/Makefile.am index d2805688..b8ee1d26 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,21 +1,21 @@ -INCLUDES = -I$(top_builddir)/include -Wall -LDADDS = ../lib/libmemcached.la - -bin_PROGRAMS = memcat memcp memstat memrm memflush - -noinst_HEADERS = client_options.h utilities.h - -memcat_SOURCES = memcat.c utilities.c -memcat_LDADD = $(LDADDS) - -memcp_SOURCES = memcp.c utilities.c -memcp_LDADD = $(LDADDS) - -memstat_SOURCES = memstat.c utilities.c -memstat_LDADD = $(LDADDS) - -memrm_SOURCES = memrm.c utilities.c -memrm_LDADD = $(LDADDS) - -memflush_SOURCES = memflush.c utilities.c -memflush_LDADD = $(LDADDS) +INCLUDES = -I$(top_builddir)/include +LDADDS = ../lib/libmemcached.la + +bin_PROGRAMS = memcat memcp memstat memrm memflush + +noinst_HEADERS = client_options.h utilities.h + +memcat_SOURCES = memcat.c utilities.c +memcat_LDADD = $(LDADDS) + +memcp_SOURCES = memcp.c utilities.c +memcp_LDADD = $(LDADDS) + +memstat_SOURCES = memstat.c utilities.c +memstat_LDADD = $(LDADDS) + +memrm_SOURCES = memrm.c utilities.c +memrm_LDADD = $(LDADDS) + +memflush_SOURCES = memflush.c utilities.c +memflush_LDADD = $(LDADDS) diff --git a/src/memcat.c b/src/memcat.c index d3360676..d263e60d 100644 --- a/src/memcat.c +++ b/src/memcat.c @@ -1,117 +1,117 @@ -#include -#include -#include -#include - -#include "client_options.h" -#include "utilities.h" - - -/* Prototypes */ -void options_parse(int argc, char *argv[]); - -static int opt_verbose= 0; -static int opt_displayflag= 0; -static char *opt_servers; - -int main(int argc, char *argv[]) -{ - memcached_st *memc; - char *string; - size_t string_length; - uint16_t flags; - memcached_return rc; - - memc= memcached_init(NULL); - - options_parse(argc, argv); - - if (opt_servers) - parse_opt_servers(memc, opt_servers); - - while (optind < argc) - { - string= memcached_get(memc, argv[optind], strlen(argv[optind]), - &string_length, &flags, &rc); - if (rc == MEMCACHED_SUCCESS) - { - if (opt_displayflag) - { - if (opt_verbose) - printf("key: %s\nflags: ", argv[optind]); - printf("%x\n", flags); - } - else - { - if (opt_verbose) - printf("key: %s\nflags: %x\nlength: %zu\nvalue: ", - argv[optind], flags, string_length); - printf("%.*s\n", (int)string_length, string); - free(string); - } - } - else - { - fprintf(stderr, "memcat: %s: memcache error %s\n", - argv[optind], memcached_strerror(memc, rc)); - } - optind++; - } - - memcached_deinit(memc); - - free(opt_servers); - - return 0; -}; - - -void options_parse(int argc, char *argv[]) -{ - int option_index= 0; - int option_rv; - - static struct option long_options[]= - { - {"version", no_argument, NULL, OPT_VERSION}, - {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {"servers", required_argument, NULL, OPT_SERVERS}, - {"flag", no_argument, &opt_displayflag, OPT_FLAG}, - {0, 0, 0, 0}, - }; - - while (1) - { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) break; - switch (option_rv) - { - case 0: - break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; - case OPT_VERSION: /* --version or -V */ - printf("memcache tools, memcat, v1.0\n"); - exit(0); - break; - case OPT_HELP: /* --help or -h */ - printf("useful help messages go here\n"); - exit(0); - break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; - case '?': - /* getopt_long already printed an error message. */ - exit(1); - default: - abort(); - } - } -} +#include +#include +#include +#include + +#include "client_options.h" +#include "utilities.h" + + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +static int opt_verbose= 0; +static int opt_displayflag= 0; +static char *opt_servers; + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + char *string; + size_t string_length; + uint16_t flags; + memcached_return rc; + + memc= memcached_init(NULL); + + options_parse(argc, argv); + + if (opt_servers) + parse_opt_servers(memc, opt_servers); + + while (optind < argc) + { + string= memcached_get(memc, argv[optind], strlen(argv[optind]), + &string_length, &flags, &rc); + if (rc == MEMCACHED_SUCCESS) + { + if (opt_displayflag) + { + if (opt_verbose) + printf("key: %s\nflags: ", argv[optind]); + printf("%x\n", flags); + } + else + { + if (opt_verbose) + printf("key: %s\nflags: %x\nlength: %zu\nvalue: ", + argv[optind], flags, string_length); + printf("%.*s\n", (int)string_length, string); + free(string); + } + } + else + { + fprintf(stderr, "memcat: %s: memcache error %s\n", + argv[optind], memcached_strerror(memc, rc)); + } + optind++; + } + + memcached_deinit(memc); + + free(opt_servers); + + return 0; +}; + + +void options_parse(int argc, char *argv[]) +{ + int option_index= 0; + int option_rv; + + static struct option long_options[]= + { + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"flag", no_argument, &opt_displayflag, OPT_FLAG}, + {0, 0, 0, 0}, + }; + + while (1) + { + option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) + { + case 0: + break; + case OPT_VERBOSE: /* --verbose or -v */ + opt_verbose = OPT_VERBOSE; + break; + case OPT_DEBUG: /* --debug or -d */ + opt_verbose = OPT_DEBUG; + break; + case OPT_VERSION: /* --version or -V */ + printf("memcache tools, memcat, v1.0\n"); + exit(0); + break; + case OPT_HELP: /* --help or -h */ + printf("useful help messages go here\n"); + exit(0); + break; + case OPT_SERVERS: /* --servers or -s */ + opt_servers= strdup(optarg); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +} diff --git a/src/memcp.c b/src/memcp.c index 0001713d..3cc0109a 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -1,179 +1,180 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include "client_options.h" -#include "utilities.h" - -/* Prototypes */ -void options_parse(int argc, char *argv[]); - -static int opt_verbose= 0; -static char *opt_servers= NULL; -static int opt_method= OPT_SET; -static uint16_t opt_flags= 0; -static time_t opt_expires= 0; - -int main(int argc, char *argv[]) -{ - memcached_st *memc; - memcached_return rc; - - options_parse(argc, argv); - - memc= memcached_init(NULL); - - if (opt_servers) - parse_opt_servers(memc, opt_servers); - else - parse_opt_servers(memc, argv[--argc]); - - while (optind < argc) - { - struct stat sbuf; - int fd; - char *ptr; - ssize_t read_length; - char *file_buffer_ptr; - - fd= open(argv[optind], O_RDONLY); - if (fd < 0) - { - fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno)); - optind++; - continue; - } - - (void)fstat(fd, &sbuf); - - ptr= rindex(argv[optind], '/'); - if (ptr) - ptr++; - else - ptr= argv[optind]; - - if (opt_verbose) - { - static char *opstr[] = { "set", "add", "replace" }; - printf("op: %s\nsource file: %s\nlength: %zu\n" - "key: %s\nflags: %x\nexpires: %llu\n", - opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size, - ptr, opt_flags, (unsigned long long)opt_expires); - } - - if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL) - { - fprintf(stderr, "malloc: %s\n", strerror(errno)); - exit(1); - } - - if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1) - { - fprintf(stderr, "read: %s\n", strerror(errno)); - exit(1); - } - assert(read_length == sbuf.st_size); - - if (opt_method == OPT_ADD) - rc= memcached_add(memc, ptr, strlen(ptr), - file_buffer_ptr, sbuf.st_size, - opt_expires, opt_flags); - else if (opt_method == OPT_REPLACE) - rc= memcached_replace(memc, ptr, strlen(ptr), - file_buffer_ptr, sbuf.st_size, - opt_expires, opt_flags); - else - rc= memcached_set(memc, ptr, strlen(ptr), - file_buffer_ptr, sbuf.st_size, - opt_expires, opt_flags); - - if (rc != MEMCACHED_SUCCESS) - fprintf(stderr, "memcp: %s: memcache error %s\n", - ptr, memcached_strerror(memc, rc)); - - free(file_buffer_ptr); - close(fd); - optind++; - } - - memcached_deinit(memc); - - free(opt_servers); - - return 0; -} - -void options_parse(int argc, char *argv[]) -{ - int option_index= 0; - int option_rv; - - static struct option long_options[]= - { - {"version", no_argument, NULL, OPT_VERSION}, - {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {"servers", required_argument, NULL, OPT_SERVERS}, - {"flag", required_argument, NULL, OPT_FLAG}, - {"expire", required_argument, NULL, OPT_EXPIRE}, - {"set", no_argument, NULL, OPT_SET}, - {"add", no_argument, NULL, OPT_ADD}, - {"replace", no_argument, NULL, OPT_REPLACE}, - {0, 0, 0, 0}, - }; - - while (1) - { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - - if (option_rv == -1) break; - - switch (option_rv) - { - case 0: - break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; - case OPT_VERSION: /* --version or -V */ - printf("memcache tools, memcp, v1.0\n"); - exit(0); - case OPT_HELP: /* --help or -h */ - printf("useful help messages go here\n"); - exit(0); - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; - case OPT_FLAG: /* --flag */ - opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16); - break; - case OPT_EXPIRE: /* --expire */ - opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10); - break; - case OPT_SET: - opt_method= OPT_SET; - break; - case OPT_REPLACE: - opt_method= OPT_REPLACE; - break; - case OPT_ADD: - opt_method= OPT_ADD; - break; - case '?': - /* getopt_long already printed an error message. */ - exit(1); - default: - abort(); - } - } -} +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "client_options.h" +#include "utilities.h" + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +static int opt_verbose= 0; +static char *opt_servers= NULL; +static int opt_method= OPT_SET; +static uint16_t opt_flags= 0; +static time_t opt_expires= 0; + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + memcached_return rc; + + options_parse(argc, argv); + + memc= memcached_init(NULL); + + if (opt_servers) + parse_opt_servers(memc, opt_servers); + else + parse_opt_servers(memc, argv[--argc]); + + while (optind < argc) + { + struct stat sbuf; + int fd; + char *ptr; + ssize_t read_length; + char *file_buffer_ptr; + + fd= open(argv[optind], O_RDONLY); + if (fd < 0) + { + fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno)); + optind++; + continue; + } + + (void)fstat(fd, &sbuf); + + ptr= rindex(argv[optind], '/'); + if (ptr) + ptr++; + else + ptr= argv[optind]; + + if (opt_verbose) + { + static char *opstr[] = { "set", "add", "replace" }; + printf("op: %s\nsource file: %s\nlength: %zu\n" + "key: %s\nflags: %x\nexpires: %llu\n", + opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size, + ptr, opt_flags, (unsigned long long)opt_expires); + } + + if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL) + { + fprintf(stderr, "malloc: %s\n", strerror(errno)); + exit(1); + } + + if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1) + { + fprintf(stderr, "read: %s\n", strerror(errno)); + exit(1); + } + assert(read_length == sbuf.st_size); + + if (opt_method == OPT_ADD) + rc= memcached_add(memc, ptr, strlen(ptr), + file_buffer_ptr, sbuf.st_size, + opt_expires, opt_flags); + else if (opt_method == OPT_REPLACE) + rc= memcached_replace(memc, ptr, strlen(ptr), + file_buffer_ptr, sbuf.st_size, + opt_expires, opt_flags); + else + rc= memcached_set(memc, ptr, strlen(ptr), + file_buffer_ptr, sbuf.st_size, + opt_expires, opt_flags); + + if (rc != MEMCACHED_SUCCESS) + fprintf(stderr, "memcp: %s: memcache error %s\n", + ptr, memcached_strerror(memc, rc)); + + free(file_buffer_ptr); + close(fd); + optind++; + } + + memcached_deinit(memc); + + free(opt_servers); + + return 0; +} + +void options_parse(int argc, char *argv[]) +{ + int option_index= 0; + int option_rv; + + static struct option long_options[]= + { + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"flag", required_argument, NULL, OPT_FLAG}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {"set", no_argument, NULL, OPT_SET}, + {"add", no_argument, NULL, OPT_ADD}, + {"replace", no_argument, NULL, OPT_REPLACE}, + {0, 0, 0, 0}, + }; + + while (1) + { + option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + + if (option_rv == -1) break; + + switch (option_rv) + { + case 0: + break; + case OPT_VERBOSE: /* --verbose or -v */ + opt_verbose = OPT_VERBOSE; + break; + case OPT_DEBUG: /* --debug or -d */ + opt_verbose = OPT_DEBUG; + break; + case OPT_VERSION: /* --version or -V */ + printf("memcache tools, memcp, v1.0\n"); + exit(0); + case OPT_HELP: /* --help or -h */ + printf("useful help messages go here\n"); + exit(0); + case OPT_SERVERS: /* --servers or -s */ + opt_servers= strdup(optarg); + break; + case OPT_FLAG: /* --flag */ + opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16); + break; + case OPT_EXPIRE: /* --expire */ + opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10); + break; + case OPT_SET: + opt_method= OPT_SET; + break; + case OPT_REPLACE: + opt_method= OPT_REPLACE; + break; + case OPT_ADD: + opt_method= OPT_ADD; + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +} diff --git a/src/memflush.c b/src/memflush.c index 95e3e986..b864bf60 100644 --- a/src/memflush.c +++ b/src/memflush.c @@ -1,92 +1,92 @@ -#include -#include -#include -#include -#include "client_options.h" -#include "utilities.h" - -static int opt_verbose= 0; -static time_t opt_expire= 0; -static char *opt_servers= NULL; - -/* Prototypes */ -void options_parse(int argc, char *argv[]); - -int main(int argc, char *argv[]) -{ - memcached_st *memc; - memcached_return rc; - - options_parse(argc, argv); - - memc= memcached_init(NULL); - - if (opt_servers) - parse_opt_servers(memc, opt_servers); - - rc = memcached_flush(memc, opt_expire); - if (rc != MEMCACHED_SUCCESS) - { - fprintf(stderr, "memflush: memcache error %s\n", - memcached_strerror(memc, rc)); - } - - memcached_deinit(memc); - - free(opt_servers); - - return 0; -} - - -void options_parse(int argc, char *argv[]) -{ - static struct option long_options[]= - { - {"version", no_argument, NULL, OPT_VERSION}, - {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {"servers", required_argument, NULL, OPT_SERVERS}, - {"expire", required_argument, NULL, OPT_EXPIRE}, - {0, 0, 0, 0}, - }; - int option_index= 0; - int option_rv; - - while (1) - { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) break; - switch (option_rv) - { - case 0: - break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; - case OPT_VERSION: /* --version or -V */ - printf("memcache tools, memflush, v1.0\n"); - exit(0); - break; - case OPT_HELP: /* --help or -h */ - printf("useful help messages go here\n"); - exit(0); - break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; - case OPT_EXPIRE: /* --expire */ - opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); - break; - case '?': - /* getopt_long already printed an error message. */ - exit(1); - default: - abort(); - } - } -} +#include +#include +#include +#include +#include "client_options.h" +#include "utilities.h" + +static int opt_verbose= 0; +static time_t opt_expire= 0; +static char *opt_servers= NULL; + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + memcached_return rc; + + options_parse(argc, argv); + + memc= memcached_init(NULL); + + if (opt_servers) + parse_opt_servers(memc, opt_servers); + + rc = memcached_flush(memc, opt_expire); + if (rc != MEMCACHED_SUCCESS) + { + fprintf(stderr, "memflush: memcache error %s\n", + memcached_strerror(memc, rc)); + } + + memcached_deinit(memc); + + free(opt_servers); + + return 0; +} + + +void options_parse(int argc, char *argv[]) +{ + static struct option long_options[]= + { + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {0, 0, 0, 0}, + }; + int option_index= 0; + int option_rv; + + while (1) + { + option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) + { + case 0: + break; + case OPT_VERBOSE: /* --verbose or -v */ + opt_verbose = OPT_VERBOSE; + break; + case OPT_DEBUG: /* --debug or -d */ + opt_verbose = OPT_DEBUG; + break; + case OPT_VERSION: /* --version or -V */ + printf("memcache tools, memflush, v1.0\n"); + exit(0); + break; + case OPT_HELP: /* --help or -h */ + printf("useful help messages go here\n"); + exit(0); + break; + case OPT_SERVERS: /* --servers or -s */ + opt_servers= strdup(optarg); + break; + case OPT_EXPIRE: /* --expire */ + opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +} diff --git a/src/memrm.c b/src/memrm.c index 005a913d..db992914 100644 --- a/src/memrm.c +++ b/src/memrm.c @@ -1,100 +1,100 @@ -#include -#include -#include -#include -#include "client_options.h" -#include "utilities.h" - -static int opt_verbose= 0; -static time_t opt_expire= 0; -static char *opt_servers= NULL; - -/* Prototypes */ -void options_parse(int argc, char *argv[]); - -int main(int argc, char *argv[]) -{ - memcached_st *memc; - memcached_return rc; - - options_parse(argc, argv); - - memc= memcached_init(NULL); - - if (opt_servers) - parse_opt_servers(memc, opt_servers); - - while (optind < argc) - { - if (opt_verbose) - printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire); - rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); - - if (rc != MEMCACHED_SUCCESS) - { - fprintf(stderr, "memrm: %s: memcache error %s\n", - argv[optind], memcached_strerror(memc, rc)); - } - - optind++; - } - - memcached_deinit(memc); - - free(opt_servers); - - return 0; -} - - -void options_parse(int argc, char *argv[]) -{ - static struct option long_options[]= - { - {"version", no_argument, NULL, OPT_VERSION}, - {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {"servers", required_argument, NULL, OPT_SERVERS}, - {"expire", required_argument, NULL, OPT_EXPIRE}, - {0, 0, 0, 0}, - }; - int option_index= 0; - int option_rv; - - while (1) - { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) break; - switch (option_rv) - { - case 0: - break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; - case OPT_VERSION: /* --version or -V */ - printf("memcache tools, memrm, v1.0\n"); - exit(0); - break; - case OPT_HELP: /* --help or -h */ - printf("useful help messages go here\n"); - exit(0); - break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; - case OPT_EXPIRE: /* --expire */ - opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); - break; - case '?': - /* getopt_long already printed an error message. */ - exit(1); - default: - abort(); - } - } -} +#include +#include +#include +#include +#include "client_options.h" +#include "utilities.h" + +static int opt_verbose= 0; +static time_t opt_expire= 0; +static char *opt_servers= NULL; + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + memcached_return rc; + + options_parse(argc, argv); + + memc= memcached_init(NULL); + + if (opt_servers) + parse_opt_servers(memc, opt_servers); + + while (optind < argc) + { + if (opt_verbose) + printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire); + rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); + + if (rc != MEMCACHED_SUCCESS) + { + fprintf(stderr, "memrm: %s: memcache error %s\n", + argv[optind], memcached_strerror(memc, rc)); + } + + optind++; + } + + memcached_deinit(memc); + + free(opt_servers); + + return 0; +} + + +void options_parse(int argc, char *argv[]) +{ + static struct option long_options[]= + { + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {0, 0, 0, 0}, + }; + int option_index= 0; + int option_rv; + + while (1) + { + option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) + { + case 0: + break; + case OPT_VERBOSE: /* --verbose or -v */ + opt_verbose = OPT_VERBOSE; + break; + case OPT_DEBUG: /* --debug or -d */ + opt_verbose = OPT_DEBUG; + break; + case OPT_VERSION: /* --version or -V */ + printf("memcache tools, memrm, v1.0\n"); + exit(0); + break; + case OPT_HELP: /* --help or -h */ + printf("useful help messages go here\n"); + exit(0); + break; + case OPT_SERVERS: /* --servers or -s */ + opt_servers= strdup(optarg); + break; + case OPT_EXPIRE: /* --expire */ + opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +} diff --git a/src/utilities.c b/src/utilities.c index 4f5b4526..159e97df 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -1,4 +1,5 @@ #include +#include #include void parse_opt_servers(memcached_st *memc, diff --git a/tests/Makefile.am b/tests/Makefile.am index ec02a49b..e0a25e74 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,31 +1,31 @@ -INCLUDES = -I$(top_builddir)/include -LDADDS = ../lib/libmemcached.la - -EXTRA_DIST = output.res output2.res - -LIBS = - -noinst_PROGRAMS = testapp - -testapp_SOURCES = test.c -testapp_LDADD = $(LDADDS) - -record: - ./testapp > output.res - -record-extended: - ./testapp extended > output2.res - -test: testapp - ./testapp > output.cmp - diff output.res output.cmp - -test-extended: testapp - ./testapp extended > output.cmp - diff output2.res output.cmp - -valgrind: - libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp - -valgrind-extended: - libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp extended +INCLUDES = -I$(top_builddir)/include +LDADDS = ../lib/libmemcached.la + +EXTRA_DIST = output.res output2.res + +LIBS = + +noinst_PROGRAMS = testapp + +testapp_SOURCES = test.c +testapp_LDADD = $(LDADDS) + +record: + ./testapp > output.res + +record-extended: + ./testapp extended > output2.res + +test: testapp + ./testapp > output.cmp + diff output.res output.cmp + +test-extended: testapp + ./testapp extended > output.cmp + diff output2.res output.cmp + +valgrind: + libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp + +valgrind-extended: + libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp extended diff --git a/tests/test.c b/tests/test.c index dabb00f4..abd4c8ae 100644 --- a/tests/test.c +++ b/tests/test.c @@ -29,7 +29,7 @@ void connection_test(void) memcached_st *memc; memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); assert(memc); memcached_deinit(memc); @@ -59,7 +59,7 @@ void set_test(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); rc= memcached_set(memc, key, strlen(key), value, strlen(value), @@ -78,7 +78,7 @@ void add_test(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); rc= memcached_add(memc, key, strlen(key), value, strlen(value), @@ -97,7 +97,7 @@ void replace_test(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); rc= memcached_replace(memc, key, strlen(key), value, strlen(value), @@ -116,7 +116,7 @@ void delete_test(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); rc= memcached_set(memc, key, strlen(key), value, strlen(value), @@ -136,7 +136,7 @@ void flush_test(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); rc= memcached_flush(memc, 0); assert(rc == MEMCACHED_SUCCESS); @@ -155,7 +155,7 @@ void get_test(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); string= memcached_get(memc, key, strlen(key), @@ -180,7 +180,7 @@ void get_test2(void) memc= memcached_init(NULL); assert(memc); - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add(memc, "zim.tangent.org", 0); assert(rc == MEMCACHED_SUCCESS); rc= memcached_set(memc, key, strlen(key), @@ -211,15 +211,17 @@ void get_test3(void) char *string; size_t string_length; uint16_t flags; - int i; + int x; - value = (char*) malloc(value_length); - for (i=0; i