2 * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 #include "mem_config.h"
28 #include <sys/types.h>
29 #include <sys/types.h>
30 #include <sys/types.h>
34 #include <libmemcached-1.0/memcached.h>
36 #include "client_options.h"
37 #include "utilities.h"
39 #define PROGRAM_NAME "memcp"
40 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
43 static void options_parse(int argc
, char *argv
[]);
45 static bool opt_binary
= false;
46 static bool opt_udp
= false;
47 static bool opt_buffer
= false;
48 static int opt_verbose
= 0;
49 static char *opt_servers
= NULL
;
50 static char *opt_hash
= NULL
;
51 static int opt_method
= OPT_SET
;
52 static uint32_t opt_flags
= 0;
53 static time_t opt_expires
= 0;
54 static char *opt_username
;
55 static char *opt_passwd
;
57 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
62 errno
= 0; /* To distinguish success/failure after call */
63 val
= strtol(nptr
, &endptr
, base
);
65 /* Check for various possible errors */
67 if ((errno
== ERANGE
and (val
== LONG_MAX
or val
== LONG_MIN
))
68 or (errno
!= 0 && val
== 0))
84 int main(int argc
, char *argv
[])
87 options_parse(argc
, argv
);
91 fprintf(stderr
, "Expected argument after options\n");
97 memcached_st
*memc
= memcached_create(NULL
);
103 std::cout
<< "Enabling UDP" << std::endl
;
106 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp
)))
108 memcached_free(memc
);
109 std::cerr
<< "Could not enable UDP protocol." << std::endl
;
118 std::cout
<< "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl
;
121 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, opt_buffer
)))
123 memcached_free(memc
);
124 std::cerr
<< "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl
;
129 process_hash_option(memc
, opt_hash
);
131 if (opt_servers
== NULL
)
135 if ((temp
= getenv("MEMCACHED_SERVERS")))
137 opt_servers
= strdup(temp
);
140 else if (argc
>= 1 and argv
[--argc
])
142 opt_servers
= strdup(argv
[argc
]);
146 if (opt_servers
== NULL
)
148 std::cerr
<< "No Servers provided" << std::endl
;
153 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
154 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
156 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
160 memcached_server_push(memc
, servers
);
161 memcached_server_list_free(servers
);
162 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
163 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
165 memcached_free(memc
);
166 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
172 memcached_return_t ret
;
173 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
175 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
176 memcached_free(memc
);
181 int exit_code
= EXIT_SUCCESS
;
182 while (optind
< argc
)
184 int fd
= open(argv
[optind
], O_RDONLY
);
189 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
192 exit_code
= EXIT_FAILURE
;
197 if (fstat(fd
, &sbuf
) == -1)
199 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
201 exit_code
= EXIT_FAILURE
;
205 char *ptr
= rindex(argv
[optind
], '/');
217 static const char *opstr
[] = { "set", "add", "replace" };
218 printf("op: %s\nsource file: %s\nlength: %lu\n"
219 "key: %s\nflags: %x\nexpires: %lu\n",
220 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
221 ptr
, opt_flags
, (unsigned long)opt_expires
);
224 // The file may be empty
225 char *file_buffer_ptr
= NULL
;
226 if (sbuf
.st_size
> 0)
228 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
230 std::cerr
<< "Error allocating file buffer(" << strerror(errno
) << ")" << std::endl
;
236 if ((read_length
= ::read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
238 std::cerr
<< "Error while reading file " << file_buffer_ptr
<< " (" << strerror(errno
) << ")" << std::endl
;
240 free(file_buffer_ptr
);
244 if (read_length
!= sbuf
.st_size
)
246 std::cerr
<< "Failure while reading file. Read length was not equal to stat() length" << std::endl
;
248 free(file_buffer_ptr
);
253 memcached_return_t rc
;
254 if (opt_method
== OPT_ADD
)
256 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
257 file_buffer_ptr
, (size_t)sbuf
.st_size
,
258 opt_expires
, opt_flags
);
260 else if (opt_method
== OPT_REPLACE
)
262 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
263 file_buffer_ptr
, (size_t)sbuf
.st_size
,
264 opt_expires
, opt_flags
);
268 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
269 file_buffer_ptr
, (size_t)sbuf
.st_size
,
270 opt_expires
, opt_flags
);
273 if (memcached_failed(rc
))
275 std::cerr
<< "Error occrrured during memcached_set(): " << memcached_last_error_message(memc
) << std::endl
;
276 exit_code
= EXIT_FAILURE
;
279 ::free(file_buffer_ptr
);
286 std::cout
<< "Calling memcached_free()" << std::endl
;
289 memcached_free(memc
);
304 static void options_parse(int argc
, char *argv
[])
306 memcached_programs_help_st help_options
[]=
311 static struct option long_options
[]=
313 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
314 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
315 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
316 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
317 {(OPTIONSTRING
)"buffer", no_argument
, NULL
, OPT_BUFFER
},
318 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
319 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
320 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
321 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
322 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
323 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
324 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
325 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
326 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
327 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
328 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
329 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
333 bool opt_version
= false;
334 bool opt_help
= false;
339 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
353 case OPT_VERBOSE
: /* --verbose or -v */
354 opt_verbose
= OPT_VERBOSE
;
357 case OPT_DEBUG
: /* --debug or -d */
358 opt_verbose
= OPT_DEBUG
;
361 case OPT_VERSION
: /* --version or -V */
365 case OPT_HELP
: /* --help or -h */
369 case OPT_SERVERS
: /* --servers or -s */
370 opt_servers
= strdup(optarg
);
373 case OPT_FLAG
: /* --flag */
376 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
377 if (strtol_error
== true)
379 fprintf(stderr
, "Bad value passed via --flag\n");
385 case OPT_EXPIRE
: /* --expire */
388 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
389 if (strtol_error
== true)
391 fprintf(stderr
, "Bad value passed via --flag\n");
402 opt_method
= OPT_REPLACE
;
410 opt_hash
= strdup(optarg
);
414 opt_username
= optarg
;
434 /* getopt_long already printed an error message. */
443 version_command(PROGRAM_NAME
);
449 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);