X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemrm.cc;h=50ead7a380679a09b61746849c9a6f642cf510cd;hb=1f483f29cd415fdb749a5571d9e1483dd11a2ab0;hp=73a29de96d9fbd9e646180f8c3f35edcce0deab2;hpb=58c279d58e2a44562f729e93e301dfedf42f530b;p=awesomized%2Flibmemcached diff --git a/clients/memrm.cc b/clients/memrm.cc index 73a29de9..50ead7a3 100644 --- a/clients/memrm.cc +++ b/clients/memrm.cc @@ -1,4 +1,5 @@ /* LibMemcached + * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ * Copyright (C) 2006-2009 Brian Aker * All rights reserved. * @@ -8,15 +9,16 @@ * Summary: * */ -#include "config.h" +#include "mem_config.h" +#include #include #include #include #include #include -#include +#include #include "client_options.h" #include "utilities.h" @@ -36,32 +38,35 @@ static void options_parse(int argc, char *argv[]); int main(int argc, char *argv[]) { - memcached_st *memc; - memcached_return_t rc; - memcached_server_st *servers; - - int return_code= 0; - options_parse(argc, argv); initialize_sockets(); - if (opt_servers == 0) + if (opt_servers == NULL) { char *temp; if ((temp= getenv("MEMCACHED_SERVERS"))) + { opt_servers= strdup(temp); - else + } + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } - memc= memcached_create(NULL); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } + + memcached_st* memc= memcached_create(NULL); process_hash_option(memc, opt_hash); - servers= memcached_servers_parse(opt_servers); memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, @@ -85,26 +90,39 @@ int main(int argc, char *argv[]) } } + int return_code= EXIT_SUCCESS; + while (optind < argc) { - if (opt_verbose) + memcached_return_t rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); + + if (rc == MEMCACHED_NOTFOUND) { - std::cout << "key: " << argv[optind] << std::endl; - std::cout << "expires: " << opt_expire << std::endl; + if (opt_verbose) + { + std::cerr << "Could not find key \"" << argv[optind] << "\"" << std::endl; + } } - rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); - - if (memcached_failed(rc)) + else if (memcached_fatal(rc)) { - std::cerr << PROGRAM_NAME << ": " << argv[optind] << ": error " << memcached_strerror(memc, rc) << std::endl; - - if (memcached_last_error_errno(memc)) + if (opt_verbose) { - std::cerr << " system error " << strerror(memcached_last_error_errno(memc)); + std::cerr << "Failed to delete key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl; } - std::cerr << std::endl; - return_code= -1; + return_code= EXIT_FAILURE; + } + else // success + { + if (opt_verbose) + { + std::cout << "Deleted key " << argv[optind]; + if (opt_expire) + { + std::cout << " expires: " << opt_expire << std::endl; + } + std::cout << std::endl; + } } optind++; @@ -113,10 +131,14 @@ int main(int argc, char *argv[]) memcached_free(memc); if (opt_servers) + { free(opt_servers); + } if (opt_hash) + { free(opt_hash); + } return return_code; } @@ -133,6 +155,7 @@ static void options_parse(int argc, char *argv[]) { {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, @@ -143,52 +166,92 @@ static void options_parse(int argc, char *argv[]) {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, {0, 0, 0, 0}, }; + + bool opt_version= false; + bool opt_help= false; 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; + int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) + { + break; + } + switch (option_rv) { case 0: break; + case OPT_BINARY: opt_binary = 1; 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 */ - version_command(PROGRAM_NAME); + opt_version= true; break; + case OPT_HELP: /* --help or -h */ - help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); + opt_help= true; break; + case OPT_SERVERS: /* --servers or -s */ opt_servers= strdup(optarg); break; + case OPT_EXPIRE: /* --expire */ + errno= 0; opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); + if (errno != 0) + { + std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl; + exit(EXIT_FAILURE); + } break; + case OPT_HASH: opt_hash= strdup(optarg); break; + case OPT_USERNAME: opt_username= optarg; break; + case OPT_PASSWD: opt_passwd= optarg; break; + + case OPT_QUIET: + close_stdio(); + break; + case '?': /* getopt_long already printed an error message. */ - exit(1); + exit(EXIT_SUCCESS); + default: abort(); } } + + if (opt_version) + { + version_command(PROGRAM_NAME); + exit(EXIT_SUCCESS); + } + + if (opt_help) + { + help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); + exit(EXIT_SUCCESS); + } }