File: meta-get.c

package info (click to toggle)
gvfs 1.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 13,592 kB
  • sloc: ansic: 100,472; python: 1,462; xml: 986; sh: 158; makefile: 117
file content (198 lines) | stat: -rw-r--r-- 4,287 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;
}