[gnome-builder] podman: read out storage location from configuration



commit 504964cfe0acf740dbbfeff2a37d60243f68d84c
Author: Günther Wagner <info gunibert de>
Date:   Wed Jan 26 22:19:10 2022 +0100

    podman: read out storage location from configuration
    
    This should determine the correct storage location if this got
    altered from the default locations.

 src/plugins/podman/gbp-podman-runtime.c         | 127 ++++++++++++++++++++----
 src/plugins/podman/meson.build                  |  17 ++++
 src/plugins/podman/test-storage-configuration.c |  60 +++++++++++
 src/plugins/podman/testdata/storage.conf        |   2 +
 4 files changed, 188 insertions(+), 18 deletions(-)
---
diff --git a/src/plugins/podman/gbp-podman-runtime.c b/src/plugins/podman/gbp-podman-runtime.c
index 9b4139095..72d373cce 100644
--- a/src/plugins/podman/gbp-podman-runtime.c
+++ b/src/plugins/podman/gbp-podman-runtime.c
@@ -89,6 +89,93 @@ gbp_podman_runtime_create_launcher (IdeRuntime  *runtime,
   return launcher;
 }
 
+typedef enum {
+  LOCAL_STORAGE_CONFIGURATION,
+  GLOBAL_STORAGE_CONFIGURATION
+} StorageType;
+
+char *
+_parse_toml_line (char *line)
+{
+  char *ret = NULL;
+  char **elements;
+
+  g_return_val_if_fail (line != NULL, NULL);
+
+  elements = g_strsplit (line, "=", -1);
+  if (g_strv_length (elements) != 2)
+    return NULL;
+  g_strstrip (g_strdelimit (elements[1], "\"", ' '));
+  ret = ide_path_expand (elements[1]);
+
+  g_strfreev (elements);
+  return ret;
+}
+
+char *
+parse_storage_configuration (const char  *storage_conf,
+                             StorageType  type)
+{
+  g_autoptr(GFile) storage_conf_file = NULL;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GFileInputStream) fis = NULL;
+  g_autoptr(GDataInputStream) dis = NULL;
+  char *line = NULL;
+
+  storage_conf_file = g_file_new_for_path (storage_conf);
+  fis = g_file_read (storage_conf_file, NULL, &error);
+  if (error)
+    return NULL;
+
+  dis = g_data_input_stream_new (G_INPUT_STREAM (fis));
+
+  while ((line = g_data_input_stream_read_line (dis, NULL, NULL, &error)) != NULL)
+    {
+      if (type == LOCAL_STORAGE_CONFIGURATION &&
+          g_str_has_prefix (line, "graphroot"))
+        {
+          return _parse_toml_line (line);
+        }
+      else if (type == GLOBAL_STORAGE_CONFIGURATION &&
+               g_str_has_prefix (line, "rootless_storage_path"))
+        {
+          return _parse_toml_line (line);
+        }
+    }
+
+  return NULL;
+}
+
+/* see man 5 containers-storage.json */
+static char *
+get_storage_directory (void)
+{
+  g_autofree char *user_local_storage_conf = NULL;
+  g_autofree char *global_storage_conf = NULL;
+
+  /* first search for user local storage configuration */
+  user_local_storage_conf = g_build_filename (g_get_home_dir (),
+                                              ".config",
+                                              "containers",
+                                              "storage.conf",
+                                              NULL);
+
+  if (g_file_test (user_local_storage_conf, G_FILE_TEST_EXISTS))
+    {
+      return parse_storage_configuration (user_local_storage_conf, LOCAL_STORAGE_CONFIGURATION);
+    }
+
+  /* second search for a global storage configuration */
+  global_storage_conf = g_build_filename ("etc", "containers", "storage.conf", NULL);
+
+  if (g_file_test (global_storage_conf, G_FILE_TEST_EXISTS))
+    {
+      return parse_storage_configuration (global_storage_conf, GLOBAL_STORAGE_CONFIGURATION);
+    }
+
+  return NULL;
+}
+
 static char *
 get_layer_dir (const char *layer)
 {
@@ -191,6 +278,7 @@ resolve_overlay (GbpPodmanRuntime *runtime)
   g_autofree gchar *container_json = NULL;
   g_autofree gchar *layer_json = NULL;
   g_autofree gchar *image_json = NULL;
+  g_autofree char *storage_directory = NULL;
   g_autoptr(JsonParser) parser;
   g_autoptr(JsonParser) image_parser;
   g_autoptr(GFile) overlay = NULL;
@@ -230,28 +318,28 @@ resolve_overlay (GbpPodmanRuntime *runtime)
       return;
     }
 
-  container_json = g_build_filename (g_get_home_dir (),
-                                     ".local",
-                                     "share",
-                                     "containers",
-                                     "storage",
+  /* find storage location first */
+  if ((storage_directory = get_storage_directory ()) == NULL)
+    {
+      /* assume default */
+      storage_directory = g_build_filename (g_get_home_dir (),
+                                            ".local",
+                                            "share",
+                                            "containers",
+                                            "storage",
+                                            NULL);
+    }
+
+  container_json = g_build_filename (storage_directory,
                                      "overlay-containers",
                                      "containers.json",
                                      NULL);
-  layer_json = g_build_filename (g_get_home_dir (),
-                                 ".local",
-                                 "share",
-                                 "containers",
-                                 "storage",
+  layer_json = g_build_filename (storage_directory,
                                  "overlay-layers",
                                  "layers.json",
                                  NULL);
 
-  image_json = g_build_filename (g_get_home_dir (),
-                                 ".local",
-                                 "share",
-                                 "containers",
-                                 "storage",
+  image_json = g_build_filename (storage_directory,
                                  "overlay-images",
                                  "images.json",
                                  NULL);
@@ -269,10 +357,11 @@ resolve_overlay (GbpPodmanRuntime *runtime)
           layer = get_layer_dir (layer_id);
           image_id = json_object_get_string_member (cont, "image");
         }
-
     }
 
-  json_parser_load_from_file (parser, layer_json, NULL);
+  json_parser_load_from_file (parser, layer_json, &error);
+  if (error)
+    return;
 
   if (layer != NULL)
     {
@@ -283,7 +372,9 @@ resolve_overlay (GbpPodmanRuntime *runtime)
     }
 
   /* apply image layer */
-  json_parser_load_from_file (image_parser, image_json, NULL);
+  json_parser_load_from_file (image_parser, image_json, &error);
+  if (error)
+    return;
 
   if ((layer = find_image_layer (image_parser, image_id)))
     {
diff --git a/src/plugins/podman/meson.build b/src/plugins/podman/meson.build
index 9fc50fdc4..0734ca34a 100644
--- a/src/plugins/podman/meson.build
+++ b/src/plugins/podman/meson.build
@@ -15,4 +15,21 @@ plugin_podman_resources = gnome.compile_resources(
 
 plugins_sources += plugin_podman_resources
 
+test_sources_podman = [
+  'test-storage-configuration.c',
+  'gbp-podman-runtime.c',
+  'gbp-podman-subprocess-launcher.c',
+]
+
+test_env_podman = [
+  'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
+  'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
+]
+
+test_podman = executable('test-podman', test_sources_podman,
+        c_args: test_cflags,
+  dependencies: [ libide_terminal_dep, libide_foundry_dep, libjson_glib_dep ],
+)
+test('test-podman', test_podman, env: test_env_podman)
+
 endif
diff --git a/src/plugins/podman/test-storage-configuration.c b/src/plugins/podman/test-storage-configuration.c
new file mode 100644
index 000000000..d6f97c7a7
--- /dev/null
+++ b/src/plugins/podman/test-storage-configuration.c
@@ -0,0 +1,60 @@
+/* test-storage-configuration.c
+ *
+ * Copyright 2022 Günther Wagner <info gunibert de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <glib.h>
+
+char *_parse_toml_line (char *line);
+char *parse_storage_configuration (const char *storage_conf, int type);
+
+static void
+test_toml_parsing (void)
+{
+  char *parsed = _parse_toml_line ((char *)"graphroot = \"/etc/containers/storage.conf\"");
+  g_assert_cmpstr (parsed, ==, "/etc/containers/storage.conf");
+  g_free (parsed);
+
+  parsed = _parse_toml_line ((char *)"graphroot=\"/etc/containers/storage.conf\"");
+  g_assert_cmpstr (parsed, ==, "/etc/containers/storage.conf");
+  g_free (parsed);
+}
+
+static void
+test_parse_storage_config (void)
+{
+  g_autofree char *testfile = g_test_build_filename (G_TEST_DIST, "testdata", "storage.conf", NULL);
+  char *path = parse_storage_configuration (testfile, 0);
+  g_assert_cmpstr (path, ==, "/var/lib/containers/storage/");
+  g_free (path);
+
+  path = parse_storage_configuration (testfile, 1);
+  g_assert_cmpstr (path, ==, "/home/user/.local/share/containers/");
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func("/podman/toml_parsing", test_toml_parsing);
+  g_test_add_func("/podman/parse_storage_configuration", test_parse_storage_config);
+
+  return g_test_run ();
+}
diff --git a/src/plugins/podman/testdata/storage.conf b/src/plugins/podman/testdata/storage.conf
new file mode 100644
index 000000000..00595fc2b
--- /dev/null
+++ b/src/plugins/podman/testdata/storage.conf
@@ -0,0 +1,2 @@
+graphroot = "/var/lib/containers/storage/"
+rootless_storage_path="/home/user/.local/share/containers/"


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