summaryrefslogtreecommitdiff
path: root/metadata/meta-daemon.c
blob: 9c102a561b1d1265344e12ac7070cd5e11d074b8 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

#include <config.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <locale.h>
#include <stdlib.h>
/*  TODO: remove + remove traces in Makefile.am  */
#include <dbus/dbus.h>
#include "metatree.h"
#include "gvfsdaemonprotocol.h"
#include "metadata-dbus.h"

#define WRITEOUT_TIMEOUT_SECS 60

typedef struct {
  char *filename;
  MetaTree *tree;
  guint writeout_timeout;
} TreeInfo;

static GHashTable *tree_infos = NULL;
static GVfsMetadata *skeleton = NULL;

static void
tree_info_free (TreeInfo *info)
{
  g_free (info->filename);
  meta_tree_unref (info->tree);
  if (info->writeout_timeout)
    g_source_remove (info->writeout_timeout);

  g_free (info);
}

static gboolean
writeout_timeout (gpointer data)
{
  TreeInfo *info = data;

  meta_tree_flush (info->tree);
  info->writeout_timeout = 0;

  return FALSE;
}

static void
tree_info_schedule_writeout (TreeInfo *info)
{
  if (info->writeout_timeout == 0)
    info->writeout_timeout =
      g_timeout_add_seconds (WRITEOUT_TIMEOUT_SECS,
			     writeout_timeout, info);
}

static TreeInfo *
tree_info_new (const char *filename)
{
  TreeInfo *info;
  MetaTree *tree;

  tree = meta_tree_open (filename, TRUE);
  if (tree == NULL)
    return NULL;

  info = g_new0 (TreeInfo, 1);
  info->filename = g_strdup (filename);
  info->tree = tree;
  info->writeout_timeout = 0;

  return info;
}

static TreeInfo *
tree_info_lookup (const char *filename)
{
  TreeInfo *info;

  info = g_hash_table_lookup (tree_infos, filename);
  if (info)
    return info;

  info = tree_info_new (filename);
  if (info)
    g_hash_table_insert (tree_infos,
			 info->filename,
			 info);
  return info;
}

static gboolean
handle_set (GVfsMetadata *object,
            GDBusMethodInvocation *invocation,
            const gchar *arg_treefile,
            const gchar *arg_path,
            GVariant *arg_data,
            GVfsMetadata *daemon)
{
  TreeInfo *info;
  const gchar *str;
  const gchar **strv;
  const gchar *key;
  GError *error;
  GVariantIter iter;
  GVariant *value;

  info = tree_info_lookup (arg_treefile);
  if (info == NULL)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_NOT_FOUND,
                                             _("Can't find metadata file %s"),
                                             arg_treefile);
      return TRUE;
    }

  error = NULL;

  g_variant_iter_init (&iter, arg_data);
  while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
    {
      if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING_ARRAY))
	{
	  /* stringv */
          strv = g_variant_get_strv (value, NULL);
	  if (!meta_tree_set_stringv (info->tree, arg_path, key, (gchar **) strv))
	    {
	      g_set_error_literal (&error, G_IO_ERROR,
                                   G_IO_ERROR_FAILED,
                                  _("Unable to set metadata key"));
	    }
	  g_free (strv);
	}
      else if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
	{
	  /* string */
          str = g_variant_get_string (value, NULL);
	  if (!meta_tree_set_string (info->tree, arg_path, key, str))
	    {
              g_set_error_literal (&error, G_IO_ERROR,
                                   G_IO_ERROR_FAILED,
                                   _("Unable to set metadata key"));
	    }
	}
      else if (g_variant_is_of_type (value, G_VARIANT_TYPE_BYTE))
	{
	  /* Unset */
	  if (!meta_tree_unset (info->tree, arg_path, key))
	    {
              g_set_error_literal (&error, G_IO_ERROR,
                                   G_IO_ERROR_FAILED,
                                   _("Unable to unset metadata key"));
	    }
	}
      g_variant_unref (value);
    }

  tree_info_schedule_writeout (info);

  if (error)
    {
      g_dbus_method_invocation_return_gerror (invocation, error);
      g_error_free (error);
    }
  else
    {
      gvfs_metadata_complete_set (object, invocation);
    }
  
  return TRUE;
}

static void
append_key (GVariantBuilder *builder,
	    MetaTree *tree,
	    const char *path,
	    const char *key)
{
  MetaKeyType keytype;
  char *str;
  char **strv;

  keytype = meta_tree_lookup_key_type (tree, path, key);

  if (keytype == META_KEY_TYPE_STRING)
    {
      str = meta_tree_lookup_string (tree, path, key);
      g_variant_builder_add (builder, "{sv}", key, g_variant_new_string (str));
      g_free (str);
    }
  else if (keytype == META_KEY_TYPE_STRINGV)
    {
      strv = meta_tree_lookup_stringv (tree, path, key);
      g_variant_builder_add (builder, "{sv}", key, g_variant_new_strv ((const gchar * const  *) strv, -1));
      g_strfreev (strv);
    }
}

static gboolean
enum_keys (const char *key,
	   MetaKeyType type,
	   gpointer value,
	   gpointer user_data)
{
  GPtrArray *keys = user_data;

  g_ptr_array_add (keys, g_strdup (key));
  return TRUE;
}

static gboolean
handle_get (GVfsMetadata *object,
            GDBusMethodInvocation *invocation,
            const gchar *arg_treefile,
            const gchar *arg_path,
            const gchar *const *arg_keys,
            GVfsMetadata *daemon)
{
  TreeInfo *info;
  GPtrArray *meta_keys;
  gboolean free_keys;
  gchar **iter_keys;
  gchar **i;
  GVariantBuilder *builder;

  info = tree_info_lookup (arg_treefile);
  if (info == NULL)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_NOT_FOUND,
                                             _("Can't find metadata file %s"),
                                             arg_treefile);
      return TRUE;
    }

  if (arg_keys == NULL)
    {
      /* Get all keys */
      free_keys = TRUE;
      meta_keys = g_ptr_array_new ();
      meta_tree_enumerate_keys (info->tree, arg_path, enum_keys, meta_keys);
      iter_keys = (gchar **) g_ptr_array_free (meta_keys, FALSE);
    }
  else
    {
      free_keys = FALSE;
      iter_keys = (gchar **) arg_keys;
    }

  builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);

  for (i = iter_keys; *i; i++)
    append_key (builder, info->tree, arg_path, *i);
  if (free_keys)
    g_strfreev (iter_keys);
  
  gvfs_metadata_complete_get (object, invocation,
                              g_variant_builder_end (builder));
  g_variant_builder_unref (builder);

  return TRUE;
}

static gboolean
handle_unset (GVfsMetadata *object,
              GDBusMethodInvocation *invocation,
              const gchar *arg_treefile,
              const gchar *arg_path,
              const gchar *arg_key,
              GVfsMetadata *daemon)
{
  TreeInfo *info;

  info = tree_info_lookup (arg_treefile);
  if (info == NULL)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_NOT_FOUND,
                                             _("Can't find metadata file %s"),
                                             arg_treefile);
      return TRUE;
    }

  if (!meta_tree_unset (info->tree, arg_path, arg_key))
    {
      g_dbus_method_invocation_return_error_literal (invocation,
                                                     G_IO_ERROR,
                                                     G_IO_ERROR_FAILED,
                                                     _("Unable to unset metadata key"));
      return TRUE;
    }

  tree_info_schedule_writeout (info);
  gvfs_metadata_complete_unset (object, invocation);

  return TRUE;
}

static gboolean
handle_remove (GVfsMetadata *object,
               GDBusMethodInvocation *invocation,
               const gchar *arg_treefile,
               const gchar *arg_path,
               GVfsMetadata *daemon)
{
  TreeInfo *info;

  info = tree_info_lookup (arg_treefile);
  if (info == NULL)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_NOT_FOUND,
                                             _("Can't find metadata file %s"),
                                             arg_treefile);
      return TRUE;
    }

  if (!meta_tree_remove (info->tree, arg_path))
    {
      g_dbus_method_invocation_return_error_literal (invocation,
                                                     G_IO_ERROR,
                                                     G_IO_ERROR_FAILED,
                                                     _("Unable to remove metadata keys"));
      return TRUE;
    }

  tree_info_schedule_writeout (info);
  gvfs_metadata_complete_remove (object, invocation);
  
  return TRUE;
}

static gboolean
handle_move (GVfsMetadata *object,
             GDBusMethodInvocation *invocation,
             const gchar *arg_treefile,
             const gchar *arg_path,
             const gchar *arg_dest_path,
             GVfsMetadata *daemon)
{
  TreeInfo *info;

  info = tree_info_lookup (arg_treefile);
  if (info == NULL)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_NOT_FOUND,
                                             _("Can't find metadata file %s"),
                                             arg_treefile);
      return TRUE;
    }

  /* Overwrites any dest */
  if (!meta_tree_copy (info->tree, arg_path, arg_dest_path))
    {
      g_dbus_method_invocation_return_error_literal (invocation,
                                                     G_IO_ERROR,
                                                     G_IO_ERROR_FAILED,
                                                     _("Unable to move metadata keys"));
      return TRUE;
    }

  /* Remove source if copy succeeded (ignoring errors) */
  meta_tree_remove (info->tree, arg_path);

  tree_info_schedule_writeout (info);
  gvfs_metadata_complete_move (object, invocation);
  
  return TRUE;
}

static void
on_name_acquired (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
  GError *error;
  
  skeleton = gvfs_metadata_skeleton_new ();
  
  g_signal_connect (skeleton, "handle-set", G_CALLBACK (handle_set), skeleton);
  g_signal_connect (skeleton, "handle-unset", G_CALLBACK (handle_unset), skeleton);
  g_signal_connect (skeleton, "handle-get", G_CALLBACK (handle_get), skeleton);
  g_signal_connect (skeleton, "handle-remove", G_CALLBACK (handle_remove), skeleton);
  g_signal_connect (skeleton, "handle-move", G_CALLBACK (handle_move), skeleton);

  error = NULL;
  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton), connection,
                                         G_VFS_DBUS_METADATA_PATH, &error))
    {
      g_printerr ("Error exporting volume monitor: %s (%s, %d)\n",
                  error->message, g_quark_to_string (error->domain), error->code);
      g_error_free (error);
    }
}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
  GMainLoop *loop = user_data;

  /* means that someone has claimed our name (we allow replacement) */
  g_main_loop_quit (loop);
}


int
main (int argc, char *argv[])
{
  GMainLoop *loop;
  GDBusConnection *conn;
  gboolean replace;
  GError *error;
  guint name_owner_id;
  GBusNameOwnerFlags flags;
  GOptionContext *context;
  const GOptionEntry options[] = {
    { "replace", 'r', 0, G_OPTION_ARG_NONE, &replace,  N_("Replace old daemon."), NULL },
    { NULL }
  };

  setlocale (LC_ALL, "");

  bindtextdomain (GETTEXT_PACKAGE, GVFS_LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);

  g_set_application_name (_("GVFS Metadata Daemon"));
  context = g_option_context_new ("");

  g_option_context_set_summary (context, _("Metadata daemon for GVFS"));

  g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);

  replace = FALSE;
  name_owner_id = 0;

  error = NULL;
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      /* Translators: the first %s is the application name, */
      /* the second %s is the error message                 */
      g_printerr (_("%s: %s"), g_get_application_name(), error->message);
      g_printerr ("\n");
      g_printerr (_("Try \"%s --help\" for more information."),
		  g_get_prgname ());
      g_printerr ("\n");
      g_error_free (error);
      g_option_context_free (context);
      return 1;
    }

  g_option_context_free (context);

  g_type_init ();

  loop = g_main_loop_new (NULL, FALSE);

  error = NULL;
  conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
  if (!conn)
    {
      g_printerr ("Failed to connect to the D-BUS daemon: %s (%s, %d)\n",
                  error->message, g_quark_to_string (error->domain), error->code);
      g_error_free (error);
      return 1;
    }
  
  flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
  if (replace)
    flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;

  name_owner_id = g_bus_own_name_on_connection (conn,
                                                G_VFS_DBUS_METADATA_NAME,
                                                flags,
                                                on_name_acquired,
                                                on_name_lost,
                                                loop,
                                                NULL);
  
  tree_infos = g_hash_table_new_full (g_str_hash,
				      g_str_equal,
				      NULL,
				      (GDestroyNotify)tree_info_free);

  g_main_loop_run (loop);
  
  if (skeleton)
    g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (skeleton));
  if (name_owner_id != 0)
    g_bus_unown_name (name_owner_id);
  if (conn)
    g_object_unref (conn);
  if (loop != NULL)
    g_main_loop_unref (loop);

  return 0;
}