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 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 int main(int argc
, char *argv
[])
48 memcached_return_t rc
;
49 memcached_server_st
*servers
;
51 options_parse(argc
, argv
);
53 memc
= memcached_create(NULL
);
54 process_hash_option(memc
, opt_hash
);
60 if ((temp
= getenv("MEMCACHED_SERVERS")))
61 opt_servers
= strdup(temp
);
64 fprintf(stderr
, "No Servers provided\n");
70 servers
= memcached_servers_parse(opt_servers
);
72 servers
= memcached_servers_parse(argv
[--argc
]);
74 memcached_server_push(memc
, servers
);
75 memcached_server_list_free(servers
);
76 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
77 (uint64_t)opt_binary
);
85 char *file_buffer_ptr
;
87 fd
= open(argv
[optind
], O_RDONLY
);
90 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
95 (void)fstat(fd
, &sbuf
);
97 ptr
= rindex(argv
[optind
], '/');
105 static const char *opstr
[] = { "set", "add", "replace" };
106 printf("op: %s\nsource file: %s\nlength: %zu\n"
107 "key: %s\nflags: %x\nexpires: %llu\n",
108 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
109 ptr
, opt_flags
, (unsigned long long)opt_expires
);
112 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
114 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
118 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
120 fprintf(stderr
, "read: %s\n", strerror(errno
));
124 if (read_length
!= sbuf
.st_size
)
126 fprintf(stderr
, "Failure reading from file\n");
130 if (opt_method
== OPT_ADD
)
131 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
132 file_buffer_ptr
, (size_t)sbuf
.st_size
,
133 opt_expires
, opt_flags
);
134 else if (opt_method
== OPT_REPLACE
)
135 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
136 file_buffer_ptr
, (size_t)sbuf
.st_size
,
137 opt_expires
, opt_flags
);
139 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
140 file_buffer_ptr
, (size_t)sbuf
.st_size
,
141 opt_expires
, opt_flags
);
143 if (rc
!= MEMCACHED_SUCCESS
)
145 fprintf(stderr
, "memcp: %s: memcache error %s",
146 ptr
, memcached_strerror(memc
, rc
));
147 if (memc
->cached_errno
)
148 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
149 fprintf(stderr
, "\n");
152 free(file_buffer_ptr
);
157 memcached_free(memc
);
167 void options_parse(int argc
, char *argv
[])
172 memcached_programs_help_st help_options
[]=
177 static struct option long_options
[]=
179 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
180 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
181 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
182 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
183 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
184 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
185 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
186 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
187 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
188 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
189 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
190 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
196 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
198 if (option_rv
== -1) break;
207 case OPT_VERBOSE
: /* --verbose or -v */
208 opt_verbose
= OPT_VERBOSE
;
210 case OPT_DEBUG
: /* --debug or -d */
211 opt_verbose
= OPT_DEBUG
;
213 case OPT_VERSION
: /* --version or -V */
214 version_command(PROGRAM_NAME
);
216 case OPT_HELP
: /* --help or -h */
217 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
219 case OPT_SERVERS
: /* --servers or -s */
220 opt_servers
= strdup(optarg
);
222 case OPT_FLAG
: /* --flag */
223 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
225 case OPT_EXPIRE
: /* --expire */
226 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
232 opt_method
= OPT_REPLACE
;
238 opt_hash
= strdup(optarg
);
241 /* getopt_long already printed an error message. */