2 * Low-level libblkid probing API
4 * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
12 * @title: Low-level probing
13 * @short_description: low-level prober initialization
15 * The low-level probing routines always and directly read information from
16 * the selected (see blkid_probe_set_device()) device.
18 * The probing routines are grouped together into separate chains. Currently,
19 * the librray provides superblocks, partitions and topology chains.
21 * The probing routines is possible to filter (enable/disable) by type (e.g.
22 * fstype "vfat" or partype "gpt") or by usage flags (e.g. BLKID_USAGE_RAID).
23 * These filters are per-chain. Note that always when you touch the chain
24 * filter the current probing position is reseted and probing starts from
25 * scratch. It means that the chain filter should not be modified during
26 * probing, for example in loop where you call blkid_do_probe().
28 * For more details see the chain specific documentation.
30 * The low-level API provides two ways how access to probing results.
32 * 1. The NAME=value (tag) interface. This interface is older and returns all data
33 * as strings. This interface is generic for all chains.
35 * 2. The binary interfaces. These interfaces return data in the native formats.
36 * The interface is always specific to the probing chain.
41 * SECTION: lowprobe-tags
42 * @title: Low-level tags
43 * @short_description: generic NAME=value interface.
45 * The probing routines inside the chain are mutually exclusive by default --
46 * only few probing routines are marked as "tolerant". The "tolerant" probing
47 * routines are used for filesystem which can share the same device with any
48 * other filesystem. The blkid_do_safeprobe() checks for the "tolerant" flag.
50 * The SUPERBLOCKS chain is enabled by default. The all others chains is
51 * necessary to enable by blkid_probe_enable_'CHAINNAME'(). See chains specific
54 * The blkid_do_probe() function returns a result from only one probing
55 * routine, and the next call from the next probing routine. It means you need
56 * to call the function in loop, for example:
60 * while((blkid_do_probe(pr) == 0)
65 * The blkid_do_safeprobe() is the same as blkid_do_probe(), but returns only
66 * first probing result for every enabled chain. This function checks for
67 * ambivalent results (e.g. more "intolerant" filesystems superblocks on the
70 * The probing result is set of NAME=value pairs (the NAME is always unique).
79 #include <sys/types.h>
80 #ifdef HAVE_SYS_STAT_H
83 #ifdef HAVE_SYS_MKDEV_H
84 #include <sys/mkdev.h>
93 # ifdef HAVE_UUID_UUID_H
94 # include <uuid/uuid.h>
104 extern const struct blkid_chaindrv superblocks_drv
;
105 extern const struct blkid_chaindrv topology_drv
;
106 extern const struct blkid_chaindrv partitions_drv
;
109 * All supported chains
111 static const struct blkid_chaindrv
*chains_drvs
[] = {
112 [BLKID_CHAIN_SUBLKS
] = &superblocks_drv
,
113 [BLKID_CHAIN_TOPLGY
] = &topology_drv
,
114 [BLKID_CHAIN_PARTS
] = &partitions_drv
117 static void blkid_probe_reset_vals(blkid_probe pr
);
122 * Returns: a pointer to the newly allocated probe struct.
124 blkid_probe
blkid_new_probe(void)
130 pr
= calloc(1, sizeof(struct blkid_struct_probe
));
134 /* initialize chains */
135 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
136 pr
->chains
[i
].driver
= chains_drvs
[i
];
137 pr
->chains
[i
].flags
= chains_drvs
[i
]->dflt_flags
;
138 pr
->chains
[i
].enabled
= chains_drvs
[i
]->dflt_enabled
;
144 * blkid_new_probe_from_filename:
145 * @filename: device or regular file
147 * This function is same as call open(filename), blkid_new_probe() and
148 * blkid_probe_set_device(pr, fd, 0, 0).
150 * The @filename is closed by blkid_free_probe() or by the
151 * blkid_probe_set_device() call.
153 * Returns: a pointer to the newly allocated probe struct or NULL in case of
156 blkid_probe
blkid_new_probe_from_filename(const char *filename
)
159 blkid_probe pr
= NULL
;
164 fd
= open(filename
, O_RDONLY
);
168 pr
= blkid_new_probe();
172 if (blkid_probe_set_device(pr
, fd
, 0, 0))
175 pr
->flags
|= BLKID_PRIVATE_FD
;
180 blkid_free_probe(pr
);
188 * Deallocates the probe struct, buffers and all allocated
189 * data that are associated with this probing control struct.
191 void blkid_free_probe(blkid_probe pr
)
198 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
199 struct blkid_chain
*ch
= &pr
->chains
[i
];
201 if (ch
->driver
->free_data
)
202 ch
->driver
->free_data(pr
, ch
->data
);
208 if ((pr
->flags
& BLKID_PRIVATE_FD
) && pr
->fd
>= 0)
213 static void blkid_probe_reset_buffer(blkid_probe pr
)
215 DBG(DEBUG_LOWPROBE
, printf("reseting blkid probe buffer\n"));
217 memset(pr
->buf
, 0, pr
->buf_max
);
221 memset(pr
->sbbuf
, 0, BLKID_SB_BUFSIZ
);
227 * Removes chain values from probing result.
229 void blkid_probe_chain_reset_vals(blkid_probe pr
, struct blkid_chain
*chn
)
231 int nvals
= pr
->nvals
;
234 for (x
= 0, i
= 0; i
< pr
->nvals
; i
++) {
235 struct blkid_prval
*v
= &pr
->vals
[i
];
237 if (v
->chain
!= chn
&& x
== i
) {
241 if (v
->chain
== chn
) {
245 memcpy(&pr
->vals
[x
++], v
, sizeof(struct blkid_prval
));
250 static void blkid_probe_chain_reset_position(struct blkid_chain
*chn
)
257 * Copies chain values from probing result to @vals, the max size of @vals is
258 * @nvals and returns real number of values.
260 int blkid_probe_chain_copy_vals(blkid_probe pr
, struct blkid_chain
*chn
,
261 struct blkid_prval
*vals
, int nvals
)
265 for (x
= 0, i
= 0; i
< pr
->nvals
&& x
< nvals
; i
++) {
266 struct blkid_prval
*v
= &pr
->vals
[i
];
270 memcpy(&vals
[x
++], v
, sizeof(struct blkid_prval
));
276 * Appends values from @vals to the probing result
278 void blkid_probe_append_vals(blkid_probe pr
, struct blkid_prval
*vals
, int nvals
)
282 while (i
< nvals
&& pr
->nvals
< BLKID_NVALS
) {
283 memcpy(&pr
->vals
[pr
->nvals
++], &vals
[i
++],
284 sizeof(struct blkid_prval
));
288 static void blkid_probe_reset_vals(blkid_probe pr
)
290 memset(pr
->vals
, 0, sizeof(pr
->vals
));
294 struct blkid_chain
*blkid_probe_get_chain(blkid_probe pr
)
296 return pr
->cur_chain
;
299 void *blkid_probe_get_binary_data(blkid_probe pr
, struct blkid_chain
*chn
)
308 blkid_probe_chain_reset_position(chn
);
310 rc
= chn
->driver
->probe(pr
, chn
);
313 pr
->cur_chain
= NULL
;
314 blkid_probe_chain_reset_position(chn
);
320 printf("returning %s binary data\n", chn
->driver
->name
));
329 * Zeroize probing results and resets the current probing (this has impact to
330 * blkid_do_probe() only). This function does not touch probing filters and
331 * keeps assigned device.
333 void blkid_reset_probe(blkid_probe pr
)
340 blkid_probe_reset_buffer(pr
);
341 blkid_probe_reset_vals(pr
);
343 pr
->cur_chain
= NULL
;
345 for (i
= 0; i
< BLKID_NCHAINS
; i
++)
346 blkid_probe_chain_reset_position(&pr
->chains
[i
]);
350 static int blkid_probe_dump_filter(blkid_probe pr, int chain)
352 struct blkid_chain *chn;
355 if (!pr || chain < 0 || chain >= BLKID_NCHAINS)
358 chn = &pr->chains[chain];
363 for (i = 0; i < chn->driver->nidinfos; i++) {
364 const struct blkid_idinfo *id = chn->driver->idinfos[i];
366 DBG(DEBUG_LOWPROBE, printf("%d: %s: %s\n",
369 blkid_bmp_get_item(chn->fltr, i)
370 ? "disabled" : "enabled <--"));
377 * Returns properly initialized chain filter
379 unsigned long *blkid_probe_get_filter(blkid_probe pr
, int chain
, int create
)
381 struct blkid_chain
*chn
;
383 if (!pr
|| chain
< 0 || chain
>= BLKID_NCHAINS
)
386 chn
= &pr
->chains
[chain
];
388 /* always when you touch the chain filter all indexes are reseted and
389 * probing starts from scratch
391 blkid_probe_chain_reset_position(chn
);
392 pr
->cur_chain
= NULL
;
394 if (!chn
->driver
->has_fltr
|| (!chn
->fltr
&& !create
))
398 chn
->fltr
= calloc(1, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
400 memset(chn
->fltr
, 0, blkid_bmp_nbytes(chn
->driver
->nidinfos
));
402 /* blkid_probe_dump_filter(pr, chain); */
407 * Generic private functions for filter setting
409 int __blkid_probe_invert_filter(blkid_probe pr
, int chain
)
412 struct blkid_chain
*chn
;
415 fltr
= blkid_probe_get_filter(pr
, chain
, FALSE
);
419 chn
= &pr
->chains
[chain
];
421 for (i
= 0; i
< blkid_bmp_nwords(chn
->driver
->nidinfos
); i
++)
424 DBG(DEBUG_LOWPROBE
, printf("probing filter inverted\n"));
425 /* blkid_probe_dump_filter(pr, chain); */
429 int __blkid_probe_reset_filter(blkid_probe pr
, int chain
)
431 return blkid_probe_get_filter(pr
, chain
, FALSE
) ? 0 : -1;
434 int __blkid_probe_filter_types(blkid_probe pr
, int chain
, int flag
, char *names
[])
437 struct blkid_chain
*chn
;
440 fltr
= blkid_probe_get_filter(pr
, chain
, TRUE
);
444 chn
= &pr
->chains
[chain
];
446 for (i
= 0; i
< chn
->driver
->nidinfos
; i
++) {
448 const struct blkid_idinfo
*id
= chn
->driver
->idinfos
[i
];
451 for (n
= names
; *n
; n
++) {
452 if (!strcmp(id
->name
, *n
)) {
457 if (flag
& BLKID_FLTR_ONLYIN
) {
459 blkid_bmp_set_item(fltr
, i
);
460 } else if (flag
& BLKID_FLTR_NOTIN
) {
462 blkid_bmp_set_item(fltr
, i
);
467 printf("%s: a new probing type-filter initialized\n",
469 /* blkid_probe_dump_filter(pr, chain); */
473 static int blkid_probe_has_buffer(blkid_probe pr
,
474 blkid_loff_t off
, blkid_loff_t len
)
476 return pr
&& (off
+ len
<= pr
->sbbuf_len
||
477 (pr
->buf_off
< off
&& off
+ len
< pr
->buf_len
));
481 * Returns buffer from the begin (69kB) of the device.
483 static unsigned char *blkid_probe_get_sb_buffer(blkid_probe pr
,
484 blkid_loff_t off
, blkid_loff_t len
)
486 if (off
+ len
> BLKID_SB_BUFSIZ
)
489 pr
->sbbuf
= malloc(BLKID_SB_BUFSIZ
);
493 if (off
+ len
> pr
->sbbuf_len
) {
495 * The sbbuf is not completely in memory.
497 * We don't read whole BLKID_SB_BUFSIZ by one read(), it's too
498 * aggresive to small devices (floppies). We read necessary
499 * data to complete the current request (off + len) only.
503 blkid_loff_t have
= pr
->sbbuf_len
,
504 want
= off
+ len
- have
;
507 printf("\tsb-buffer read() off=%jd len=%jd\n", have
, want
));
509 if (lseek(pr
->fd
, pr
->off
+ have
, SEEK_SET
) < 0)
512 ret_read
= read(pr
->fd
, pr
->sbbuf
+ have
, want
);
515 pr
->sbbuf_len
= have
+ ret_read
;
517 if (off
+ len
> pr
->sbbuf_len
)
519 return pr
->sbbuf
+ off
;
523 * Returns pointer to the buffer on arbitrary offset on the device
525 unsigned char *blkid_probe_get_extra_buffer(blkid_probe pr
,
526 blkid_loff_t off
, blkid_loff_t len
)
528 unsigned char *newbuf
= NULL
;
530 if (off
+ len
<= BLKID_SB_BUFSIZ
&&
531 (!blkid_probe_is_tiny(pr
) || blkid_probe_has_buffer(pr
, off
, len
)))
533 * Don't use extra buffer for superblock data if
534 * - data are already in SB buffer
535 * - or the device is large and we needn't extra
536 * optimalization for tiny devices
538 return blkid_probe_get_sb_buffer(pr
, off
, len
);
540 if (len
> pr
->buf_max
) {
541 newbuf
= realloc(pr
->buf
, len
);
549 if (newbuf
|| off
< pr
->buf_off
||
550 off
+ len
> pr
->buf_off
+ pr
->buf_len
) {
553 if (blkid_llseek(pr
->fd
, pr
->off
+ off
, SEEK_SET
) < 0)
557 printf("\textra-buffer read: off=%jd len=%jd\n", off
, len
));
559 ret_read
= read(pr
->fd
, pr
->buf
, len
);
560 if (ret_read
!= (ssize_t
) len
)
565 return off
? pr
->buf
+ (off
- pr
->buf_off
) : pr
->buf
;
570 * @off: offset within probing area
571 * @len: size of requested buffer
573 * The probing area is between pr->off and pr->size. The @off = 0 is pr->off, the
574 * max @len is pr->size.
576 * Note that we have two offsets:
578 * 1/ general device offset (pr->off), that's useful for example when we
579 * probe a partition from whole disk image:
580 * blkid -O <partition_position> -S <size> disk.img
582 * 2/ buffer offset (the @off argument), that useful for offsets in
585 * That means never use lseek(fd, 0, SEEK_SET), the zero position is always
586 * pr->off, so lseek(fd, pr->off, SEEK_SET).
589 unsigned char *blkid_probe_get_buffer(blkid_probe pr
,
590 blkid_loff_t off
, blkid_loff_t len
)
592 if (off
< 0 || len
< 0) {
594 printf("unexpected offset or length of buffer requested\n"));
597 if (off
+ len
> pr
->size
)
599 if (off
+ len
<= BLKID_SB_BUFSIZ
)
600 return blkid_probe_get_sb_buffer(pr
, off
, len
);
601 return blkid_probe_get_extra_buffer(pr
, off
, len
);
605 * Small devices need a special care.
607 int blkid_probe_is_tiny(blkid_probe pr
)
609 return (pr
&& pr
->size
<= 1440 * 1024 && !S_ISCHR(pr
->mode
));
613 * blkid_probe_set_device:
615 * @fd: device file descriptor
616 * @off: begin of probing area
617 * @size: size of probing area (zero means whole device/file)
619 * Assigns the device to probe control struct, resets internal buffers and
620 * resets the current probing.
622 * Returns: -1 in case of failure, or 0 on success.
624 int blkid_probe_set_device(blkid_probe pr
, int fd
,
625 blkid_loff_t off
, blkid_loff_t size
)
630 blkid_reset_probe(pr
);
632 if ((pr
->flags
& BLKID_PRIVATE_FD
) && pr
->fd
>= 0)
635 pr
->flags
&= ~BLKID_PRIVATE_FD
;
651 pr
->mode
= sb
.st_mode
;
653 if (S_ISBLK(sb
.st_mode
))
654 blkdev_get_size(fd
, (unsigned long long *) &pr
->size
);
655 else if (S_ISCHR(sb
.st_mode
))
656 pr
->size
= 1; /* UBI devices are char... */
657 else if (S_ISREG(sb
.st_mode
))
658 pr
->size
= sb
.st_size
; /* regular file */
660 if (S_ISBLK(sb
.st_mode
) || S_ISCHR(sb
.st_mode
))
661 pr
->devno
= sb
.st_rdev
;
663 if (pr
->off
> pr
->size
)
666 /* The probing area cannot be larger than whole device, pr->off
667 * is offset within the device */
673 DBG(DEBUG_LOWPROBE
, printf("ready for low-probing, offset=%zd, size=%zd\n",
678 printf("failed to prepare a device for low-probing\n"));
683 int blkid_probe_get_dimension(blkid_probe pr
,
684 blkid_loff_t
*off
, blkid_loff_t
*size
)
694 int blkid_probe_set_dimension(blkid_probe pr
,
695 blkid_loff_t off
, blkid_loff_t size
)
700 DBG(DEBUG_LOWPROBE
, printf(
701 "changing probing area: size=%llu, off=%llu "
702 "-to-> size=%llu, off=%llu\n",
703 (unsigned long long) pr
->size
,
704 (unsigned long long) pr
->off
,
705 (unsigned long long) size
,
706 (unsigned long long) off
));
711 blkid_probe_reset_buffer(pr
);
720 * Calls probing functions in all enabled chains. The superblocks chain is
721 * enabled by default. The blkid_do_probe() stores result from only one
722 * probing function. It's necessary to call this routine in a loop to get
723 * results from all probing functions in all chains. The probing is reseted
724 * by blkid_reset_probe() or by filter functions.
726 * This is string-based NAME=value interface only.
729 * <title>basic case - use the first result only</title>
732 * if (blkid_do_probe(pr) == 0) {
733 * int nvals = blkid_probe_numof_values(pr);
734 * for (n = 0; n < nvals; n++) {
735 * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
736 * printf("%s = %s\n", name, data);
743 * <title>advanced case - probe for all signatures</title>
746 * while (blkid_do_probe(pr) == 0) {
747 * int nvals = blkid_probe_numof_values(pr);
753 * See also blkid_reset_probe().
755 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
757 int blkid_do_probe(blkid_probe pr
)
765 struct blkid_chain
*chn
= pr
->cur_chain
;
768 chn
= pr
->cur_chain
= &pr
->chains
[0];
770 /* we go to the next chain only when the previous probing
771 * result was nothing (rc == 1) and when the current chain is
772 * disabled or we are at end of the current chain (chain->idx +
775 else if (rc
== 1 && (chn
->enabled
== FALSE
||
776 chn
->idx
+ 1 == chn
->driver
->nidinfos
)) {
778 int idx
= chn
->driver
->id
+ 1;
780 if (idx
< BLKID_NCHAINS
)
781 chn
= pr
->cur_chain
= &pr
->chains
[idx
];
783 return 1; /* all chains already probed */
786 chn
->binary
= FALSE
; /* for sure... */
788 DBG(DEBUG_LOWPROBE
, printf("chain probe %s %s (idx=%d)\n",
790 chn
->enabled
? "ENABLED" : "DISABLED",
796 /* rc: -1 = error, 0 = success, 1 = no result */
797 rc
= chn
->driver
->probe(pr
, chn
);
805 * blkid_do_safeprobe:
808 * This function gathers probing results from all enabled chains and checks
809 * for ambivalent results (e.g. more filesystems on the device).
811 * This is string-based NAME=value interface only.
813 * Note about suberblocks chain -- the function does not check for filesystems
814 * when a RAID signature is detected. The function also does not check for
815 * collision between RAIDs. The first detected RAID is returned.
817 * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalen result is
818 * detected and -1 on case of error.
820 int blkid_do_safeprobe(blkid_probe pr
)
822 int i
, count
= 0, rc
= 0;
827 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
828 struct blkid_chain
*chn
;
830 chn
= pr
->cur_chain
= &pr
->chains
[i
];
831 chn
->binary
= FALSE
; /* for sure... */
833 DBG(DEBUG_LOWPROBE
, printf("chain safeprobe %s %s\n",
835 chn
->enabled
? "ENABLED" : "DISABLED"));
840 blkid_probe_chain_reset_position(chn
);
842 rc
= chn
->driver
->safeprobe(pr
, chn
);
844 blkid_probe_chain_reset_position(chn
);
846 /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */
848 goto done
; /* error */
850 count
++; /* success */
854 pr
->cur_chain
= NULL
;
857 return count
? 0 : 1;
861 * blkid_do_fullprobe:
864 * This function gathers probing results from all enabled chains. Same as
865 * blkid_so_safeprobe() but does not check for collision between probing
868 * This is string-based NAME=value interface only.
870 * Returns: 0 on success, 1 if nothing is detected or -1 on case of error.
872 int blkid_do_fullprobe(blkid_probe pr
)
874 int i
, count
= 0, rc
= 0;
879 for (i
= 0; i
< BLKID_NCHAINS
; i
++) {
881 struct blkid_chain
*chn
;
883 chn
= pr
->cur_chain
= &pr
->chains
[i
];
884 chn
->binary
= FALSE
; /* for sure... */
886 DBG(DEBUG_LOWPROBE
, printf("chain fullprobe %s: %s\n",
888 chn
->enabled
? "ENABLED" : "DISABLED"));
893 blkid_probe_chain_reset_position(chn
);
895 rc
= chn
->driver
->probe(pr
, chn
);
897 blkid_probe_chain_reset_position(chn
);
899 /* rc: -1 = error, 0 = success, 1 = no result */
901 goto done
; /* error */
903 count
++; /* success */
907 pr
->cur_chain
= NULL
;
910 return count
? 0 : 1;
913 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
914 unsigned char *blkid_probe_get_sector(blkid_probe pr
, unsigned int sector
)
916 return pr
? blkid_probe_get_buffer(pr
,
917 ((blkid_loff_t
) sector
) << 9, 0x200) : NULL
;
920 struct blkid_prval
*blkid_probe_assign_value(
921 blkid_probe pr
, const char *name
)
923 struct blkid_prval
*v
;
927 if (pr
->nvals
>= BLKID_NVALS
)
930 v
= &pr
->vals
[pr
->nvals
];
932 v
->chain
= pr
->cur_chain
;
936 printf("assigning %s [%s]\n", name
, v
->chain
->driver
->name
));
940 int blkid_probe_reset_last_value(blkid_probe pr
)
942 struct blkid_prval
*v
;
944 if (pr
== NULL
|| pr
->nvals
== 0)
947 v
= &pr
->vals
[pr
->nvals
- 1];
950 printf("un-assigning %s [%s]\n", v
->name
, v
->chain
->driver
->name
));
952 memset(v
, 0, sizeof(struct blkid_prval
));
959 int blkid_probe_set_value(blkid_probe pr
, const char *name
,
960 unsigned char *data
, size_t len
)
962 struct blkid_prval
*v
;
964 if (len
> BLKID_PROBVAL_BUFSIZ
)
965 len
= BLKID_PROBVAL_BUFSIZ
;
967 v
= blkid_probe_assign_value(pr
, name
);
971 memcpy(v
->data
, data
, len
);
976 int blkid_probe_vsprintf_value(blkid_probe pr
, const char *name
,
977 const char *fmt
, va_list ap
)
979 struct blkid_prval
*v
;
982 v
= blkid_probe_assign_value(pr
, name
);
986 len
= vsnprintf((char *) v
->data
, sizeof(v
->data
), fmt
, ap
);
989 blkid_probe_reset_last_value(pr
);
996 int blkid_probe_sprintf_value(blkid_probe pr
, const char *name
,
997 const char *fmt
, ...)
1003 rc
= blkid_probe_vsprintf_value(pr
, name
, fmt
, ap
);
1010 * blkid_probe_get_devno:
1013 * Returns: block device number, or 0 for regilar files.
1015 dev_t
blkid_probe_get_devno(blkid_probe pr
)
1020 if (fstat(pr
->fd
, &sb
) == 0 &&
1021 (S_ISBLK(sb
.st_mode
) || S_ISCHR(sb
.st_mode
)))
1022 pr
->devno
= sb
.st_rdev
;
1028 * blkid_probe_get_size:
1031 * Returns: block device (or file) size in bytes or -1 in case of error.
1033 blkid_loff_t
blkid_probe_get_size(blkid_probe pr
)
1035 return pr
? pr
->size
: -1;
1039 * blkid_probe_get_sectorsize:
1042 * Returns: block device logical sector size (BLKSSZGET ioctl, default 512).
1044 unsigned int blkid_probe_get_sectorsize(blkid_probe pr
)
1047 return DEFAULT_SECTOR_SIZE
; /*... and good luck! */
1053 if (fstat(pr
->fd
, &st
))
1055 pr
->mode
= st
.st_mode
;
1057 if (S_ISBLK(pr
->mode
)) {
1058 if (blkdev_get_sector_size(pr
->fd
, (int *) &pr
->blkssz
))
1065 pr
->blkssz
= DEFAULT_SECTOR_SIZE
;
1070 * blkid_probe_numof_values:
1073 * Returns: number of values in probing result or -1 in case of error.
1075 int blkid_probe_numof_values(blkid_probe pr
)
1083 * blkid_probe_get_value:
1085 * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1
1086 * @name: pointer to return value name or NULL
1087 * @data: pointer to return value data or NULL
1088 * @len: pointer to return value length or NULL
1090 * Note, the @len returns length of the @data, including the terminating
1093 * Returns: 0 on success, or -1 in case of error.
1095 int blkid_probe_get_value(blkid_probe pr
, int num
, const char **name
,
1096 const char **data
, size_t *len
)
1098 struct blkid_prval
*v
= __blkid_probe_get_value(pr
, num
);
1105 *data
= (char *) v
->data
;
1109 DBG(DEBUG_LOWPROBE
, printf("returning %s value\n", v
->name
));
1114 * blkid_probe_lookup_value:
1116 * @name: name of value
1117 * @data: pointer to return value data or NULL
1118 * @len: pointer to return value length or NULL
1120 * Note, the @len returns length of the @data, including the terminating
1123 * Returns: 0 on success, or -1 in case of error.
1125 int blkid_probe_lookup_value(blkid_probe pr
, const char *name
,
1126 const char **data
, size_t *len
)
1128 struct blkid_prval
*v
= __blkid_probe_lookup_value(pr
, name
);
1133 *data
= (char *) v
->data
;
1140 * blkid_probe_has_value:
1142 * @name: name of value
1144 * Returns: 1 if value exist in probing result, otherwise 0.
1146 int blkid_probe_has_value(blkid_probe pr
, const char *name
)
1148 if (blkid_probe_lookup_value(pr
, name
, NULL
, NULL
) == 0)
1153 struct blkid_prval
*__blkid_probe_get_value(blkid_probe pr
, int num
)
1155 if (pr
== NULL
|| num
< 0 || num
>= pr
->nvals
)
1158 return &pr
->vals
[num
];
1161 struct blkid_prval
*__blkid_probe_lookup_value(blkid_probe pr
, const char *name
)
1165 if (pr
== NULL
|| pr
->nvals
== 0 || name
== NULL
)
1168 for (i
= 0; i
< pr
->nvals
; i
++) {
1169 struct blkid_prval
*v
= &pr
->vals
[i
];
1171 if (v
->name
&& strcmp(name
, v
->name
) == 0) {
1172 DBG(DEBUG_LOWPROBE
, printf("returning %s value\n", v
->name
));
1180 /* converts DCE UUID (uuid[16]) to human readable string
1181 * - the @len should be always 37 */
1182 void blkid_unparse_uuid(const unsigned char *uuid
, char *str
, size_t len
)
1185 uuid_unparse(uuid
, str
);
1188 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1189 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1193 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
1198 /* Removes whitespace from the right-hand side of a string (trailing
1201 * Returns size of the new string (without \0).
1203 size_t blkid_rtrim_whitespace(unsigned char *str
)
1205 size_t i
= strlen((char *) str
);
1208 if (!isspace(str
[i
]))