memcp: fix #83
[awesomized/libmemcached] / clients / memcp.cc
index 7b7454760e7e8a4cd36db8a675e91f08930fbc13..d87e0cad0cb4bc1486ab60b9ef23a9f9b4f1bdc8 100644 (file)
@@ -1,4 +1,5 @@
 /* LibMemcached
+ * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  * Copyright (C) 2006-2009 Brian Aker
  * All rights reserved.
  *
  *
  */
 
-#include "config.h"
+#include "mem_config.h"
 
-#include <iostream>
-#include <stdio.h>
-#include <stdlib.h>
-#include <inttypes.h>
-#include <unistd.h>
+#include <cerrno>
+#include <climits>
+#include <cstdio>
+#include <cstdlib>
+#include <cstdlib>
+#include <cstring>
+#include <fcntl.h>
 #include <getopt.h>
-#include <sys/types.h>
+#include <iostream>
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <strings.h>
-#include <string.h>
 #include <sys/types.h>
-#include <stdlib.h>
-#include <limits.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 
-#include <libmemcached/memcached.h>
+#include <libmemcached-1.0/memcached.h>
 
 #include "client_options.h"
 #include "utilities.h"
@@ -40,7 +42,9 @@
 /* Prototypes */
 static void options_parse(int argc, char *argv[]);
 
-static int opt_binary=0;
+static bool opt_binary= false;
+static bool opt_udp= false;
+static bool opt_buffer= false;
 static int opt_verbose= 0;
 static char *opt_servers= NULL;
 static char *opt_hash= NULL;
@@ -60,17 +64,17 @@ static long strtol_wrapper(const char *nptr, int base, bool *error)
 
   /* Check for various possible errors */
 
-  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
-      || (errno != 0 && val == 0))
+  if ((errno == ERANGE and (val == LONG_MAX or val == LONG_MIN))
+      or (errno != 0 && val == 0))
   {
     *error= true;
-    return EXIT_SUCCESS;
+    return 0;
   }
 
   if (endptr == nptr)
   {
     *error= true;
-    return EXIT_SUCCESS;
+    return 0;
   }
 
   *error= false;
@@ -79,19 +83,52 @@ static long strtol_wrapper(const char *nptr, int base, bool *error)
 
 int main(int argc, char *argv[])
 {
-  memcached_st *memc;
-  memcached_return_t rc;
-  memcached_server_st *servers;
-
-  int return_code= 0;
 
   options_parse(argc, argv);
+
+  if (optind >= argc)
+  {
+    fprintf(stderr, "Expected argument after options\n");
+    exit(EXIT_FAILURE);
+  }
+
   initialize_sockets();
 
-  memc= memcached_create(NULL);
+  memcached_st *memc= memcached_create(NULL);
+
+  if (opt_udp)
+  {
+    if (opt_verbose)
+    {
+      std::cout << "Enabling UDP" << std::endl;
+    }
+
+    if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp)))
+    {
+      memcached_free(memc);
+      std::cerr << "Could not enable UDP protocol." << std::endl;
+      return EXIT_FAILURE;
+    }
+  }
+
+  if (opt_buffer)
+  {
+    if (opt_verbose)
+    {
+      std::cout << "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl;
+    }
+
+    if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, opt_buffer)))
+    {
+      memcached_free(memc);
+      std::cerr << "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl;
+      return EXIT_FAILURE;
+    }
+  }
+
   process_hash_option(memc, opt_hash);
 
-  if (!opt_servers)
+  if (opt_servers == NULL)
   {
     char *temp;
 
@@ -99,23 +136,30 @@ int main(int argc, char *argv[])
     {
       opt_servers= strdup(temp);
     }
-    else
+#if 0
+    else if (argc >= 1 and argv[--argc])
     {
-      fprintf(stderr, "No Servers provided\n");
-      exit(1);
+      opt_servers= strdup(argv[argc]);
+    }
+#endif
+
+    if (opt_servers == NULL)
+    {
+      std::cerr << "No Servers provided" << std::endl;
+      exit(EXIT_FAILURE);
     }
   }
 
-  if (opt_servers)
-    servers= memcached_servers_parse(opt_servers);
-  else
-    servers= memcached_servers_parse(argv[--argc]);
+  memcached_server_st* servers= memcached_servers_parse(opt_servers);
+  if (servers == NULL or memcached_server_list_count(servers) == 0)
+  {
+    std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
+    return EXIT_FAILURE;
+  }
 
   memcached_server_push(memc, servers);
   memcached_server_list_free(servers);
-  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
-                         (uint64_t)opt_binary);
-
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
   if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
   {
     memcached_free(memc);
@@ -134,29 +178,36 @@ int main(int argc, char *argv[])
     }
   }
 
+  int exit_code= EXIT_SUCCESS;
   while (optind < argc)
   {
-    struct stat sbuf;
-    int fd;
-    char *ptr;
-    ssize_t read_length;
-    char *file_buffer_ptr;
-
-    fd= open(argv[optind], O_RDONLY);
+    int fd= open(argv[optind], O_RDONLY);
     if (fd < 0)
     {
-      fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
+      std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
       optind++;
+      exit_code= EXIT_FAILURE;
       continue;
     }
 
-    (void)fstat(fd, &sbuf);
+    struct stat sbuf;
+    if (fstat(fd, &sbuf) == -1)
+    {
+      std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
+      optind++;
+      exit_code= EXIT_FAILURE;
+      continue;
+    }
 
-    ptr= rindex(argv[optind], '/');
+    char *ptr= rindex(argv[optind], '/');
     if (ptr)
+    {
       ptr++;
+    }
     else
+    {
       ptr= argv[optind];
+    }
 
     if (opt_verbose)
     {
@@ -167,68 +218,88 @@ int main(int argc, char *argv[])
             ptr, opt_flags, (unsigned long)opt_expires);
     }
 
-    if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
+    // The file may be empty
+    char *file_buffer_ptr= NULL;
+    if (sbuf.st_size > 0)
     {
-      fprintf(stderr, "malloc: %s\n", strerror(errno));
-      exit(1);
-    }
+      if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
+      {
+        std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl;
+        close(fd);
+        exit(EXIT_FAILURE);
+      }
 
-    if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
-    {
-      fprintf(stderr, "read: %s\n", strerror(errno));
-      exit(1);
-    }
+      ssize_t read_length;
+      if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
+      {
+        std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
+        close(fd);
+        free(file_buffer_ptr);
+        exit(EXIT_FAILURE);
+      }
 
-    if (read_length != sbuf.st_size)
-    {
-      fprintf(stderr, "Failure reading from file\n");
-      exit(1);
+      if (read_length != sbuf.st_size)
+      {
+        std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
+        close(fd);
+        free(file_buffer_ptr);
+        exit(EXIT_FAILURE);
+      }
     }
 
+    memcached_return_t rc;
     if (opt_method == OPT_ADD)
+    {
       rc= memcached_add(memc, ptr, strlen(ptr),
                         file_buffer_ptr, (size_t)sbuf.st_size,
                        opt_expires, opt_flags);
+    }
     else if (opt_method == OPT_REPLACE)
+    {
       rc= memcached_replace(memc, ptr, strlen(ptr),
                            file_buffer_ptr, (size_t)sbuf.st_size,
                            opt_expires, opt_flags);
+    }
     else
+    {
       rc= memcached_set(memc, ptr, strlen(ptr),
                         file_buffer_ptr, (size_t)sbuf.st_size,
                         opt_expires, opt_flags);
+    }
 
-    if (rc != MEMCACHED_SUCCESS)
+    if (memcached_failed(rc))
     {
-      fprintf(stderr, "memcp: %s: memcache error %s",
-             ptr, memcached_strerror(memc, rc));
-      if (memcached_last_error_errno(memc))
-       fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
-      fprintf(stderr, "\n");
-
-      return_code= -1;
+      std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl;
+      exit_code= EXIT_FAILURE;
     }
 
-    free(file_buffer_ptr);
-    close(fd);
+    ::free(file_buffer_ptr);
+    ::close(fd);
     optind++;
   }
 
+  if (opt_verbose)
+  {
+    std::cout << "Calling memcached_free()" << std::endl;
+  }
+
   memcached_free(memc);
 
   if (opt_servers)
+  {
     free(opt_servers);
+  }
+
   if (opt_hash)
+  {
     free(opt_hash);
+  }
 
-  return return_code;
+  return exit_code;
 }
 
 static void options_parse(int argc, char *argv[])
 {
-  int option_index= 0;
-  int option_rv;
-
   memcached_programs_help_st help_options[]=
   {
     {0},
@@ -238,6 +309,9 @@ static void options_parse(int argc, char *argv[])
     {
       {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
       {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
+      {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
+      {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
+      {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER},
       {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
       {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
       {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
@@ -253,34 +327,46 @@ static void options_parse(int argc, char *argv[])
       {0, 0, 0, 0},
     };
 
+  bool opt_version= false;
+  bool opt_help= false;
+  int option_index= 0;
+
   while (1)
   {
-    option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
+    int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
 
-    if (option_rv == -1) break;
+    if (option_rv == -1)
+      break;
 
     switch (option_rv)
     {
     case 0:
       break;
+
     case OPT_BINARY:
-      opt_binary = 1;
+      opt_binary= true;
       break;
+
     case OPT_VERBOSE: /* --verbose or -v */
-      opt_verbose = OPT_VERBOSE;
+      opt_verbose= OPT_VERBOSE;
       break;
+
     case OPT_DEBUG: /* --debug or -d */
-      opt_verbose = OPT_DEBUG;
+      opt_verbose= OPT_DEBUG;
       break;
+
     case OPT_VERSION: /* --version or -V */
-      version_command(PROGRAM_NAME);
+      opt_version= true;
       break;
+
     case OPT_HELP: /* --help or -h */
-      help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
+      opt_help= true;
       break;
+
     case OPT_SERVERS: /* --servers or -s */
       opt_servers= strdup(optarg);
       break;
+
     case OPT_FLAG: /* --flag */
       {
         bool strtol_error;
@@ -290,41 +376,74 @@ static void options_parse(int argc, char *argv[])
           fprintf(stderr, "Bad value passed via --flag\n");
           exit(1);
         }
-        break;
       }
+      break;
+
     case OPT_EXPIRE: /* --expire */
       {
         bool strtol_error;
-        opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
+        opt_expires= (time_t)strtol_wrapper(optarg, 10, &strtol_error);
         if (strtol_error == true)
         {
-          fprintf(stderr, "Bad value passed via --flag\n");
+          fprintf(stderr, "Bad value passed via --expire\n");
           exit(1);
         }
       }
+      break;
+
     case OPT_SET:
       opt_method= OPT_SET;
       break;
+
     case OPT_REPLACE:
       opt_method= OPT_REPLACE;
       break;
+
     case OPT_ADD:
       opt_method= OPT_ADD;
       break;
+
     case OPT_HASH:
       opt_hash= strdup(optarg);
       break;
+
     case OPT_USERNAME:
       opt_username= optarg;
       break;
+
     case OPT_PASSWD:
       opt_passwd= optarg;
       break;
-   case '?':
+
+    case OPT_QUIET:
+      close_stdio();
+      break;
+
+    case OPT_UDP:
+      opt_udp= true;
+      break;
+
+    case OPT_BUFFER:
+      opt_buffer= true;
+      break;
+
+    case '?':
       /* getopt_long already printed an error message. */
       exit(1);
     default:
       abort();
     }
   }
+
+  if (opt_version)
+  {
+    version_command(PROGRAM_NAME);
+    exit(EXIT_SUCCESS);
+  }
+
+  if (opt_help)
+  {
+    help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
+    exit(EXIT_SUCCESS);
+  }
 }