Found bug in multi get where key size was not being calculated.
[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 <fcntl.h>
8 #include <errno.h>
9 #include <strings.h>
10
11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
14
15 #define PROGRAM_NAME "memcp"
16 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
17
18 /* Prototypes */
19 void options_parse(int argc, char *argv[]);
20
21 static int opt_verbose= 0;
22 static char *opt_servers= NULL;
23 static int opt_method= OPT_SET;
24 static uint16_t opt_flags= 0;
25 static time_t opt_expires= 0;
26
27 int main(int argc, char *argv[])
28 {
29 memcached_st *memc;
30 memcached_return rc;
31 memcached_server_st *servers;
32
33 options_parse(argc, argv);
34
35 memc= memcached_create(NULL);
36
37 if (!opt_servers)
38 {
39 char *temp;
40
41 if ((temp= getenv("MEMCACHED_SERVERS")))
42 opt_servers= strdup(temp);
43 else
44 exit(1);
45 }
46
47 if (opt_servers)
48 servers= memcached_servers_parse(opt_servers);
49 else
50 servers= memcached_servers_parse(argv[--argc]);
51
52 memcached_server_push(memc, servers);
53 memcached_server_list_free(servers);
54
55 while (optind < argc)
56 {
57 struct stat sbuf;
58 int fd;
59 char *ptr;
60 ssize_t read_length;
61 char *file_buffer_ptr;
62
63 fd= open(argv[optind], O_RDONLY);
64 if (fd < 0)
65 {
66 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
67 optind++;
68 continue;
69 }
70
71 (void)fstat(fd, &sbuf);
72
73 ptr= rindex(argv[optind], '/');
74 if (ptr)
75 ptr++;
76 else
77 ptr= argv[optind];
78
79 if (opt_verbose)
80 {
81 static char *opstr[] = { "set", "add", "replace" };
82 printf("op: %s\nsource file: %s\nlength: %zu\n"
83 "key: %s\nflags: %x\nexpires: %llu\n",
84 opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size,
85 ptr, opt_flags, (unsigned long long)opt_expires);
86 }
87
88 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
89 {
90 fprintf(stderr, "malloc: %s\n", strerror(errno));
91 exit(1);
92 }
93
94 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
95 {
96 fprintf(stderr, "read: %s\n", strerror(errno));
97 exit(1);
98 }
99 assert(read_length == sbuf.st_size);
100
101 if (opt_method == OPT_ADD)
102 rc= memcached_add(memc, ptr, strlen(ptr),
103 file_buffer_ptr, sbuf.st_size,
104 opt_expires, opt_flags);
105 else if (opt_method == OPT_REPLACE)
106 rc= memcached_replace(memc, ptr, strlen(ptr),
107 file_buffer_ptr, sbuf.st_size,
108 opt_expires, opt_flags);
109 else
110 rc= memcached_set(memc, ptr, strlen(ptr),
111 file_buffer_ptr, sbuf.st_size,
112 opt_expires, opt_flags);
113
114 if (rc != MEMCACHED_SUCCESS)
115 fprintf(stderr, "memcp: %s: memcache error %s\n",
116 ptr, memcached_strerror(memc, rc));
117
118 free(file_buffer_ptr);
119 close(fd);
120 optind++;
121 }
122
123 memcached_free(memc);
124
125 free(opt_servers);
126
127 return 0;
128 }
129
130 void options_parse(int argc, char *argv[])
131 {
132 int option_index= 0;
133 int option_rv;
134
135 memcached_programs_help_st help_options[]=
136 {
137 {0},
138 };
139
140 static struct option long_options[]=
141 {
142 {"version", no_argument, NULL, OPT_VERSION},
143 {"help", no_argument, NULL, OPT_HELP},
144 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
145 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
146 {"servers", required_argument, NULL, OPT_SERVERS},
147 {"flag", required_argument, NULL, OPT_FLAG},
148 {"expire", required_argument, NULL, OPT_EXPIRE},
149 {"set", no_argument, NULL, OPT_SET},
150 {"add", no_argument, NULL, OPT_ADD},
151 {"replace", no_argument, NULL, OPT_REPLACE},
152 {0, 0, 0, 0},
153 };
154
155 while (1)
156 {
157 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
158
159 if (option_rv == -1) break;
160
161 switch (option_rv)
162 {
163 case 0:
164 break;
165 case OPT_VERBOSE: /* --verbose or -v */
166 opt_verbose = OPT_VERBOSE;
167 break;
168 case OPT_DEBUG: /* --debug or -d */
169 opt_verbose = OPT_DEBUG;
170 break;
171 case OPT_VERSION: /* --version or -V */
172 version_command(PROGRAM_NAME);
173 break;
174 case OPT_HELP: /* --help or -h */
175 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
176 break;
177 case OPT_SERVERS: /* --servers or -s */
178 opt_servers= strdup(optarg);
179 break;
180 case OPT_FLAG: /* --flag */
181 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);
182 break;
183 case OPT_EXPIRE: /* --expire */
184 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
185 break;
186 case OPT_SET:
187 opt_method= OPT_SET;
188 break;
189 case OPT_REPLACE:
190 opt_method= OPT_REPLACE;
191 break;
192 case OPT_ADD:
193 opt_method= OPT_ADD;
194 break;
195 case '?':
196 /* getopt_long already printed an error message. */
197 exit(1);
198 default:
199 abort();
200 }
201 }
202 }