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
);
90 memc
= memcached_create(NULL
);
91 process_hash_option(memc
, opt_hash
);
97 if ((temp
= getenv("MEMCACHED_SERVERS")))
99 opt_servers
= strdup(temp
);
103 fprintf(stderr
, "No Servers provided\n");
109 servers
= memcached_servers_parse(opt_servers
);
111 servers
= memcached_servers_parse(argv
[--argc
]);
113 memcached_server_push(memc
, servers
);
114 memcached_server_list_free(servers
);
115 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
116 (uint64_t)opt_binary
);
117 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
119 memcached_free(memc
);
123 while (optind
< argc
)
129 char *file_buffer_ptr
;
131 fd
= open(argv
[optind
], O_RDONLY
);
134 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
139 (void)fstat(fd
, &sbuf
);
141 ptr
= rindex(argv
[optind
], '/');
149 static const char *opstr
[] = { "set", "add", "replace" };
150 printf("op: %s\nsource file: %s\nlength: %zu\n"
151 "key: %s\nflags: %x\nexpires: %llu\n",
152 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
153 ptr
, opt_flags
, (unsigned long long)opt_expires
);
156 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
158 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
162 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
164 fprintf(stderr
, "read: %s\n", strerror(errno
));
168 if (read_length
!= sbuf
.st_size
)
170 fprintf(stderr
, "Failure reading from file\n");
174 if (opt_method
== OPT_ADD
)
175 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
176 file_buffer_ptr
, (size_t)sbuf
.st_size
,
177 opt_expires
, opt_flags
);
178 else if (opt_method
== OPT_REPLACE
)
179 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
180 file_buffer_ptr
, (size_t)sbuf
.st_size
,
181 opt_expires
, opt_flags
);
183 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
184 file_buffer_ptr
, (size_t)sbuf
.st_size
,
185 opt_expires
, opt_flags
);
187 if (rc
!= MEMCACHED_SUCCESS
)
189 fprintf(stderr
, "memcp: %s: memcache error %s",
190 ptr
, memcached_strerror(memc
, rc
));
191 if (memc
->cached_errno
)
192 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
193 fprintf(stderr
, "\n");
198 free(file_buffer_ptr
);
203 memcached_free(memc
);
214 static void options_parse(int argc
, char *argv
[])
219 memcached_programs_help_st help_options
[]=
224 static struct option long_options
[]=
226 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
227 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
228 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
229 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
230 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
231 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
232 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
233 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
234 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
235 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
236 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
237 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
238 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
239 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
245 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
247 if (option_rv
== -1) break;
256 case OPT_VERBOSE
: /* --verbose or -v */
257 opt_verbose
= OPT_VERBOSE
;
259 case OPT_DEBUG
: /* --debug or -d */
260 opt_verbose
= OPT_DEBUG
;
262 case OPT_VERSION
: /* --version or -V */
263 version_command(PROGRAM_NAME
);
265 case OPT_HELP
: /* --help or -h */
266 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
268 case OPT_SERVERS
: /* --servers or -s */
269 opt_servers
= strdup(optarg
);
271 case OPT_FLAG
: /* --flag */
274 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
275 if (strtol_error
== true)
277 fprintf(stderr
, "Bad value passed via --flag\n");
282 case OPT_EXPIRE
: /* --expire */
285 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
286 if (strtol_error
== true)
288 fprintf(stderr
, "Bad value passed via --flag\n");
296 opt_method
= OPT_REPLACE
;
302 opt_hash
= strdup(optarg
);
305 opt_username
= optarg
;
311 /* getopt_long already printed an error message. */