X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemcp.cc;h=4ad0919d008a540d763cea73718c71762aa1a7fe;hb=2569efffe16042f47dc5d2c34528a9ec22fc18b0;hp=228362513905beabef0108285301d6c48f3d741f;hpb=fd632c80c5d393253d394c9a8721339876602882;p=awesomized%2Flibmemcached diff --git a/clients/memcp.cc b/clients/memcp.cc index 22836251..4ad0919d 100644 --- a/clients/memcp.cc +++ b/clients/memcp.cc @@ -1,4 +1,5 @@ /* LibMemcached + * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ * Copyright (C) 2006-2009 Brian Aker * All rights reserved. * @@ -30,7 +31,7 @@ #include -#include +#include #include "client_options.h" #include "utilities.h" @@ -41,7 +42,9 @@ /* Prototypes */ static void options_parse(int argc, char *argv[]); -static int opt_binary=0; +static bool opt_binary= false; +static bool opt_udp= false; +static bool opt_buffer= false; static int opt_verbose= 0; static char *opt_servers= NULL; static char *opt_hash= NULL; @@ -61,8 +64,8 @@ static long strtol_wrapper(const char *nptr, int base, bool *error) /* Check for various possible errors */ - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) - || (errno != 0 && val == 0)) + if ((errno == ERANGE and (val == LONG_MAX or val == LONG_MIN)) + or (errno != 0 && val == 0)) { *error= true; return EXIT_SUCCESS; @@ -80,19 +83,45 @@ static long strtol_wrapper(const char *nptr, int base, bool *error) 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(); - memc= memcached_create(NULL); + memcached_st *memc= memcached_create(NULL); + + if (opt_udp) + { + if (opt_verbose) + { + std::cout << "Enabling UDP" << std::endl; + } + + if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp))) + { + memcached_free(memc); + std::cerr << "Could not enable UDP protocol." << std::endl; + return EXIT_FAILURE; + } + } + + if (opt_buffer) + { + if (opt_verbose) + { + std::cout << "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl; + } + + if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, opt_buffer))) + { + memcached_free(memc); + std::cerr << "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl; + return EXIT_FAILURE; + } + } + process_hash_option(memc, opt_hash); - if (!opt_servers) + if (opt_servers == NULL) { char *temp; @@ -102,21 +131,24 @@ int main(int argc, char *argv[]) } else { - fprintf(stderr, "No Servers provided\n"); - exit(1); + std::cerr << "No Servers provided" << std::endl; + exit(EXIT_FAILURE); } } + memcached_server_st *servers; if (opt_servers) + { servers= memcached_servers_parse(opt_servers); + } else + { servers= memcached_servers_parse(argv[--argc]); + } memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t)opt_binary); - + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary); if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); @@ -135,29 +167,33 @@ int main(int argc, char *argv[]) } } + int exit_code= EXIT_SUCCESS; while (optind < argc) { - struct stat sbuf; - int fd; - char *ptr; - ssize_t read_length; - char *file_buffer_ptr; - - fd= open(argv[optind], O_RDONLY); + int fd= open(argv[optind], O_RDONLY); if (fd < 0) { - fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno)); - optind++; + if (opt_verbose) + { + std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl; + optind++; + } + exit_code= EXIT_FAILURE; continue; } + struct stat sbuf; (void)fstat(fd, &sbuf); - ptr= rindex(argv[optind], '/'); + char *ptr= rindex(argv[optind], '/'); if (ptr) + { ptr++; + } else + { ptr= argv[optind]; + } if (opt_verbose) { @@ -168,68 +204,82 @@ int main(int argc, char *argv[]) ptr, opt_flags, (unsigned long)opt_expires); } + char *file_buffer_ptr; if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL) { - fprintf(stderr, "malloc: %s\n", strerror(errno)); - exit(1); + std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl; + close(fd); + exit(EXIT_FAILURE); } - if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1) + ssize_t read_length; + if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1) { - fprintf(stderr, "read: %s\n", strerror(errno)); - exit(1); + std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl; + close(fd); + exit(EXIT_FAILURE); } if (read_length != sbuf.st_size) { - fprintf(stderr, "Failure reading from file\n"); - exit(1); + std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl; + close(fd); + exit(EXIT_FAILURE); } + memcached_return_t rc; if (opt_method == OPT_ADD) + { rc= memcached_add(memc, ptr, strlen(ptr), file_buffer_ptr, (size_t)sbuf.st_size, opt_expires, opt_flags); + } else if (opt_method == OPT_REPLACE) + { rc= memcached_replace(memc, ptr, strlen(ptr), file_buffer_ptr, (size_t)sbuf.st_size, opt_expires, opt_flags); + } else + { rc= memcached_set(memc, ptr, strlen(ptr), file_buffer_ptr, (size_t)sbuf.st_size, opt_expires, opt_flags); + } - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { - fprintf(stderr, "memcp: %s: memcache error %s", - ptr, memcached_strerror(memc, rc)); - if (memcached_last_error_errno(memc)) - fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc))); - fprintf(stderr, "\n"); - - return_code= -1; + std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl; + exit_code= EXIT_FAILURE; } - free(file_buffer_ptr); - close(fd); + ::free(file_buffer_ptr); + ::close(fd); optind++; } + if (opt_verbose) + { + std::cout << "Calling memcached_free()" << std::endl; + } + memcached_free(memc); if (opt_servers) + { free(opt_servers); + } + if (opt_hash) + { free(opt_hash); + } - return return_code; + return exit_code; } static void options_parse(int argc, char *argv[]) { - int option_index= 0; - int option_rv; - memcached_programs_help_st help_options[]= { {0}, @@ -239,6 +289,9 @@ 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)"udp", no_argument, NULL, OPT_UDP}, + {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER}, {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, @@ -254,34 +307,46 @@ static void options_parse(int argc, char *argv[]) {0, 0, 0, 0}, }; + bool opt_version= false; + bool opt_help= false; + int option_index= 0; + while (1) { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) break; + if (option_rv == -1) + break; switch (option_rv) { case 0: break; + case OPT_BINARY: - opt_binary = 1; + opt_binary= true; break; + case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; + opt_verbose= OPT_VERBOSE; break; + case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; + 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_FLAG: /* --flag */ { bool strtol_error; @@ -291,8 +356,9 @@ static void options_parse(int argc, char *argv[]) fprintf(stderr, "Bad value passed via --flag\n"); exit(1); } - break; } + break; + case OPT_EXPIRE: /* --expire */ { bool strtol_error; @@ -303,29 +369,61 @@ static void options_parse(int argc, char *argv[]) exit(1); } } + 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 OPT_HASH: opt_hash= strdup(optarg); break; + case OPT_USERNAME: opt_username= optarg; break; + case OPT_PASSWD: opt_passwd= optarg; break; - case '?': + + case OPT_QUIET: + close_stdio(); + break; + + case OPT_UDP: + opt_udp= true; + break; + + case OPT_BUFFER: + opt_buffer= true; + break; + + case '?': /* getopt_long already printed an error message. */ exit(1); 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); + } }