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
use dbus::arg::{Variant, RefArg};
use std::collections::HashMap;
use std::path::PathBuf;
use utils::*;

#[derive(Clone, Debug, Default)]
pub struct Block {
    pub crypto_backing_device: String,
    pub device_number: u64,
    pub device: PathBuf,
    pub drive: String,
    pub encrypted: Option<Encrypted>,
    pub hint_auto: bool,
    pub hint_icon_name: Option<String>,
    pub hint_ignore: bool,
    pub hint_name: Option<String>,
    pub hint_partitionable: bool,
    pub hint_symbolic_icon_name: Option<String>,
    pub hint_system: bool,
    pub id_label: Option<String>,
    pub id_type: Option<String>,
    pub id_usage: Option<String>,
    pub id_uuid: Option<String>,
    pub id_version: Option<String>,
    pub id: String,
    pub loopback: bool,
    pub mdraid: PathBuf,
    pub mdraid_member: PathBuf,
    pub mount_points: Vec<PathBuf>,
    pub partition: Option<Partition>,
    pub path: String,
    pub preferred_device: PathBuf,
    pub read_only: bool,
    pub size: u64,
    pub swapspace: Option<bool>,
    pub symlinks: Vec<PathBuf>,
    pub table: Option<PartitionTable>,
    pub userspace_mount_options: Vec<String>,
    pub configuration: Option<BlockConfiguration>,
}

impl Block {
    /// This will be true if this block contains an encrypted volume.
    pub fn is_encrypted(&self) -> bool {
        self.encrypted.is_some()
    }

    /// If this block contains an encrypted volume, find the block associated with it.
    pub fn get_encrypted_block<'a>(&self, within: &'a [Block]) -> Option<&'a Block> {
        if self.encrypted.is_some() {
            within.iter().find(|b| b.crypto_backing_device == self.path)
        } else {
            None
        }
    }
}

impl ParseFrom for Block {
    fn parse_from(path: &str, objects: &HashMap<String, HashMap<String, Variant<Box<RefArg>>>>) -> Option<Block> {
        if objects.get("org.freedesktop.UDisks2.Loop").is_some() {
            return None;
        }

        let mut block = Block::default();
        block.path = path.to_owned();

        match objects.get("org.freedesktop.UDisks2.Block") {
            Some(object) => {
                for (key, ref value) in object {
                    match key.as_str() {
                        "CryptoBackingDevice" => block.crypto_backing_device = get_string(value).unwrap(),
                        "Device" => block.device = PathBuf::from(get_byte_array(value).unwrap()),
                        "DeviceNumber" => block.device_number = get_u64(value),
                        "Drive" => block.drive = get_string(value).unwrap(),
                        "HintAuto" => block.hint_auto = get_bool(value),
                        "HintIconName" => block.hint_icon_name = get_string(value),
                        "HintIgnore" => block.hint_ignore = get_bool(value),
                        "HintName" => block.hint_name = get_string(value),
                        "HintPartitionable" => block.hint_partitionable = get_bool(value),
                        "HintSymbolicIconName" => block.hint_symbolic_icon_name = get_string(value),
                        "HintSystem" => block.hint_system = get_bool(value),
                        "Id" => block.id = get_string(value).unwrap_or_default(),
                        "IdLabel" => block.id_label = get_string(value),
                        "IdType" => block.id_type = get_string(value),
                        "IdUsage" => block.id_usage = get_string(value),
                        "IdUUID" => block.id_uuid = get_string(value),
                        "IdVersion" => block.id_version = get_string(value),
                        "MDRaid" => block.mdraid = get_string(value).map(PathBuf::from).unwrap_or_default(),
                        "MDRaidMember" => block.mdraid_member = get_string(value).map(PathBuf::from).unwrap_or_default(),
                        "PreferredDevice" => block.preferred_device = PathBuf::from(get_byte_array(value).unwrap()),
                        "ReadOnly" => block.read_only = get_bool(value),
                        "Size" => block.size = get_u64(value),
                        "Symlinks" => block.symlinks = get_array_of_byte_arrays(value)
                            .map(|paths| paths.into_iter().map(PathBuf::from).collect::<Vec<_>>())
                            .unwrap_or_default(),
                        "UserspaceMountOptions" => {
                            block.userspace_mount_options = get_string_array(value).unwrap_or_default()
                        },
                        "Configuration" => {
                            let mut configuration = BlockConfiguration::default();
                            for value in value.as_iter().unwrap() {
                                if let Some(mut iterator) = value.as_iter() {
                                    if let Some(mut iterator) = iterator.next().and_then(|i| i.as_iter()) {
                                        if let (Some(key), Some(mut array)) = (iterator.next(), iterator.next().and_then(|i| i.as_iter())) {
                                            if let Some(key) = key.as_str() {
                                                if key == "fstab" {
                                                    while let (Some(key), Some(value)) = (array.next(), array.next()) {
                                                        if let Some(key) = key.as_str() {
                                                            match key {
                                                                "fsname" => configuration.fstab.fsname = vva(value).unwrap_or_default(),
                                                                "dir" => configuration.fstab.dir = vva(value).unwrap_or_default(),
                                                                "type" => configuration.fstab.type_ = vva(value).unwrap_or_default(),
                                                                "opts" => configuration.fstab.opts = vva(value).unwrap_or_default(),
                                                                "freq" => configuration.fstab.freq = value.as_u64().unwrap_or_default() as i32,
                                                                "passno" => configuration.fstab.passno = value.as_u64().unwrap_or_default() as i32,
                                                                _ => {
                                                                    eprintln!("unhandled block config fstab key: {:?}, {:?}", key, value);
                                                                }
                                                            }
                                                        }
                                                    }
                                                } else if key == "crypttab" {
                                                    while let (Some(key), Some(value)) = (array.next(), array.next()) {
                                                        if let Some(key) = key.as_str() {
                                                            match key {
                                                                "name" => configuration.crypttab.name = vva(value).unwrap_or_default(),
                                                                "device" => configuration.crypttab.device = vva(value).unwrap_or_default(),
                                                                "passphrase-path" => configuration.crypttab.passphrase_path = vva(value).unwrap_or_default(),
                                                                "options" => configuration.crypttab.options = vva(value).unwrap_or_default(),
                                                                _ => {
                                                                    eprintln!("unhandled block config crypttab key: {:?}, {:?}", key, value);
                                                                }
                                                            }
                                                        }
                                                    }
                                                } else {
                                                    eprintln!("unknown block config key: {}", key);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            block.configuration = Some(configuration);
                        }
                        _ => {
                            #[cfg(debug_assertions)]
                            eprintln!("unhandled org.freedesktop.UDisks2.Block.{}", key);
                            eprintln!("value: {:#?}", value);
                        }
                    }
                }

            }
            None => return None
        }

        for (key, object) in objects {
            match key.as_str() {
                "org.freedesktop.UDisks2.Block" => (),
                "org.freedesktop.UDisks2.Swapspace" => {
                    block.swapspace = Some(object.get("Active").map_or(false, get_bool));
                },
                "org.freedesktop.UDisks2.PartitionTable" => {
                    let mut table = PartitionTable::default();
                    for (key, ref value) in object {
                        match key.as_str() {
                            "Type" => table.type_ = get_string(value).unwrap_or_default(),
                            "Partitions" => {
                                table.partitions = get_string_array(value).unwrap_or_default();
                                table.partitions.sort_unstable();
                            },
                            _ => {
                                #[cfg(debug_assertions)]
                                eprintln!("unhandled org.freedesktop.UDisks2.PartitionTable.{}", key);
                            }
                        }
                    }

                    block.table = Some(table);
                },
                "org.freedesktop.UDisks2.Partition" => {
                    let mut partition = Partition::default();
                    for (key, value) in object {
                        match key.as_str() {
                            "Type" => partition.type_ = get_string(value).unwrap_or_default(),
                            "Name" => partition.name = get_string(value).unwrap_or_default(),
                            "UUID" => partition.uuid = get_string(value).unwrap_or_default(),
                            "Table" => partition.table = get_string(value).expect("partition is not part of a table"),
                            "Flags" => partition.flags = get_u64(value),
                            "Offset" => partition.offset = get_u64(value),
                            "Size" => partition.size = get_u64(value),
                            "Number" => partition.number = get_u64(value) as u32,
                            "IsContained" => partition.is_contained = get_bool(value),
                            "IsContainer" => partition.is_container = get_bool(value),
                            _ => {
                                #[cfg(debug_assertions)]
                                eprintln!("unhandled org.freedesktop.UDisks2.Partition.{}", key);
                            }
                        }
                    }

                    block.partition = Some(partition);
                }
                "org.freedesktop.UDisks2.Filesystem" => {
                    block.mount_points = object.get("MountPoints")
                        .and_then(get_array_of_byte_arrays)
                        .map(|paths| paths.into_iter().map(PathBuf::from).collect::<Vec<_>>())
                        .unwrap_or_default()
                }
                "org.freedesktop.UDisks2.Encrypted" => {
                    let mut encrypted = Encrypted::default();
                    for (key, ref value) in object {
                        match key.as_str() {
                            "HintEncryptionType" => encrypted.hint_encryption_type = get_string(value).unwrap_or_default(),
                            "MetadataSize" => encrypted.metadata_size = get_u64(value),
                            "CleartextDevice" => encrypted.cleartext_device = get_string(value).unwrap_or_default(),
                            _ => {
                                #[cfg(debug_assertions)]
                                eprintln!("unhandled org.freedesktop.UDisks2.Encrypted.{}", key);
                            }
                        }
                    }

                    block.encrypted = Some(encrypted);
                }
                _ => {
                    #[cfg(debug_assertions)]
                    eprintln!("unhandled org.freedesktop.UDisks2.{}", key);
                }
            }
        }

        Some(block)
    }
}

#[derive(Clone, Debug, Default)]
pub struct BlockConfiguration {
    pub fstab: BlockConfigurationFstab,
    pub crypttab: BlockConfigurationCrypttab
}

#[derive(Clone, Debug, Default)]
pub struct BlockConfigurationFstab {
    fsname: String,
    dir: String,
    type_: String,
    opts: String,
    freq: i32,
    passno: i32,
}

#[derive(Clone, Debug, Default)]
pub struct BlockConfigurationCrypttab {
    name: String,
    device: String,
    passphrase_path: String,
    options: String,
}

#[derive(Clone, Debug, Default)]
pub struct Encrypted {
    pub hint_encryption_type: String,
    pub metadata_size: u64,
    pub cleartext_device: String
}

#[derive(Clone, Debug, Default)]
pub struct PartitionTable {
    pub type_: String,
    // Partitions are listed by their dbus paths.
    pub partitions: Vec<String>,
}

#[derive(Clone, Debug, Default)]
pub struct Partition {
    // Defines the file system by a type UUID.
    pub type_: String,
    // An optional label that may be applied to a disk.
    pub name: String,
    // Points to the dbus path that this partition exists within.
    pub table: String,
    pub flags: u64,
    pub number: u32,
    pub offset: u64,
    pub size: u64,
    pub uuid: String,
    pub is_container: bool,
    pub is_contained: bool,
}