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.
12 #include "libmemcached/common.h"
18 #include <sys/types.h>
20 #include <sys/types.h>
26 #include <libmemcached/memcached.h>
28 #include "client_options.h"
29 #include "utilities.h"
31 #define PROGRAM_NAME "memcp"
32 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
35 static void options_parse(int argc
, char *argv
[]);
37 static int opt_binary
=0;
38 static int opt_verbose
= 0;
39 static char *opt_servers
= NULL
;
40 static char *opt_hash
= NULL
;
41 static int opt_method
= OPT_SET
;
42 static uint32_t opt_flags
= 0;
43 static time_t opt_expires
= 0;
44 static char *opt_username
;
45 static char *opt_passwd
;
47 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
52 errno
= 0; /* To distinguish success/failure after call */
53 val
= strtol(nptr
, &endptr
, base
);
55 /* Check for various possible errors */
57 if ((errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
))
58 || (errno
!= 0 && val
== 0))
74 int main(int argc
, char *argv
[])
77 memcached_return_t rc
;
78 memcached_server_st
*servers
;
82 options_parse(argc
, argv
);
84 memc
= memcached_create(NULL
);
85 process_hash_option(memc
, opt_hash
);
91 if ((temp
= getenv("MEMCACHED_SERVERS")))
93 opt_servers
= strdup(temp
);
97 fprintf(stderr
, "No Servers provided\n");
103 servers
= memcached_servers_parse(opt_servers
);
105 servers
= memcached_servers_parse(argv
[--argc
]);
107 memcached_server_push(memc
, servers
);
108 memcached_server_list_free(servers
);
109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
110 (uint64_t)opt_binary
);
111 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
113 memcached_free(memc
);
117 while (optind
< argc
)
123 char *file_buffer_ptr
;
125 fd
= open(argv
[optind
], O_RDONLY
);
128 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
133 (void)fstat(fd
, &sbuf
);
135 ptr
= rindex(argv
[optind
], '/');
143 static const char *opstr
[] = { "set", "add", "replace" };
144 printf("op: %s\nsource file: %s\nlength: %zu\n"
145 "key: %s\nflags: %x\nexpires: %llu\n",
146 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
147 ptr
, opt_flags
, (unsigned long long)opt_expires
);
150 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
152 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
156 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
158 fprintf(stderr
, "read: %s\n", strerror(errno
));
162 if (read_length
!= sbuf
.st_size
)
164 fprintf(stderr
, "Failure reading from file\n");
168 if (opt_method
== OPT_ADD
)
169 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
170 file_buffer_ptr
, (size_t)sbuf
.st_size
,
171 opt_expires
, opt_flags
);
172 else if (opt_method
== OPT_REPLACE
)
173 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
174 file_buffer_ptr
, (size_t)sbuf
.st_size
,
175 opt_expires
, opt_flags
);
177 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
178 file_buffer_ptr
, (size_t)sbuf
.st_size
,
179 opt_expires
, opt_flags
);
181 if (rc
!= MEMCACHED_SUCCESS
)
183 fprintf(stderr
, "memcp: %s: memcache error %s",
184 ptr
, memcached_strerror(memc
, rc
));
185 if (memc
->cached_errno
)
186 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
187 fprintf(stderr
, "\n");
192 free(file_buffer_ptr
);
197 memcached_free(memc
);
208 static void options_parse(int argc
, char *argv
[])
213 memcached_programs_help_st help_options
[]=
218 static struct option long_options
[]=
220 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
221 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
222 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
223 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
224 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
225 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
226 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
227 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
228 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
229 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
230 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
231 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
232 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
233 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
239 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
241 if (option_rv
== -1) break;
250 case OPT_VERBOSE
: /* --verbose or -v */
251 opt_verbose
= OPT_VERBOSE
;
253 case OPT_DEBUG
: /* --debug or -d */
254 opt_verbose
= OPT_DEBUG
;
256 case OPT_VERSION
: /* --version or -V */
257 version_command(PROGRAM_NAME
);
259 case OPT_HELP
: /* --help or -h */
260 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
262 case OPT_SERVERS
: /* --servers or -s */
263 opt_servers
= strdup(optarg
);
265 case OPT_FLAG
: /* --flag */
268 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
269 if (strtol_error
== true)
271 fprintf(stderr
, "Bad value passed via --flag\n");
276 case OPT_EXPIRE
: /* --expire */
279 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
280 if (strtol_error
== true)
282 fprintf(stderr
, "Bad value passed via --flag\n");
290 opt_method
= OPT_REPLACE
;
296 opt_hash
= strdup(optarg
);
299 opt_username
= optarg
;
305 /* getopt_long already printed an error message. */