X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fmemcp.c;h=fc396ff1b09c4b4163bc3040802ccd8d42b8f5bb;hb=634d51bb8d523b73f5a2507e913cd3ebee4c6cd7;hp=3430ab54ad935be6815ef7b8dfc28ddd338f9c26;hpb=5f50148c208a764fa3c477436050356b2e24c121;p=m6w6%2Flibmemcached diff --git a/src/memcp.c b/src/memcp.c index 3430ab54..fc396ff1 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -1,92 +1,153 @@ #include +#include +#include #include #include #include #include #include +#include #include +#include "client_options.h" + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +static int opt_verbose; +static char *opt_servers; +static int opt_replace; +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; memcached_return rc; - if (argc < 3) - return 0; + options_parse(argc, argv); memc= memcached_init(NULL); + parse_opt_servers(memc, opt_servers); - /* Get the server name */ - { - char *ptr; - char *hostname; - size_t hostname_length; - unsigned int port; - - ptr= index(argv[argc-1], ':'); - - if (ptr) - { - hostname_length= ptr - argv[argc-1]; - hostname= (char *)malloc(hostname_length+1); - memset(hostname, 0, hostname_length+1); - memcpy(hostname, argv[argc-1], hostname_length); - - ptr++; - - port= strtol(ptr, (char **)NULL, 10); - - memcached_server_add(memc, hostname, port); - free(hostname); - } - else - { - memcached_server_add(memc, argv[argc -1], 0); - } - } - - for (x= 1; x < argc-1; x++) + while (optind <= argc) { char *mptr; struct stat sbuf; int fd; char *ptr; - fd= open(argv[x], O_RDONLY); - - if (fd == -1) - { - fprintf(stderr, "Failed opening %s\n", argv[x]); - exit(1); + 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); 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[x], '/'); + 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: %d\n" + "key: %s\nflags: %d\n expires: %ld\n", + opstr[opt_replace], 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) + rc= memcached_add(memc, ptr, strlen(ptr), + mptr, sbuf.st_size, + opt_expires, opt_flags); + else if (opt_replace == 2) + rc= memcached_replace(memc, ptr, strlen(ptr), + mptr, sbuf.st_size, + opt_expires, opt_flags); else - { - ptr= argv[x]; + abort(); + + if (rc != MEMCACHED_SUCCESS) { + fprintf(stderr, "memcp: %s: memcache error %s\n", + ptr, memcached_strerror(memc, rc)); } - - rc= memcached_set(memc, ptr, strlen(ptr), - mptr, sbuf.st_size, - (time_t)0, (uint16_t)0); munmap(mptr, sbuf.st_size); close(fd); + optind++; } memcached_deinit(memc); 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, &opt_replace, OPT_SET}, + {"add", no_argument, &opt_replace, OPT_ADD}, + {"replace", no_argument, &opt_replace, 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= optarg; + break; + case OPT_FLAG: /* --flag */ + opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10); + break; + case OPT_EXPIRE: /* --expire */ + opt_expires= (time_t)strtol(optarg, (char **)NULL, 10); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +}