summaryrefslogtreecommitdiff
path: root/daemon/mount.c
blob: eeab058309472577bfeae9461c47a2bda2c28aa6 (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
#include <config.h>

#include <string.h>

#include <glib.h>
#include <dbus/dbus.h>
#include <glib/gi18n.h>
#include "mount.h"
#include "gmountoperationdbus.h"

struct _Mountable {
  char *type;
  char *exec;
  gboolean automount;
}; 

static GList *mountables;

void
mount_init (void)
{
  GDir *dir;
  char *mount_dir, *path;
  const char *filename;
  GKeyFile *keyfile;
  char *type, *exec;
  Mountable *mountable;
  gboolean automount;
  
  mount_dir = MOUNTABLE_DIR;
  dir = g_dir_open (mount_dir, 0, NULL);

  if (dir)
    {
      while ((filename = g_dir_read_name (dir)) != NULL)
	{
	  path = g_build_filename (mount_dir, filename, NULL);
	  
	  keyfile = g_key_file_new ();
	  if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL))
	    {
	      type = g_key_file_get_string (keyfile, "Mount", "Type", NULL);
	      exec = g_key_file_get_string (keyfile, "Mount", "Exec", NULL);
	      automount = g_key_file_get_boolean (keyfile, "Mount", "AutoMount", NULL);
	      if (type != NULL && exec != NULL)
		{
		  mountable = g_new0 (Mountable, 1);
		  mountable->type = g_strdup (type);
		  mountable->exec = g_strdup (exec);
		  mountable->automount = automount;
		  
		  mountables = g_list_prepend (mountables, mountable);
		}
	      g_free (type);
	      g_free (exec);
	    }
	  g_key_file_free (keyfile);
	  g_free (path);
	}
    }
}

gboolean
mountable_is_automount (Mountable *mountable)
{
  return mountable->automount;
}

static Mountable *
find_mountable (const char *type)
{
  GList *l;

  for (l = mountables; l != NULL; l = l->next)
    {
      Mountable *mountable = l->data;

      if (strcmp (mountable->type, type) == 0)
	return mountable;
    }
  
  return NULL;
}

Mountable *
lookup_mountable (GMountSpec *spec)
{
  const char *type;
  
  type = g_mount_spec_get_type (spec);
  if (type == NULL)
    return NULL;

  return find_mountable (type);
}

GMountOperation *
mountable_mount (Mountable *mountable,
		 GMountSpec *spec,
		 GError **error)
{
  DBusConnection *conn;
  const char *id;
  char *exec;
  gboolean res;
  GMountOperationDBus *op;

  conn = dbus_bus_get (DBUS_BUS_SESSION, NULL);
  id = dbus_bus_get_unique_name (conn);
  dbus_connection_unref (conn);

  op = g_mount_operation_dbus_new (spec);
  
  exec = g_strconcat (mountable->exec, " ", id, " ", op->obj_path, NULL);
  
  res = g_spawn_command_line_async (exec, error);
  g_free (exec);
  
  if (!res)
    {
      g_object_unref (op);
      return NULL;
    }
  
  return G_MOUNT_OPERATION (op);
}