gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] branch master updated: fix redux cli logic


From: gnunet
Subject: [taler-anastasis] branch master updated: fix redux cli logic
Date: Tue, 20 Oct 2020 16:00:34 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new 8251c41  fix redux cli logic
8251c41 is described below

commit 8251c41665651e0ef2aafd6a3d52f52b98ae217c
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Tue Oct 20 16:00:32 2020 +0200

    fix redux cli logic
---
 src/cli/anastasis-cli-redux.c | 382 +++++++++++++++++-------------------------
 1 file changed, 150 insertions(+), 232 deletions(-)

diff --git a/src/cli/anastasis-cli-redux.c b/src/cli/anastasis-cli-redux.c
index 5fc37d3..2abb657 100644
--- a/src/cli/anastasis-cli-redux.c
+++ b/src/cli/anastasis-cli-redux.c
@@ -49,24 +49,19 @@ static int b_flag;
 static int r_flag;
 
 /**
- * -a option given.
+ * Input to -a option given.
  */
-static int a_flag;
+static char *input;
 
 /**
- * JSON containing previous state
+ * Output filename, if given.
  */
-static json_t *prev_state;
+static char *output_filename;
 
 /**
- * JSON containing new state
- */
-static json_t *new_state;
-
-/**
- * path to new state
+ * JSON containing previous state
  */
-static char *path_new_state;
+static json_t *prev_state;
 
 /**
  * JSON containing arguments for action
@@ -84,15 +79,46 @@ static char *action;
 static struct ANASTASIS_ReduxAction *ra;
 
 /**
- * Persist a json state.
+ * Return value from main.
+ */
+static int global_ret;
+
+
+/**
+ * Persist a json state, report errors.
  *
  * @param state to persist
- * @return 0 if operation failed
+ * @param filename where to write the state to, NULL for stdout
  */
-static int
-persist_new_state (json_t *state)
+static void
+persist_new_state (json_t *state,
+                   const char *filename)
 {
-  return json_dump_file (state, path_new_state, JSON_COMPACT);
+  if (NULL != filename)
+  {
+    if (0 !=
+        json_dump_file (state,
+                        filename,
+                        JSON_COMPACT))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Could not dump state to `%s'\n",
+                  filename);
+      return;
+    }
+    return;
+  }
+  if (-1 !=
+      fprintf (stdout,
+               "%s",
+               json_dumps (init_state,
+                           JSON_COMPACT)))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not dump state to stdout\n");
+    return 1;
+  }
+  return 0;
 }
 
 
@@ -109,25 +135,15 @@ action_cb (void *cls,
            enum ANASTASIS_ErrorCode error_code,
            json_t *result_state)
 {
-  new_state = result_state;
-
-  if (NULL != new_state)
-  {
-    if (! isatty (STDOUT_FILENO))
-    {
-      fprintf (stdout,
-               "%s",
-               json_dumps (new_state,
-                           JSON_COMPACT));
-    }
-    else
-    {
-      if (0 != persist_new_state (new_state))
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "Failed to persist new state! \n");
-    }
-  }
+  if (NULL != result_state)
+    persist_new_state (result_state,
+                       output_filename);
+  if (TALER_EC_NONE != error_code)
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Redux failed with error %d\n",
+                error_code);
   GNUNET_SCHEDULER_shutdown ();
+  global_ret = (TALER_EC_NONE != error_code) ? 1 : 0;
 }
 
 
@@ -143,7 +159,11 @@ shutdown_task (void *cls)
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Shutdown initiated\n");
-  // FIXME shutdown routine here
+  if (NULL != ra)
+  {
+    ANASTASIS_redux_action_cancel (ra);
+    ra = NULL;
+  }
   if (NULL != ctx)
   {
     GNUNET_CURL_fini (ctx);
@@ -154,12 +174,16 @@ shutdown_task (void *cls)
     GNUNET_CURL_gnunet_rc_destroy (rc);
     rc = NULL;
   }
-  if (NULL != ra)
+  if (NULL != arguments)
   {
-    GNUNET_free (ra);
-    ra = NULL;
+    json_decref (arguments);
+    arguments = NULL;
+  }
+  if (NULL != prev_state)
+  {
+    json_decref (prev_state);
+    prev_state = NULL;
   }
-
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Shutdown complete\n");
 }
@@ -180,222 +204,114 @@ run (void *cls,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   (void) cls;
-  (void) args;
-  (void) cfg;
-  // FIXME: error handling json error
   json_error_t error;
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Starting anastasis-reducer\n");
-
   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                  NULL);
+  if (b_flag && r_flag)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                "We cannot start backup and recovery at the same time!\n");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (r_flag)
+  {
+    json_t *init_state;
 
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_log_setup ("anastasis-reducer",
-                                   "WARNING",
-                                   NULL));
-
-
-  if (r_flag ^ b_flag)
+    init_state = ANASTASIS_recovery_start (cfg);
+    if (NULL == init_state)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  "Failed to create an initial recovery state!\n");
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    persist_new_state (init_state,
+                       args[0]);
+    json_decref (init_state);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (b_flag)
   {
     json_t *init_state;
-    if (b_flag)
+
+    init_state = ANASTASIS_backup_start (cfg);
+    if (NULL == init_state)
     {
-      init_state = ANASTASIS_backup_start (cfg);
-      if (NULL == init_state)
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "At %s:%u failed to get an initial backup state!\n",
-                    __FILE__,
-                    __LINE__);
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  "Failed to create an initial backup state!\n");
+      GNUNET_SCHEDULER_shutdown ();
+      return;
     }
-    else
+    persist_new_state (init_state,
+                       args[0]);
+    json_decref (init_state);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+
+  /* action processing */
+  {
+    const char *action = args[0];
+
+    if (NULL == action)
     {
-      init_state = ANASTASIS_recovery_start (cfg);
-      if (NULL == init_state)
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "At %s:%u failed to get an initial recovery state!\n",
-                    __FILE__,
-                    __LINE__);
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  "You must specify an action as the first argument (or `-b' 
or `-r')\n");
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  "Example: anastasis-reducer back\n");
+      GNUNET_SCHEDULER_shutdown ();
+      return;
     }
-    if (isatty (STDOUT_FILENO))
+    args++;
+    if (NULL != input)
     {
-      if (args[0])
+      arguments = json_loads (input,
+                              JSON_DECODE_ANY,
+                              &error);
+      if (NULL == arguments)
       {
-        path_new_state = (char *) args[0];
-        if (0 != persist_new_state (init_state))
-        {
-          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                      "At %s:%u failed to dump json into file!\n",
-                      __FILE__,
-                      __LINE__);
-          GNUNET_SCHEDULER_shutdown ();
-          return;
-        }
-        return; // success!
-      }
-      else
-      {
-        fprintf (stderr,
-                 "Please give a path to store initial json state!\n");
-        fprintf (stderr,
-                 "Example: anastasis-reducer -b init_state.json\n");
+        GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                    "Failed to parse arguments on line %u:%u: %s!\n",
+                    error.line,
+                    error.column,
+                    error.text);
         GNUNET_SCHEDULER_shutdown ();
         return;
       }
     }
-    else
+    if (NULL != args[0])
     {
-      fprintf (stdout,
-               "%s",
-               json_dumps (init_state,
-                           JSON_COMPACT));
-      return; // success!
+      prev_state = json_load_file (args[0],
+                                   JSON_DECODE_ANY,
+                                   &error);
+      args++;
     }
-  }
-  else
-  {
-    // check if we have redirection to this reducer
-    if (! isatty (STDIN_FILENO))
+    else
     {
-      if (a_flag)
-      {
-        if (NULL == args[0])
-        {
-          fprintf (stderr,
-                   "Please give arguments!\n");
-          fprintf (stderr,
-                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
< prev_state.json > new_state.json\n");
-          GNUNET_SCHEDULER_shutdown ();
-          return;
-        }
-        arguments = json_loads (args[0],
-                                JSON_DECODE_ANY,
-                                &error);
-        if (NULL == arguments)
-        {
-          fprintf (stderr,
-                   "Failed to parse arguments!\n");
-          fprintf (stderr,
-                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
-          GNUNET_SCHEDULER_shutdown ();
-          return;
-        }
-        if (NULL == args[1])
-        {
-          fprintf (stderr,
-                   "Failed to parse arguments!\n");
-          fprintf (stderr,
-                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
-          GNUNET_SCHEDULER_shutdown ();
-          return;
-        }
-        action = (char *) args[1];
-        if (NULL == action)
-        {
-          fprintf (stderr,
-                   "Failed to parse arguments!\n");
-          fprintf (stderr,
-                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
-          GNUNET_SCHEDULER_shutdown ();
-          return;
-        }
-        char *prev_state_str = NULL;
-        size_t size;
-        if (getline (&prev_state_str,
-                     &size,
-                     stdin)
-            == EOF)
-        {
-          fprintf (stderr,
-                   "No redirection given!\n");
-        }
-        prev_state = json_loads (prev_state_str,
-                                 JSON_DECODE_ANY,
-                                 &error);
-        if (NULL == prev_state)
-        {
-          fprintf (stderr,
-                   "Failed to load previous state!\n");
-          fprintf (stderr,
-                   "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
-          GNUNET_SCHEDULER_shutdown ();
-          return;
-        }
-      }
-      else
-      {
-        fprintf (stderr,
-                 "Please give arguments!\n");
-        fprintf (stderr,
-                 "Example: anastasis-reducer -a '{\"continent\":\"Europe\"}' 
select-continent < prev_state.json > new_state.json\n");
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
+      prev_state = json_loadf (stdin,
+                               JSON_DECODE_ANY,
+                               &error);
     }
-    else
+    if (NULL == prev_state)
     {
-      if (args[0])
-        prev_state = json_load_file ((char *) args[0], JSON_DECODE_ANY, 
&error);
-      else
-      {
-        fprintf (stderr,
-                 "Please give path for json containing previous state!\n");
-        fprintf (stderr,
-                 "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
-
-      if (args[1])
-        action = (char *) args[1];
-      else
-      {
-        fprintf (stderr,
-                 "Please give an action!\n");
-        fprintf (stderr,
-                 "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
-
-      if (args[2])
-        arguments = json_load_file ((char *) args[2], JSON_DECODE_ANY, &error);
-      else
-      {
-        fprintf (stderr,
-                 "Please give path for json containing arguments for 
action!\n");
-        fprintf (stderr,
-                 "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
-
-      if (args[3])
-        path_new_state = (char *) args[3];
-      else
-      {
-        fprintf (stderr,
-                 "Please give path for json containing new state!\n");
-        fprintf (stderr,
-                 "Example: anastasis-reducer prev.json action arguments.json 
new.json\n");
-        GNUNET_SCHEDULER_shutdown ();
-        return;
-      }
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  "Failed to parse initial state on line %u:%u: %s!\n",
+                  error.line,
+                  error.column,
+                  error.text);
+      GNUNET_SCHEDULER_shutdown ();
+      return;
     }
-    /* initialize HTTP client FIXME: Do we need http client?*/
+    output_filename = args[0];
+    /* initialize HTTP client event loop */
     ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
                             &rc);
     rc = GNUNET_CURL_gnunet_rc_create (ctx);
-
     ra = ANASTASIS_redux_action (prev_state,
                                  action,
                                  arguments,
@@ -405,6 +321,7 @@ run (void *cls,
   }
 }
 
+
 int
 main (int argc,
       char *const *argv)
@@ -421,10 +338,11 @@ main (int argc,
                                "restore",
                                "use reducer to handle states for restore 
process",
                                &r_flag),
-    GNUNET_GETOPT_option_flag ('a',
-                               "arguments",
-                               "pass a json string containing arguments to 
reducer",
-                               &a_flag),
+    GNUNET_GETOPT_option_string ('a',
+                                 "arguments",
+                                 "JSON",
+                                 "pass a JSON string containing arguments to 
reducer",
+                                 &input),
 
     GNUNET_GETOPT_OPTION_END
   };
@@ -442,7 +360,7 @@ main (int argc,
                             &run,
                             NULL);
 
-  return (GNUNET_OK == ret) ? 0 : 1;
+  return (GNUNET_OK == ret) ? global_ret : 1;
 }
 
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]