summaryrefslogtreecommitdiff
path: root/diskman/disk.cc
blob: 5fd526a268cd50c7a6daffed0dd4ff412c8898a9 (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
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
/*
 * disk.cc - Implementation of the Disk 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 "disk.hh"

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

namespace Horizon {
namespace DiskMan {

#define SAFE_SET(ivar, udev_call) \
    value = udev_call;\
    if(value != nullptr) {\
        ivar = std::string(value);\
    }

Disk::Disk(void *creation, int type, bool partition) {
    switch(type) {
    case 0: { /* udev */
        struct udev_device *device = static_cast<struct udev_device *>(creation);
        const char *value;

        SAFE_SET(_name, udev_device_get_sysname(device));
        SAFE_SET(_model, udev_device_get_property_value(device, "ID_MODEL"));
        SAFE_SET(_full_serial, udev_device_get_property_value(device, "ID_SERIAL"));
        SAFE_SET(_node, udev_device_get_devnode(device));
        SAFE_SET(_devpath, udev_device_get_devpath(device));

        value = udev_device_get_property_value(device, "ID_PART_TABLE_TYPE");
        if(value == nullptr) {
            _has_label = false;
            _label = Unknown;
        } else {
            _has_label = true;
            if(::strcmp(value, "apm") == 0) {
                _label = APM;
            } else if(::strcmp(value, "dos") == 0) {
                _label = MBR;
            } else if(::strcmp(value, "gpt") == 0) {
                _label = GPT;
            } else {
                _label = Unknown;
            }
        }

        value = udev_device_get_property_value(device, "ID_FS_TYPE");
        if(value == nullptr) {
            _has_fs = false;
        } else {
            _has_fs = true;
            _fs_type = std::string(value);
            SAFE_SET(_fs_label, udev_device_get_property_value(device, "ID_FS_LABEL"));
        }
        break;
    }
    default:
        throw new std::invalid_argument{ "invalid type code" };
    }

    total_mb = free_mb = contiguous_mb = 0;

    struct fdisk_context *ctxt = fdisk_new_context();
    if(ctxt != nullptr) {
        /* Open the device in read-only mode.  We don't need to write to it */
        if(fdisk_assign_device(ctxt, _node.c_str(), 1) == 0) {
            unsigned long ssize = fdisk_get_sector_size(ctxt);
            total_mb = (fdisk_get_nsectors(ctxt) * ssize) / 1048576;
            struct fdisk_table *frees = nullptr;
            if(fdisk_has_label(ctxt) != 1) {
                /* Disk has no label, so consider it empty */
                free_mb = contiguous_mb = total_mb;
            } else if(fdisk_get_freespaces(ctxt, &frees) == 0) {
                for(size_t next = 0; next < fdisk_table_get_nents(frees);
                    next++) {
                    /* Each entry in frees is a "free space partition". */
                    struct fdisk_partition *part =
                            fdisk_table_get_partition(frees, next);
                    fdisk_sector_t size;
                    if(!fdisk_partition_has_size(part)) continue;
                    size = (fdisk_partition_get_size(part) * ssize) / 1048576;
                    free_mb += size;
                    if(size > contiguous_mb) contiguous_mb = size;
                }
                fdisk_unref_table(frees);
            }
            /* We used to free ctxt here, but it's useful for partition probing */
        } else {
            fdisk_unref_context(ctxt);
            ctxt = nullptr;
        }
    }

    if(partition) {
        if(reload_partitions()) {
            /* We're good */
        } else if(type == 0) {
            /* fallback to udev, if available */
            std::cerr << "Falling back to udev partition probing" << std::endl;

            struct udev_device *device = static_cast<struct udev_device *>(creation);
            struct udev *udev = udev_device_get_udev(device);
            struct udev_enumerate *part_enum = udev_enumerate_new(udev);
            if(part_enum != NULL) {
                struct udev_list_entry *first;

                udev_enumerate_add_match_subsystem(part_enum, "block");
                udev_enumerate_add_match_property(part_enum, "DEVTYPE",
                                                  "partition");
                udev_enumerate_add_match_parent(part_enum, device);
                udev_enumerate_scan_devices(part_enum);

                first = udev_enumerate_get_list_entry(part_enum);
                if(first != NULL) {
                    struct udev_list_entry *item;
                    udev_list_entry_foreach(item, first) {
                        const char *path = udev_list_entry_get_name(item);
                        struct udev_device *part_device = udev_device_new_from_syspath(udev, path);
                        if(part_device != nullptr) {
                            _partitions.push_back(Partition(*this, part_device, 1));
                            udev_device_unref(part_device);
                        }
                    }
                }
                udev_enumerate_unref(part_enum);
            }
        } else {
            std::cerr << "Cannot load partitions for " << _name << std::endl;
        }
    }

    if(ctxt != nullptr) {
        fdisk_unref_context(ctxt);
    }
}

bool Disk::reload_partitions() {
    bool success = false;
    struct fdisk_context *ctxt = fdisk_new_context();
    struct fdisk_table *parts = nullptr;

    if(ctxt == nullptr) {
        return false;
    }

    /* Open the device in read-only mode.  We don't need to write to it */
    if(fdisk_assign_device(ctxt, _node.c_str(), 1) != 0) {
        goto destroy_context;
    }

    if(fdisk_get_partitions(ctxt, &parts) == 0) {
        _partitions.clear();
        for(size_t next = 0; next < fdisk_table_get_nents(parts);
            next++) {
            struct fdisk_partition *part =
                    fdisk_table_get_partition(parts, next);
            _partitions.push_back(Partition(*this, part, 0));
        }
        success = true;
        fdisk_unref_table(parts);
    }

destroy_context:
    fdisk_unref_context(ctxt);
    return success;
}

const std::vector<Partition> Disk::partitions() const {
    if(!this->has_label()) {
        throw std::logic_error{ "attempt to retrieve partitions for non-labelled disk" };
    }

    return this->_partitions;
}

}
}