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 int opt_verbose
= 0;
46 static char *opt_servers
= NULL
;
47 static char *opt_hash
= NULL
;
48 static int opt_method
= OPT_SET
;
49 static uint32_t opt_flags
= 0;
50 static time_t opt_expires
= 0;
51 static char *opt_username
;
52 static char *opt_passwd
;
54 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
59 errno
= 0; /* To distinguish success/failure after call */
60 val
= strtol(nptr
, &endptr
, base
);
62 /* Check for various possible errors */
64 if ((errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
))
65 || (errno
!= 0 && val
== 0))
81 int main(int argc
, char *argv
[])
84 options_parse(argc
, argv
);
87 memcached_st
*memc
= memcached_create(NULL
);
88 process_hash_option(memc
, opt_hash
);
90 if (opt_servers
== NULL
)
94 if ((temp
= getenv("MEMCACHED_SERVERS")))
96 opt_servers
= strdup(temp
);
100 std::cerr
<< "No Servers provided" << std::endl
;
105 memcached_server_st
*servers
;
108 servers
= memcached_servers_parse(opt_servers
);
112 servers
= memcached_servers_parse(argv
[--argc
]);
115 memcached_server_push(memc
, servers
);
116 memcached_server_list_free(servers
);
117 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
118 (uint64_t)opt_binary
);
120 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
122 memcached_free(memc
);
123 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
129 memcached_return_t ret
;
130 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
132 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
133 memcached_free(memc
);
138 int exit_code
= EXIT_SUCCESS
;
139 while (optind
< argc
)
141 int fd
= open(argv
[optind
], O_RDONLY
);
146 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
149 exit_code
= EXIT_FAILURE
;
154 (void)fstat(fd
, &sbuf
);
156 char *ptr
= rindex(argv
[optind
], '/');
168 static const char *opstr
[] = { "set", "add", "replace" };
169 printf("op: %s\nsource file: %s\nlength: %lu\n"
170 "key: %s\nflags: %x\nexpires: %lu\n",
171 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
172 ptr
, opt_flags
, (unsigned long)opt_expires
);
175 char *file_buffer_ptr
;
176 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
178 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
183 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
185 fprintf(stderr
, "read: %s\n", strerror(errno
));
189 if (read_length
!= sbuf
.st_size
)
191 fprintf(stderr
, "Failure reading from file\n");
195 memcached_return_t rc
;
196 if (opt_method
== OPT_ADD
)
197 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
198 file_buffer_ptr
, (size_t)sbuf
.st_size
,
199 opt_expires
, opt_flags
);
200 else if (opt_method
== OPT_REPLACE
)
201 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
202 file_buffer_ptr
, (size_t)sbuf
.st_size
,
203 opt_expires
, opt_flags
);
205 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
206 file_buffer_ptr
, (size_t)sbuf
.st_size
,
207 opt_expires
, opt_flags
);
209 if (rc
!= MEMCACHED_SUCCESS
)
211 fprintf(stderr
, "memcp: %s: memcache error %s",
212 ptr
, memcached_strerror(memc
, rc
));
213 if (memcached_last_error_errno(memc
))
214 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
215 fprintf(stderr
, "\n");
217 exit_code
= EXIT_FAILURE
;
220 free(file_buffer_ptr
);
225 memcached_free(memc
);
235 static void options_parse(int argc
, char *argv
[])
237 memcached_programs_help_st help_options
[]=
242 static struct option long_options
[]=
244 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
245 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
246 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
247 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
248 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
249 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
250 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
251 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
252 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
253 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
254 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
255 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
256 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
257 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
258 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
262 bool opt_version
= false;
263 bool opt_help
= false;
268 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
281 case OPT_VERBOSE
: /* --verbose or -v */
282 opt_verbose
= OPT_VERBOSE
;
285 case OPT_DEBUG
: /* --debug or -d */
286 opt_verbose
= OPT_DEBUG
;
289 case OPT_VERSION
: /* --version or -V */
293 case OPT_HELP
: /* --help or -h */
297 case OPT_SERVERS
: /* --servers or -s */
298 opt_servers
= strdup(optarg
);
301 case OPT_FLAG
: /* --flag */
304 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
305 if (strtol_error
== true)
307 fprintf(stderr
, "Bad value passed via --flag\n");
313 case OPT_EXPIRE
: /* --expire */
316 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
317 if (strtol_error
== true)
319 fprintf(stderr
, "Bad value passed via --flag\n");
330 opt_method
= OPT_REPLACE
;
338 opt_hash
= strdup(optarg
);
342 opt_username
= optarg
;
354 /* getopt_long already printed an error message. */
363 version_command(PROGRAM_NAME
);
369 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);