2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
27 #include <sys/types.h>
28 #include <sys/types.h>
29 #include <sys/types.h>
33 #include <libmemcached/memcached.h>
35 #include "client_options.h"
36 #include "utilities.h"
38 #define PROGRAM_NAME "memcp"
39 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
42 static void options_parse(int argc
, char *argv
[]);
44 static bool opt_binary
= false;
45 static bool opt_udp
= false;
46 static bool opt_buffer
= false;
47 static int opt_verbose
= 0;
48 static char *opt_servers
= NULL
;
49 static char *opt_hash
= NULL
;
50 static int opt_method
= OPT_SET
;
51 static uint32_t opt_flags
= 0;
52 static time_t opt_expires
= 0;
53 static char *opt_username
;
54 static char *opt_passwd
;
56 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
61 errno
= 0; /* To distinguish success/failure after call */
62 val
= strtol(nptr
, &endptr
, base
);
64 /* Check for various possible errors */
66 if ((errno
== ERANGE
and (val
== LONG_MAX
or val
== LONG_MIN
))
67 or (errno
!= 0 && val
== 0))
83 int main(int argc
, char *argv
[])
86 options_parse(argc
, argv
);
89 memcached_st
*memc
= memcached_create(NULL
);
95 std::cout
<< "Enabling UDP" << std::endl
;
98 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp
)))
100 memcached_free(memc
);
101 std::cerr
<< "Could not enable UDP protocol." << std::endl
;
110 std::cout
<< "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl
;
113 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, opt_buffer
)))
115 memcached_free(memc
);
116 std::cerr
<< "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl
;
121 process_hash_option(memc
, opt_hash
);
123 if (opt_servers
== NULL
)
127 if ((temp
= getenv("MEMCACHED_SERVERS")))
129 opt_servers
= strdup(temp
);
133 std::cerr
<< "No Servers provided" << std::endl
;
138 memcached_server_st
*servers
;
141 servers
= memcached_servers_parse(opt_servers
);
145 servers
= memcached_servers_parse(argv
[--argc
]);
148 memcached_server_push(memc
, servers
);
149 memcached_server_list_free(servers
);
150 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
151 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
153 memcached_free(memc
);
154 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
160 memcached_return_t ret
;
161 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
163 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
164 memcached_free(memc
);
169 int exit_code
= EXIT_SUCCESS
;
170 while (optind
< argc
)
172 int fd
= open(argv
[optind
], O_RDONLY
);
177 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
180 exit_code
= EXIT_FAILURE
;
185 (void)fstat(fd
, &sbuf
);
187 char *ptr
= rindex(argv
[optind
], '/');
199 static const char *opstr
[] = { "set", "add", "replace" };
200 printf("op: %s\nsource file: %s\nlength: %lu\n"
201 "key: %s\nflags: %x\nexpires: %lu\n",
202 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
203 ptr
, opt_flags
, (unsigned long)opt_expires
);
206 char *file_buffer_ptr
;
207 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
209 std::cerr
<< "Error allocating file buffer(" << strerror(errno
) << ")" << std::endl
;
214 if ((read_length
= ::read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
216 std::cerr
<< "Error while reading file " << file_buffer_ptr
<< " (" << strerror(errno
) << ")" << std::endl
;
220 if (read_length
!= sbuf
.st_size
)
222 std::cerr
<< "Failure while reading file. Read length was not equal to stat() length" << std::endl
;
226 memcached_return_t rc
;
227 if (opt_method
== OPT_ADD
)
229 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
230 file_buffer_ptr
, (size_t)sbuf
.st_size
,
231 opt_expires
, opt_flags
);
233 else if (opt_method
== OPT_REPLACE
)
235 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
236 file_buffer_ptr
, (size_t)sbuf
.st_size
,
237 opt_expires
, opt_flags
);
241 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
242 file_buffer_ptr
, (size_t)sbuf
.st_size
,
243 opt_expires
, opt_flags
);
246 if (memcached_failed(rc
))
248 std::cerr
<< "Error occrrured during memcached_set(): " << memcached_last_error_message(memc
) << std::endl
;
249 exit_code
= EXIT_FAILURE
;
252 ::free(file_buffer_ptr
);
259 std::cout
<< "Calling memcached_free()" << std::endl
;
262 memcached_free(memc
);
277 static void options_parse(int argc
, char *argv
[])
279 memcached_programs_help_st help_options
[]=
284 static struct option long_options
[]=
286 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
287 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
288 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
289 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
290 {(OPTIONSTRING
)"buffer", no_argument
, NULL
, OPT_BUFFER
},
291 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
292 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
293 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
294 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
295 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
296 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
297 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
298 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
299 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
300 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
301 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
302 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
306 bool opt_version
= false;
307 bool opt_help
= false;
312 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
326 case OPT_VERBOSE
: /* --verbose or -v */
327 opt_verbose
= OPT_VERBOSE
;
330 case OPT_DEBUG
: /* --debug or -d */
331 opt_verbose
= OPT_DEBUG
;
334 case OPT_VERSION
: /* --version or -V */
338 case OPT_HELP
: /* --help or -h */
342 case OPT_SERVERS
: /* --servers or -s */
343 opt_servers
= strdup(optarg
);
346 case OPT_FLAG
: /* --flag */
349 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
350 if (strtol_error
== true)
352 fprintf(stderr
, "Bad value passed via --flag\n");
358 case OPT_EXPIRE
: /* --expire */
361 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
362 if (strtol_error
== true)
364 fprintf(stderr
, "Bad value passed via --flag\n");
375 opt_method
= OPT_REPLACE
;
383 opt_hash
= strdup(optarg
);
387 opt_username
= optarg
;
407 /* getopt_long already printed an error message. */
416 version_command(PROGRAM_NAME
);
422 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);