X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fmemcp.c;h=ec9a2fafb12a2b1bf88389cb4c6d6585c7ef5b32;hb=5672a07389f0e0ea5dc4858c02ac3f3bc7ecf3f7;hp=517ac940d6f9701107a40021589bdc791f4fb3c2;hpb=926592394900405948468504e1e2ca0aabe88981;p=m6w6%2Flibmemcached diff --git a/src/memcp.c b/src/memcp.c index 517ac940..ec9a2faf 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -4,140 +4,171 @@ #include #include #include -#include #include +#include #include #include "client_options.h" +#include "utilities.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= OPT_SET; +static uint16_t opt_flags= 0; +static 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; - 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}, - {"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}, - {0, 0, 0, 0}, - }; - int option_index = 0; - int option_rv; - while (1) - { - option_rv = getopt_long(argc, argv, "", 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 */ - printf("memcache tools, memcp, v1.0\n"); - exit(0); - break; - case OPT_HELP: /* --help */ - printf("useful help messages go here\n"); - exit(0); - break; - case OPT_SERVERS: /* --servers */ - opt_servers = strdup(optarg); - break; - case OPT_FLAG: /* --flag */ - flags = (uint16_t) atoi(optarg); - break; - case OPT_EXPIRE: /* --expire */ - expires = (time_t)atoi(optarg); - break; - case '?': - /* getopt_long already printed an error message. */ - exit(1); - default: - abort(); - } - } + options_parse(argc, argv); memc= memcached_init(NULL); - memc= parse_opt_servers(memc, opt_servers); - while (optind <= argc) { - char *mptr; + 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 == -1) + if (fd < 0) { - fprintf(stderr, "Failed opening %s\n", argv[optind]); + 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); 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\n expires: %llu\n", + opstr[opt_method], argv[optind], (size_t)sbuf.st_size, + ptr, opt_flags, (unsigned long long)opt_expires); } - if (opt_replace == 0) { - rc= memcached_set(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, - expires, 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, - expires, flags); - } else if (opt_replace == 2) { + file_buffer_ptr, sbuf.st_size, + opt_expires, opt_flags); + else if (opt_method == OPT_REPLACE) rc= memcached_replace(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, - expires, flags); - } else { - abort(); - } + 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)); - munmap(mptr, sbuf.st_size); + WATCHPOINT; + 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_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(); + } + } +}