X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fmemcp.c;h=bba346b973f047856fb1eff95df45e0f8ff177c9;hb=ca065f55235fd9979ede818963780f417d70c64e;hp=52c373a1d0905b9aee6a8bdcbe16ec21362355f7;hpb=8a5eacba0596082949034333adbc4e01f339069b;p=m6w6%2Flibmemcached diff --git a/src/memcp.c b/src/memcp.c index 52c373a1..bba346b9 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -4,78 +4,163 @@ #include #include #include -#include #include +#include #include +#include "client_options.h" -static int opt_verbose; -static char *opt_servers; -static int opt_replace; +/* Prototypes */ +void options_parse(int argc, char *argv[]); -struct memcached_st *parse_opt_servers (struct memcached_st *m, - char *opt_servers) -{ - char *s, *hostname; - unsigned int portnum; - while (s = strsep(&opt_servers, ",")) { - hostname = strsep(&s, ":"); - portnum = atoi(s); - memcached_server_add(m, hostname, portnum); - } - return m; -} +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; int main(int argc, char *argv[]) { memcached_st *memc; char *string; - unsigned int x; size_t string_length; - uint16_t flags = 0; - time_t expires = 0; 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: %u\n expires: %llu\n", + opstr[opt_method], argv[optind], sbuf.st_size, + ptr, opt_flags, 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)); + + WATCHPOINT; + free(file_buffer_ptr); + close(fd); + optind++; + } + + memcached_deinit(memc); + + cleanup(); + + 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, 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", required_argument, NULL, OPT_FLAG}, {"expire", required_argument, NULL, OPT_EXPIRE}, - {"set", no_argument, &opt_replace, 0}, - {"add", no_argument, &opt_replace, 1}, - {"replace", no_argument, &opt_replace, 2}, + {"set", no_argument, NULL, OPT_SET}, + {"add", no_argument, NULL, OPT_ADD}, + {"replace", no_argument, NULL, OPT_REPLACE}, {0, 0, 0, 0}, }; - int option_index = 0; - int option_rv; + while (1) { - option_rv = getopt_long(argc, argv, "", long_options, &option_index); + option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) { case 0: - if (long_options[option_index].name) break; - case OPT_VERSION: /* --version */ + case OPT_VERSION: /* --version or -V */ printf("memcache tools, memcp, v1.0\n"); exit(0); - break; - case OPT_HELP: /* --help */ + case OPT_HELP: /* --help or -h */ printf("useful help messages go here\n"); exit(0); - break; - case OPT_SERVERS: /* --servers */ - opt_servers = strdup(optarg); + case OPT_SERVERS: /* --servers or -s */ + opt_servers= strdup_cleanup(optarg); break; case OPT_FLAG: /* --flag */ - flags = (uint16_t) atoi(optarg); + opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10); break; case OPT_EXPIRE: /* --expire */ - expires = (time_t)atoi(optarg); + 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. */ @@ -84,47 +169,4 @@ int main(int argc, char *argv[]) abort(); } } - - memc= memcached_init(NULL); - memc= parse_opt_servers(memc, opt_servers); - - while (optind < argc) { - char *mptr; - struct stat sbuf; - int fd; - char *ptr; - - fd= open(argv[optind], O_RDONLY); - - if (fd == -1) - { - fprintf(stderr, "Failed opening %s\n", argv[optind]); - continue; - } - - (void)fstat(fd, &sbuf); - mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - - ptr= rindex(argv[optind], '/'); - if (ptr) - { - ptr++; - } - else - { - ptr= argv[optind]; - } - - rc= memcached_set(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, - expire, flags); - - munmap(mptr, sbuf.st_size); - close(fd); - optind++; - } - - memcached_deinit(memc); - - return 0; -}; +}