1 #include "libmemcached/common.h"
15 #include <libmemcached/memcached.h>
17 #include "client_options.h"
18 #include "utilities.h"
20 #define PROGRAM_NAME "memcp"
21 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
24 void options_parse(int argc
, char *argv
[]);
26 static int opt_binary
=0;
27 static int opt_verbose
= 0;
28 static char *opt_servers
= NULL
;
29 static char *opt_hash
= NULL
;
30 static int opt_method
= OPT_SET
;
31 static uint32_t opt_flags
= 0;
32 static time_t opt_expires
= 0;
34 int main(int argc
, char *argv
[])
37 memcached_return_t rc
;
38 memcached_server_st
*servers
;
40 options_parse(argc
, argv
);
42 memc
= memcached_create(NULL
);
43 process_hash_option(memc
, opt_hash
);
49 if ((temp
= getenv("MEMCACHED_SERVERS")))
50 opt_servers
= strdup(temp
);
53 fprintf(stderr
, "No Servers provided\n");
59 servers
= memcached_servers_parse(opt_servers
);
61 servers
= memcached_servers_parse(argv
[--argc
]);
63 memcached_server_push(memc
, servers
);
64 memcached_server_list_free(servers
);
65 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
66 (uint64_t)opt_binary
);
74 char *file_buffer_ptr
;
76 fd
= open(argv
[optind
], O_RDONLY
);
79 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
84 (void)fstat(fd
, &sbuf
);
86 ptr
= rindex(argv
[optind
], '/');
94 static const char *opstr
[] = { "set", "add", "replace" };
95 printf("op: %s\nsource file: %s\nlength: %zu\n"
96 "key: %s\nflags: %x\nexpires: %llu\n",
97 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
98 ptr
, opt_flags
, (unsigned long long)opt_expires
);
101 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
103 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
107 if ((read_length
= read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
109 fprintf(stderr
, "read: %s\n", strerror(errno
));
113 if (read_length
!= sbuf
.st_size
)
115 fprintf(stderr
, "Failure reading from file\n");
119 if (opt_method
== OPT_ADD
)
120 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
121 file_buffer_ptr
, (size_t)sbuf
.st_size
,
122 opt_expires
, opt_flags
);
123 else if (opt_method
== OPT_REPLACE
)
124 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
125 file_buffer_ptr
, (size_t)sbuf
.st_size
,
126 opt_expires
, opt_flags
);
128 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
129 file_buffer_ptr
, (size_t)sbuf
.st_size
,
130 opt_expires
, opt_flags
);
132 if (rc
!= MEMCACHED_SUCCESS
)
134 fprintf(stderr
, "memcp: %s: memcache error %s",
135 ptr
, memcached_strerror(memc
, rc
));
136 if (memc
->cached_errno
)
137 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
138 fprintf(stderr
, "\n");
141 free(file_buffer_ptr
);
146 memcached_free(memc
);
156 void options_parse(int argc
, char *argv
[])
161 memcached_programs_help_st help_options
[]=
166 static struct option long_options
[]=
168 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
169 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
170 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
171 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
172 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
173 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
174 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
175 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
176 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
177 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
178 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
179 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
185 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
187 if (option_rv
== -1) break;
196 case OPT_VERBOSE
: /* --verbose or -v */
197 opt_verbose
= OPT_VERBOSE
;
199 case OPT_DEBUG
: /* --debug or -d */
200 opt_verbose
= OPT_DEBUG
;
202 case OPT_VERSION
: /* --version or -V */
203 version_command(PROGRAM_NAME
);
205 case OPT_HELP
: /* --help or -h */
206 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
208 case OPT_SERVERS
: /* --servers or -s */
209 opt_servers
= strdup(optarg
);
211 case OPT_FLAG
: /* --flag */
212 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
214 case OPT_EXPIRE
: /* --expire */
215 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
221 opt_method
= OPT_REPLACE
;
227 opt_hash
= strdup(optarg
);
230 /* getopt_long already printed an error message. */