add error reporting to tools, and link memcached_strerror into library
[awesomized/libmemcached] / src / memcp.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <errno.h>
10
11 #include <memcached.h>
12 #include "client_options.h"
13
14 /* Prototypes */
15 void options_parse(int argc, char *argv[]);
16
17 static int opt_verbose;
18 static char *opt_servers;
19 static int opt_replace;
20 uint16_t opt_flags= 0;
21 time_t opt_expires= 0;
22
23 int main(int argc, char *argv[])
24 {
25 memcached_st *memc;
26 char *string;
27 size_t string_length;
28 memcached_return rc;
29
30 options_parse(argc, argv);
31
32 memc= memcached_init(NULL);
33 parse_opt_servers(memc, opt_servers);
34
35 while (optind <= argc)
36 {
37 char *mptr;
38 struct stat sbuf;
39 int fd;
40 char *ptr;
41
42 fd= open(argv[optind], O_RDONLY);
43 if (fd < 0) {
44 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
45 optind++;
46 continue;
47 }
48
49 (void)fstat(fd, &sbuf);
50 mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
51 if (mptr == MAP_FAILED) {
52 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
53 close(fd);
54 optind++;
55 continue;
56 }
57
58 ptr= rindex(argv[optind], '/');
59 if (ptr)
60 ptr++;
61 else
62 ptr= argv[optind];
63
64 if (opt_verbose) {
65 static char *opstr[] = { "set", "add", "replace" };
66 printf("op: %s\nsource file: %s\nlength: %d\n"
67 "key: %s\nflags: %d\n expires: %ld\n",
68 opstr[opt_replace], argv[optind], sbuf.st_size,
69 ptr, opt_flags, opt_expires);
70 }
71
72 if (opt_replace == 0)
73 rc= memcached_set(memc, ptr, strlen(ptr),
74 mptr, sbuf.st_size,
75 opt_expires, opt_flags);
76 else if (opt_replace == 1)
77 rc= memcached_add(memc, ptr, strlen(ptr),
78 mptr, sbuf.st_size,
79 opt_expires, opt_flags);
80 else if (opt_replace == 2)
81 rc= memcached_replace(memc, ptr, strlen(ptr),
82 mptr, sbuf.st_size,
83 opt_expires, opt_flags);
84 else
85 abort();
86
87 if (rc != MEMCACHED_SUCCESS) {
88 fprintf(stderr, "memcp: %s: memcache error %s\n",
89 ptr, memcached_strerror(memc, rc));
90 }
91
92 munmap(mptr, sbuf.st_size);
93 close(fd);
94 optind++;
95 }
96
97 memcached_deinit(memc);
98
99 return 0;
100 };
101
102 void options_parse(int argc, char *argv[])
103 {
104 int option_index= 0;
105 int option_rv;
106
107 static struct option long_options[] =
108 {
109 {"version", no_argument, NULL, OPT_VERSION},
110 {"help", no_argument, NULL, OPT_HELP},
111 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
112 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
113 {"servers", required_argument, NULL, OPT_SERVERS},
114 {"flag", required_argument, NULL, OPT_FLAG},
115 {"expire", required_argument, NULL, OPT_EXPIRE},
116 {"set", no_argument, &opt_replace, OPT_SET},
117 {"add", no_argument, &opt_replace, OPT_ADD},
118 {"replace", no_argument, &opt_replace, OPT_REPLACE},
119 {0, 0, 0, 0},
120 };
121
122 while (1)
123 {
124 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
125
126 if (option_rv == -1) break;
127
128 switch (option_rv) {
129 case 0:
130 break;
131 case OPT_VERSION: /* --version or -V */
132 printf("memcache tools, memcp, v1.0\n");
133 exit(0);
134 case OPT_HELP: /* --help or -h */
135 printf("useful help messages go here\n");
136 exit(0);
137 case OPT_SERVERS: /* --servers or -s */
138 opt_servers= optarg;
139 break;
140 case OPT_FLAG: /* --flag */
141 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);
142 break;
143 case OPT_EXPIRE: /* --expire */
144 opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);
145 break;
146 case '?':
147 /* getopt_long already printed an error message. */
148 exit(1);
149 default:
150 abort();
151 }
152 }
153 }