Debian Codegraph @

modules/btrfs/udiskslinuxmodulebtrfs.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2020 Tomas Bzatek <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "config.h"
22
23 #include <blockdev/blockdev.h>
24
25 #include <src/udisksdaemon.h>
26 #include <src/udiskslogging.h>
27 #include <src/udiskslinuxdevice.h>
28 #include <src/udisksmodulemanager.h>
29 #include <src/udisksmodule.h>
30 #include <src/udisksmoduleobject.h>
31 #include <src/udiskslinuxblockobject.h>
32
33 #include "udiskslinuxmodulebtrfs.h"
34 #include "udiskslinuxmanagerbtrfs.h"
35 #include "udiskslinuxfilesystembtrfs.h"
36
37 /**
38 * SECTION:udiskslinuxmodulebtrfs
39 * @title: UDisksLinuxModuleBTRFS
40 * @short_description: BTRFS module.
41 *
42 * The BTRFS module.
43 */
44
45 /**
46 * UDisksLinuxModuleBTRFS:
47 *
48 * The #UDisksLinuxModuleBTRFS structure contains only private data
49 * and should only be accessed using the provided API.
50 */
51 struct _UDisksLinuxModuleBTRFS {
52 UDisksModule parent_instance;
53
54 };
55
56 typedef struct _UDisksLinuxModuleBTRFSClass UDisksLinuxModuleBTRFSClass;
57
58 struct _UDisksLinuxModuleBTRFSClass {
59 UDisksModuleClass parent_class;
60 };
61
62 static void initable_iface_init (GInitableIface *initable_iface);
63
64 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxModuleBTRFS, udisks_linux_module_btrfs, UDISKS_TYPE_MODULE,
65 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init));
66
67
68 static void
69 udisks_linux_module_btrfs_init (UDisksLinuxModuleBTRFS *module)
70 {
71 }
72
73 static void
74 udisks_linux_module_btrfs_constructed (GObject *object)
75 {
76 if (G_OBJECT_CLASS (udisks_linux_module_btrfs_parent_class)->constructed)
77 G_OBJECT_CLASS (udisks_linux_module_btrfs_parent_class)->constructed (object);
78 }
79
80 static void
81 udisks_linux_module_btrfs_finalize (GObject *object)
82 {
83 if (G_OBJECT_CLASS (udisks_linux_module_btrfs_parent_class)->finalize)
84 G_OBJECT_CLASS (udisks_linux_module_btrfs_parent_class)->finalize (object);
85 }
86
87 gchar *
88 udisks_module_id (void)
89 {
90 return g_strdup (BTRFS_MODULE_NAME);
91 }
92
93 /**
94 * udisks_module_btrfs_new:
95 * @daemon: A #UDisksDaemon.
96 * @cancellable: (nullable): A #GCancellable or %NULL
97 * @error: Return location for error or %NULL.
98 *
99 * Creates new #UDisksLinuxModuleBTRFS object.
100 *
101 * Returns: (transfer full) (type UDisksLinuxModuleBTRFS): A
102 * #UDisksLinuxModuleBTRFS object or %NULL if @error is set. Free
103 * with g_object_unref().
104 */
105 UDisksModule *
106 udisks_module_btrfs_new (UDisksDaemon *daemon,
107 GCancellable *cancellable,
108 GError **error)
109 {
110 GInitable *initable;
111
112 g_return_val_if_fail (UDISKS_IS_DAEMON (daemon), NULL);
113 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
114
115 initable = g_initable_new (UDISKS_TYPE_LINUX_MODULE_BTRFS,
116 cancellable,
117 error,
118 "daemon", daemon,
119 "name", BTRFS_MODULE_NAME,
120 NULL);
121
122 if (initable == NULL)
123 return NULL;
124 else
125 return UDISKS_MODULE (initable);
126 }
127
128 /* ---------------------------------------------------------------------------------------------------- */
129
130 static gboolean
131 initable_init (GInitable *initable,
132 GCancellable *cancellable,
133 GError **error)
134 {
135 /* NULL means no specific so_name (implementation) */
136 BDPluginSpec btrfs_plugin = { BD_PLUGIN_BTRFS, NULL };
137 BDPluginSpec *plugins[] = { &btrfs_plugin, NULL };
138
139 if (! bd_is_plugin_available (BD_PLUGIN_BTRFS))
140 {
141 if (! bd_reinit (plugins, FALSE, NULL, error))
142 return FALSE;
143 }
144
145 return TRUE;
146 }
147
148 static void
149 initable_iface_init (GInitableIface *initable_iface)
150 {
151 initable_iface->init = initable_init;
152 }
153
154 /* ---------------------------------------------------------------------------------------------------- */
155
156 static GDBusInterfaceSkeleton *
157 udisks_linux_module_btrfs_new_manager (UDisksModule *module)
158 {
159 UDisksLinuxManagerBTRFS *manager;
160
161 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_BTRFS (module), NULL);
162
163 manager = udisks_linux_manager_btrfs_new (UDISKS_LINUX_MODULE_BTRFS (module));
164
165 return G_DBUS_INTERFACE_SKELETON (manager);
166 }
167
168 /* ---------------------------------------------------------------------------------------------------- */
169
170 static GType *
171 udisks_linux_module_btrfs_get_block_object_interface_types (UDisksModule *module)
172 {
173 static GType block_object_interface_types[2];
174
175 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_BTRFS (module), NULL);
176
177 if (g_once_init_enter (&block_object_interface_types[0]))
178 g_once_init_leave (&block_object_interface_types[0], UDISKS_TYPE_LINUX_FILESYSTEM_BTRFS);
179
180 return block_object_interface_types;
181 }
182
183 static GDBusInterfaceSkeleton *
184 udisks_linux_module_btrfs_new_block_object_interface (UDisksModule *module,
185 UDisksLinuxBlockObject *object,
186 GType interface_type)
187 {
188 GDBusInterfaceSkeleton *interface = NULL;
189 UDisksLinuxDevice *device;
190 const gchar *fs_type;
191
192 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_BTRFS (module), NULL);
193
194 if (interface_type == UDISKS_TYPE_LINUX_FILESYSTEM_BTRFS)
195 {
196 /* Check filesystem type from udev property. */
197 device = udisks_linux_block_object_get_device (UDISKS_LINUX_BLOCK_OBJECT (object));
198 fs_type = g_udev_device_get_property (device->udev_device, "ID_FS_TYPE");
199 if (g_strcmp0 (fs_type, "btrfs") == 0)
200 {
201 interface = G_DBUS_INTERFACE_SKELETON (udisks_linux_filesystem_btrfs_new (UDISKS_LINUX_MODULE_BTRFS (module), object));
202 }
203 g_object_unref (device);
204 }
205 else
206 {
207 udisks_error ("Invalid interface type");
208 }
209
210 return interface;
211 }
212
213 /* ---------------------------------------------------------------------------------------------------- */
214
215 static void
216 udisks_linux_module_btrfs_class_init (UDisksLinuxModuleBTRFSClass *klass)
217 {
218 GObjectClass *gobject_class;
219 UDisksModuleClass *module_class;
220
221 gobject_class = G_OBJECT_CLASS (klass);
222 gobject_class->constructed = udisks_linux_module_btrfs_constructed;
223 gobject_class->finalize = udisks_linux_module_btrfs_finalize;
224
225 module_class = UDISKS_MODULE_CLASS (klass);
226 module_class->new_manager = udisks_linux_module_btrfs_new_manager;
227 module_class->get_block_object_interface_types = udisks_linux_module_btrfs_get_block_object_interface_types;
228 module_class->new_block_object_interface = udisks_linux_module_btrfs_new_block_object_interface;
229 }