-LRN: initialize rd to avoid having garbage in flags
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
index f839df3f5b0fbfb1f026124b5c83a339f505c20a..5cac2e1336ab27e92a3b8213c183c829c43c2366 100644 (file)
@@ -63,6 +63,11 @@ static int add;
  */
 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
 
+/**
+ * Queue entry for the 'add-uri' operation.
+ */
+static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
+
 /**
  * Desired action is to list records.
  */
@@ -118,6 +123,11 @@ static char *typestring;
  */
 static char *expirationstring;
 
+/**
+ * Global return value
+ */
+static int ret;
+
 
 /**
  * Task run on shutdown.  Cleans up everything.
@@ -129,6 +139,26 @@ static void
 do_shutdown (void *cls,
             const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  if (NULL != list_it)
+  {
+    GNUNET_NAMESTORE_zone_iteration_stop (list_it);
+    list_it = NULL;
+  }
+  if (NULL != add_qe)
+  {
+    GNUNET_NAMESTORE_cancel (add_qe);
+    add_qe = NULL;
+  }
+  if (NULL != add_qe_uri)
+  {
+    GNUNET_NAMESTORE_cancel (add_qe_uri);
+    add_qe_uri = NULL;
+  }
+  if (NULL != del_qe)
+  {
+    GNUNET_NAMESTORE_cancel (del_qe);
+    del_qe = NULL;
+  }
   if (NULL != ns)
   {
     GNUNET_NAMESTORE_disconnect (ns);
@@ -151,7 +181,7 @@ do_shutdown (void *cls,
  * Continuation called to notify client about result of the
  * operation.
  *
- * @param cls closure, unused
+ * @param cls closure, location of the QueueEntry pointer to NULL out
  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
  *                GNUNET_NO if content was already there
  *                GNUNET_YES (or other positive value) on success
@@ -162,12 +192,20 @@ add_continuation (void *cls,
                  int32_t success,
                  const char *emsg)
 {
-  add_qe = NULL;
-  if (success != GNUNET_YES)
+  struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
+
+  *qe = NULL;
+  if (GNUNET_YES != success)
+  {
     fprintf (stderr,
             _("Adding record failed: %s\n"),
-            (success == GNUNET_NO) ? "record exists" : emsg);
-  if ( (NULL == del_qe) &&
+            (GNUNET_NO == success) ? "record exists" : emsg);
+    if (GNUNET_NO != success)
+      ret = 1;
+  }
+  if ( (NULL == add_qe) &&
+       (NULL == add_qe_uri) &&
+       (NULL == del_qe) &&
        (NULL == list_it) )
     GNUNET_SCHEDULER_shutdown ();
 }
@@ -194,6 +232,7 @@ del_continuation (void *cls,
             _("Deleting record failed: %s\n"),
             emsg);
   if ( (NULL == add_qe) &&
+       (NULL == add_qe_uri) &&
        (NULL == list_it) )
     GNUNET_SCHEDULER_shutdown ();
 }
@@ -227,11 +266,15 @@ display_record (void *cls,
   const char *typestring;
   char *s;
   unsigned int i;
+  char *etime;
+  struct GNUNET_TIME_Absolute aex;
+  struct GNUNET_TIME_Relative rex;
 
   if (NULL == name)
   {
     list_it = NULL;
     if ( (NULL == del_qe) &&
+        (NULL == add_qe_uri) &&
         (NULL == add_qe) )
       GNUNET_SCHEDULER_shutdown ();
     return;
@@ -251,7 +294,22 @@ display_record (void *cls,
               (unsigned int) rd[i].record_type);
       continue;
     }
-    FPRINTF (stdout, "\t%s: %s\n", typestring, s);
+    if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
+    {
+      rex.rel_value = rd[i].expiration_time;
+      etime = GNUNET_STRINGS_relative_time_to_string (rex);
+    }
+    else
+    {
+      aex.abs_value = rd[i].expiration_time;
+      etime = GNUNET_STRINGS_absolute_time_to_string (aex);
+    }
+    FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s, 
+            (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) 
+            ? _(/* what follows is relative expiration */ "for at least")
+            : _(/* what follows is absolute expiration */ "until"),
+            etime);
+    GNUNET_free (etime);
     GNUNET_free (s);    
   }
   FPRINTF (stdout, "%s", "\n");
@@ -280,19 +338,21 @@ run (void *cls, char *const *args, const char *cfgfile,
   int etime_is_rel = GNUNET_SYSERR;
   struct GNUNET_NAMESTORE_RecordData rd;
 
+  if ( (NULL != args[0]) && (NULL == uri) )
+    uri = GNUNET_strdup (args[0]);
   if (NULL == keyfile)
   {
-      if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
-                                                 "ZONEKEY", &keyfile))
-      {
-        fprintf (stderr,
-                 _("Option `%s' not given, but I need a zone key file!\n"),
-                 "z");
-        return;
-      }
+    if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
+                                                             "ZONEKEY", &keyfile))
+    {
       fprintf (stderr,
-               _("Using default zone file `%s'\n"),
-               keyfile);
+              _("Option `%s' not given, but I need a zone key file!\n"),
+              "z");
+      return;
+    }
+    fprintf (stderr,
+            _("Using default zone file `%s'\n"),
+            keyfile);
   }
   zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
   GNUNET_free (keyfile);
@@ -333,6 +393,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   {
     fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
     GNUNET_SCHEDULER_shutdown ();
+    ret = 1;
     return;
   }
   if ((NULL == typestring) && (add | del))
@@ -341,6 +402,7 @@ run (void *cls, char *const *args, const char *cfgfile,
             _("Missing option `%s' for operation `%s'\n"),
             "-t", _("add/del"));
     GNUNET_SCHEDULER_shutdown ();
+    ret = 1;
     return;     
   }
   if (NULL != value)
@@ -355,6 +417,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                 value,
                 typestring);
        GNUNET_SCHEDULER_shutdown ();
+       ret = 1;
        return;
       }
   } else if (add | del)
@@ -362,6 +425,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     fprintf (stderr,
             _("Missing option `%s' for operation `%s'\n"),
             "-V", _("add/del"));
+    ret = 1;   
     GNUNET_SCHEDULER_shutdown ();
     return;     
   }
@@ -390,6 +454,7 @@ run (void *cls, char *const *args, const char *cfgfile,
               _("Invalid time format `%s'\n"),
               expirationstring);
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
       return;     
     }
   } 
@@ -399,8 +464,10 @@ run (void *cls, char *const *args, const char *cfgfile,
             _("Missing option `%s' for operation `%s'\n"),
             "-e", _("add"));
     GNUNET_SCHEDULER_shutdown ();
+    ret = 1;    
     return;     
   }
+  memset (&rd, 0, sizeof (rd));
   if (add)
   {
     if (NULL == name)
@@ -409,6 +476,7 @@ run (void *cls, char *const *args, const char *cfgfile,
               _("Missing option `%s' for operation `%s'\n"),
               "-n", _("add"));
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;    
       return;     
     }
     rd.data = data;
@@ -427,6 +495,7 @@ run (void *cls, char *const *args, const char *cfgfile,
               _("No valid expiration time for operation `%s'\n"),
               _("add"));
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
       return;
     }
     if (1 != nonauthority)
@@ -438,7 +507,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                                             name,
                                             &rd,
                                             &add_continuation,
-                                            NULL);
+                                            &add_qe);
   }
   if (del)
   {
@@ -448,6 +517,7 @@ run (void *cls, char *const *args, const char *cfgfile,
               _("Missing option `%s' for operation `%s'\n"),
               "-n", _("del"));
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
       return;     
     }
     rd.data = data;
@@ -474,7 +544,7 @@ run (void *cls, char *const *args, const char *cfgfile,
 
     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
                                                     &zone,
-                                                    0,
+                                                    GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
                                                     must_not_flags,
                                                     &display_record,
                                                     NULL);
@@ -493,13 +563,12 @@ run (void *cls, char *const *args, const char *cfgfile,
          GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
     {
       fprintf (stderr, 
-              _("Invalid URI `%s'"),
+              _("Invalid URI `%s'\n"),
               uri);
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
       return;
     }
-
-
     rd.data = ≻
     rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
     rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
@@ -515,12 +584,12 @@ run (void *cls, char *const *args, const char *cfgfile,
     if (1 != nonauthority)
       rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
 
-    add_qe = GNUNET_NAMESTORE_record_create (ns,
-                                            zone_pkey,
-                                            name,
-                                            &rd,
-                                            &add_continuation,
-                                            NULL);
+    add_qe_uri = GNUNET_NAMESTORE_record_create (ns,
+                                                zone_pkey,
+                                                name,
+                                                &rd,
+                                                &add_continuation,
+                                                &add_qe_uri);
   }
   GNUNET_free_non_null (data);
 }
@@ -576,18 +645,16 @@ main (int argc, char *const *argv)
     GNUNET_GETOPT_OPTION_END
   };
 
-  int ret;
-
   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
     return 2;
 
   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
-  ret =
-      (GNUNET_OK ==
-       GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
-                           _("GNUnet zone manipulation tool"), 
-                          options,
-                           &run, NULL)) ? 0 : 1;
+  if (GNUNET_OK !=
+      GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
+                         _("GNUnet zone manipulation tool"), 
+                         options,
+                         &run, NULL))
+    return 1;
 
   return ret;
 }