| 1 |
|
| 2 |
#include "fdiskP.h" |
| 3 |
#include "pathnames.h" |
| 4 |
#include "canonicalize.h" |
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
|
| 8 |
/** |
| 9 |
* SECTION: utils |
| 10 |
* @title: Utils |
| 11 |
* @short_description: misc fdisk functions |
| 12 |
*/ |
| 13 |
|
| 14 |
static int read_from_device(struct fdisk_context *cxt, |
| 15 |
unsigned char *buf, |
| 16 |
uintmax_t start, size_t size) |
| 17 |
{ |
| 18 |
ssize_t r; |
| 19 |
|
| 20 |
assert(cxt); |
| 21 |
|
| 22 |
DBG(CXT, ul_debugobj(cxt, "reading: offset=%ju, size=%zu", |
| 23 |
start, size)); |
| 24 |
|
| 25 |
r = lseek(cxt->dev_fd, start, SEEK_SET); |
| 26 |
if (r == -1) |
| 27 |
{ |
| 28 |
DBG(CXT, ul_debugobj(cxt, "failed to seek to offset %ju: %m", start)); |
| 29 |
return -errno; |
| 30 |
} |
| 31 |
|
| 32 |
errno = 0; |
| 33 |
r = read(cxt->dev_fd, buf, size); |
| 34 |
if (r < 0 || (size_t)r != size) { |
| 35 |
if (!errno) |
| 36 |
errno = EINVAL; /* probably too small file/device */ |
| 37 |
DBG(CXT, ul_debugobj(cxt, "failed to read %zu from offset %ju: %m", |
| 38 |
size, start)); |
| 39 |
return -errno; |
| 40 |
} |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/* |
| 47 |
* Zeros in-memory first sector buffer |
| 48 |
*/ |
| 49 |
int fdisk_init_firstsector_buffer(struct fdisk_context *cxt, |
| 50 |
unsigned int protect_off, |
| 51 |
unsigned int protect_size) |
| 52 |
{ |
| 53 |
if (!cxt) |
| 54 |
return -EINVAL; |
| 55 |
|
| 56 |
assert(protect_off + protect_size <= cxt->sector_size); |
| 57 |
|
| 58 |
if (!cxt->firstsector || cxt->firstsector_bufsz != cxt->sector_size) { |
| 59 |
/* Let's allocate a new buffer if no allocated yet, or the |
| 60 |
* current buffer has incorrect size */ |
| 61 |
if (!cxt->parent || cxt->parent->firstsector != cxt->firstsector) |
| 62 |
free(cxt->firstsector); |
| 63 |
|
| 64 |
DBG(CXT, ul_debugobj(cxt, "initialize in-memory first sector " |
| 65 |
"buffer [sector_size=%lu]", cxt->sector_size)); |
| 66 |
cxt->firstsector = calloc(1, cxt->sector_size); |
| 67 |
if (!cxt->firstsector) |
| 68 |
return -ENOMEM; |
| 69 |
|
| 70 |
cxt->firstsector_bufsz = cxt->sector_size; |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
DBG(CXT, ul_debugobj(cxt, "zeroize in-memory first sector buffer")); |
| 75 |
memset(cxt->firstsector, 0, cxt->firstsector_bufsz); |
| 76 |
|
| 77 |
if (protect_size) { |
| 78 |
/* |
| 79 |
* It would be possible to reuse data from cxt->firstsector |
| 80 |
* (call memset() for non-protected area only) and avoid one |
| 81 |
* read() from the device, but it seems like a too fragile |
| 82 |
* solution as we have no clue about stuff in the buffer -- |
| 83 |
* maybe it was already modified. Let's re-read from the device |
| 84 |
* to be sure. -- kzak 13-Apr-2015 |
| 85 |
*/ |
| 86 |
DBG(CXT, ul_debugobj(cxt, "first sector protection enabled -- re-reading")); |
| 87 |
read_from_device(cxt, cxt->firstsector, protect_off, protect_size); |
| 88 |
} |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
int fdisk_read_firstsector(struct fdisk_context *cxt) |
| 93 |
{ |
| 94 |
int rc; |
| 95 |
|
| 96 |
assert(cxt); |
| 97 |
assert(cxt->sector_size); |
| 98 |
|
| 99 |
rc = fdisk_init_firstsector_buffer(cxt, 0, 0); |
| 100 |
if (rc) |
| 101 |
return rc; |
| 102 |
|
| 103 |
assert(cxt->sector_size == cxt->firstsector_bufsz); |
| 104 |
|
| 105 |
|
| 106 |
return read_from_device(cxt, cxt->firstsector, 0, cxt->sector_size); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* fdisk_partname: |
| 111 |
* @dev: device name |
| 112 |
* @partno: partition name |
| 113 |
* |
| 114 |
* Return: allocated buffer with partition name, use free() to deallocate. |
| 115 |
*/ |
| 116 |
char *fdisk_partname(const char *dev, size_t partno) |
| 117 |
{ |
| 118 |
char *res = NULL; |
| 119 |
const char *p = ""; |
| 120 |
char *dev_mapped = NULL; |
| 121 |
int w = 0; |
| 122 |
|
| 123 |
if (!dev || !*dev) { |
| 124 |
if (asprintf(&res, "%zd", partno) > 0) |
| 125 |
return res; |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
|
| 129 |
/* It is impossible to predict /dev/dm-N partition names. */ |
| 130 |
if (strncmp(dev, "/dev/dm-", sizeof("/dev/dm-") - 1) == 0) { |
| 131 |
dev_mapped = ul_canonicalize_dm_name (dev + 5); |
| 132 |
if (dev_mapped) |
| 133 |
dev = dev_mapped; |
| 134 |
} |
| 135 |
|
| 136 |
w = strlen(dev); |
| 137 |
if (isdigit(dev[w - 1])) |
| 138 |
#ifdef __GNU__ |
| 139 |
p = "s"; |
| 140 |
#else |
| 141 |
p = "p"; |
| 142 |
#endif |
| 143 |
|
| 144 |
/* devfs kludge - note: fdisk partition names are not supposed |
| 145 |
to equal kernel names, so there is no reason to do this */ |
| 146 |
if (ul_endswith(dev, "disc")) { |
| 147 |
w -= 4; |
| 148 |
p = "part"; |
| 149 |
} |
| 150 |
|
| 151 |
/* udev names partitions by appending -partN |
| 152 |
e.g. ata-SAMSUNG_SV8004H_0357J1FT712448-part1 |
| 153 |
multipath-tools kpartx.rules also append -partN */ |
| 154 |
if ((strncmp(dev, _PATH_DEV_BYID, sizeof(_PATH_DEV_BYID) - 1) == 0) || |
| 155 |
strncmp(dev, _PATH_DEV_BYPATH, sizeof(_PATH_DEV_BYPATH) - 1) == 0 || |
| 156 |
strncmp(dev, "/dev/mapper", sizeof("/dev/mapper") - 1) == 0) { |
| 157 |
|
| 158 |
/* check for <name><partno>, e.g. mpatha1 */ |
| 159 |
if (asprintf(&res, "%.*s%zu", w, dev, partno) <= 0) |
| 160 |
res = NULL; |
| 161 |
if (res && access(res, F_OK) == 0) |
| 162 |
goto done; |
| 163 |
|
| 164 |
free(res); |
| 165 |
|
| 166 |
/* check for partition separator "p" */ |
| 167 |
if (asprintf(&res, "%.*sp%zu", w, dev, partno) <= 0) |
| 168 |
res = NULL; |
| 169 |
if (res && access(res, F_OK) == 0) |
| 170 |
goto done; |
| 171 |
|
| 172 |
free(res); |
| 173 |
|
| 174 |
/* otherwise, default to "-path" */ |
| 175 |
p = "-part"; |
| 176 |
} |
| 177 |
|
| 178 |
if (asprintf(&res, "%.*s%s%zu", w, dev, p, partno) <= 0) |
| 179 |
res = NULL; |
| 180 |
done: |
| 181 |
free(dev_mapped); |
| 182 |
return res; |
| 183 |
} |
| 184 |
|
| 185 |
#ifdef TEST_PROGRAM |
| 186 |
struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt __attribute__((unused))) { return NULL; } |
| 187 |
struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt __attribute__((unused))) { return NULL; } |
| 188 |
|
| 189 |
static int test_partnames(struct fdisk_test *ts __attribute__((unused)), |
| 190 |
int argc, char *argv[]) |
| 191 |
{ |
| 192 |
if (argc != 2) |
| 193 |
return -1; |
| 194 |
|
| 195 |
size_t i; |
| 196 |
const char *disk = argv[1]; |
| 197 |
|
| 198 |
for (i = 0; i < 5; i++) { |
| 199 |
char *p = fdisk_partname(disk, i + 1); |
| 200 |
if (p) |
| 201 |
printf("%zu: '%s'\n", i + 1, p); |
| 202 |
free(p); |
| 203 |
} |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
int main(int argc, char *argv[]) |
| 209 |
{ |
| 210 |
struct fdisk_test tss[] = { |
| 211 |
{ "--partnames", test_partnames, "<diskname>" }, |
| 212 |
{ NULL } |
| 213 |
}; |
| 214 |
|
| 215 |
return fdisk_run_test(tss, argc, argv); |
| 216 |
} |
| 217 |
|
| 218 |
#endif |