key_length, key,
offset);
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
+ if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
{
fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (ptr->number_of_hosts+1));
- memset(ptr->hosts, 0, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts);
ptr->hosts[ptr->number_of_hosts].hostname=
(char *)malloc(sizeof(char) * (strlen(hostname)+1));
memset(ptr->hosts[ptr->number_of_hosts].hostname, 0, strlen(hostname)+1);
else
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"flush_all\r\n");
- if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1))
+ if ((write(ptr->hosts[x].fd, buffer, send_length) == -1))
{
fprintf(stderr, "failed flush_all TCP\n");
if (*error != MEMCACHED_SUCCESS)
return NULL;
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
+ if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
{
fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key);
*error= MEMCACHED_WRITE_FAILURE;
send_length= snprintf(buffer, HUGE_STRING_LEN,
"stats\r\n");
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
+ if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
{
fprintf(stderr, "failed on stats\n");
uint16_t flags,
char *verb)
{
- size_t send_length;
+ size_t write_length;
+ ssize_t sent_length;
memcached_return rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
unsigned int server_key;
rc= memcached_connect(ptr);
+ assert(value);
+ assert(value_length);
if (rc != MEMCACHED_SUCCESS)
return rc;
server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
- send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
+ write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%s %.*s %u %u %u\r\n", verb,
key_length, key, flags, expiration, value_length);
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
+ if ((sent_length= write(ptr->hosts[server_key].fd, buffer, write_length)) == -1)
{
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
+ fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno));
return MEMCACHED_WRITE_FAILURE;
}
+ assert(write_length == sent_length);
- send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
- "%.*s\r\n",
- value_length, value);
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
+ WATCHPOINT;
+ printf("About to push %.*s\n", value_length, value);
+ WATCHPOINT;
+ if ((sent_length= write(ptr->hosts[server_key].fd, value, value_length)) == -1)
{
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
+ fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno));
return MEMCACHED_WRITE_FAILURE;
}
-
- send_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
+ assert(value_length == sent_length);
+ if ((sent_length= write(ptr->hosts[server_key].fd, "\r\n", 2)) == -1)
+ {
+ fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno));
+
+ return MEMCACHED_WRITE_FAILURE;
+ }
+ assert(2 == sent_length);
+
+ sent_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
- if (send_length && buffer[0] == 'S') /* STORED */
+ if (sent_length && buffer[0] == 'S') /* STORED */
return MEMCACHED_SUCCESS;
- else if (send_length && buffer[0] == 'N') /* NOT_STORED */
+ else if (write_length && buffer[0] == 'N') /* NOT_STORED */
return MEMCACHED_NOTSTORED;
else
return MEMCACHED_READ_FAILURE;
{
memcached_return rc;
- if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1))
+ if ((write(ptr->hosts[x].fd, buffer, send_length) == -1))
{
fprintf(stderr, "failed verbosity\n");
\r
options_parse(argc, argv);\r
\r
- parse_opt_servers(memc, opt_servers);\r
+ if (opt_servers)\r
+ parse_opt_servers(memc, opt_servers);\r
\r
while (optind <= argc) \r
{\r
if (opt_displayflag)\r
{\r
if (opt_verbose)\r
- printf("key: %.*s\nflags: ", argv[optind]);\r
- printf("%d\n", flags);\r
+ printf("key: %s\nflags: ", argv[optind]);\r
+ printf("%u\n", flags);\r
}\r
else \r
{\r
if (opt_verbose)\r
- printf("key: %.*s\nflags: %d\nlength: %d\nvalue: ",\r
+ printf("key: %s\nflags: %u\nlength: %uz\nvalue: ",\r
argv[optind], flags, string_length);\r
printf("%.*s\n", string_length, string);\r
free(string);\r
{\r
{"version", no_argument, NULL, OPT_VERSION},\r
{"help", no_argument, NULL, OPT_HELP},\r
- {"verbose", no_argument, &opt_verbose, 1},\r
- {"debug", no_argument, &opt_verbose, 2},\r
+ {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},\r
+ {"debug", no_argument, &opt_verbose, OPT_DEBUG},\r
{"servers", required_argument, NULL, OPT_SERVERS},\r
{"flag", no_argument, &opt_displayflag, OPT_FLAG},\r
{0, 0, 0, 0},\r
#include <sys/types.h>\r
#include <sys/stat.h>\r
#include <sys/types.h>\r
-#include <sys/mman.h>\r
#include <fcntl.h>\r
#include <errno.h>\r
\r
/* Prototypes */\r
void options_parse(int argc, char *argv[]);\r
\r
-static int opt_verbose;\r
-static char *opt_servers;\r
-static int opt_replace;\r
+static int opt_verbose= 0;\r
+static char *opt_servers= NULL;\r
+static int opt_method= 0;\r
uint16_t opt_flags= 0;\r
time_t opt_expires= 0;\r
\r
char *string;\r
size_t string_length;\r
memcached_return rc;\r
-\r
options_parse(argc, argv);\r
\r
memc= memcached_init(NULL);\r
- parse_opt_servers(memc, opt_servers);\r
+ if (opt_servers)\r
+ parse_opt_servers(memc, opt_servers);\r
+ else\r
+ parse_opt_servers(memc, argv[--argc]);\r
\r
- while (optind <= argc) \r
+ while (optind < argc) \r
{\r
- char *mptr;\r
struct stat sbuf;\r
int fd;\r
char *ptr;\r
+ ssize_t read_length;\r
+ char *file_buffer_ptr;\r
\r
fd= open(argv[optind], O_RDONLY);\r
- if (fd < 0) {\r
+ if (fd < 0) \r
+ {\r
fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));\r
optind++;\r
continue;\r
}\r
\r
(void)fstat(fd, &sbuf);\r
- mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\r
- if (mptr == MAP_FAILED) {\r
- fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));\r
- close(fd);\r
- optind++;\r
- continue;\r
- }\r
\r
ptr= rindex(argv[optind], '/');\r
if (ptr)\r
else\r
ptr= argv[optind];\r
\r
- if (opt_verbose) {\r
+ if (opt_verbose) \r
+ {\r
static char *opstr[] = { "set", "add", "replace" };\r
- printf("op: %s\nsource file: %s\nlength: %d\n"\r
- "key: %s\nflags: %d\n expires: %ld\n",\r
- opstr[opt_replace], argv[optind], sbuf.st_size,\r
+ printf("op: %s\nsource file: %s\nlength: %zu\n"\r
+ "key: %s\nflags: %u\n expires: %llu\n",\r
+ opstr[opt_method], argv[optind], sbuf.st_size,\r
ptr, opt_flags, opt_expires);\r
}\r
\r
- if (opt_replace == 0)\r
- rc= memcached_set(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
- opt_expires, opt_flags);\r
- else if (opt_replace == 1)\r
+ if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)\r
+ {\r
+ fprintf(stderr, "malloc: %s\n", strerror(errno)); \r
+ exit(1);\r
+ }\r
+\r
+ if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)\r
+ {\r
+ fprintf(stderr, "read: %s\n", strerror(errno)); \r
+ exit(1);\r
+ }\r
+ assert(read_length == sbuf.st_size);\r
+\r
+ if (opt_method == OPT_ADD)\r
rc= memcached_add(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
+ file_buffer_ptr, sbuf.st_size,\r
opt_expires, opt_flags);\r
- else if (opt_replace == 2)\r
+ else if (opt_method == OPT_REPLACE)\r
rc= memcached_replace(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
+ file_buffer_ptr, sbuf.st_size,\r
opt_expires, opt_flags);\r
else\r
- abort();\r
+ rc= memcached_set(memc, ptr, strlen(ptr),\r
+ file_buffer_ptr, sbuf.st_size,\r
+ opt_expires, opt_flags);\r
\r
- if (rc != MEMCACHED_SUCCESS) {\r
+ if (rc != MEMCACHED_SUCCESS)\r
fprintf(stderr, "memcp: %s: memcache error %s\n", \r
ptr, memcached_strerror(memc, rc));\r
- }\r
\r
- munmap(mptr, sbuf.st_size);\r
+ WATCHPOINT;\r
+ free(file_buffer_ptr);\r
close(fd);\r
optind++;\r
}\r
\r
memcached_deinit(memc);\r
\r
+ cleanup();\r
+\r
return 0;\r
};\r
\r
{"servers", required_argument, NULL, OPT_SERVERS},\r
{"flag", required_argument, NULL, OPT_FLAG},\r
{"expire", required_argument, NULL, OPT_EXPIRE},\r
- {"set", no_argument, &opt_replace, OPT_SET},\r
- {"add", no_argument, &opt_replace, OPT_ADD},\r
- {"replace", no_argument, &opt_replace, OPT_REPLACE},\r
+ {"set", no_argument, NULL, OPT_SET},\r
+ {"add", no_argument, NULL, OPT_ADD},\r
+ {"replace", no_argument, NULL, OPT_REPLACE},\r
{0, 0, 0, 0},\r
};\r
\r
printf("useful help messages go here\n");\r
exit(0);\r
case OPT_SERVERS: /* --servers or -s */\r
- opt_servers= optarg;\r
+ opt_servers= strdup_cleanup(optarg);\r
break;\r
case OPT_FLAG: /* --flag */\r
opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);\r
case OPT_EXPIRE: /* --expire */\r
opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);\r
break;\r
+ case OPT_SET:\r
+ opt_method= OPT_SET;\r
+ break;\r
+ case OPT_REPLACE:\r
+ opt_method= OPT_REPLACE;\r
+ break;\r
+ case OPT_ADD:\r
+ opt_method= OPT_ADD;\r
+ break;\r
case '?':\r
/* getopt_long already printed an error message. */\r
exit(1);\r
{\r
{"version", no_argument, NULL, OPT_VERSION},\r
{"help", no_argument, NULL, OPT_HELP},\r
- {"verbose", no_argument, &opt_verbose, 1},\r
- {"debug", no_argument, &opt_verbose, 2},\r
+ {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},\r
+ {"debug", no_argument, &opt_verbose, OPT_DEBUG},\r
{"servers", required_argument, NULL, OPT_SERVERS},\r
{"expire", required_argument, NULL, OPT_EXPIRE},\r
{0, 0, 0, 0},\r
\r
while (optind <= argc) \r
{\r
- if (opt_verbose) {\r
- printf("key: %s\nexpires: %ld\n", argv[optind], expires);\r
- }\r
+ if (opt_verbose) \r
+ printf("key: %s\nexpires: %llu\n", argv[optind], expires);\r
rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), expires);\r
\r
- if (rc != MEMCACHED_SUCCESS) {\r
+ if (rc != MEMCACHED_SUCCESS) \r
+ {\r
fprintf(stderr, "memrm: %s: memcache error %s\n", \r
argv[optind], memcached_strerror(memc, rc));\r
}\r
#include <memcached.h>
+static char **cleanup_list= NULL;
+static char cleanup_list_length= 0;
+
void parse_opt_servers(memcached_st *memc,
- char *opt_servers)
+ char *server_strings)
{
char *string;
unsigned int port;
char *begin_ptr;
+ char *end_ptr;
+
+ end_ptr= server_strings + strlen(server_strings);
+
+ assert(server_strings);
+ assert(memc);
- for (begin_ptr= opt_servers, string= index(opt_servers, ',');
- begin_ptr[0];
+ for (begin_ptr= server_strings, string= index(server_strings, ',');
+ begin_ptr == end_ptr;
begin_ptr= ++string, string= index(begin_ptr, ','))
{
char buffer[HUGE_STRING_LEN];
}
else
{
- size_t length= strlen(opt_servers);
+ size_t length= strlen(server_strings);
memcpy(buffer, begin_ptr, length);
- string= opt_servers+length;
+ string= server_strings+length;
}
ptr= index(buffer, ':');
}
}
+void cleanup(void)
+{
+ unsigned int x;
+ for (x= 0; x < cleanup_list_length; x++)
+ free(cleanup_list[x]);
+
+ free(cleanup_list);
+}
+
+char *strdup_cleanup(const char *str)
+{
+ char *ptr;
+
+ ptr= strdup(str);
+
+ cleanup_list= (char **)realloc(cleanup_list, sizeof(char *) * (cleanup_list_length+1));
+ cleanup_list[cleanup_list_length]= ptr;
+ cleanup_list_length++;
+
+ return ptr;
+}
void parse_opt_servers (memcached_st *ptr,
char *opt_servers);
+char *strdup_cleanup(const char *str);
+void cleanup(void);