Debian Codegraph @

libfdisk/src/parttype.c

raw
1
2 #include <ctype.h>
3
4 #include "fdiskP.h"
5 #include "cctype.h"
6 #include "strutils.h"
7
8 /**
9 * SECTION: parttype
10 * @title: Partition types
11 * @short_description: abstraction to partition types
12 *
13 * There are two basic types of parttypes, string based (e.g. GPT)
14 * and code/hex based (e.g. MBR).
15 */
16
17 /**
18 * fdisk_new_parttype:
19 *
20 * It's recommended to use fdisk_label_get_parttype_from_code() or
21 * fdisk_label_get_parttype_from_string() for well known types rather
22 * than allocate a new instance.
23 *
24 * Returns: new instance.
25 */
26 struct fdisk_parttype *fdisk_new_parttype(void)
27 {
28 struct fdisk_parttype *t = calloc(1, sizeof(*t));
29
30 if (!t)
31 return NULL;
32
33 t->refcount = 1;
34 t->flags = FDISK_PARTTYPE_ALLOCATED;
35 DBG(PARTTYPE, ul_debugobj(t, "alloc"));
36 return t;
37 }
38
39 /**
40 * fdisk_ref_parttype:
41 * @t: partition type
42 *
43 * Increments reference counter for allocated types
44 */
45 void fdisk_ref_parttype(struct fdisk_parttype *t)
46 {
47 if (fdisk_parttype_is_allocated(t))
48 t->refcount++;
49 }
50
51 /**
52 * fdisk_unref_parttype
53 * @t: partition pointer
54 *
55 * Decrements reference counter, on zero the @t is automatically
56 * deallocated.
57 */
58 void fdisk_unref_parttype(struct fdisk_parttype *t)
59 {
60 if (!fdisk_parttype_is_allocated(t))
61 return;
62
63 t->refcount--;
64 if (t->refcount <= 0) {
65 DBG(PARTTYPE, ul_debugobj(t, "free"));
66 free(t->typestr);
67 free(t->name);
68 free(t);
69 }
70 }
71
72 /**
73 * fdisk_parttype_set_name:
74 * @t: partition type
75 * @str: type name
76 *
77 * Sets type name to allocated partition type, for static types
78 * it returns -EINVAL.
79 *
80 * Return: 0 on success, <0 on error
81 */
82 int fdisk_parttype_set_name(struct fdisk_parttype *t, const char *str)
83 {
84 if (!t || !fdisk_parttype_is_allocated(t))
85 return -EINVAL;
86 return strdup_to_struct_member(t, name, str);
87 }
88
89 /**
90 * fdisk_parttype_set_typestr:
91 * @t: partition type
92 * @str: type identifier (e.g. GUID for GPT)
93 *
94 * Sets type string to allocated partition type, for static types
95 * it returns -EINVAL. Don't use this function for MBR, see
96 * fdisk_parttype_set_code().
97 *
98 * Return: 0 on success, <0 on error
99 */
100 int fdisk_parttype_set_typestr(struct fdisk_parttype *t, const char *str)
101 {
102 if (!t || !fdisk_parttype_is_allocated(t))
103 return -EINVAL;
104 return strdup_to_struct_member(t, typestr, str);
105 }
106
107 /**
108 * fdisk_parttype_set_code:
109 * @t: partition type
110 * @code: type identifier (e.g. MBR type codes)
111 *
112 * Sets type code to allocated partition type, for static types it returns
113 * -EINVAL. Don't use this function for GPT, see fdisk_parttype_set_typestr().
114 *
115 * Return: 0 on success, <0 on error
116 */
117 int fdisk_parttype_set_code(struct fdisk_parttype *t, int code)
118 {
119 if (!t || !fdisk_parttype_is_allocated(t))
120 return -EINVAL;
121 t->code = code;
122 return 0;
123 }
124
125 /**
126 * fdisk_label_get_nparttypes:
127 * @lb: label
128 *
129 * Returns: number of types supported by label.
130 */
131 size_t fdisk_label_get_nparttypes(const struct fdisk_label *lb)
132 {
133 if (!lb)
134 return 0;
135 return lb->nparttypes;
136 }
137
138 /**
139 * fdisk_label_get_parttype:
140 * @lb: label
141 * @n: number
142 *
143 * Returns: return parttype
144 */
145 struct fdisk_parttype *fdisk_label_get_parttype(const struct fdisk_label *lb, size_t n)
146 {
147 if (!lb || n >= lb->nparttypes)
148 return NULL;
149 return (struct fdisk_parttype *)&lb->parttypes[n];
150 }
151
152 /**
153 * fdisk_label_get_parttype_shortcut:
154 * @lb: label
155 * @n: number
156 * @typestr: returns type as string
157 * @shortcut: returns type shortcut string
158 * @alias: returns type alias string
159 *
160 * Returns: return 0 on success, <0 on error, 2 for deprecated alias, 1 for @n out of range
161 *
162 * Since: 2.36
163 */
164 int fdisk_label_get_parttype_shortcut(const struct fdisk_label *lb, size_t n,
165 const char **typestr, const char **shortcut, const char **alias)
166 {
167 const struct fdisk_shortcut *sc;
168
169 if (!lb)
170 return -EINVAL;
171 if (n >= lb->nparttype_cuts)
172 return 1;
173
174 sc = &lb->parttype_cuts[n];
175 if (typestr)
176 *typestr = sc->data;
177 if (shortcut)
178 *shortcut = sc->shortcut;
179 if (alias)
180 *alias = sc->alias;
181
182 return sc->deprecated == 1 ? 2 : 0;
183
184 }
185
186
187 /**
188 * fdisk_label_has_code_parttypes:
189 * @lb: label
190 *
191 * Returns: 1 if the label uses code as partition type
192 * identifiers (e.g. MBR) or 0.
193 */
194 int fdisk_label_has_code_parttypes(const struct fdisk_label *lb)
195 {
196 assert(lb);
197
198 if (lb->parttypes && lb->parttypes[0].typestr)
199 return 0;
200 return 1;
201 }
202
203 /**
204 * fdisk_label_has_parttypes_shortcuts
205 * @lb: label
206 *
207 * Returns: 1 if the label support shortuts/aliases for partition types or 0.
208 *
209 * Since: 2.36
210 */
211 int fdisk_label_has_parttypes_shortcuts(const struct fdisk_label *lb)
212 {
213 assert(lb);
214 return lb->nparttype_cuts ? 1 : 0;
215 }
216
217
218 /**
219 * fdisk_label_get_parttype_from_code:
220 * @lb: label
221 * @code: code to search for
222 *
223 * Search for partition type in label-specific table. The result
224 * is pointer to static array of label types.
225 *
226 * Returns: partition type or NULL upon failure or invalid @code.
227 */
228 struct fdisk_parttype *fdisk_label_get_parttype_from_code(
229 const struct fdisk_label *lb,
230 unsigned int code)
231 {
232 size_t i;
233
234 assert(lb);
235
236 if (!lb->nparttypes)
237 return NULL;
238
239 for (i = 0; i < lb->nparttypes; i++)
240 if (lb->parttypes[i].code == code)
241 return (struct fdisk_parttype *)&lb->parttypes[i];
242 return NULL;
243 }
244
245 /**
246 * fdisk_label_get_parttype_from_string:
247 * @lb: label
248 * @str: string to search for
249 *
250 * Search for partition type in label-specific table. The result
251 * is pointer to static array of label types.
252 *
253 * Returns: partition type or NULL upon failure or invalid @str.
254 */
255 struct fdisk_parttype *fdisk_label_get_parttype_from_string(
256 const struct fdisk_label *lb,
257 const char *str)
258 {
259 size_t i;
260
261 assert(lb);
262
263 if (!lb->nparttypes)
264 return NULL;
265
266 for (i = 0; i < lb->nparttypes; i++)
267 if (lb->parttypes[i].typestr
268 && c_strcasecmp(lb->parttypes[i].typestr, str) == 0)
269 return (struct fdisk_parttype *)&lb->parttypes[i];
270
271 return NULL;
272 }
273
274 /**
275 * fdisk_new_unknown_parttype:
276 * @code: type as number
277 * @typestr: type as string
278
279 * Allocates new 'unknown' partition type. Use fdisk_unref_parttype() to
280 * deallocate.
281 *
282 * Returns: newly allocated partition type, or NULL upon failure.
283 */
284 struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code,
285 const char *typestr)
286 {
287 struct fdisk_parttype *t = fdisk_new_parttype();
288
289 if (!t)
290 return NULL;
291
292 fdisk_parttype_set_name(t, _("unknown"));
293 fdisk_parttype_set_code(t, code);
294 fdisk_parttype_set_typestr(t, typestr);
295 t->flags |= FDISK_PARTTYPE_UNKNOWN;
296
297 return t;
298 }
299
300 /**
301 * fdisk_copy_parttype:
302 * @type: type to copy
303 *
304 * Use fdisk_unref_parttype() to deallocate.
305 *
306 * Returns: newly allocated partition type, or NULL upon failure.
307 */
308 struct fdisk_parttype *fdisk_copy_parttype(const struct fdisk_parttype *type)
309 {
310 struct fdisk_parttype *t = fdisk_new_parttype();
311
312 if (!t)
313 return NULL;
314
315 fdisk_parttype_set_name(t, type->name);
316 fdisk_parttype_set_code(t, type->code);
317 fdisk_parttype_set_typestr(t, type->typestr);
318
319 return t;
320 }
321
322 static struct fdisk_parttype *parttype_from_data(
323 const struct fdisk_label *lb,
324 const char *str,
325 unsigned int *xcode,
326 int use_seqnum)
327 {
328 const struct fdisk_parttype *types;
329 struct fdisk_parttype *ret = NULL;
330 char *end = NULL;
331
332 assert(lb);
333 assert(str);
334
335 if (xcode)
336 *xcode = 0;
337 if (!lb->nparttypes)
338 return NULL;
339
340 DBG(LABEL, ul_debugobj(lb, " parsing '%s' data", str));
341 types = lb->parttypes;
342
343 if (types[0].typestr == NULL) {
344 unsigned int code;
345
346 DBG(LABEL, ul_debugobj(lb, " +hex"));
347
348 errno = 0;
349 code = strtol(str, &end, 16);
350
351 if (errno || *end != '\0') {
352 DBG(LABEL, ul_debugobj(lb, " failed: %m"));
353 return NULL;
354 }
355 if (xcode)
356 *xcode = code;
357 ret = fdisk_label_get_parttype_from_code(lb, code);
358 } else {
359 DBG(LABEL, ul_debugobj(lb, " +string"));
360
361 /* maybe specified by type string (e.g. UUID) */
362 ret = fdisk_label_get_parttype_from_string(lb, str);
363
364 if (!ret) {
365 /* maybe specified by order number */
366 int i;
367
368 errno = 0;
369 i = strtol(str, &end, 0);
370
371 if (use_seqnum && errno == 0
372 && *end == '\0' && i > 0
373 && i - 1 < (int) lb->nparttypes)
374 ret = (struct fdisk_parttype *)&types[i - 1];
375 }
376 }
377
378 if (ret)
379 DBG(PARTTYPE, ul_debugobj(ret, " result '%s'", ret->name));
380 return ret;
381 }
382
383 static struct fdisk_parttype *parttype_from_shortcut(
384 const struct fdisk_label *lb,
385 const char *str, int deprecated)
386 {
387 size_t i;
388
389 DBG(LABEL, ul_debugobj(lb, " parsing '%s' shortcut", str));
390
391 for (i = 0; i < lb->nparttype_cuts; i++) {
392 const struct fdisk_shortcut *sc = &lb->parttype_cuts[i];
393
394 if (sc->deprecated && !deprecated)
395 continue;
396 if (sc->shortcut && strcmp(sc->shortcut, str) == 0)
397 return parttype_from_data(lb, sc->data, NULL, 0);
398 }
399 return NULL;
400 }
401
402 static struct fdisk_parttype *parttype_from_alias(
403 const struct fdisk_label *lb,
404 const char *str, int deprecated)
405 {
406 size_t i;
407
408 DBG(LABEL, ul_debugobj(lb, " parsing '%s' alias", str));
409
410 for (i = 0; i < lb->nparttype_cuts; i++) {
411 const struct fdisk_shortcut *sc = &lb->parttype_cuts[i];
412
413 if (sc->deprecated && !deprecated)
414 continue;
415 if (sc->alias && strcmp(sc->alias, str) == 0)
416 return parttype_from_data(lb, sc->data, NULL, 0);
417 }
418 return NULL;
419 }
420
421 static struct fdisk_parttype *parttype_from_name(
422 const struct fdisk_label *lb,
423 const char *str)
424 {
425 size_t i;
426
427 DBG(LABEL, ul_debugobj(lb, " parsing '%s' name", str));
428
429 for (i = 0; i < lb->nparttypes; i++) {
430 const char *name = lb->parttypes[i].name;
431
432 if (name && *name && ul_stralnumcmp(name, str) == 0)
433 return (struct fdisk_parttype *)&lb->parttypes[i];
434 }
435
436 return NULL;
437 }
438
439 /**
440 * fdisk_label_advparse_parttype:
441 * @lb: label
442 * @str: string to parse from
443 * @flags: FDISK_PARTTYPE_PARSE_*
444 *
445 * This function is advanced partition types parser. It parses partition type
446 * from @str according to the label. The function returns a pointer to static
447 * table of the partition types, or newly allocated partition type for unknown
448 * types (see fdisk_parttype_is_unknown(). It's safe to call fdisk_unref_parttype()
449 * for all results.
450 *
451 * The @str may be type data (hex code or UUID), alias or shortcut. For GPT
452 * also sequence number of the type in the list of the supported types.
453 *
454 * Returns: pointer to type or NULL on error.
455 */
456 struct fdisk_parttype *fdisk_label_advparse_parttype(
457 const struct fdisk_label *lb,
458 const char *str,
459 int flags)
460 {
461 struct fdisk_parttype *res = NULL;
462 unsigned int code = 0;
463
464 if (!lb || !lb->nparttypes)
465 return NULL;
466
467 DBG(LABEL, ul_debugobj(lb, "parsing '%s' (%s) type", str, lb->name));
468
469 if ((flags & FDISK_PARTTYPE_PARSE_DATA)
470 && !(flags & FDISK_PARTTYPE_PARSE_DATALAST))
471 res = parttype_from_data(lb, str, &code,
472 flags & FDISK_PARTTYPE_PARSE_SEQNUM);
473
474 if (!res && (flags & FDISK_PARTTYPE_PARSE_ALIAS))
475 res = parttype_from_alias(lb, str,
476 flags & FDISK_PARTTYPE_PARSE_DEPRECATED);
477
478 if (!res && (flags & FDISK_PARTTYPE_PARSE_SHORTCUT))
479 res = parttype_from_shortcut(lb, str,
480 flags & FDISK_PARTTYPE_PARSE_DEPRECATED);
481
482 if (!res && (flags & FDISK_PARTTYPE_PARSE_NAME))
483 res = parttype_from_name(lb, str);
484
485 if (!res && (flags & FDISK_PARTTYPE_PARSE_DATA)
486 && (flags & FDISK_PARTTYPE_PARSE_DATALAST))
487 res = parttype_from_data(lb, str, &code,
488 flags & FDISK_PARTTYPE_PARSE_SEQNUM);
489
490 if (!res && !(flags & FDISK_PARTTYPE_PARSE_NOUNKNOWN)) {
491 if (lb->parttypes[0].typestr)
492 res = fdisk_new_unknown_parttype(0, str);
493 else
494 res = fdisk_new_unknown_parttype(code, NULL);
495 }
496
497 if (res)
498 DBG(PARTTYPE, ul_debugobj(res, "returns parsed '%s' [%s] partition type",
499 res->name, res->typestr ? : ""));
500 return res;
501 }
502
503 /**
504 * fdisk_label_parse_parttype:
505 * @lb: label
506 * @str: string to parse from (type name, UUID, etc.)
507 *
508 * Parses partition type from @str according to the label. The function returns
509 * a pointer to static table of the partition types, or newly allocated
510 * partition type for unknown types (see fdisk_parttype_is_unknown(). It's
511 * safe to call fdisk_unref_parttype() for all results.
512 *
513 * Note that for GPT it accepts sequence number of UUID.
514 *
515 * Returns: pointer to type or NULL on error.
516 */
517 struct fdisk_parttype *fdisk_label_parse_parttype(
518 const struct fdisk_label *lb,
519 const char *str)
520 {
521 return fdisk_label_advparse_parttype(lb, str, FDISK_PARTTYPE_PARSE_DATA);
522 }
523
524 /**
525 * fdisk_parttype_get_string:
526 * @t: type
527 *
528 * Returns: partition type string (e.g. GUID for GPT)
529 */
530 const char *fdisk_parttype_get_string(const struct fdisk_parttype *t)
531 {
532 assert(t);
533 return t->typestr && *t->typestr ? t->typestr : NULL;
534 }
535
536 /**
537 * fdisk_parttype_get_code:
538 * @t: type
539 *
540 * Returns: partition type code (e.g. for MBR)
541 */
542 unsigned int fdisk_parttype_get_code(const struct fdisk_parttype *t)
543 {
544 assert(t);
545 return t->code;
546 }
547
548 /**
549 * fdisk_parttype_get_name:
550 * @t: type
551 *
552 * Returns: partition type human readable name
553 */
554 const char *fdisk_parttype_get_name(const struct fdisk_parttype *t)
555 {
556 assert(t);
557 return t->name;
558 }
559
560 /**
561 * fdisk_parttype_is_unknown:
562 * @t: type
563 *
564 * Checks for example result from fdisk_label_parse_parttype().
565 *
566 * Returns: 1 is type is "unknown" or 0.
567 */
568 int fdisk_parttype_is_unknown(const struct fdisk_parttype *t)
569 {
570 return t && (t->flags & FDISK_PARTTYPE_UNKNOWN) ? 1 : 0;
571 }