File: metadata-nautilus.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 (220 lines) | stat: -rw-r--r-- 4,603 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <string.h>
#include "metabuilder.h"
#include <libxml/tree.h>

static xmlNodePtr
xml_get_children (xmlNodePtr parent)
{
	if (parent == NULL) {
		return NULL;
	}
	return parent->children;
}

static xmlNodePtr
xml_get_root_children (xmlDocPtr document)
{
	return xml_get_children (xmlDocGetRootElement (document));
}


static char *
get_uri_from_nautilus_metafile_name (const char *filename)
{
  GString *s;
  char c;
  char *base_name, *p;
  int len;

  base_name = g_path_get_basename (filename);
  len = strlen (base_name);
  if (len <=  4 ||
      strcmp (base_name + len - 4, ".xml") != 0)
    {
      g_free (base_name);
      return NULL;
    }
  base_name[len-4] = 0;

  s = g_string_new (NULL);

  p = base_name;
  while (*p)
    {
      c = *p++;
      if (c == '%')
	{
	  c =
	    g_ascii_xdigit_value (p[0]) << 4 |
	    g_ascii_xdigit_value (p[1]);
	  p += 2;
	}
      g_string_append_c (s, c);
    }
  g_free (base_name);

  return g_string_free (s, FALSE);
}

static void
parse_xml_node (MetaFile *metafile,
		xmlNodePtr filenode)
{
  xmlChar *data;
  guint64 timestamp;
  xmlNodePtr node;
  xmlAttrPtr attr;
  xmlChar *property;
  char *combined_key;

  data = xmlGetProp (filenode, (xmlChar *)"timestamp");
  if (data)
    {
      timestamp = g_ascii_strtoll ((char *)data, NULL, 10);
      if (timestamp != 0)
	metafile->last_changed = timestamp;
    }

  for (attr = filenode->properties; attr != NULL; attr = attr->next)
    {
      if (strcmp ((char *)attr->name, "name") == 0 ||
	  strcmp ((char *)attr->name, "timestamp") == 0)
	continue;

      property = xmlGetProp (filenode, attr->name);
      if (property)
	metafile_key_set_value (metafile, (char *)attr->name, (char *)property);
      xmlFree (property);
    }

  for (node = filenode->children; node != NULL; node = node->next)
    {
      for (attr = node->properties; attr != NULL; attr = attr->next)
	{
	  property = xmlGetProp (node, attr->name);
	  if (property)
	    {
	      combined_key = g_strconcat ((char *)node->name,
					  "-",
					  (char *)attr->name,
					  NULL);
	      metafile_key_list_add (metafile, combined_key, (char *)property);
	      g_free (combined_key);
	    }
	  xmlFree (property);
	}
    }
}

static void
parse_xml_file (MetaBuilder *builder,
		xmlDocPtr xml,
		char *dir)
{
  xmlNodePtr node;
  xmlChar *name;
  char *unescaped_name;
  MetaFile *dir_metafile, *metafile;

  dir_metafile =  meta_builder_lookup (builder, dir, TRUE);

  for (node = xml_get_root_children (xml);
       node != NULL; node = node->next)
    {
      if (strcmp ((char *)node->name, "file") == 0)
	{
	  name = xmlGetProp (node, (xmlChar *)"name");
	  unescaped_name = g_uri_unescape_string ((char *)name, "/");
	  xmlFree (name);

	  if (strcmp (unescaped_name, ".") == 0)
	    metafile = dir_metafile;
	  else
	    metafile = metafile_lookup_child (dir_metafile, unescaped_name, TRUE);

	  parse_xml_node (metafile, node);
	  g_free (unescaped_name);
	}
    }
}

static void
parse_nautilus_file (MetaBuilder *builder,
		     char *file)
{
  char *uri;
  char *dir;
  gchar *contents;
  gsize length;
  xmlDocPtr xml;

  if (!g_file_get_contents (file, &contents, &length, NULL))
    {
      g_print ("failed to load %s\n", file);
      return;
    }

  uri = get_uri_from_nautilus_metafile_name (file);
  if (uri == NULL)
    {
      g_free (contents);
      return;
    }

  dir = g_filename_from_uri (uri, NULL, NULL);
  g_free (uri);
  if (dir == NULL)
    {
      g_free (contents);
      return;
    }

  xml = xmlParseMemory (contents, length);
  g_free (contents);
  if (xml == NULL)
    return;

  parse_xml_file (builder, xml, dir);
  xmlFreeDoc (xml);
}

/*static gboolean recursive = FALSE;*/
static char *filename = NULL;
static GOptionEntry entries[] =
{
  { "out", 'o', 0, G_OPTION_ARG_FILENAME, &filename, "Output filename", NULL },
  { NULL }
};

int
main (int argc, char *argv[])
{
  GOptionContext *context;
  MetaBuilder *builder;
  GError *error = NULL;
  int i;

  context = g_option_context_new ("<nautilus metadata files> - convert nautilus metadata");
  g_option_context_add_main_entries (context, entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_printerr ("option parsing failed: %s\n", error->message);
      return 1;
    }

  if (argc < 2)
    {
      g_print ("No files specified\n");
      return 1;
    }

  builder = meta_builder_new ();
  for (i = 1; i < argc; i++)
    parse_nautilus_file (builder, argv[i]);
  if (filename)
    meta_builder_write (builder, filename);
  else
    meta_builder_print (builder);

  return 0;
}