rsfdisk 0.2.0

Safe Rust wrapper around the `util-linux/libfdisk` C library
Documentation
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
// Copyright (c) 2023 Nick Piaddo
// SPDX-License-Identifier: Apache-2.0 OR MIT

// From dependency library

// From standard library
use std::mem::MaybeUninit;

// From this library
use crate::core::errors::PartitionTableError;

use crate::core::partition::PartitionKind;
use crate::core::partition_table::Field;
use crate::core::partition_table::FieldFormat;
use crate::core::partition_table::InputType;
use crate::core::partition_table::PartitionTableKind;
use crate::core::partition_table::Range;
use crate::core::partition_table::Shortcut;

use crate::ffi_to_string_or_empty;
use crate::ffi_utils;

/// A partition table.
#[derive(Debug)]
#[repr(transparent)]
pub struct PartitionTable {
    pub(crate) inner: *mut libfdisk::fdisk_label,
}

impl PartitionTable {
    #[doc(hidden)]
    #[allow(dead_code)]
    /// Wraps a raw `libfdisk::fdisk_label` with a safe `PartitionTable`.
    pub(crate) fn from_ptr(ptr: *mut libfdisk::fdisk_label) -> PartitionTable {
        Self { inner: ptr }
    }

    #[doc(hidden)]
    #[allow(dead_code)]
    /// Wraps a boxed raw `libfdisk::fdisk_label` pointer in a safe reference.
    pub(crate) unsafe fn ref_from_boxed_ptr<'a>(
        ptr: Box<*mut libfdisk::fdisk_label>,
    ) -> (*mut *mut libfdisk::fdisk_label, &'a Self) {
        let raw_ptr = Box::into_raw(ptr);
        let entry_ref = unsafe { &*(raw_ptr as *const _ as *const Self) };

        (raw_ptr, entry_ref)
    }

    #[doc(hidden)]
    #[allow(dead_code)]
    /// Wraps a boxed raw `libfdisk::fdisk_label` pointer in a safe reference.
    pub(crate) unsafe fn mut_from_boxed_ptr<'a>(
        ptr: Box<*mut libfdisk::fdisk_label>,
    ) -> (*mut *mut libfdisk::fdisk_label, &'a mut Self) {
        let raw_ptr = Box::into_raw(ptr);
        let entry_ref = unsafe { &mut *(raw_ptr as *mut Self) };

        (raw_ptr, entry_ref)
    }

    /// Returns this `PartitionTable`'s type.
    pub fn kind(&self) -> PartitionTableKind {
        let code = unsafe { libfdisk::fdisk_label_get_type(self.inner) };
        // It is safe to assume, `libfdisk` will not return an unsupported ID.
        let kind = PartitionTableKind::try_from(code as u32)
            .map_err(|e| {
                let err_msg = format!("unsupported partition table type. code: {}. {}", code, e);
                PartitionTableError::PartitionTableKind(err_msg)
            })
            .unwrap();

        log::debug!(
            "PartitionTable::kind values code: {:?}, kind: {:?}",
            code,
            kind
        );

        kind
    }

    /// Returns the [`PartitionTable`]'s name.
    pub fn name(&self) -> Option<&str> {
        log::debug!("PartitionTable::name getting partition table's name");

        let mut ptr = MaybeUninit::<*const libc::c_char>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_get_name(self.inner));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                log::debug!("PartitionTable::name no name provided. libfdisk::fdisk_label_get_name returned a NULL pointer");

                None
            }
            name_ptr => {
                let name = ffi_utils::const_char_array_to_str_ref(name_ptr).ok();

                log::debug!(
                    "PartitionTable::name got partition table's name: {:?}",
                    name,
                );

                name
            }
        }
    }

    /// Returns the [`Range`] of admissible cylinder values.
    pub fn geometry_cylinders(&self) -> Option<Range> {
        log::debug!(
            "PartitionTable::geometry_cylinders getting admissible range for cylinder values"
        );

        let mut min = MaybeUninit::<libfdisk::fdisk_sector_t>::zeroed();
        let mut max = MaybeUninit::<libfdisk::fdisk_sector_t>::zeroed();

        let result = unsafe {
            libfdisk::fdisk_label_get_geomrange_cylinders(
                self.inner,
                min.as_mut_ptr(),
                max.as_mut_ptr(),
            )
        };

        match result {
            0 => {
                let lower_bound = unsafe { min.assume_init() };
                let upper_bound = unsafe { max.assume_init() };
                let range = Range::new(lower_bound, upper_bound);

                log::debug!("PartitionTable::geometry_cylinders got admissible range for cylinder values: {:?}", range);

                Some(range)
            }
            code => {
                let err_msg = "failed to get admissible range for cylinder values".to_owned();
                log::debug!("PartitionTable::geometry_cylinders {}. libfdisk::fdisk_label_get_geomrange_cylinders returned error code: {:?}", err_msg, code);

                None
            }
        }
    }

    /// Returns the [`Range`] of admissible head values.
    pub fn geometry_heads(&self) -> Option<Range> {
        log::debug!("PartitionTable::geometry_heads getting admissible range for head values");

        let mut min = MaybeUninit::<libc::c_uint>::zeroed();
        let mut max = MaybeUninit::<libc::c_uint>::zeroed();

        let result = unsafe {
            libfdisk::fdisk_label_get_geomrange_heads(
                self.inner,
                min.as_mut_ptr(),
                max.as_mut_ptr(),
            )
        };

        match result {
            0 => {
                let lower_bound = unsafe { min.assume_init() as u64 };
                let upper_bound = unsafe { max.assume_init() as u64 };
                let range = Range::new(lower_bound, upper_bound);

                log::debug!(
                    "PartitionTable::geometry_heads got admissible range for head values: {:?}",
                    range
                );

                Some(range)
            }
            code => {
                let err_msg = "failed to get admissible range for head values".to_owned();
                log::debug!("PartitionTable::geometry_heads {}. libfdisk::fdisk_label_get_geomrange_heads returned error code: {:?}", err_msg, code);

                None
            }
        }
    }

    /// Returns the [`Range`] of admissible sector values.
    pub fn geometry_sectors(&self) -> Option<Range> {
        log::debug!("PartitionTable::geometry_sectors getting admissible range for sector values");

        let mut min = MaybeUninit::<libfdisk::fdisk_sector_t>::zeroed();
        let mut max = MaybeUninit::<libfdisk::fdisk_sector_t>::zeroed();

        let result = unsafe {
            libfdisk::fdisk_label_get_geomrange_sectors(
                self.inner,
                min.as_mut_ptr(),
                max.as_mut_ptr(),
            )
        };

        match result {
            0 => {
                let lower_bound = unsafe { min.assume_init() };
                let upper_bound = unsafe { max.assume_init() };
                let range = Range::new(lower_bound, upper_bound);

                log::debug!(
                    "PartitionTable::geometry_sectors got admissible range for sector values: {:?}",
                    range
                );

                Some(range)
            }
            code => {
                let err_msg = "failed to get admissible range for sector values".to_owned();
                log::debug!("PartitionTable::geometry_sectors {}. libfdisk::fdisk_label_get_geomrange_sectors returned error code: {:?}", err_msg, code);

                None
            }
        }
    }

    /// Marks this `PartitionTable` as `disabled`, the library will ignore this partition table
    /// when scanning for partition tables on a device.
    pub fn disable(&self) {
        log::debug!("PartitionTable::disable disabling partition table");

        unsafe { libfdisk::fdisk_label_set_disabled(self.inner, 1) }
    }

    /// Marks this `PartitionTable` as `enabled`.
    pub fn enable(&mut self) {
        log::debug!("PartitionTable::enable disabling partition table");

        unsafe { libfdisk::fdisk_label_set_disabled(self.inner, 0) }
    }

    /// Returns the parameters for formatting a [`Field`] when printing it on the terminal.
    pub fn partition_field_format(&self, field: Field) -> Option<FieldFormat> {
        log::debug!(
            "PartitionTable::partition_field_format getting Partition Entry Array's formatting parameters: {:?}",
            field
        );

        let field_int = field as u32 as i32;

        let mut ptr = MaybeUninit::<*const libfdisk::fdisk_field>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_get_field(self.inner, field_int));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                let err_msg = format!(
                    "failed to get Partition Entry Array's formatting parameters: {:?}",
                    field
                );
                log::debug!("PartitionTable::partition_field_format {}. libfdisk::fdisk_label_get_field  returned a NULL pointer", err_msg);

                None
            }
            ptr => {
                log::debug!(
                    "PartitionTable::partition_field_format got Partition Entry Array's formatting parameters: {:?}",
                    field
                );
                let format = FieldFormat::from_ptr(ptr);

                Some(format)
            }
        }
    }

    /// Returns the parameters for formatting a partition entry field matching the given `name`
    /// when printing it on the terminal.
    pub fn partition_field_format_by_name<T>(&self, name: T) -> Option<FieldFormat>
    where
        T: AsRef<str>,
    {
        let name = name.as_ref();
        let name_cstr = ffi_utils::as_ref_str_to_c_string(name).ok()?;
        log::debug!(
            "PartitionTable::partition_field_format_by_name getting Partition Entry Array's formatting parameters: {:?}",
            name
        );

        let mut ptr = MaybeUninit::<*const libfdisk::fdisk_field>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_get_field_by_name(
                self.inner,
                name_cstr.as_ptr(),
            ));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                let err_msg = format!(
                    "failed to get Partition Entry Array's formatting parameters: {:?}",
                    name
                );
                log::debug!("PartitionTable::partition_field_format_by_name {}. libfdisk::fdisk_label_get_field_by_name returned a NULL pointer", err_msg);

                None
            }
            ptr => {
                log::debug!(
                    "PartitionTable::partition_field_format_by_name got partition formatting parameters: {:?}",
                    name
                );
                let field = FieldFormat::from_ptr(ptr);

                Some(field)
            }
        }
    }

    /// Returns the number of partition types supported by this `PartitionTable`.
    pub fn count_supported_partition_types(&self) -> usize {
        let count = unsafe { libfdisk::fdisk_label_get_nparttypes(self.inner) };
        log::debug!(
            "PartitionTable::count_supported_partition_types value: {:?}",
            count
        );

        count
    }

    /// Returns the `nth` supported partition type.
    pub fn supported_partition_types(&self, nth: usize) -> Option<PartitionKind> {
        log::debug!("PartitionTable::nth_supported_partition_types getting supported partition type number: {:?}", nth);

        let mut ptr = MaybeUninit::<*mut libfdisk::fdisk_parttype>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_get_parttype(self.inner, nth));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                log::debug!("PartitionTable::supported_partition_types no partition type at index: {:?}. libfdisk::fdisk_label_get_parttype returned a NULL pointer", nth);

                None
            }
            ptr => {
                log::debug!("PartitionTable::nth_supported_partition_types got supported partition type number: {:?}", nth);
                let kind = PartitionKind::from_ptr(ptr);

                Some(kind)
            }
        }
    }

    /// Returns the [`Shortcut`] of the `nth` supported partition type.
    pub fn partition_type_shortcut(&self, nth: usize) -> Option<Shortcut> {
        log::debug!("PartitionTable::partition_type_shortcut getting shortcut for supported partition type number: {:?}", nth);

        let mut typestr = MaybeUninit::<*const libc::c_char>::zeroed();
        let mut shortcut = MaybeUninit::<*const libc::c_char>::zeroed();
        let mut alias = MaybeUninit::<*const libc::c_char>::zeroed();

        let result = unsafe {
            libfdisk::fdisk_label_get_parttype_shortcut(
                self.inner,
                nth,
                typestr.as_mut_ptr(),
                shortcut.as_mut_ptr(),
                alias.as_mut_ptr(),
            )
        };

        match result {
            code if code == 0 || code == 2 => {
                let typestr_cstr = unsafe { typestr.assume_init() };
                let shortcut_cstr = unsafe { shortcut.assume_init() };
                let alias_cstr = unsafe { alias.assume_init() };

                let type_string = ffi_to_string_or_empty!(typestr_cstr);
                let shortcut = ffi_to_string_or_empty!(shortcut_cstr);
                let alias = ffi_to_string_or_empty!(alias_cstr);
                let deprecated_alias = code == 2;

                let shortcut = Shortcut::new(alias, shortcut, type_string, deprecated_alias);

                Some(shortcut)
            }
            code => {
                let reason = if code == 1 {
                    format!(", index {} out of bounds", nth)
                } else {
                    String::new()
                };
                let err_msg = format!(
                    "failed to get shortcut for partition type number: {:?}{}",
                    nth, reason
                );
                log::debug!("PartitionTable::partition_type_shortcut {}. libfdisk::fdisk_label_get_parttype_shortcut returned error code: {:?}", err_msg, code);

                None
            }
        }
    }

    /// Each partition table type supports specific kinds of partitions. These partition types have
    /// various identification schemes, for example the type of a partition in:
    /// - an `MBR` partition table is referred to by a numerical code in hexadecimal (i.e. `0x83`
    ///   for a Linux native partition, `0x39` for a Plan 9 edition 3 partition, etc.),
    /// - a `GPT` partition table is characterized by a partition UUID (i.e.
    ///   `83bd6b9d-7f41-11dc-be0b-001560b84f0f` for a FreeBSD boot partition)
    ///
    /// Given a string identifier, this function will try to parse it into the corresponding
    /// [`PartitionKind`] supported by this `PartitionTable`. If the identifier is unknown, this
    /// method will return an "unknown" `PartitionKind` for which [`PartitionKind::is_unknown_type`]
    /// will return `true`.
    ///
    /// **Note:** for `GPT` partition tables, this function accepts the same sequence value (as a
    /// string) as the one used by [`PartitionTable::supported_partition_types`] to identify a
    /// supported [`PartitionKind`].
    pub fn partition_type_from_string_id<T>(
        &self,
        id: T,
    ) -> Result<PartitionKind, PartitionTableError>
    where
        T: AsRef<str>,
    {
        let id = id.as_ref();
        let id_cstr = ffi_utils::as_ref_str_to_c_string(id).map_err(|e| {
            let err_msg = format!("failed to convert value to `CString` {e}");
            PartitionTableError::CStringConversion(err_msg)
        })?;

        log::debug!(
            "PartitionTable::partition_type_from_string_id parsing: {:?} into a `PartitionKind`",
            id
        );

        let mut ptr = MaybeUninit::<*mut libfdisk::fdisk_parttype>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_parse_parttype(
                self.inner,
                id_cstr.as_ptr(),
            ));
        }
        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                let err_msg = format!("failed to parse: {:?} into a partition type", id);
                log::debug!("PartitionTable::partition_type_from_string_id {}. libfdisk::fdisk_label_get_parttype_from_string returned a NULL pointer", err_msg);

                Err(PartitionTableError::Parse(err_msg))
            }
            ptr => {
                log::debug!(
                    "PartitionTable::partition_type_from_string_id parsed id: {:?} into a partition type",
                    id
                );
                let kind = PartitionKind::from_ptr(ptr);

                Ok(kind)
            }
        }
    }

    /// Each partition table type supports specific kinds of partitions which follow various
    /// identification schemes.  Given a string identifier, this generic parser will try to turn it
    /// into the right [`PartitionKind`] supported by this `PartitionTable`. You can provide a list
    /// of [`InputType`] hints to direct the parser, and narrow the search.
    ///
    /// This identifier string can be any of:
    /// - a hexadecimal code,
    /// - a UUID,
    /// - an alias (i.e. `linux` for a Linux partition),
    /// - a shortcut (i.e. `L` for a Linux partition).
    ///
    /// This method will return, unless told otherwise, an "unknown" `PartitionKind` for
    /// which [`PartitionKind::is_unknown_type`] will return `true` if it can't identify the
    /// corresponding partition type.
    ///
    /// **Note:** for `GPT` partition tables, this function accepts the same sequence value (as a
    /// string) as the one used by [`PartitionTable::supported_partition_types`] to identify a
    /// supported [`PartitionKind`].
    pub fn partition_type_parse<T, U>(
        &self,
        string: T,
        flags: U,
    ) -> Result<PartitionKind, PartitionTableError>
    where
        T: AsRef<str>,
        U: AsRef<[InputType]>,
    {
        let string = string.as_ref();
        let flags = flags.as_ref();

        let string_cstr = ffi_utils::as_ref_str_to_c_string(string).map_err(|e| {
            let err_msg = format!("failed to convert value to `CString` {e}");
            PartitionTableError::CStringConversion(err_msg)
        })?;

        let c_flags = flags.iter().fold(0, |acc, &flag| acc | flag as u32);

        log::debug!("PartitionTable::partition_type_parse parsing: {:?} with flags: {:?} into a `PartitionKind`", string, flags);

        let mut ptr = MaybeUninit::<*mut libfdisk::fdisk_parttype>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_advparse_parttype(
                self.inner,
                string_cstr.as_ptr(),
                c_flags as i32,
            ));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                let err_msg = format!(
                    "failed to parse {:?} with flags {:?} into a partition type",
                    string, flags
                );
                log::debug!("PartitionTable::partition_type_parse {}. libfdisk::fdisk_label_advparse_parttype returned a NULL pointer", err_msg);

                Err(PartitionTableError::Parse(err_msg))
            }
            ptr => {
                log::debug!("PartitionTable::partition_type_parse parsed: {:?} with flags: {:?} into a `PartitionKind`", string, flags);
                let kind = PartitionKind::from_ptr(ptr);

                Ok(kind)
            }
        }
    }

    /// Converts a partition type's identification `code` into a [`PartitionKind`].
    pub fn partition_type_from_code(
        &self,
        code: u32,
    ) -> Result<PartitionKind, PartitionTableError> {
        log::debug!(
            "PartitionTable::partition_type_from_code converting code: {:?} to partition type",
            code
        );

        let mut ptr = MaybeUninit::<*mut libfdisk::fdisk_parttype>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_get_parttype_from_code(
                self.inner, code,
            ));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                let err_msg = format!("failed to convert code: {:?} to partition type", code);
                log::debug!("PartitionTable::partition_type_from_code {}. libfdisk::fdisk_label_get_parttype_from_code returned a NULL pointer", err_msg);

                Err(PartitionTableError::Conversion(err_msg))
            }
            ptr => {
                log::debug!("PartitionTable::partition_type_from_code converted code: {:?} to partition type", code);
                let kind = PartitionKind::from_ptr(ptr);

                Ok(kind)
            }
        }
    }

    /// Converts a `string` partition type identifier into a [`PartitionKind`].
    pub fn partition_type_from_string<T>(
        &self,
        string: T,
    ) -> Result<PartitionKind, PartitionTableError>
    where
        T: AsRef<str>,
    {
        let string = string.as_ref();
        let string_cstr = ffi_utils::as_ref_str_to_c_string(string).map_err(|e| {
            let err_msg = format!("failed to convert value to `CString` {e}");
            PartitionTableError::CStringConversion(err_msg)
        })?;

        log::debug!(
            "PartitionTable::partition_type_from_code converting string: {:?} to partition type",
            string
        );

        let mut ptr = MaybeUninit::<*mut libfdisk::fdisk_parttype>::zeroed();
        unsafe {
            ptr.write(libfdisk::fdisk_label_get_parttype_from_string(
                self.inner,
                string_cstr.as_ptr(),
            ));
        }

        match unsafe { ptr.assume_init() } {
            ptr if ptr.is_null() => {
                let err_msg = format!("failed to convert string: {:?} to partition type", string);
                log::debug!("PartitionTable::partition_type_from_code {}. libfdisk::fdisk_label_get_parttype_from_string returned a NULL pointer", err_msg);

                Err(PartitionTableError::Parse(err_msg))
            }
            ptr => {
                log::debug!("PartitionTable::partition_type_from_code converted string: {:?} to partition type", string);
                let kind = PartitionKind::from_ptr(ptr);

                Ok(kind)
            }
        }
    }

    /// `PartitionTable` keeps track of changes, so calling this function is not required unless
    /// you want to force an [`Fdisk`](crate::fdisk::Fdisk) instance to use the current state of
    /// this `PartitionTable` when writing data to disk.
    pub fn mark_as_changed(&mut self) {
        log::debug!("PartitionTable::mark_as_changed marking partition table as changed");

        unsafe { libfdisk::fdisk_label_set_changed(self.inner, 1) }
    }

    /// Marks the in-memory partition table as `unchanged`.
    pub fn mark_as_unchanged(&mut self) {
        log::debug!("PartitionTable::mark_as_unchanged marking partition table as unchanged");

        unsafe { libfdisk::fdisk_label_set_changed(self.inner, 0) }
    }

    //---- END mutators

    //---- BEGIN predicates

    /// Returns `true` when this `PartitionTable` is marked as `disabled`.
    pub fn is_disabled(&self) -> bool {
        let state = unsafe { libfdisk::fdisk_label_is_disabled(self.inner) == 1 };
        log::debug!("PartitionTable::is_disabled value: {:?}", state);

        state
    }

    /// Returns `true` when the in-memory `PartitionTable` has been modified.
    pub fn has_changes(&self) -> bool {
        let state = unsafe { libfdisk::fdisk_label_is_changed(self.inner) == 1 };
        log::debug!("PartitionTable::has_changes value: {:?}", state);

        state
    }

    /// Returns `true` when this `PartitionTable` requires accessing partitions by Cylinder-Head-Sector (CHS) addressing.
    pub fn requires_chs_addressing(&self) -> bool {
        let state = unsafe { libfdisk::fdisk_label_require_geometry(self.inner) == 1 };
        log::debug!("PartitionTable::requires_chs_addressing value: {:?}", state);

        state
    }

    /// Returns `true` when this `PartitionTable` supports [`Shortcut`]s/aliases for partition
    /// types.
    pub fn supports_partition_type_shortcuts(&self) -> bool {
        let state = unsafe { libfdisk::fdisk_label_has_parttypes_shortcuts(self.inner) == 1 };
        log::debug!(
            "PartitionTable::has_partition_type_shortcuts value: {:?}",
            state
        );

        state
    }

    /// Returns `true` when this `PartitionTable` uses codes to identify partition types (e.g.
    /// the [`OSType`
    /// field](https://uefi.org/specs/UEFI/2.10/05_GUID_Partition_Table_Format.html#legacy-mbr-partition-record)
    /// in an `MBR` Partition Entry).
    pub fn uses_partition_type_codes(&self) -> bool {
        let state = unsafe { libfdisk::fdisk_label_has_code_parttypes(self.inner) == 1 };
        log::debug!(
            "PartitionTable::uses_partition_type_codes value: {:?}",
            state
        );

        state
    }

    //---- END predicates
}

impl AsRef<PartitionTable> for PartitionTable {
    #[inline]
    fn as_ref(&self) -> &PartitionTable {
        self
    }
}