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;
45 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
50 errno
= 0; /* To distinguish success/failure after call */
51 val
= strtol(nptr
, &endptr
, base
);
53 /* Check for various possible errors */
55 if ((errno
== ERANGE
&& (val
== LONG_MAX
|| val
== LONG_MIN
))
56 || (errno
!= 0 && val
== 0))
72 int main(int argc
, char *argv
[])
75 memcached_return_t rc
;
76 memcached_server_st
*servers
;
78 options_parse(argc
, argv
);
80 memc
= memcached_create(NULL
);
81 process_hash_option(memc
, opt_hash
);
87 if ((temp
= getenv("MEMCACHED_SERVERS")))
88 opt_servers
= strdup(temp
);
91 fprintf(stderr
, "No Servers provided\n");
97 servers
= memcached_servers_parse(opt_servers
);
99 servers
= memcached_servers_parse(argv
[--argc
]);
101 memcached_server_push(memc
, servers
);
102 memcached_server_list_free(servers
);
103 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
104 (uint64_t)opt_binary
);
106 while (optind
< argc
)
112 char *file_buffer_ptr
;
114 fd
= open(argv
[optind
], O_RDONLY
);
117 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
122 (void)fstat(fd
, &sbuf
);
124 ptr
= rindex(argv
[optind
], '/');
132 static const char *opstr
[] = { "set", "add", "replace" };
133 printf("op: %s\nsource file: %s\nlength: %zu\n"
134 "key: %s\nflags: %x\nexpires: %llu\n",
135 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
136 ptr
, opt_flags
, (unsigned long long)opt_expires
);
139 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
141 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
145 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
147 fprintf(stderr
, "read: %s\n", strerror(errno
));
151 if (read_length
!= sbuf
.st_size
)
153 fprintf(stderr
, "Failure reading from file\n");
157 if (opt_method
== OPT_ADD
)
158 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
159 file_buffer_ptr
, (size_t)sbuf
.st_size
,
160 opt_expires
, opt_flags
);
161 else if (opt_method
== OPT_REPLACE
)
162 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
163 file_buffer_ptr
, (size_t)sbuf
.st_size
,
164 opt_expires
, opt_flags
);
166 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
167 file_buffer_ptr
, (size_t)sbuf
.st_size
,
168 opt_expires
, opt_flags
);
170 if (rc
!= MEMCACHED_SUCCESS
)
172 fprintf(stderr
, "memcp: %s: memcache error %s",
173 ptr
, memcached_strerror(memc
, rc
));
174 if (memc
->cached_errno
)
175 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
176 fprintf(stderr
, "\n");
179 free(file_buffer_ptr
);
184 memcached_free(memc
);
194 static void options_parse(int argc
, char *argv
[])
199 memcached_programs_help_st help_options
[]=
204 static struct option long_options
[]=
206 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
207 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
208 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
209 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
210 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
211 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
212 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
213 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
214 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
215 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
216 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
217 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
223 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
225 if (option_rv
== -1) break;
234 case OPT_VERBOSE
: /* --verbose or -v */
235 opt_verbose
= OPT_VERBOSE
;
237 case OPT_DEBUG
: /* --debug or -d */
238 opt_verbose
= OPT_DEBUG
;
240 case OPT_VERSION
: /* --version or -V */
241 version_command(PROGRAM_NAME
);
243 case OPT_HELP
: /* --help or -h */
244 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
246 case OPT_SERVERS
: /* --servers or -s */
247 opt_servers
= strdup(optarg
);
249 case OPT_FLAG
: /* --flag */
252 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
253 if (strtol_error
== true)
255 fprintf(stderr
, "Bad value passed via --flag\n");
260 case OPT_EXPIRE
: /* --expire */
263 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
264 if (strtol_error
== true)
266 fprintf(stderr
, "Bad value passed via --flag\n");
274 opt_method
= OPT_REPLACE
;
280 opt_hash
= strdup(optarg
);
283 /* getopt_long already printed an error message. */