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 int opt_binary
=0;
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 memcached_return_t rc
;
85 memcached_server_st
*servers
;
89 options_parse(argc
, argv
);
92 memc
= memcached_create(NULL
);
93 process_hash_option(memc
, opt_hash
);
99 if ((temp
= getenv("MEMCACHED_SERVERS")))
101 opt_servers
= strdup(temp
);
105 fprintf(stderr
, "No Servers provided\n");
111 servers
= memcached_servers_parse(opt_servers
);
113 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 while (optind
< argc
)
144 char *file_buffer_ptr
;
146 fd
= open(argv
[optind
], O_RDONLY
);
149 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
154 (void)fstat(fd
, &sbuf
);
156 ptr
= rindex(argv
[optind
], '/');
164 static const char *opstr
[] = { "set", "add", "replace" };
165 printf("op: %s\nsource file: %s\nlength: %lu\n"
166 "key: %s\nflags: %x\nexpires: %lu\n",
167 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
168 ptr
, opt_flags
, (unsigned long)opt_expires
);
171 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
173 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
177 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
179 fprintf(stderr
, "read: %s\n", strerror(errno
));
183 if (read_length
!= sbuf
.st_size
)
185 fprintf(stderr
, "Failure reading from file\n");
189 if (opt_method
== OPT_ADD
)
190 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
191 file_buffer_ptr
, (size_t)sbuf
.st_size
,
192 opt_expires
, opt_flags
);
193 else if (opt_method
== OPT_REPLACE
)
194 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
195 file_buffer_ptr
, (size_t)sbuf
.st_size
,
196 opt_expires
, opt_flags
);
198 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
199 file_buffer_ptr
, (size_t)sbuf
.st_size
,
200 opt_expires
, opt_flags
);
202 if (rc
!= MEMCACHED_SUCCESS
)
204 fprintf(stderr
, "memcp: %s: memcache error %s",
205 ptr
, memcached_strerror(memc
, rc
));
206 if (memcached_last_error_errno(memc
))
207 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
208 fprintf(stderr
, "\n");
213 free(file_buffer_ptr
);
218 memcached_free(memc
);
228 static void options_parse(int argc
, char *argv
[])
233 memcached_programs_help_st help_options
[]=
238 static struct option long_options
[]=
240 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
241 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
242 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
243 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
244 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
245 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
246 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
247 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
248 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
249 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
250 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
251 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
252 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
253 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
259 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
261 if (option_rv
== -1) break;
270 case OPT_VERBOSE
: /* --verbose or -v */
271 opt_verbose
= OPT_VERBOSE
;
273 case OPT_DEBUG
: /* --debug or -d */
274 opt_verbose
= OPT_DEBUG
;
276 case OPT_VERSION
: /* --version or -V */
277 version_command(PROGRAM_NAME
);
279 case OPT_HELP
: /* --help or -h */
280 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
282 case OPT_SERVERS
: /* --servers or -s */
283 opt_servers
= strdup(optarg
);
285 case OPT_FLAG
: /* --flag */
288 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
289 if (strtol_error
== true)
291 fprintf(stderr
, "Bad value passed via --flag\n");
296 case OPT_EXPIRE
: /* --expire */
299 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
300 if (strtol_error
== true)
302 fprintf(stderr
, "Bad value passed via --flag\n");
310 opt_method
= OPT_REPLACE
;
316 opt_hash
= strdup(optarg
);
319 opt_username
= optarg
;
325 /* getopt_long already printed an error message. */