[gvfs] Add various metadata test programs



commit 3dfd6eaaaaba5e914c6f2269dce98854fa84deb5
Author: Alexander Larsson <alexl redhat com>
Date:   Mon Jun 22 20:40:19 2009 +0200

    Add various metadata test programs
    
    These are useful to interactively test the metadata stores but
    are not installed.

 metadata/Makefile.am     |   16 ++++
 metadata/meta-get-tree.c |   59 +++++++++++++
 metadata/meta-get.c      |  198 ++++++++++++++++++++++++++++++++++++++++++
 metadata/meta-ls.c       |   81 +++++++++++++++++
 metadata/meta-set.c      |  214 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 568 insertions(+), 0 deletions(-)
---
diff --git a/metadata/Makefile.am b/metadata/Makefile.am
index 3116f0c..9f1b58d 100644
--- a/metadata/Makefile.am
+++ b/metadata/Makefile.am
@@ -3,6 +3,10 @@ NULL =
 noinst_LTLIBRARIES=libmetadata.la
 
 APPS = 	\
+	meta-ls		\
+	meta-get	\
+	meta-set	\
+	meta-get-tree	\
 	$(NULL)
 
 if HAVE_LIBXML
@@ -30,6 +34,18 @@ libmetadata_la_SOURCES = 		\
 
 libmetadata_la_LIBADD = $(GLIB_LIBS) $(UDEV_LIBS)
 
+meta_ls_LDADD = libmetadata.la
+meta_ls_SOURCES = meta-ls.c
+
+meta_set_LDADD = libmetadata.la $(DBUS_LIBS) ../common/libgvfscommon.la
+meta_set_SOURCES = meta-set.c
+
+meta_get_LDADD = libmetadata.la
+meta_get_SOURCES = meta-get.c
+
+meta_get_tree_LDADD = libmetadata.la
+meta_get_tree_SOURCES = meta-get-tree.c
+
 convert_nautilus_metadata_LDADD = libmetadata.la $(LIBXML_LIBS)
 convert_nautilus_metadata_SOURCES = metadata-nautilus.c
 
diff --git a/metadata/meta-get-tree.c b/metadata/meta-get-tree.c
new file mode 100644
index 0000000..bfe911b
--- /dev/null
+++ b/metadata/meta-get-tree.c
@@ -0,0 +1,59 @@
+#include "config.h"
+#include "metatree.h"
+#include <glib/gstdio.h>
+
+/*static gboolean recursive = FALSE;*/
+static gboolean verbose = FALSE;
+static gboolean pause = FALSE;
+static GOptionEntry entries[] =
+{
+  { "verbose", 'l', 0, G_OPTION_ARG_NONE, &verbose, "Verbose", NULL },
+  { "pause", 'p', 0, G_OPTION_ARG_NONE, &pause, "Pause", NULL },
+  { NULL }
+};
+
+int
+main (int argc,
+      char *argv[])
+{
+  GError *error = NULL;
+  GOptionContext *context;
+  MetaLookupCache *cache;
+  MetaTree *tree;
+  char *tree_path;
+  struct stat statbuf;
+  int i;
+
+  context = g_option_context_new ("<tree file> <dir in tree> - list entries");
+  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_printerr ("option parsing failed: %s\n", error->message);
+      return 1;
+    }
+
+
+  cache = meta_lookup_cache_new ();
+
+  for (i = 1; i < argc; i++)
+    {
+      if (g_lstat (argv[i], &statbuf) != 0)
+	{
+	  g_printerr ("can't stat %s\n", argv[i]);
+	  return 1;
+	}
+
+      tree_path = NULL;
+      tree = meta_lookup_cache_lookup_path (cache, argv[i], statbuf.st_dev,
+					    FALSE, &tree_path);
+      g_print ("tree: %s (exists: %d), tree path: %s\n", meta_tree_get_filename (tree), meta_tree_exists (tree), tree_path);
+      if (pause)
+	{
+	  char buffer[1000];
+	  g_print ("Pausing, press enter\n");
+	  gets(buffer);
+	}
+    }
+
+  return 0;
+}
diff --git a/metadata/meta-get.c b/metadata/meta-get.c
new file mode 100644
index 0000000..a1d676e
--- /dev/null
+++ b/metadata/meta-get.c
@@ -0,0 +1,198 @@
+#include "config.h"
+#include "metatree.h"
+#include <glib/gstdio.h>
+
+static char *treename = NULL;
+static char *treefilename = NULL;
+static gboolean recursive = FALSE;
+static GOptionEntry entries[] =
+{
+  { "tree", 't', 0, G_OPTION_ARG_STRING, &treename, "Tree", NULL},
+  { "file", 'f', 0, G_OPTION_ARG_STRING, &treefilename, "Tree file", NULL},
+  { "recursive", 'r', 0, G_OPTION_ARG_NONE, &recursive, "Recursive", NULL},
+  { NULL }
+};
+
+static gboolean
+print_key (const char *key,
+	   MetaKeyType type,
+	   gpointer value,
+	   gpointer user_data)
+{
+  int indent = GPOINTER_TO_INT (user_data);
+  char **values;
+  int i;
+
+  g_assert (type != META_KEY_TYPE_NONE);
+
+  if (type == META_KEY_TYPE_STRING)
+    g_print ("%*s%s=%s\n", indent, "",key, (char *)value);
+  else
+    {
+      values = value;
+      g_print ("%*s%s=[", indent, "",key);
+      for (i = 0; values[i] != NULL; i++)
+	{
+	  if (values[i+1] != NULL)
+	    g_print ("%s,", values[i]);
+	  else
+	    g_print ("%s", values[i]);
+	}
+      g_print ("]\n");
+    }
+  return TRUE;
+}
+
+static gboolean
+prepend_name (const char *entry,
+	      guint64 last_changed,
+	      gboolean has_children,
+	      gboolean has_data,
+	      gpointer user_data)
+{
+  GList **children = user_data;
+
+  *children = g_list_prepend (*children,
+			      g_strdup (entry));
+  return TRUE;
+}
+
+static void
+enum_keys (MetaTree *tree, char *path,
+	   gboolean recurse, int indent)
+{
+  GList *children, *l;
+  char *child_name, *child_path;
+
+  g_print ("%*s%s\n", indent, "", path);
+  meta_tree_enumerate_keys (tree, path,
+			    print_key, GINT_TO_POINTER (indent+1));
+
+  if (recurse)
+    {
+      children = NULL;
+      meta_tree_enumerate_dir (tree, path,
+			       prepend_name,
+			       &children);
+      for (l = children; l != NULL; l = l->next)
+	{
+	  child_name = l->data;
+	  child_path = g_build_filename (path, l->data, NULL);
+	  g_free (child_name);
+
+	  enum_keys (tree, child_path, recurse, indent + 3);
+
+	  g_free (child_path);
+	}
+      g_list_free (children);
+    }
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+  MetaTree *tree;
+  GError *error = NULL;
+  GOptionContext *context;
+  MetaKeyType type;
+  const char *path, *key;
+  MetaLookupCache *lookup;
+  struct stat statbuf;
+  char *tree_path;
+  char **strings;
+  int i, j;
+
+  context = g_option_context_new ("<path> [keys..]- read metadata");
+  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_printerr ("option parsing failed: %s\n", error->message);
+      return 1;
+    }
+
+  if (argc < 2)
+    {
+      g_printerr ("no path specified\n");
+      return 1;
+    }
+  path = argv[1];
+
+  if (treefilename)
+    {
+      tree = meta_tree_open (treefilename, FALSE);
+      if (tree)
+	tree_path = g_strdup (path);
+
+      if (tree == NULL)
+	{
+	  g_printerr ("can't open metadata file %s\n", treefilename);
+	  return 1;
+	}
+    }
+  else if (treename)
+    {
+      tree = meta_tree_lookup_by_name (treename, FALSE);
+      if (tree)
+	tree_path = g_strdup (path);
+
+      if (tree == NULL)
+	{
+	  g_printerr ("can't open metadata tree %s\n", path);
+	  return 1;
+	}
+    }
+  else
+    {
+      lookup = meta_lookup_cache_new ();
+      if (g_lstat (path, &statbuf) != 0)
+	{
+	  g_printerr ("can't find file %s\n", path);
+	  return 1;
+	}
+      tree = meta_lookup_cache_lookup_path (lookup,
+					    path,
+					    statbuf.st_dev,
+					    FALSE,
+					    &tree_path);
+      meta_lookup_cache_free (lookup);
+
+      if (tree == NULL)
+	{
+	  g_printerr ("can't open metadata tree for file %s\n", path);
+	  return 1;
+	}
+    }
+
+  if (argc > 2)
+    {
+      for (i = 2; i < argc; i++)
+	{
+	  key = argv[i];
+	  type = meta_tree_lookup_key_type  (tree, tree_path, key);
+	  if (type == META_KEY_TYPE_NONE)
+	    g_print ("%s Not set\n", key);
+	  else if (type == META_KEY_TYPE_STRING)
+	    g_print ("%s=%s\n", key, meta_tree_lookup_string (tree, path, key));
+	  else
+	    {
+	      strings = meta_tree_lookup_stringv (tree, path, key);
+	      g_print ("%s=[", key);
+	      for (j = 0; strings[j] != NULL; j++)
+		{
+		  if (strings[j+1] == NULL)
+		    g_print ("%s", strings[j]);
+		  else
+		    g_print ("%s,", strings[j]);
+		}
+	      g_print ("]\n");
+	    }
+	}
+    }
+  else
+    {
+      enum_keys (tree, tree_path, recursive, 0);
+    }
+
+  return 0;
+}
diff --git a/metadata/meta-ls.c b/metadata/meta-ls.c
new file mode 100644
index 0000000..ec7e4bf
--- /dev/null
+++ b/metadata/meta-ls.c
@@ -0,0 +1,81 @@
+#include "config.h"
+#include "metatree.h"
+
+/*static gboolean recursive = FALSE;*/
+static gboolean verbose = FALSE;
+static GOptionEntry entries[] =
+{
+  { "verbose", 'l', 0, G_OPTION_ARG_NONE, &verbose, "Verbose", NULL },
+  /*  { "recursive", 'r', 0, G_OPTION_ARG_NONE, &recursive, "Recursively list", NULL }, */
+  { NULL }
+};
+
+static gboolean
+print_dir (const char *name,
+	   guint64 last_changed,
+	   gboolean has_children,
+	   gboolean has_data,
+	   gpointer user_data)
+{
+  if (verbose)
+    g_print ("%-16s %s%s  %"G_GUINT64_FORMAT"\n",
+	     name,
+	     has_children?"c":" ",
+	     has_data?"d":" ",
+	     last_changed);
+  else
+    g_print ("%s\n", name);
+  return TRUE;
+}
+
+static void
+dir (MetaTree *tree,
+     const char *path)
+{
+  meta_tree_enumerate_dir (tree, path,
+			   print_dir, NULL);
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+  MetaTree *tree;
+  GError *error = NULL;
+  GOptionContext *context;
+  int i;
+
+  context = g_option_context_new ("<tree file> <dir in tree> - list entries");
+  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_printerr ("option parsing failed: %s\n", error->message);
+      return 1;
+    }
+
+  if (argc < 3)
+    {
+      if (argc < 2)
+	g_printerr ("No metadata tree specified\n");
+      else
+	g_printerr ("no dir specified\n");
+      return 1;
+    }
+
+  tree = meta_tree_open (argv[1], TRUE);
+  if (tree == NULL)
+    {
+      g_printerr ("can't open metadata tree %s\n", argv[1]);
+      return 1;
+    }
+
+  for (i = 2; i < argc; i++)
+    {
+      if (argc > 3)
+	g_print ("%s:\n", argv[i]);
+
+      dir (tree, argv[i]);
+    }
+
+  return 0;
+}
diff --git a/metadata/meta-set.c b/metadata/meta-set.c
new file mode 100644
index 0000000..e3d115d
--- /dev/null
+++ b/metadata/meta-set.c
@@ -0,0 +1,214 @@
+#include "config.h"
+#include "metatree.h"
+#include <glib/gstdio.h>
+#include <dbus/dbus.h>
+#include "gvfsdaemonprotocol.h"
+#include "gdbusutils.h"
+
+static gboolean unset = FALSE;
+static gboolean list = FALSE;
+static gboolean use_dbus = FALSE;
+static char *treename = NULL;
+static GOptionEntry entries[] =
+{
+  { "dbus", 'd', 0, G_OPTION_ARG_NONE, &use_dbus, "Use dbus", NULL},
+  { "tree", 't', 0, G_OPTION_ARG_STRING, &treename, "Tree", NULL},
+  { "unset", 'u', 0, G_OPTION_ARG_NONE, &unset, "Unset", NULL },
+  { "list", 'l', 0, G_OPTION_ARG_NONE, &list, "Set as list", NULL },
+  { NULL }
+};
+
+int
+main (int argc,
+      char *argv[])
+{
+  MetaTree *tree;
+  GError *error = NULL;
+  GOptionContext *context;
+  MetaLookupCache *lookup;
+  struct stat statbuf;
+  const char *path, *key;
+  DBusConnection *connection;
+  DBusMessage *message, *reply;
+  const char *metatreefile;
+  DBusError derror;
+  char *tree_path;
+
+  context = g_option_context_new ("<path> <key> <value> - set metadata");
+  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_printerr ("option parsing failed: %s\n", error->message);
+      return 1;
+    }
+
+  if (argc < 2)
+    {
+      g_printerr ("no path specified\n");
+      return 1;
+    }
+  path = argv[1];
+
+  if (argc < 3)
+    {
+      g_printerr ("no key specified\n");
+      return 1;
+    }
+  key = argv[2];
+
+  if (!list && !unset && argc != 4)
+    {
+      g_print ("No value specified\n");
+      return 1;
+    }
+
+  if (treename)
+    {
+      tree = meta_tree_lookup_by_name (treename, TRUE);
+      if (tree)
+	tree_path = g_strdup (path);
+
+      if (tree == NULL)
+	{
+	  g_printerr ("can't open metadata tree %s\n", path);
+	  return 1;
+	}
+    }
+  else
+    {
+      lookup = meta_lookup_cache_new ();
+      if (g_lstat (path, &statbuf) != 0)
+	{
+	  g_printerr ("can't find file %s\n", path);
+	  return 1;
+	}
+      tree = meta_lookup_cache_lookup_path (lookup,
+					    path,
+					    statbuf.st_dev,
+					    TRUE,
+					    &tree_path);
+      meta_lookup_cache_free (lookup);
+
+      if (tree == NULL)
+	{
+	  g_printerr ("can't open metadata tree for file %s\n", path);
+	  return 1;
+	}
+    }
+
+  connection = NULL;
+  if (use_dbus)
+    {
+      dbus_error_init (&derror);
+      connection = dbus_bus_get (DBUS_BUS_SESSION, &derror);
+      if (connection == NULL)
+	{
+	  g_printerr ("Unable to connect to dbus: %s\n", derror.message);
+	  dbus_error_free (&derror);
+	  return 1;
+	}
+    }
+
+  if (unset)
+    {
+      if (use_dbus)
+	{
+	  message =
+	    dbus_message_new_method_call (G_VFS_DBUS_METADATA_NAME,
+					  G_VFS_DBUS_METADATA_PATH,
+					  G_VFS_DBUS_METADATA_INTERFACE,
+					  G_VFS_DBUS_METADATA_OP_UNSET);
+	  metatreefile = meta_tree_get_filename (tree);
+	  _g_dbus_message_append_args (message,
+				       G_DBUS_TYPE_CSTRING, &metatreefile,
+				       G_DBUS_TYPE_CSTRING, &tree_path,
+				       DBUS_TYPE_STRING, &key,
+				       0);
+	  reply = dbus_connection_send_with_reply_and_block (connection, message, 1000*30,
+							     &derror);
+	  if (reply == NULL)
+	    {
+	      g_printerr ("Unset error: %s\n", derror.message);
+	      return 1;
+	    }
+	}
+      else
+	{
+	  if (!meta_tree_unset (tree, tree_path, key))
+	    {
+	      g_printerr ("Unable to unset key\n");
+	      return 1;
+	    }
+	}
+    }
+  else if (list)
+    {
+      if (use_dbus)
+	{
+	  char **strv;
+	  message =
+	    dbus_message_new_method_call (G_VFS_DBUS_METADATA_NAME,
+					  G_VFS_DBUS_METADATA_PATH,
+					  G_VFS_DBUS_METADATA_INTERFACE,
+					  G_VFS_DBUS_METADATA_OP_SET);
+	  metatreefile = meta_tree_get_filename (tree);
+	  strv = &argv[3];
+	  _g_dbus_message_append_args (message,
+				       G_DBUS_TYPE_CSTRING, &metatreefile,
+				       G_DBUS_TYPE_CSTRING, &tree_path,
+				       DBUS_TYPE_STRING, &key,
+				       DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &strv, argc - 3,
+				       0);
+	  reply = dbus_connection_send_with_reply_and_block (connection, message, 1000*30,
+							     &derror);
+	  if (reply == NULL)
+	    {
+	      g_printerr ("SetStringv error: %s\n", derror.message);
+	      return 1;
+	    }
+	}
+      else
+	{
+	  if (!meta_tree_set_stringv (tree, tree_path, key, &argv[3]))
+	    {
+	      g_printerr ("Unable to set key\n");
+	      return 1;
+	    }
+	}
+    }
+  else
+    {
+      if (use_dbus)
+	{
+	  message =
+	    dbus_message_new_method_call (G_VFS_DBUS_METADATA_NAME,
+					  G_VFS_DBUS_METADATA_PATH,
+					  G_VFS_DBUS_METADATA_INTERFACE,
+					  G_VFS_DBUS_METADATA_OP_SET);
+	  metatreefile = meta_tree_get_filename (tree);
+	  _g_dbus_message_append_args (message,
+				       G_DBUS_TYPE_CSTRING, &metatreefile,
+				       G_DBUS_TYPE_CSTRING, &tree_path,
+				       DBUS_TYPE_STRING, &key,
+				       DBUS_TYPE_STRING, &argv[3],
+				       0);
+	  reply = dbus_connection_send_with_reply_and_block (connection, message, 1000*30,
+							     &derror);
+	  if (reply == NULL)
+	    {
+	      g_printerr ("SetString error: %s\n", derror.message);
+	      return 1;
+	    }
+	}
+      else
+	{
+	  if (!meta_tree_set_string (tree, tree_path, key, argv[3]))
+	    {
+	      g_printerr ("Unable to set key\n");
+	      return 1;
+	    }
+	}
+    }
+
+  return 0;
+}



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