]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libfdisk/src/parttype.c
712994efb2b35d73a7b938ca3bb06c7184d03249
9 * @title: Partition types
10 * @short_description: abstraction to partition types
12 * There are two basic types of parttypes, string based (e.g. GPT)
13 * and code/hex based (e.g. MBR).
19 * It's recommended to use fdisk_label_get_parttype_from_code() or
20 * fdisk_label_get_parttype_from_string() for well known types rather
21 * than allocate a new instance.
23 * Returns: new instance.
25 struct fdisk_parttype
*fdisk_new_parttype(void)
27 struct fdisk_parttype
*t
= calloc(1, sizeof(*t
));
30 t
->flags
= FDISK_PARTTYPE_ALLOCATED
;
31 DBG(PARTTYPE
, ul_debugobj(t
, "alloc"));
39 * Incremparts reference counter for allocated types
41 void fdisk_ref_parttype(struct fdisk_parttype
*t
)
43 if (fdisk_parttype_is_allocated(t
))
48 * fdisk_unref_parttype
49 * @t: partition pointer
51 * De-incremparts reference counter, on zero the @t is automatically
54 void fdisk_unref_parttype(struct fdisk_parttype
*t
)
56 if (!fdisk_parttype_is_allocated(t
))
60 if (t
->refcount
<= 0) {
61 DBG(PARTTYPE
, ul_debugobj(t
, "free"));
69 * fdisk_parttype_set_name:
73 * Sets type name to allocated partition type, for static types
76 * Return: 0 on success, <0 on error
78 int fdisk_parttype_set_name(struct fdisk_parttype
*t
, const char *str
)
82 if (!t
|| !fdisk_parttype_is_allocated(t
))
96 * fdisk_parttype_set_typestr:
98 * @str: type identificator (e.g. GUID for GPT)
100 * Sets type string to allocated partition type, for static types
101 * it returns -EINVAL. Don't use this function for MBR, see
102 * fdisk_parttype_set_code().
104 * Return: 0 on success, <0 on error
106 int fdisk_parttype_set_typestr(struct fdisk_parttype
*t
, const char *str
)
110 if (!t
|| !fdisk_parttype_is_allocated(t
))
124 * fdisk_parttype_set_code:
126 * @code: type identificator (e.g. MBR type codes)
128 * Sets type code to allocated partition type, for static types it returns
129 * -EINVAL. Don't use this function for GPT, see fdisk_parttype_set_typestr().
131 * Return: 0 on success, <0 on error
133 int fdisk_parttype_set_code(struct fdisk_parttype
*t
, int code
)
135 if (!t
|| !fdisk_parttype_is_allocated(t
))
142 * fdisk_label_get_nparttypes:
145 * Returns: number of types supported by label.
147 size_t fdisk_label_get_nparttypes(const struct fdisk_label
*lb
)
151 return lb
->nparttypes
;
155 * fdisk_label_get_parttype:
159 * Returns: return parttype
161 struct fdisk_parttype
*fdisk_label_get_parttype(const struct fdisk_label
*lb
, size_t n
)
163 if (!lb
|| n
>= lb
->nparttypes
)
165 return &lb
->parttypes
[n
];
169 * fdisk_label_has_code_parttypes:
172 * Returns: 1 if the label uses code as partition type
173 * identifiers (e.g. MBR) or 0.
175 int fdisk_label_has_code_parttypes(const struct fdisk_label
*lb
)
179 if (lb
->parttypes
&& lb
->parttypes
[0].typestr
)
186 * fdisk_label_get_parttype_from_code:
188 * @code: code to search for
190 * Search for partition type in label-specific table. The result
191 * is pointer to static array of label types.
193 * Returns: partition type or NULL upon failure or invalid @code.
195 struct fdisk_parttype
*fdisk_label_get_parttype_from_code(
196 const struct fdisk_label
*lb
,
206 for (i
= 0; i
< lb
->nparttypes
; i
++)
207 if (lb
->parttypes
[i
].code
== code
)
208 return &lb
->parttypes
[i
];
213 * fdisk_label_get_parttype_from_string:
215 * @str: string to search for
217 * Search for partition type in label-specific table. The result
218 * is pointer to static array of label types.
220 * Returns: partition type or NULL upon failure or invalid @str.
222 struct fdisk_parttype
*fdisk_label_get_parttype_from_string(
223 const struct fdisk_label
*lb
,
233 for (i
= 0; i
< lb
->nparttypes
; i
++)
234 if (lb
->parttypes
[i
].typestr
235 && strcasecmp(lb
->parttypes
[i
].typestr
, str
) == 0)
236 return &lb
->parttypes
[i
];
242 * fdisk_new_unknown_parttype:
243 * @code: type as number
244 * @typestr: type as string
246 * Allocates new 'unknown' partition type. Use fdisk_unref_parttype() to
249 * Returns: newly allocated partition type, or NULL upon failure.
251 struct fdisk_parttype
*fdisk_new_unknown_parttype(unsigned int code
,
254 struct fdisk_parttype
*t
= fdisk_new_parttype();
259 fdisk_parttype_set_name(t
, _("unknown"));
260 fdisk_parttype_set_code(t
, code
);
261 fdisk_parttype_set_typestr(t
, typestr
);
262 t
->flags
|= FDISK_PARTTYPE_UNKNOWN
;
268 * fdisk_copy_parttype:
269 * @type: type to copy
271 * Use fdisk_unref_parttype() to deallocate.
273 * Returns: newly allocated partition type, or NULL upon failure.
275 struct fdisk_parttype
*fdisk_copy_parttype(const struct fdisk_parttype
*type
)
277 struct fdisk_parttype
*t
= fdisk_new_parttype();
282 fdisk_parttype_set_name(t
, type
->name
);
283 fdisk_parttype_set_code(t
, type
->code
);
284 fdisk_parttype_set_typestr(t
, type
->typestr
);
290 * fdisk_label_parse_parttype:
292 * @str: string to parse from
294 * Parses partition type from @str according to the label. Thefunction returns
295 * a pointer to static table of the partition types, or newly allocated
296 * partition type for unknown types (see fdisk_parttype_is_unknown(). It's
297 * safe to call fdisk_unref_parttype() for all results.
299 * Returns: pointer to type or NULL on error.
301 struct fdisk_parttype
*fdisk_label_parse_parttype(
302 const struct fdisk_label
*lb
,
305 struct fdisk_parttype
*types
, *ret
= NULL
;
313 DBG(LABEL
, ul_debugobj((void *) lb
, "parsing '%s' (%s) partition type",
315 types
= lb
->parttypes
;
317 if (types
[0].typestr
== NULL
&& isxdigit(*str
)) {
318 unsigned int code
= 0;
321 code
= strtol(str
, &end
, 16);
323 if (errno
|| *end
!= '\0') {
324 DBG(LABEL
, ul_debugobj((void *) lb
, "parsing failed: %m"));
327 ret
= fdisk_label_get_parttype_from_code(lb
, code
);
331 ret
= fdisk_new_unknown_parttype(code
, NULL
);
335 /* maybe specified by type string (e.g. UUID) */
336 ret
= fdisk_label_get_parttype_from_string(lb
, str
);
340 /* maybe specified by order number */
342 i
= strtol(str
, &end
, 0);
343 if (errno
== 0 && *end
== '\0' && i
> 0
344 && i
- 1 < (int) lb
->nparttypes
) {
349 ret
= fdisk_new_unknown_parttype(0, str
);
353 DBG(PARTTYPE
, ul_debugobj(ret
, "returns parsed '%s' [%s] partition type",
354 ret
->name
, ret
->typestr
? : ""));
359 * fdisk_parttype_get_string:
362 * Returns: partition type string (e.g. GUID for GPT)
364 const char *fdisk_parttype_get_string(const struct fdisk_parttype
*t
)
367 return t
->typestr
&& *t
->typestr
? t
->typestr
: NULL
;
371 * fdisk_parttype_get_code:
374 * Returns: partition type code (e.g. for MBR)
376 unsigned int fdisk_parttype_get_code(const struct fdisk_parttype
*t
)
383 * fdisk_parttype_get_name:
386 * Returns: partition type human readable name
388 const char *fdisk_parttype_get_name(const struct fdisk_parttype
*t
)
395 * fdisk_parttype_is_unknown:
398 * Checks for example result from fdisk_label_parse_parttype().
400 * Returns: 1 is type is "unknonw" or 0.
402 int fdisk_parttype_is_unknown(const struct fdisk_parttype
*t
)
404 return t
&& (t
->flags
& FDISK_PARTTYPE_UNKNOWN
) ? 1 : 0;