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.
19 #include <sys/types.h>
21 #include <sys/types.h>
27 #include <libmemcached/memcached.h>
29 #include "client_options.h"
30 #include "utilities.h"
32 #define PROGRAM_NAME "memcp"
33 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
36 static void options_parse(int argc
, char *argv
[]);
38 static int opt_binary
=0;
39 static int opt_verbose
= 0;
40 static char *opt_servers
= NULL
;
41 static char *opt_hash
= NULL
;
42 static int opt_method
= OPT_SET
;
43 static uint32_t opt_flags
= 0;
44 static time_t opt_expires
= 0;
45 static char *opt_username
;
46 static char *opt_passwd
;
48 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
53 errno
= 0; /* To distinguish success/failure after call */
54 val
= strtol(nptr
, &endptr
, base
);
56 /* Check for various possible errors */
58 if ((errno
== ERANGE
&& (val
== INTMAX_MAX
|| val
== INTMAX_MIN
))
59 || (errno
!= 0 && val
== 0))
75 int main(int argc
, char *argv
[])
78 memcached_return_t rc
;
79 memcached_server_st
*servers
;
83 options_parse(argc
, argv
);
85 memc
= memcached_create(NULL
);
86 process_hash_option(memc
, opt_hash
);
92 if ((temp
= getenv("MEMCACHED_SERVERS")))
94 opt_servers
= strdup(temp
);
98 fprintf(stderr
, "No Servers provided\n");
104 servers
= memcached_servers_parse(opt_servers
);
106 servers
= memcached_servers_parse(argv
[--argc
]);
108 memcached_server_push(memc
, servers
);
109 memcached_server_list_free(servers
);
110 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
111 (uint64_t)opt_binary
);
112 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
114 memcached_free(memc
);
118 while (optind
< argc
)
124 char *file_buffer_ptr
;
126 fd
= open(argv
[optind
], O_RDONLY
);
129 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
134 (void)fstat(fd
, &sbuf
);
136 ptr
= rindex(argv
[optind
], '/');
144 static const char *opstr
[] = { "set", "add", "replace" };
145 printf("op: %s\nsource file: %s\nlength: %zu\n"
146 "key: %s\nflags: %x\nexpires: %llu\n",
147 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
148 ptr
, opt_flags
, (unsigned long long)opt_expires
);
151 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
153 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
157 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
159 fprintf(stderr
, "read: %s\n", strerror(errno
));
163 if (read_length
!= sbuf
.st_size
)
165 fprintf(stderr
, "Failure reading from file\n");
169 if (opt_method
== OPT_ADD
)
170 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
171 file_buffer_ptr
, (size_t)sbuf
.st_size
,
172 opt_expires
, opt_flags
);
173 else if (opt_method
== OPT_REPLACE
)
174 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
175 file_buffer_ptr
, (size_t)sbuf
.st_size
,
176 opt_expires
, opt_flags
);
178 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
179 file_buffer_ptr
, (size_t)sbuf
.st_size
,
180 opt_expires
, opt_flags
);
182 if (rc
!= MEMCACHED_SUCCESS
)
184 fprintf(stderr
, "memcp: %s: memcache error %s",
185 ptr
, memcached_strerror(memc
, rc
));
186 if (memc
->cached_errno
)
187 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
188 fprintf(stderr
, "\n");
193 free(file_buffer_ptr
);
198 memcached_free(memc
);
209 static void options_parse(int argc
, char *argv
[])
214 memcached_programs_help_st help_options
[]=
219 static struct option long_options
[]=
221 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
222 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
223 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
224 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
225 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
226 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
227 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
228 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
229 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
230 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
231 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
232 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
233 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
234 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
240 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
242 if (option_rv
== -1) break;
251 case OPT_VERBOSE
: /* --verbose or -v */
252 opt_verbose
= OPT_VERBOSE
;
254 case OPT_DEBUG
: /* --debug or -d */
255 opt_verbose
= OPT_DEBUG
;
257 case OPT_VERSION
: /* --version or -V */
258 version_command(PROGRAM_NAME
);
260 case OPT_HELP
: /* --help or -h */
261 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
263 case OPT_SERVERS
: /* --servers or -s */
264 opt_servers
= strdup(optarg
);
266 case OPT_FLAG
: /* --flag */
269 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
270 if (strtol_error
== true)
272 fprintf(stderr
, "Bad value passed via --flag\n");
277 case OPT_EXPIRE
: /* --expire */
280 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
281 if (strtol_error
== true)
283 fprintf(stderr
, "Bad value passed via --flag\n");
291 opt_method
= OPT_REPLACE
;
297 opt_hash
= strdup(optarg
);
300 opt_username
= optarg
;
306 /* getopt_long already printed an error message. */