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.
20 #include <sys/types.h>
22 #include <sys/types.h>
27 #include <sys/types.h>
32 #include <libmemcached/memcached.h>
34 #include "client_options.h"
35 #include "utilities.h"
37 #define PROGRAM_NAME "memcp"
38 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
41 static void options_parse(int argc
, char *argv
[]);
43 static int opt_binary
=0;
44 static int opt_verbose
= 0;
45 static char *opt_servers
= NULL
;
46 static char *opt_hash
= NULL
;
47 static int opt_method
= OPT_SET
;
48 static uint32_t opt_flags
= 0;
49 static time_t opt_expires
= 0;
50 static char *opt_username
;
51 static char *opt_passwd
;
53 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
58 errno
= 0; /* To distinguish success/failure after call */
59 val
= strtol(nptr
, &endptr
, base
);
61 /* Check for various possible errors */
63 if ((errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
))
64 || (errno
!= 0 && val
== 0))
80 int main(int argc
, char *argv
[])
83 memcached_return_t rc
;
84 memcached_server_st
*servers
;
88 options_parse(argc
, argv
);
91 memc
= memcached_create(NULL
);
92 process_hash_option(memc
, opt_hash
);
98 if ((temp
= getenv("MEMCACHED_SERVERS")))
100 opt_servers
= strdup(temp
);
104 fprintf(stderr
, "No Servers provided\n");
110 servers
= memcached_servers_parse(opt_servers
);
112 servers
= memcached_servers_parse(argv
[--argc
]);
114 memcached_server_push(memc
, servers
);
115 memcached_server_list_free(servers
);
116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
117 (uint64_t)opt_binary
);
119 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
121 memcached_free(memc
);
122 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
126 if (initialize_sasl(memc
, opt_username
, opt_passwd
) == false)
128 std::cerr
<< "Failed to initialize SASL support." << std::endl
;
129 memcached_free(memc
);
133 while (optind
< argc
)
139 char *file_buffer_ptr
;
141 fd
= open(argv
[optind
], O_RDONLY
);
144 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
149 (void)fstat(fd
, &sbuf
);
151 ptr
= rindex(argv
[optind
], '/');
159 static const char *opstr
[] = { "set", "add", "replace" };
160 printf("op: %s\nsource file: %s\nlength: %lu\n"
161 "key: %s\nflags: %x\nexpires: %lu\n",
162 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
163 ptr
, opt_flags
, (unsigned long)opt_expires
);
166 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
168 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
172 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
174 fprintf(stderr
, "read: %s\n", strerror(errno
));
178 if (read_length
!= sbuf
.st_size
)
180 fprintf(stderr
, "Failure reading from file\n");
184 if (opt_method
== OPT_ADD
)
185 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
186 file_buffer_ptr
, (size_t)sbuf
.st_size
,
187 opt_expires
, opt_flags
);
188 else if (opt_method
== OPT_REPLACE
)
189 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
190 file_buffer_ptr
, (size_t)sbuf
.st_size
,
191 opt_expires
, opt_flags
);
193 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
194 file_buffer_ptr
, (size_t)sbuf
.st_size
,
195 opt_expires
, opt_flags
);
197 if (rc
!= MEMCACHED_SUCCESS
)
199 fprintf(stderr
, "memcp: %s: memcache error %s",
200 ptr
, memcached_strerror(memc
, rc
));
201 if (memcached_last_error_errno(memc
))
202 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
203 fprintf(stderr
, "\n");
208 free(file_buffer_ptr
);
213 memcached_free(memc
);
224 static void options_parse(int argc
, char *argv
[])
229 memcached_programs_help_st help_options
[]=
234 static struct option long_options
[]=
236 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
237 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
238 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
239 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
240 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
241 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
242 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
243 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
244 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
245 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
246 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
247 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
248 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
249 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
255 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
257 if (option_rv
== -1) break;
266 case OPT_VERBOSE
: /* --verbose or -v */
267 opt_verbose
= OPT_VERBOSE
;
269 case OPT_DEBUG
: /* --debug or -d */
270 opt_verbose
= OPT_DEBUG
;
272 case OPT_VERSION
: /* --version or -V */
273 version_command(PROGRAM_NAME
);
275 case OPT_HELP
: /* --help or -h */
276 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
278 case OPT_SERVERS
: /* --servers or -s */
279 opt_servers
= strdup(optarg
);
281 case OPT_FLAG
: /* --flag */
284 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
285 if (strtol_error
== true)
287 fprintf(stderr
, "Bad value passed via --flag\n");
292 case OPT_EXPIRE
: /* --expire */
295 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
296 if (strtol_error
== true)
298 fprintf(stderr
, "Bad value passed via --flag\n");
306 opt_method
= OPT_REPLACE
;
312 opt_hash
= strdup(optarg
);
315 opt_username
= optarg
;
321 /* getopt_long already printed an error message. */