From: Date: Sat, 22 Sep 2007 04:14:02 +0000 (+0200) Subject: Removed dumb bug concerning set (was still using a buffer). X-Git-Tag: 0.2~11 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=ca065f55235fd9979ede818963780f417d70c64e;p=m6w6%2Flibmemcached Removed dumb bug concerning set (was still using a buffer). Cleanup method for string Moved from send to write. More enum (raw numbers bad!) --- diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index 268f95b8..975be824 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -23,7 +23,7 @@ static memcached_return memcached_auto(memcached_st *ptr, key_length, key, offset); - if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) + if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index 592ede44..d015c68a 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -10,7 +10,6 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigne ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (ptr->number_of_hosts+1)); - memset(ptr->hosts, 0, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts); ptr->hosts[ptr->number_of_hosts].hostname= (char *)malloc(sizeof(char) * (strlen(hostname)+1)); memset(ptr->hosts[ptr->number_of_hosts].hostname, 0, strlen(hostname)+1); diff --git a/lib/memcached_flush.c b/lib/memcached_flush.c index f7527a7c..a3e1be0b 100644 --- a/lib/memcached_flush.c +++ b/lib/memcached_flush.c @@ -20,7 +20,7 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) else send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "flush_all\r\n"); - if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1)) + if ((write(ptr->hosts[x].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed flush_all TCP\n"); diff --git a/lib/memcached_get.c b/lib/memcached_get.c index c688af34..36d96505 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -23,7 +23,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, if (*error != MEMCACHED_SUCCESS) return NULL; - if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) + if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key); *error= MEMCACHED_WRITE_FAILURE; diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index 2511532c..8928ee09 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -136,7 +136,7 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, send_length= snprintf(buffer, HUGE_STRING_LEN, "stats\r\n"); - if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) + if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed on stats\n"); diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 3df4913a..5c34a573 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -16,43 +16,55 @@ static memcached_return memcached_send(memcached_st *ptr, uint16_t flags, char *verb) { - size_t send_length; + size_t write_length; + ssize_t sent_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; rc= memcached_connect(ptr); + assert(value); + assert(value_length); if (rc != MEMCACHED_SUCCESS) return rc; server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts; - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %.*s %u %u %u\r\n", verb, key_length, key, flags, expiration, value_length); - if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) + if ((sent_length= write(ptr->hosts[server_key].fd, buffer, write_length)) == -1) { - fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); + fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno)); return MEMCACHED_WRITE_FAILURE; } + assert(write_length == sent_length); - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "%.*s\r\n", - value_length, value); - if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) + WATCHPOINT; + printf("About to push %.*s\n", value_length, value); + WATCHPOINT; + if ((sent_length= write(ptr->hosts[server_key].fd, value, value_length)) == -1) { - fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); + fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno)); return MEMCACHED_WRITE_FAILURE; } - - send_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + assert(value_length == sent_length); + if ((sent_length= write(ptr->hosts[server_key].fd, "\r\n", 2)) == -1) + { + fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno)); + + return MEMCACHED_WRITE_FAILURE; + } + assert(2 == sent_length); + + sent_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); - if (send_length && buffer[0] == 'S') /* STORED */ + if (sent_length && buffer[0] == 'S') /* STORED */ return MEMCACHED_SUCCESS; - else if (send_length && buffer[0] == 'N') /* NOT_STORED */ + else if (write_length && buffer[0] == 'N') /* NOT_STORED */ return MEMCACHED_NOTSTORED; else return MEMCACHED_READ_FAILURE; diff --git a/lib/memcached_verbosity.c b/lib/memcached_verbosity.c index 4e586cfb..bb4f68c0 100644 --- a/lib/memcached_verbosity.c +++ b/lib/memcached_verbosity.c @@ -19,7 +19,7 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) { memcached_return rc; - if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1)) + if ((write(ptr->hosts[x].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed verbosity\n"); diff --git a/src/memcat.c b/src/memcat.c index 1a6e3898..4253aa00 100644 --- a/src/memcat.c +++ b/src/memcat.c @@ -26,7 +26,8 @@ int main(int argc, char *argv[]) options_parse(argc, argv); - parse_opt_servers(memc, opt_servers); + if (opt_servers) + parse_opt_servers(memc, opt_servers); while (optind <= argc) { @@ -37,13 +38,13 @@ int main(int argc, char *argv[]) if (opt_displayflag) { if (opt_verbose) - printf("key: %.*s\nflags: ", argv[optind]); - printf("%d\n", flags); + printf("key: %s\nflags: ", argv[optind]); + printf("%u\n", flags); } else { if (opt_verbose) - printf("key: %.*s\nflags: %d\nlength: %d\nvalue: ", + printf("key: %s\nflags: %u\nlength: %uz\nvalue: ", argv[optind], flags, string_length); printf("%.*s\n", string_length, string); free(string); @@ -72,8 +73,8 @@ void options_parse(int argc, char *argv[]) { {"version", no_argument, NULL, OPT_VERSION}, {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, 1}, - {"debug", no_argument, &opt_verbose, 2}, + {"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}, diff --git a/src/memcp.c b/src/memcp.c index fc396ff1..bba346b9 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -14,9 +13,9 @@ /* Prototypes */ void options_parse(int argc, char *argv[]); -static int opt_verbose; -static char *opt_servers; -static int opt_replace; +static int opt_verbose= 0; +static char *opt_servers= NULL; +static int opt_method= 0; uint16_t opt_flags= 0; time_t opt_expires= 0; @@ -26,34 +25,31 @@ int main(int argc, char *argv[]) char *string; size_t string_length; memcached_return rc; - options_parse(argc, argv); memc= memcached_init(NULL); - parse_opt_servers(memc, opt_servers); + if (opt_servers) + parse_opt_servers(memc, opt_servers); + else + parse_opt_servers(memc, argv[--argc]); - while (optind <= argc) + while (optind < argc) { - char *mptr; struct stat sbuf; int fd; char *ptr; + ssize_t read_length; + char *file_buffer_ptr; fd= open(argv[optind], O_RDONLY); - if (fd < 0) { + if (fd < 0) + { fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno)); optind++; continue; } (void)fstat(fd, &sbuf); - mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (mptr == MAP_FAILED) { - fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno)); - close(fd); - optind++; - continue; - } ptr= rindex(argv[optind], '/'); if (ptr) @@ -61,41 +57,55 @@ int main(int argc, char *argv[]) else ptr= argv[optind]; - if (opt_verbose) { + if (opt_verbose) + { static char *opstr[] = { "set", "add", "replace" }; - printf("op: %s\nsource file: %s\nlength: %d\n" - "key: %s\nflags: %d\n expires: %ld\n", - opstr[opt_replace], argv[optind], sbuf.st_size, + printf("op: %s\nsource file: %s\nlength: %zu\n" + "key: %s\nflags: %u\n expires: %llu\n", + opstr[opt_method], argv[optind], sbuf.st_size, ptr, opt_flags, opt_expires); } - if (opt_replace == 0) - rc= memcached_set(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, - opt_expires, opt_flags); - else if (opt_replace == 1) + 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), - mptr, sbuf.st_size, + file_buffer_ptr, sbuf.st_size, opt_expires, opt_flags); - else if (opt_replace == 2) + else if (opt_method == OPT_REPLACE) rc= memcached_replace(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, + file_buffer_ptr, sbuf.st_size, opt_expires, opt_flags); else - abort(); + rc= memcached_set(memc, ptr, strlen(ptr), + file_buffer_ptr, sbuf.st_size, + opt_expires, opt_flags); - if (rc != MEMCACHED_SUCCESS) { + if (rc != MEMCACHED_SUCCESS) fprintf(stderr, "memcp: %s: memcache error %s\n", ptr, memcached_strerror(memc, rc)); - } - munmap(mptr, sbuf.st_size); + WATCHPOINT; + free(file_buffer_ptr); close(fd); optind++; } memcached_deinit(memc); + cleanup(); + return 0; }; @@ -113,9 +123,9 @@ void options_parse(int argc, char *argv[]) {"servers", required_argument, NULL, OPT_SERVERS}, {"flag", required_argument, NULL, OPT_FLAG}, {"expire", required_argument, NULL, OPT_EXPIRE}, - {"set", no_argument, &opt_replace, OPT_SET}, - {"add", no_argument, &opt_replace, OPT_ADD}, - {"replace", no_argument, &opt_replace, OPT_REPLACE}, + {"set", no_argument, NULL, OPT_SET}, + {"add", no_argument, NULL, OPT_ADD}, + {"replace", no_argument, NULL, OPT_REPLACE}, {0, 0, 0, 0}, }; @@ -135,7 +145,7 @@ void options_parse(int argc, char *argv[]) printf("useful help messages go here\n"); exit(0); case OPT_SERVERS: /* --servers or -s */ - opt_servers= optarg; + opt_servers= strdup_cleanup(optarg); break; case OPT_FLAG: /* --flag */ opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10); @@ -143,6 +153,15 @@ void options_parse(int argc, char *argv[]) case OPT_EXPIRE: /* --expire */ opt_expires= (time_t)strtol(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); diff --git a/src/memrm.c b/src/memrm.c index 49172e5a..4a3da021 100644 --- a/src/memrm.c +++ b/src/memrm.c @@ -19,8 +19,8 @@ int main(int argc, char *argv[]) { {"version", no_argument, NULL, OPT_VERSION}, {"help", no_argument, NULL, OPT_HELP}, - {"verbose", no_argument, &opt_verbose, 1}, - {"debug", no_argument, &opt_verbose, 2}, + {"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}, @@ -62,12 +62,12 @@ int main(int argc, char *argv[]) while (optind <= argc) { - if (opt_verbose) { - printf("key: %s\nexpires: %ld\n", argv[optind], expires); - } + if (opt_verbose) + printf("key: %s\nexpires: %llu\n", argv[optind], expires); rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), expires); - if (rc != MEMCACHED_SUCCESS) { + if (rc != MEMCACHED_SUCCESS) + { fprintf(stderr, "memrm: %s: memcache error %s\n", argv[optind], memcached_strerror(memc, rc)); } diff --git a/src/utilities.c b/src/utilities.c index 5dd66f62..ce6cd925 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -1,14 +1,23 @@ #include +static char **cleanup_list= NULL; +static char cleanup_list_length= 0; + void parse_opt_servers(memcached_st *memc, - char *opt_servers) + char *server_strings) { char *string; unsigned int port; char *begin_ptr; + char *end_ptr; + + end_ptr= server_strings + strlen(server_strings); + + assert(server_strings); + assert(memc); - for (begin_ptr= opt_servers, string= index(opt_servers, ','); - begin_ptr[0]; + for (begin_ptr= server_strings, string= index(server_strings, ','); + begin_ptr == end_ptr; begin_ptr= ++string, string= index(begin_ptr, ',')) { char buffer[HUGE_STRING_LEN]; @@ -21,9 +30,9 @@ void parse_opt_servers(memcached_st *memc, } else { - size_t length= strlen(opt_servers); + size_t length= strlen(server_strings); memcpy(buffer, begin_ptr, length); - string= opt_servers+length; + string= server_strings+length; } ptr= index(buffer, ':'); @@ -45,3 +54,24 @@ void parse_opt_servers(memcached_st *memc, } } +void cleanup(void) +{ + unsigned int x; + for (x= 0; x < cleanup_list_length; x++) + free(cleanup_list[x]); + + free(cleanup_list); +} + +char *strdup_cleanup(const char *str) +{ + char *ptr; + + ptr= strdup(str); + + cleanup_list= (char **)realloc(cleanup_list, sizeof(char *) * (cleanup_list_length+1)); + cleanup_list[cleanup_list_length]= ptr; + cleanup_list_length++; + + return ptr; +} diff --git a/src/utilities.h b/src/utilities.h index fe797817..7d442f23 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -2,3 +2,5 @@ void parse_opt_servers (memcached_st *ptr, char *opt_servers); +char *strdup_cleanup(const char *str); +void cleanup(void);