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
|
/* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, 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>
#include "metatree.h"
#include "gvfsdaemonprotocol.h"
#include "metadata-dbus.h"
#ifdef HAVE_GUDEV
#include <gudev/gudev.h>
#endif
#if MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif MAJOR_IN_SYSMACROS
#include <sys/sysmacros.h>
#endif
#define WRITEOUT_TIMEOUT_SECS 60
#define WRITEOUT_TIMEOUT_SECS_NFS 15
typedef struct {
char *filename;
MetaTree *tree;
guint writeout_timeout;
} TreeInfo;
static GHashTable *tree_infos = NULL;
static GVfsMetadata *skeleton = NULL;
#ifdef HAVE_GUDEV
static GUdevClient *gudev_client = NULL;
#endif
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)
{
gboolean on_nfs;
if (info->writeout_timeout == 0)
{
on_nfs = meta_tree_is_on_nfs (info->tree);
info->writeout_timeout =
g_timeout_add_seconds (on_nfs ? WRITEOUT_TIMEOUT_SECS_NFS : WRITEOUT_TIMEOUT_SECS,
writeout_timeout, info);
}
}
static void
flush_single (const gchar *filename,
TreeInfo *info,
gpointer user_data)
{
if (info->writeout_timeout != 0)
{
g_source_remove (info->writeout_timeout);
writeout_timeout (info);
}
}
static void
flush_all ()
{
g_hash_table_foreach (tree_infos, (GHFunc) flush_single, NULL);
}
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 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 gboolean
handle_get_tree_from_device (GVfsMetadata *object,
GDBusMethodInvocation *invocation,
guint arg_major,
guint arg_minor)
{
char *res = NULL;
#ifdef HAVE_GUDEV
GUdevDeviceNumber devnum = makedev (arg_major, arg_minor);
GUdevDevice *device;
if (g_once_init_enter (&gudev_client))
g_once_init_leave (&gudev_client, g_udev_client_new (NULL));
device = g_udev_client_query_by_device_number (gudev_client, G_UDEV_DEVICE_TYPE_BLOCK, devnum);
if (device != NULL)
{
if (g_udev_device_has_property (device, "ID_FS_UUID_ENC"))
res = g_strconcat ("uuid-", g_udev_device_get_property (device, "ID_FS_UUID_ENC"), NULL);
else if (g_udev_device_has_property (device, "ID_FS_LABEL_ENC"))
res = g_strconcat ("label-", g_udev_device_get_property (device, "ID_FS_LABEL_ENC"), NULL);
g_clear_object (&device);
}
#endif
gvfs_metadata_complete_get_tree_from_device (object, invocation, res ? res : "");
g_free (res);
return TRUE;
}
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) */
flush_all ();
g_main_loop_quit (loop);
}
static void
on_connection_closed (GDBusConnection *connection,
gboolean remote_peer_vanished,
GError *error,
gpointer user_data)
{
GMainLoop *loop = user_data;
/* session bus died */
flush_all ();
g_main_loop_quit (loop);
}
int
main (int argc, char *argv[])
{
GMainLoop *loop;
GDBusConnection *conn;
gboolean replace;
gboolean show_version;
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 },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Show program version."), 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;
show_version = 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);
if (show_version)
{
g_print(PACKAGE_STRING "\n");
return 0;
}
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;
}
tree_infos = g_hash_table_new_full (g_str_hash,
g_str_equal,
NULL,
(GDestroyNotify)tree_info_free);
loop = g_main_loop_new (NULL, FALSE);
g_dbus_connection_set_exit_on_close (conn, FALSE);
g_signal_connect (conn, "closed", G_CALLBACK (on_connection_closed), loop);
flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
if (replace)
flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
skeleton = gvfs_metadata_skeleton_new ();
g_signal_connect (skeleton, "handle-set", G_CALLBACK (handle_set), skeleton);
g_signal_connect (skeleton, "handle-remove", G_CALLBACK (handle_remove), skeleton);
g_signal_connect (skeleton, "handle-move", G_CALLBACK (handle_move), skeleton);
g_signal_connect (skeleton, "handle-get-tree-from-device", G_CALLBACK (handle_get_tree_from_device), skeleton);
error = NULL;
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton), conn,
G_VFS_DBUS_METADATA_PATH, &error))
{
g_printerr ("Error exporting metadata daemon: %s (%s, %d)\n",
error->message, g_quark_to_string (error->domain), error->code);
g_error_free (error);
g_object_unref (conn);
g_main_loop_unref (loop);
return 1;
}
name_owner_id = g_bus_own_name_on_connection (conn,
G_VFS_DBUS_METADATA_NAME,
flags,
NULL,
on_name_lost,
loop,
NULL);
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);
#ifdef HAVE_GUDEV
g_clear_object (&gudev_client);
#endif
return 0;
}
|