Add a method for checking error/returning error.
[awesomized/libmemcached] / libmemcached / stats.cc
index d5d34a84285562b9510dc174e38f9a122c01a309..d4d8b33b25c751711e3118d14f86115dcefa7d9d 100644 (file)
@@ -55,7 +55,21 @@ static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char
   }
   else if (not strcmp("pid", key))
   {
-    memc_stat->pid= strtoul(value, (char **)NULL, 10);
+    int64_t temp= strtoll(value, (char **)NULL, 10);
+
+    if (temp <= INT32_MAX and ( sizeof(pid_t) == sizeof(int32_t) ))
+    {
+      memc_stat->pid= temp;
+    }
+    else if (temp > -1)
+    {
+      memc_stat->pid= temp;
+    }
+    else
+    {
+      // If we got a value less then -1 then something went wrong in the
+      // protocol
+    }
   }
   else if (not strcmp("uptime", key))
   {
@@ -188,7 +202,7 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_
 
   if (not memcmp("pid", key, sizeof("pid") -1))
   {
-    length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->pid);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%lld", (signed long long)memc_stat->pid);
   }
   else if (not memcmp("uptime", key, sizeof("uptime") -1))
   {
@@ -384,29 +398,28 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
                                             memcached_server_write_instance_st instance,
                                             struct local_context *check)
 {
-  memcached_return_t rc;
   char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
   int send_length;
 
   if (args)
-    send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-                                   "stats %s\r\n", args);
+  {
+    send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "stats %s\r\n", args);
+  }
   else
-    send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-                                   "stats\r\n");
+  {
+    send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "stats\r\n");
+  }
 
   if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
-    return MEMCACHED_WRITE_FAILURE;
-
-  rc= memcached_do(instance, buffer, (size_t)send_length, true);
-  if (rc != MEMCACHED_SUCCESS)
-    goto error;
-
-  while (1)
   {
-    rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
+    return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                               memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
+  }
 
-    if (rc == MEMCACHED_STAT)
+  memcached_return_t rc= memcached_do(instance, buffer, (size_t)send_length, true);
+  if (memcached_success(rc))
+  {
+    while ((rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL)) == MEMCACHED_STAT)
     {
       char *string_ptr, *end_ptr;
       char *key, *value;
@@ -439,13 +452,8 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
                     check->context);
       }
     }
-    else
-    {
-      break;
-    }
   }
 
-error:
   if (rc == MEMCACHED_END)
     return MEMCACHED_SUCCESS;
   else
@@ -454,21 +462,25 @@ error:
 
 memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_return_t *error)
 {
+  memcached_return_t unused;
+  if (error == NULL)
+  {
+    error= &unused;
+  }
+
   memcached_return_t rc;
-  if ((rc= initialize_query(self)) != MEMCACHED_SUCCESS)
+  if (memcached_failed(rc= initialize_query(self)))
   {
-    if (error)
-      *error= rc;
+    *error= rc;
 
     return NULL;
   }
 
   WATCHPOINT_ASSERT(error);
 
-  unlikely (self->flags.use_udp)
+  if (self->flags.use_udp)
   {
-    if (error)
-      *error= MEMCACHED_NOT_SUPPORTED;
+    *error= memcached_set_error(*self, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
 
     return NULL;
   }
@@ -477,8 +489,7 @@ memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_retu
 
   if (not stats)
   {
-    if (error)
-      *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+    *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
 
     return NULL;
   }
@@ -493,6 +504,7 @@ memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_retu
 
     stat_instance= stats +x;
 
+    stat_instance->pid= -1;
     stat_instance->root= self;
 
     instance= memcached_server_instance_fetch(self, x);
@@ -506,12 +518,13 @@ memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_retu
       temp_return= ascii_stats_fetch(stat_instance, args, instance, NULL);
     }
 
-    if (temp_return != MEMCACHED_SUCCESS)
+    if (memcached_failed(temp_return))
+    {
       rc= MEMCACHED_SOME_ERRORS;
+    }
   }
 
-  if (error)
-    *error= rc;
+  *error= rc;
 
   return stats;
 }
@@ -577,14 +590,11 @@ char ** memcached_stat_get_keys(memcached_st *ptr,
   return list;
 }
 
-void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat)
+void memcached_stat_free(const memcached_st *, memcached_stat_st *memc_stat)
 {
-  if (not ptr)
-    return;
-
+  WATCHPOINT_ASSERT(memc_stat); // Be polite, but when debugging catch this as an error
   if (not memc_stat)
   {
-    WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
     return;
   }
 
@@ -594,7 +604,7 @@ void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat)
     return;
   }
 
-  libmemcached_free(ptr, memc_stat);
+  libmemcached_free(NULL, memc_stat);
 }
 
 static memcached_return_t call_stat_fn(memcached_st *ptr,