summaryrefslogtreecommitdiff
path: root/diskman/partition.cc
blob: 323cd307dbb3d93fe22d96aedfe65b7062317b62 (plain) (blame)
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
/*
 * partition.cc - Implementation of the Partition class
 * diskman, the Disk Manipulation library for
 * Project Horizon
 *
 * Copyright (c) 2020 Adélie Linux and contributors.  All rights reserved.
 * This code is licensed under the AGPL 3.0 license, as noted in the
 * LICENSE-code file in the root directory of this repository.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

#include "partition.hh"
#include "disk.hh"

#include <blkid/blkid.h>
#include <iostream>
#include <libfdisk/libfdisk.h>
#include <libudev.h>
#include <stdexcept>

namespace Horizon {
namespace DiskMan {

Partition::Partition(Disk &d, void *creation, int type) {
    switch(type) {
    case 0: { /* libfdisk */
        struct fdisk_partition *part = static_cast<struct fdisk_partition *>(creation);
        if(fdisk_partition_has_size(part)) {
            this->_size = fdisk_partition_get_size(part) * d.sector_size();
        } else {
            this->_size = 0;
        }
        char *name = fdisk_partname(d.node().c_str(),
                                    fdisk_partition_get_partno(part) + 1);
        this->_node = std::string(name);
        char *value;
        value = blkid_get_tag_value(nullptr, "TYPE", name);
        if(value != nullptr) {
            this->_fs_type = std::string(value);
            free(value);
        }
        value = blkid_get_tag_value(nullptr, "LABEL", name);
        if(value != nullptr) {
            this->_label = std::string(value);
            free(value);
        }
        free(name);
        break;
    }
    case 1: { /* udev */
        struct udev_device *dev = static_cast<struct udev_device *>(creation);
        const char *value;
        value = udev_device_get_property_value(dev, "ID_FS_TYPE");
        if(value != nullptr) this->_fs_type = std::string(value);
        value = udev_device_get_property_value(dev, "ID_FS_LABEL");
        if(value != nullptr) this->_label = std::string(value);
        value = udev_device_get_property_value(dev, "ID_PART_ENTRY_SIZE");
        if(value != nullptr) this->_size = strtoull(value, nullptr, 10) * 512;
        value = udev_device_get_property_value(dev, "DEVNAME");
        if(value != nullptr) this->_node = std::string(value);
        break;
    }
    default:
        throw std::invalid_argument{ "invalid type code" };
    }
}

}
}