14 #include <libmemcached/memcached.h>
16 #include "client_options.h"
17 #include "utilities.h"
19 #define PROGRAM_NAME "memcp"
20 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
23 void options_parse(int argc
, char *argv
[]);
25 static int opt_binary
=0;
26 static int opt_verbose
= 0;
27 static char *opt_servers
= NULL
;
28 static char *opt_hash
= NULL
;
29 static int opt_method
= OPT_SET
;
30 static uint32_t opt_flags
= 0;
31 static time_t opt_expires
= 0;
33 int main(int argc
, char *argv
[])
37 memcached_server_st
*servers
;
39 options_parse(argc
, argv
);
41 memc
= memcached_create(NULL
);
42 process_hash_option(memc
, opt_hash
);
48 if ((temp
= getenv("MEMCACHED_SERVERS")))
49 opt_servers
= strdup(temp
);
52 fprintf(stderr
, "No Servers provided\n");
58 servers
= memcached_servers_parse(opt_servers
);
60 servers
= memcached_servers_parse(argv
[--argc
]);
62 memcached_server_push(memc
, servers
);
63 memcached_server_list_free(servers
);
64 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
72 char *file_buffer_ptr
;
74 fd
= open(argv
[optind
], O_RDONLY
);
77 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
82 (void)fstat(fd
, &sbuf
);
84 ptr
= rindex(argv
[optind
], '/');
92 static char *opstr
[] = { "set", "add", "replace" };
93 printf("op: %s\nsource file: %s\nlength: %zu\n"
94 "key: %s\nflags: %x\nexpires: %llu\n",
95 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
96 ptr
, opt_flags
, (unsigned long long)opt_expires
);
99 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
101 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
105 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
107 fprintf(stderr
, "read: %s\n", strerror(errno
));
111 if (read_length
!= sbuf
.st_size
)
113 fprintf(stderr
, "Failure reading from file\n");
117 if (opt_method
== OPT_ADD
)
118 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
119 file_buffer_ptr
, sbuf
.st_size
,
120 opt_expires
, opt_flags
);
121 else if (opt_method
== OPT_REPLACE
)
122 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
123 file_buffer_ptr
, sbuf
.st_size
,
124 opt_expires
, opt_flags
);
126 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
127 file_buffer_ptr
, sbuf
.st_size
,
128 opt_expires
, opt_flags
);
130 if (rc
!= MEMCACHED_SUCCESS
)
132 fprintf(stderr
, "memcp: %s: memcache error %s",
133 ptr
, memcached_strerror(memc
, rc
));
134 if (memc
->cached_errno
)
135 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
136 fprintf(stderr
, "\n");
139 free(file_buffer_ptr
);
144 memcached_free(memc
);
154 void options_parse(int argc
, char *argv
[])
159 memcached_programs_help_st help_options
[]=
164 static struct option long_options
[]=
166 {"version", no_argument
, NULL
, OPT_VERSION
},
167 {"help", no_argument
, NULL
, OPT_HELP
},
168 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
169 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
170 {"servers", required_argument
, NULL
, OPT_SERVERS
},
171 {"flag", required_argument
, NULL
, OPT_FLAG
},
172 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
173 {"set", no_argument
, NULL
, OPT_SET
},
174 {"add", no_argument
, NULL
, OPT_ADD
},
175 {"replace", no_argument
, NULL
, OPT_REPLACE
},
176 {"hash", required_argument
, NULL
, OPT_HASH
},
177 {"binary", no_argument
, NULL
, OPT_BINARY
},
183 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
185 if (option_rv
== -1) break;
194 case OPT_VERBOSE
: /* --verbose or -v */
195 opt_verbose
= OPT_VERBOSE
;
197 case OPT_DEBUG
: /* --debug or -d */
198 opt_verbose
= OPT_DEBUG
;
200 case OPT_VERSION
: /* --version or -V */
201 version_command(PROGRAM_NAME
);
203 case OPT_HELP
: /* --help or -h */
204 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
206 case OPT_SERVERS
: /* --servers or -s */
207 opt_servers
= strdup(optarg
);
209 case OPT_FLAG
: /* --flag */
210 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
212 case OPT_EXPIRE
: /* --expire */
213 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
219 opt_method
= OPT_REPLACE
;
224 opt_hash
= strdup(optarg
);
227 /* getopt_long already printed an error message. */