forked from storaged-project/udisks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudiskslinuxpartitiontable.c
More file actions
741 lines (657 loc) · 27.3 KB
/
udiskslinuxpartitiontable.c
File metadata and controls
741 lines (657 loc) · 27.3 KB
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/file.h>
#include <glib/gstdio.h>
#include <blockdev/part.h>
#include <blockdev/fs.h>
#include "udiskslogging.h"
#include "udiskslinuxpartitiontable.h"
#include "udiskslinuxblockobject.h"
#include "udisksdaemon.h"
#include "udisksdaemonutil.h"
#include "udiskslinuxdevice.h"
#include "udiskslinuxblock.h"
#include "udiskslinuxpartition.h"
#include "udiskssimplejob.h"
/**
* SECTION:udiskslinuxpartitiontable
* @title: UDisksLinuxPartitionTable
* @short_description: Linux implementation of #UDisksPartitionTable
*
* This type provides an implementation of the #UDisksPartitionTable
* interface on Linux.
*/
typedef struct _UDisksLinuxPartitionTableClass UDisksLinuxPartitionTableClass;
/**
* UDisksLinuxPartitionTable:
*
* The #UDisksLinuxPartitionTable structure contains only private data and should
* only be accessed using the provided API.
*/
struct _UDisksLinuxPartitionTable
{
UDisksPartitionTableSkeleton parent_instance;
};
struct _UDisksLinuxPartitionTableClass
{
UDisksPartitionTableSkeletonClass parent_class;
};
static void partition_table_iface_init (UDisksPartitionTableIface *iface);
G_DEFINE_TYPE_WITH_CODE (UDisksLinuxPartitionTable, udisks_linux_partition_table, UDISKS_TYPE_PARTITION_TABLE_SKELETON,
G_IMPLEMENT_INTERFACE (UDISKS_TYPE_PARTITION_TABLE, partition_table_iface_init));
/* ---------------------------------------------------------------------------------------------------- */
static void
udisks_linux_partition_table_init (UDisksLinuxPartitionTable *partition_table)
{
g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (partition_table),
G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
}
static void
udisks_linux_partition_table_class_init (UDisksLinuxPartitionTableClass *klass)
{
}
/**
* udisks_linux_partition_table_new:
*
* Creates a new #UDisksLinuxPartitionTable instance.
*
* Returns: A new #UDisksLinuxPartitionTable. Free with g_object_unref().
*/
UDisksPartitionTable *
udisks_linux_partition_table_new (void)
{
return UDISKS_PARTITION_TABLE (g_object_new (UDISKS_TYPE_LINUX_PARTITION_TABLE,
NULL));
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* udisks_linux_partition_table_update:
* @table: A #UDisksLinuxPartitionTable.
* @object: The enclosing #UDisksLinuxBlockObject instance.
*
* Updates the interface.
*/
void
udisks_linux_partition_table_update (UDisksLinuxPartitionTable *table,
UDisksLinuxBlockObject *object)
{
UDisksLinuxDevice *device = NULL;
UDisksDaemon *daemon = NULL;
guint num_parts = 0;
const gchar **partition_object_paths = NULL;
const gchar *part_type = NULL;
GList *partition_objects = NULL;
GList *object_p = NULL;
guint i = 0;
const gchar *device_file;
BDPartDiskSpec *spec;
GError *error = NULL;
/* update list of partitions */
daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
partition_objects = udisks_linux_partition_table_get_partitions (daemon, UDISKS_PARTITION_TABLE (table), &num_parts);
partition_object_paths = g_new0 (const gchar *, num_parts + 1);
for (i = 0, object_p = partition_objects; object_p != NULL; object_p = object_p->next, i++)
{
partition_object_paths[i] = g_dbus_object_get_object_path (g_dbus_interface_get_object (G_DBUS_INTERFACE (object_p->data)));
}
udisks_partition_table_set_partitions (UDISKS_PARTITION_TABLE (table),
partition_object_paths);
/* update partition table type */
device = udisks_linux_block_object_get_device (object);
if (device)
{
part_type = g_udev_device_get_property (device->udev_device, "ID_PART_TABLE_TYPE");
if (!part_type && num_parts > 0)
{
/* probe the device in case no cached udev property is available */
device_file = g_udev_device_get_device_file (device->udev_device);
if (device_file)
{
spec = bd_part_get_disk_spec (device_file, &error);
if (spec)
{
part_type = bd_part_get_part_table_type_str (spec->table_type, NULL);
bd_part_disk_spec_free (spec);
}
else
{
udisks_warning ("Partitions found on device '%s' but couldn't read partition table signature: %s",
device_file, error->message);
g_clear_error (&error);
}
}
}
}
udisks_partition_table_set_type_ (UDISKS_PARTITION_TABLE (table), part_type);
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (table));
g_free (partition_object_paths);
g_clear_object (&device);
g_list_free_full (partition_objects, g_object_unref);
}
/* ---------------------------------------------------------------------------------------------------- */
GList *
udisks_linux_partition_table_get_partitions (UDisksDaemon *daemon,
UDisksPartitionTable *table,
guint *num_partitions)
{
GList *ret = NULL;
GDBusObject *table_object;
const gchar *table_object_path;
GList *l, *object_proxies = NULL;
*num_partitions = 0;
table_object = g_dbus_interface_get_object (G_DBUS_INTERFACE (table));
if (table_object == NULL)
goto out;
table_object_path = g_dbus_object_get_object_path (table_object);
object_proxies = udisks_daemon_get_objects (daemon);
for (l = object_proxies; l != NULL; l = l->next)
{
UDisksObject *object = UDISKS_OBJECT (l->data);
UDisksPartition *partition;
partition = udisks_object_get_partition (object);
if (partition == NULL)
continue;
if (g_strcmp0 (udisks_partition_get_table (partition), table_object_path) == 0)
{
ret = g_list_prepend (ret, g_object_ref (partition));
(*num_partitions)++;
}
g_object_unref (partition);
}
ret = g_list_reverse (ret);
out:
g_list_free_full (object_proxies, g_object_unref);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
typedef struct
{
UDisksObject *partition_table_object;
guint64 pos_to_wait_for;
gboolean ignore_container;
} WaitForPartitionData;
static UDisksObject *
wait_for_partition (UDisksDaemon *daemon,
gpointer user_data)
{
WaitForPartitionData *data = user_data;
UDisksObject *ret = NULL;
GList *objects, *l;
objects = udisks_daemon_get_objects (daemon);
for (l = objects; l != NULL; l = l->next)
{
UDisksObject *object = UDISKS_OBJECT (l->data);
UDisksPartition *partition = udisks_object_get_partition (object);
if (partition != NULL)
{
if (g_strcmp0 (udisks_partition_get_table (partition),
g_dbus_object_get_object_path (G_DBUS_OBJECT (data->partition_table_object))) == 0)
{
guint64 offset = udisks_partition_get_offset (partition);
guint64 size = udisks_partition_get_size (partition);
if (data->pos_to_wait_for >= offset && data->pos_to_wait_for < offset + size)
{
if (!(udisks_partition_get_is_container (partition) && data->ignore_container))
{
g_object_unref (partition);
ret = g_object_ref (object);
goto out;
}
}
}
g_object_unref (partition);
}
}
out:
g_list_free_full (objects, g_object_unref);
return ret;
}
#define MIB_SIZE (1048576L)
static UDisksObject *
udisks_linux_partition_table_handle_create_partition (UDisksPartitionTable *table,
GDBusMethodInvocation *invocation,
guint64 offset,
guint64 size,
const gchar *type,
const gchar *name,
GVariant *options)
{
const gchar *action_id = NULL;
const gchar *message = NULL;
UDisksBlock *block = NULL;
UDisksObject *object = NULL;
UDisksDaemon *daemon = NULL;
gchar *device_name = NULL;
WaitForPartitionData *wait_data = NULL;
UDisksObject *partition_object = NULL;
UDisksBlock *partition_block = NULL;
BDPartSpec *part_spec = NULL;
BDPartSpec *overlapping_part = NULL;
BDPartTypeReq part_type = 0;
gchar *table_type = NULL;
uid_t caller_uid;
GError *error = NULL;
UDisksBaseJob *job = NULL;
const gchar *partition_type = NULL;
const gchar *partition_uuid = NULL;
object = udisks_daemon_util_dup_object (table, &error);
if (object == NULL)
{
g_dbus_method_invocation_return_gerror (invocation, error);
goto out;
}
daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
g_variant_lookup (options, "partition-type", "&s", &partition_type);
g_variant_lookup (options, "partition-uuid", "&s", &partition_uuid);
block = udisks_object_get_block (object);
if (block == NULL)
{
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Partition table object is not a block device");
goto out;
}
if (!udisks_daemon_util_get_caller_uid_sync (daemon,
invocation,
NULL /* GCancellable */,
&caller_uid,
&error))
{
g_dbus_method_invocation_return_gerror (invocation, error);
goto out;
}
action_id = "org.freedesktop.udisks2.modify-device";
/* Translators: Shown in authentication dialog when the user
* requests creating a new partition.
*
* Do not translate $(drive), it's a placeholder and
* will be replaced by the name of the drive/device in question
*/
message = N_("Authentication is required to create a partition on $(drive)");
if (!udisks_daemon_util_setup_by_user (daemon, object, caller_uid))
{
if (udisks_block_get_hint_system (block))
{
action_id = "org.freedesktop.udisks2.modify-device-system";
}
else if (!udisks_daemon_util_on_user_seat (daemon, object, caller_uid))
{
action_id = "org.freedesktop.udisks2.modify-device-other-seat";
}
}
if (!udisks_daemon_util_check_authorization_sync (daemon,
object,
action_id,
options,
message,
invocation))
goto out;
device_name = g_strdup (udisks_block_get_device (block));
table_type = udisks_partition_table_dup_type_ (table);
wait_data = g_new0 (WaitForPartitionData, 1);
if (g_strcmp0 (table_type, "dos") == 0)
{
char *endp;
gint type_as_int;
if (strlen (name) > 0)
{
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"MBR partition table does not support names");
goto out;
}
type_as_int = strtol (type, &endp, 0);
/* Determine whether we are creating a primary, extended or logical partition */
if (partition_type != NULL)
{
if (g_strcmp0 (partition_type, "primary") == 0)
{
part_type = BD_PART_TYPE_REQ_NORMAL;
}
else if (g_strcmp0 (partition_type, "extended") == 0)
{
part_type = BD_PART_TYPE_REQ_EXTENDED;
}
else if (g_strcmp0 (partition_type, "logical") == 0)
{
part_type = BD_PART_TYPE_REQ_LOGICAL;
}
else
{
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Don't know how to create partition of type `%s'",
partition_type);
goto out;
}
}
else if (type[0] != '\0' && *endp == '\0' &&
(type_as_int == 0x05 || type_as_int == 0x0f || type_as_int == 0x85))
{
part_type = BD_PART_TYPE_REQ_EXTENDED;
}
else
part_type = BD_PART_TYPE_REQ_NEXT;
}
else if (g_strcmp0 (table_type, "gpt") == 0)
{
part_type = BD_PART_TYPE_REQ_NORMAL;
}
else
{
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Don't know how to create partitions this partition table of type `%s'",
table_type);
goto out;
}
job = udisks_daemon_launch_simple_job (daemon,
UDISKS_OBJECT (object),
"partition-create",
caller_uid,
NULL);
if (job == NULL)
{
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Failed to create a job object");
goto out;
}
/* Users might want to specify logical partitions start and size using size of
* of the extended partition. If this happens we need to shift start (offset)
* of the logical partition.
* XXX: We really shouldn't allow creation of overlapping partitions and
* and just automatically fix this. But udisks currently doesn't have
* functions to get free regions on the disks so this is a somewhat valid
* use case. But we should definitely provide some functionality to get
* right "numbers" and stop doing this.
*/
overlapping_part = bd_part_get_part_by_pos (device_name, offset, &error);
if (overlapping_part != NULL && ! (overlapping_part->type & BD_PART_TYPE_FREESPACE))
{
/* extended partition or metadata of the extended partition */
if (overlapping_part->type & BD_PART_TYPE_EXTENDED || overlapping_part->type & (BD_PART_TYPE_LOGICAL | BD_PART_TYPE_METADATA))
{
if (overlapping_part->start == offset)
{
/* just add 1 byte, libblockdev will adjust it */
offset += 1;
udisks_warning ("Requested start of the logical partition overlaps "
"with extended partition metadata. Start of the "
"partition moved to %"G_GUINT64_FORMAT".", offset);
}
}
else
{
/* overlapping partition is not a free space nor an extended part -> error */
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Requested start for the new partition %"G_GUINT64_FORMAT" "
"overlaps with existing partition %s.",
offset, overlapping_part->path);
goto out;
}
}
else
g_clear_error (&error);
part_spec = bd_part_create_part (device_name, part_type, offset,
size, BD_PART_ALIGN_OPTIMAL, &error);
if (!part_spec)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error creating partition on %s: %s",
udisks_block_get_device (block),
error->message);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, error->message);
goto out;
}
/* set name and UUID if given */
if (g_strcmp0 (table_type, "gpt") == 0)
{
if (strlen (name) > 0)
{
if (!bd_part_set_part_name (device_name, part_spec->path, name, &error))
{
g_prefix_error (&error, "Error setting name for newly created partition: ");
g_dbus_method_invocation_return_gerror (invocation, error);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, error->message);
goto out;
}
}
else if (partition_uuid)
{
if (!bd_part_set_part_uuid (device_name, part_spec->path, partition_uuid, &error))
{
g_prefix_error (&error, "Error setting partition UUID for newly created partition: ");
g_dbus_method_invocation_return_gerror (invocation, error);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, error->message);
goto out;
}
}
}
/* set type if given and if not extended partition */
if (part_spec->type != BD_PART_TYPE_EXTENDED && strlen (type) > 0)
{
gboolean ret = FALSE;
if (g_strcmp0 (table_type, "gpt") == 0)
ret = bd_part_set_part_type (device_name, part_spec->path, type, &error);
else if (g_strcmp0 (table_type, "dos") == 0)
ret = bd_part_set_part_id (device_name, part_spec->path, type, &error);
if (!ret)
{
g_prefix_error (&error, "Error setting type for newly created partition: ");
g_dbus_method_invocation_return_gerror (invocation, error);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, error->message);
goto out;
}
}
/* wipe the newly created partition if wanted */
if (part_spec->type != BD_PART_TYPE_EXTENDED)
{
if (!bd_fs_wipe (part_spec->path, TRUE, FALSE, &error))
{
if (g_error_matches (error, BD_FS_ERROR, BD_FS_ERROR_NOFS))
g_clear_error (&error);
else
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Error wiping newly created partition %s: %s",
part_spec->path,
error->message);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, error->message);
goto out;
}
}
}
wait_data->ignore_container = (part_spec->type == BD_PART_TYPE_LOGICAL);
wait_data->pos_to_wait_for = part_spec->start + (part_spec->size / 2L);
/* sit and wait for the partition to show up */
g_warn_if_fail (wait_data->pos_to_wait_for > 0);
wait_data->partition_table_object = object;
partition_object = udisks_daemon_wait_for_object_sync (daemon,
wait_for_partition,
wait_data,
NULL,
UDISKS_DEFAULT_WAIT_TIMEOUT,
&error);
if (partition_object == NULL)
{
g_prefix_error (&error, "Error waiting for partition to appear: ");
g_dbus_method_invocation_return_gerror (invocation, error);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, error->message);
goto out;
}
partition_block = udisks_object_get_block (partition_object);
if (partition_block == NULL)
{
g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Partition object is not a block device");
g_clear_object (&partition_object);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), FALSE, NULL);
goto out;
}
/* Trigger uevent on the disk -- we sometimes get add-remove-add uevents for
the new partition without getting change event for the disks after the
last add event and this breaks the "Partitions" property on the
"PartitionTable" interface. */
udisks_linux_block_object_trigger_uevent_sync (UDISKS_LINUX_BLOCK_OBJECT (object),
UDISKS_DEFAULT_WAIT_TIMEOUT);
udisks_simple_job_complete (UDISKS_SIMPLE_JOB (job), TRUE, NULL);
out:
g_free (table_type);
g_free (wait_data);
g_clear_error (&error);
g_clear_object (&partition_block);
g_free (device_name);
g_clear_object (&object);
g_clear_object (&block);
if (part_spec)
bd_part_spec_free (part_spec);
if (overlapping_part)
bd_part_spec_free (overlapping_part);
return partition_object;
}
static int
flock_block_dev (UDisksPartitionTable *iface)
{
UDisksObject *object = udisks_daemon_util_dup_object (iface, NULL);
UDisksBlock *block = object? udisks_object_peek_block (object) : NULL;
int fd = block? open (udisks_block_get_device (block), O_RDONLY) : -1;
if (fd >= 0)
flock (fd, LOCK_SH | LOCK_NB);
g_clear_object (&object);
return fd;
}
static void
unflock_block_dev (int fd)
{
if (fd >= 0)
close (fd);
}
/* runs in thread dedicated to handling @invocation */
static gboolean
handle_create_partition (UDisksPartitionTable *table,
GDBusMethodInvocation *invocation,
guint64 offset,
guint64 size,
const gchar *type,
const gchar *name,
GVariant *options)
{
/* We (try to) take a shared lock on the partition table while
creating and formatting a new partition, here and also in
handle_create_partition_and_format.
This lock prevents udevd from issuing a BLKRRPART ioctl call.
That ioctl is undesired because it might temporarily remove the
block device of the newly created block device. It does so only
temporarily, but it still happens that the block device is
missing exactly when wipefs or mkfs try to access it.
Also, a pair of remove/add events will cause udisks to create a
new internal UDisksObject to represent the block device of the
partition. The code currently doesn't handle this and waits for
changes (such as an expected filesystem type or UUID) to a
obsolete internal object that will never see them.
*/
int fd = flock_block_dev (table);
UDisksObject *partition_object =
udisks_linux_partition_table_handle_create_partition (table,
invocation,
offset,
size,
type,
name,
options);
if (partition_object)
{
udisks_partition_table_complete_create_partition
(table, invocation, g_dbus_object_get_object_path (G_DBUS_OBJECT (partition_object)));
g_object_unref (partition_object);
}
unflock_block_dev (fd);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* runs in thread dedicated to handling @invocation */
struct FormatCompleteData {
UDisksPartitionTable *table;
GDBusMethodInvocation *invocation;
UDisksObject *partition_object;
int lock_fd;
};
static void
handle_format_complete (gpointer user_data)
{
struct FormatCompleteData *data = user_data;
udisks_partition_table_complete_create_partition
(data->table, data->invocation, g_dbus_object_get_object_path (G_DBUS_OBJECT (data->partition_object)));
unflock_block_dev (data->lock_fd);
}
static gboolean
handle_create_partition_and_format (UDisksPartitionTable *table,
GDBusMethodInvocation *invocation,
guint64 offset,
guint64 size,
const gchar *type,
const gchar *name,
GVariant *options,
const gchar *format_type,
GVariant *format_options)
{
/* See handle_create_partition for a motivation of taking the lock.
*/
int fd = flock_block_dev (table);
UDisksObject *partition_object =
udisks_linux_partition_table_handle_create_partition (table,
invocation,
offset,
size,
type,
name,
options);
if (partition_object)
{
struct FormatCompleteData data;
data.table = table;
data.invocation = invocation;
data.partition_object = partition_object;
data.lock_fd = fd;
udisks_linux_block_handle_format (udisks_object_peek_block (partition_object),
invocation,
format_type,
format_options,
handle_format_complete, &data);
g_object_unref (partition_object);
}
else
unflock_block_dev (fd);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* ---------------------------------------------------------------------------------------------------- */
static void
partition_table_iface_init (UDisksPartitionTableIface *iface)
{
iface->handle_create_partition = handle_create_partition;
iface->handle_create_partition_and_format = handle_create_partition_and_format;
}