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>
26 #include <sys/types.h>
31 #include <libmemcached/memcached.h>
33 #include "client_options.h"
34 #include "utilities.h"
36 #define PROGRAM_NAME "memcp"
37 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
40 static void options_parse(int argc
, char *argv
[]);
42 static int opt_binary
=0;
43 static int opt_verbose
= 0;
44 static char *opt_servers
= NULL
;
45 static char *opt_hash
= NULL
;
46 static int opt_method
= OPT_SET
;
47 static uint32_t opt_flags
= 0;
48 static time_t opt_expires
= 0;
49 static char *opt_username
;
50 static char *opt_passwd
;
52 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
57 errno
= 0; /* To distinguish success/failure after call */
58 val
= strtol(nptr
, &endptr
, base
);
60 /* Check for various possible errors */
62 if ((errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
))
63 || (errno
!= 0 && val
== 0))
79 int main(int argc
, char *argv
[])
82 memcached_return_t rc
;
83 memcached_server_st
*servers
;
87 options_parse(argc
, argv
);
89 memc
= memcached_create(NULL
);
90 process_hash_option(memc
, opt_hash
);
96 if ((temp
= getenv("MEMCACHED_SERVERS")))
98 opt_servers
= strdup(temp
);
102 fprintf(stderr
, "No Servers provided\n");
108 servers
= memcached_servers_parse(opt_servers
);
110 servers
= memcached_servers_parse(argv
[--argc
]);
112 memcached_server_push(memc
, servers
);
113 memcached_server_list_free(servers
);
114 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
115 (uint64_t)opt_binary
);
116 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
118 memcached_free(memc
);
122 while (optind
< argc
)
128 char *file_buffer_ptr
;
130 fd
= open(argv
[optind
], O_RDONLY
);
133 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
138 (void)fstat(fd
, &sbuf
);
140 ptr
= rindex(argv
[optind
], '/');
148 static const char *opstr
[] = { "set", "add", "replace" };
149 printf("op: %s\nsource file: %s\nlength: %zu\n"
150 "key: %s\nflags: %x\nexpires: %llu\n",
151 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
152 ptr
, opt_flags
, (unsigned long long)opt_expires
);
155 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
157 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
161 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
163 fprintf(stderr
, "read: %s\n", strerror(errno
));
167 if (read_length
!= sbuf
.st_size
)
169 fprintf(stderr
, "Failure reading from file\n");
173 if (opt_method
== OPT_ADD
)
174 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
175 file_buffer_ptr
, (size_t)sbuf
.st_size
,
176 opt_expires
, opt_flags
);
177 else if (opt_method
== OPT_REPLACE
)
178 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
179 file_buffer_ptr
, (size_t)sbuf
.st_size
,
180 opt_expires
, opt_flags
);
182 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
183 file_buffer_ptr
, (size_t)sbuf
.st_size
,
184 opt_expires
, opt_flags
);
186 if (rc
!= MEMCACHED_SUCCESS
)
188 fprintf(stderr
, "memcp: %s: memcache error %s",
189 ptr
, memcached_strerror(memc
, rc
));
190 if (memc
->cached_errno
)
191 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
192 fprintf(stderr
, "\n");
197 free(file_buffer_ptr
);
202 memcached_free(memc
);
213 static void options_parse(int argc
, char *argv
[])
218 memcached_programs_help_st help_options
[]=
223 static struct option long_options
[]=
225 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
226 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
227 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
228 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
229 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
230 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
231 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
232 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
233 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
234 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
235 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
236 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
237 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
238 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
244 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
246 if (option_rv
== -1) break;
255 case OPT_VERBOSE
: /* --verbose or -v */
256 opt_verbose
= OPT_VERBOSE
;
258 case OPT_DEBUG
: /* --debug or -d */
259 opt_verbose
= OPT_DEBUG
;
261 case OPT_VERSION
: /* --version or -V */
262 version_command(PROGRAM_NAME
);
264 case OPT_HELP
: /* --help or -h */
265 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
267 case OPT_SERVERS
: /* --servers or -s */
268 opt_servers
= strdup(optarg
);
270 case OPT_FLAG
: /* --flag */
273 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
274 if (strtol_error
== true)
276 fprintf(stderr
, "Bad value passed via --flag\n");
281 case OPT_EXPIRE
: /* --expire */
284 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
285 if (strtol_error
== true)
287 fprintf(stderr
, "Bad value passed via --flag\n");
295 opt_method
= OPT_REPLACE
;
301 opt_hash
= strdup(optarg
);
304 opt_username
= optarg
;
310 /* getopt_long already printed an error message. */