- Tests did not clean up: TEST_HOME with namestore db was not removed after test
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
index 3fa20a5e6ecc75a924241ac9d83ee23c808e4e42..5de86182f69365074ba845bdebe60dabcd4fff79 100644 (file)
@@ -31,6 +31,7 @@
 #include <gnunet_dnsparser_lib.h>
 #include <gnunet_identity_service.h>
 #include <gnunet_gnsrecord_lib.h>
+#include <gnunet_gns_service.h>
 #include <gnunet_namestore_service.h>
 
 
@@ -59,11 +60,6 @@ static char *ego_name;
  */
 static int add;
 
-/**
- * Iterator for the 'add' operation.
- */
-static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
-
 /**
  * Queue entry for the 'add-uri' operation.
  */
@@ -139,6 +135,11 @@ static char *typestring;
  */
 static char *expirationstring;
 
+/**
+ * Desired nick name.
+ */
+static char *nickstring;
+
 /**
  * Global return value
  */
@@ -305,10 +306,20 @@ del_continuation (void *cls,
                  const char *emsg)
 {
   del_qe = NULL;
-  if (success != GNUNET_YES)
+  if (GNUNET_NO == success)
+  {
+    fprintf (stderr,
+            _("Deleting record failed, record does not exist%s%s\n"),
+            (NULL != emsg) ? ": " : "",
+            (NULL != emsg) ? emsg : "");
+  }
+  if (GNUNET_SYSERR == success)
+  {
     fprintf (stderr,
-            _("Deleting record failed: %s\n"),
-            emsg);
+             _("Deleting record failed%s%s\n"),
+             (NULL != emsg) ? ": " : "",
+             (NULL != emsg) ? emsg : "");
+  }
   test_finished ();
 }
 
@@ -374,10 +385,13 @@ display_record (void *cls,
       ets = GNUNET_STRINGS_absolute_time_to_string (at);
     }
     FPRINTF (stdout,
-            "\t%s: %s (%s)\n",
+            "\t%s: %s (%s)\t%s\t%s\t%s\n",
             typestring,
             s,
-             ets);
+             ets,
+             (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
+             (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "",
+             (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) ? "PENDING" : "");
     GNUNET_free (s);
   }
   FPRINTF (stdout, "%s", "\n");
@@ -417,12 +431,17 @@ get_existing_record (void *cls,
   struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
   struct GNUNET_GNSRECORD_Data *rde;
 
+  add_qe = NULL;
   if ( (NULL != zone_key) &&
        (0 != strcmp (rec_name, name)) )
   {
-    GNUNET_NAMESTORE_zone_iterator_next (add_zit);
+    GNUNET_break (0);
     return;
   }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u records for name `%s'\n",
+      rd_count, rec_name);
+
   memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
   memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
   /* FIXME: should add some logic to overwrite records if there
@@ -432,7 +451,7 @@ get_existing_record (void *cls,
   rde->data = data;
   rde->data_size = data_size;
   rde->record_type = type;
-  if (1 != shadow)
+  if (1 == shadow)
     rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
   if (1 != public)
     rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
@@ -453,11 +472,6 @@ get_existing_record (void *cls,
                                           rde,
                                           &add_continuation,
                                           &add_qe);
-  /* only cancel if we were not told that this
-     was the end of the iteration already */
-  if (NULL != rec_name)
-    GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
-  add_zit = NULL;
 }
 
 
@@ -513,7 +527,7 @@ testservice_task (void *cls,
             "namestore");
     return;
   }
-  if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
+  if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
   {
     /* nothing more to be done */
     fprintf (stderr,
@@ -616,10 +630,8 @@ testservice_task (void *cls,
       ret = 1;
       return;
     }
-    add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
-                                                    &zone_pkey,
-                                                    &get_existing_record,
-                                                    NULL);
+    add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
+        &get_existing_record, NULL );
   }
   if (del)
   {
@@ -699,6 +711,7 @@ testservice_task (void *cls,
       rd.expiration_time = etime_abs.abs_value_us;
     else
       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
+
     if (1 != shadow)
       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
@@ -709,10 +722,25 @@ testservice_task (void *cls,
                                                 &add_continuation,
                                                 &add_qe_uri);
   }
+  if (NULL != nickstring)
+  {
+    if (0 == strlen(nickstring))
+    {
+      fprintf (stderr,
+               _("Invalid nick `%s'\n"),
+               nickstring);
+      GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
+      return;
+    }
+    add_qe_uri = GNUNET_NAMESTORE_set_nick(ns, &zone_pkey, nickstring,
+        &add_continuation, &add_qe_uri);
+  }
   if (monitor)
   {
     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
                                              &zone_pkey,
+                                              GNUNET_YES,
                                              &display_record,
                                              &sync_cb,
                                              NULL);
@@ -770,6 +798,7 @@ run (void *cls, char *const *args, const char *cfgfile,
             _("You must specify which zone should be accessed\n"));
     return;
   }
+
   if ( (NULL != args[0]) && (NULL == uri) )
     uri = GNUNET_strdup (args[0]);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
@@ -792,6 +821,7 @@ int
 main (int argc, char *const *argv)
 {
   public = -1;
+  shadow = -1;
 
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'a', "add", NULL,
@@ -806,6 +836,9 @@ main (int argc, char *const *argv)
     {'e', "expiration", "TIME",
      gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
      &GNUNET_GETOPT_set_string, &expirationstring},
+    {'i', "nick", "NICKNAME",
+     gettext_noop ("set the desired nick name for the zone"), 1,
+     &GNUNET_GETOPT_set_string, &nickstring},
     {'m', "monitor", NULL,
      gettext_noop ("monitor changes in the namestore"), 0,
      &GNUNET_GETOPT_set_one, &monitor},