bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * superblocks.c - reads information from filesystem and raid superblocks |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com> |
| 5 | * |
| 6 | * This file may be redistributed under the terms of the |
| 7 | * GNU Lesser General Public License. |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <ctype.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdarg.h> |
| 21 | |
| 22 | #include "superblocks.h" |
| 23 | |
| 24 | /** |
| 25 | * SECTION:superblocks |
| 26 | * @title: Superblocks probing |
| 27 | * @short_description: filesystems and raids superblocks probing. |
| 28 | * |
| 29 | * The library API has been originally designed for superblocks probing only. |
| 30 | * This is reason why some *deprecated* superblock specific functions don't use |
| 31 | * '_superblocks_' namespace in the function name. Please, don't use these |
| 32 | * functions in new code. |
| 33 | * |
| 34 | * The 'superblocks' probers support NAME=value (tags) interface only. The |
| 35 | * superblocks probing is enabled by default (and controlled by |
| 36 | * blkid_probe_enable_superblocks()). |
| 37 | * |
| 38 | * Currently supported tags: |
| 39 | * |
| 40 | * @TYPE: filesystem type |
| 41 | * |
| 42 | * @SEC_TYPE: secondary filesystem type |
| 43 | * |
| 44 | * @LABEL: filesystem label |
| 45 | * |
| 46 | * @LABEL_RAW: raw label from FS superblock |
| 47 | * |
| 48 | * @UUID: filesystem UUID (lower case) |
| 49 | * |
| 50 | * @UUID_SUB: subvolume uuid (e.g. btrfs) |
| 51 | * |
| 52 | * @UUID_RAW: raw UUID from FS superblock |
| 53 | * |
| 54 | * @EXT_JOURNAL: external journal UUID |
| 55 | * |
| 56 | * @USAGE: usage string: "raid", "filesystem", ... |
| 57 | * |
| 58 | * @VERSION: filesystem version |
| 59 | * |
| 60 | * @MOUNT: cluster mount name (?) -- ocfs only |
| 61 | * |
| 62 | * @SBMAGIC: super block magic string |
| 63 | * |
| 64 | * @SBMAGIC_OFFSET: offset of SBMAGIC |
| 65 | * |
| 66 | * @FSSIZE: size of filessystem [not-implemented yet] |
| 67 | * |
| 68 | * @SYSTEM_ID: ISO9660 system identifier |
| 69 | * |
| 70 | * @PUBLISHER_ID: ISO9660 publisher identifier |
| 71 | * |
| 72 | * @APPLICATION_ID: ISO9660 application identifier |
| 73 | * |
| 74 | * @BOOT_SYSTEM_ID: ISO9660 boot system identifier |
| 75 | */ |
| 76 | |
| 77 | static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn); |
| 78 | static int superblocks_safeprobe(blkid_probe pr, struct blkid_chain *chn); |
| 79 | |
| 80 | static int blkid_probe_set_usage(blkid_probe pr, int usage); |
| 81 | |
| 82 | |
| 83 | /* |
| 84 | * Superblocks chains probing functions |
| 85 | */ |
| 86 | static const struct blkid_idinfo *idinfos[] = |
| 87 | { |
| 88 | /* RAIDs */ |
| 89 | &linuxraid_idinfo, |
| 90 | &ddfraid_idinfo, |
| 91 | &iswraid_idinfo, |
| 92 | &lsiraid_idinfo, |
| 93 | &viaraid_idinfo, |
| 94 | &silraid_idinfo, |
| 95 | &nvraid_idinfo, |
| 96 | &pdcraid_idinfo, |
| 97 | &highpoint45x_idinfo, |
| 98 | &highpoint37x_idinfo, |
| 99 | &adraid_idinfo, |
| 100 | &jmraid_idinfo, |
| 101 | |
| 102 | &drbd_idinfo, |
| 103 | &drbdproxy_datalog_idinfo, |
| 104 | &lvm2_idinfo, |
| 105 | &lvm1_idinfo, |
| 106 | &snapcow_idinfo, |
| 107 | &verity_hash_idinfo, |
| 108 | &luks_idinfo, |
| 109 | &vmfs_volume_idinfo, |
| 110 | |
| 111 | /* Filesystems */ |
| 112 | &vfat_idinfo, |
| 113 | &swsuspend_idinfo, |
| 114 | &swap_idinfo, |
| 115 | &xfs_idinfo, |
| 116 | &ext4dev_idinfo, |
| 117 | &ext4_idinfo, |
| 118 | &ext3_idinfo, |
| 119 | &ext2_idinfo, |
| 120 | &jbd_idinfo, |
| 121 | &reiser_idinfo, |
| 122 | &reiser4_idinfo, |
| 123 | &jfs_idinfo, |
| 124 | &udf_idinfo, |
| 125 | &iso9660_idinfo, |
| 126 | &zfs_idinfo, |
| 127 | &hfsplus_idinfo, |
| 128 | &hfs_idinfo, |
| 129 | &ufs_idinfo, |
| 130 | &hpfs_idinfo, |
| 131 | &sysv_idinfo, |
| 132 | &xenix_idinfo, |
| 133 | &ntfs_idinfo, |
| 134 | &cramfs_idinfo, |
| 135 | &romfs_idinfo, |
| 136 | &minix_idinfo, |
| 137 | &gfs_idinfo, |
| 138 | &gfs2_idinfo, |
| 139 | &ocfs_idinfo, |
| 140 | &ocfs2_idinfo, |
| 141 | &oracleasm_idinfo, |
| 142 | &vxfs_idinfo, |
| 143 | &squashfs_idinfo, |
| 144 | &netware_idinfo, |
| 145 | &btrfs_idinfo, |
| 146 | &ubifs_idinfo, |
| 147 | &bfs_idinfo, |
| 148 | &vmfs_fs_idinfo, |
| 149 | &befs_idinfo, |
| 150 | &nilfs2_idinfo, |
| 151 | &exfat_idinfo, |
| 152 | &f2fs_idinfo |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * Driver definition |
| 157 | */ |
| 158 | const struct blkid_chaindrv superblocks_drv = { |
| 159 | .id = BLKID_CHAIN_SUBLKS, |
| 160 | .name = "superblocks", |
| 161 | .dflt_enabled = TRUE, |
| 162 | .dflt_flags = BLKID_SUBLKS_DEFAULT, |
| 163 | .idinfos = idinfos, |
| 164 | .nidinfos = ARRAY_SIZE(idinfos), |
| 165 | .has_fltr = TRUE, |
| 166 | .probe = superblocks_probe, |
| 167 | .safeprobe = superblocks_safeprobe, |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * blkid_probe_enable_superblocks: |
| 172 | * @pr: probe |
| 173 | * @enable: TRUE/FALSE |
| 174 | * |
| 175 | * Enables/disables the superblocks probing for non-binary interface. |
| 176 | * |
| 177 | * Returns: 0 on success, or -1 in case of error. |
| 178 | */ |
| 179 | int blkid_probe_enable_superblocks(blkid_probe pr, int enable) |
| 180 | { |
| 181 | if (!pr) |
| 182 | return -1; |
| 183 | pr->chains[BLKID_CHAIN_SUBLKS].enabled = enable; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * blkid_probe_set_superblocks_flags: |
| 189 | * @pr: prober |
| 190 | * @flags: BLKID_SUBLKS_* flags |
| 191 | * |
| 192 | * Sets probing flags to the superblocks prober. This function is optional, the |
| 193 | * default are BLKID_SUBLKS_DEFAULTS flags. |
| 194 | * |
| 195 | * Returns: 0 on success, or -1 in case of error. |
| 196 | */ |
| 197 | int blkid_probe_set_superblocks_flags(blkid_probe pr, int flags) |
| 198 | { |
| 199 | if (!pr) |
| 200 | return -1; |
| 201 | |
| 202 | pr->chains[BLKID_CHAIN_SUBLKS].flags = flags; |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * blkid_probe_reset_superblocks_filter: |
| 208 | * @pr: prober |
| 209 | * |
| 210 | * Resets superblocks probing filter |
| 211 | * |
| 212 | * Returns: 0 on success, or -1 in case of error. |
| 213 | */ |
| 214 | int blkid_probe_reset_superblocks_filter(blkid_probe pr) |
| 215 | { |
| 216 | return __blkid_probe_reset_filter(pr, BLKID_CHAIN_SUBLKS); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * blkid_probe_invert_superblocks_filter: |
| 221 | * @pr: prober |
| 222 | * |
| 223 | * Inverts superblocks probing filter |
| 224 | * |
| 225 | * Returns: 0 on success, or -1 in case of error. |
| 226 | */ |
| 227 | int blkid_probe_invert_superblocks_filter(blkid_probe pr) |
| 228 | { |
| 229 | return __blkid_probe_invert_filter(pr, BLKID_CHAIN_SUBLKS); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * blkid_probe_filter_superblocks_type: |
| 234 | * @pr: prober |
| 235 | * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag |
| 236 | * @names: NULL terminated array of probing function names (e.g. "vfat"). |
| 237 | * |
| 238 | * %BLKID_FLTR_NOTIN - probe for all items which are NOT IN @names; |
| 239 | * |
| 240 | * %BLKID_FLTR_ONLYIN - probe for items which are IN @names |
| 241 | * |
| 242 | * Returns: 0 on success, or -1 in case of error. |
| 243 | */ |
| 244 | int blkid_probe_filter_superblocks_type(blkid_probe pr, int flag, char *names[]) |
| 245 | { |
| 246 | return __blkid_probe_filter_types(pr, BLKID_CHAIN_SUBLKS, flag, names); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * blkid_probe_filter_superblocks_usage: |
| 251 | * @pr: prober |
| 252 | * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag |
| 253 | * @usage: BLKID_USAGE_* flags |
| 254 | * |
| 255 | * %BLKID_FLTR_NOTIN - probe for all items which are NOT IN @usage; |
| 256 | * |
| 257 | * %BLKID_FLTR_ONLYIN - probe for items which are IN @usage |
| 258 | * |
| 259 | * Returns: 0 on success, or -1 in case of error. |
| 260 | */ |
| 261 | int blkid_probe_filter_superblocks_usage(blkid_probe pr, int flag, int usage) |
| 262 | { |
| 263 | unsigned long *fltr; |
| 264 | struct blkid_chain *chn; |
| 265 | size_t i; |
| 266 | |
| 267 | fltr = blkid_probe_get_filter(pr, BLKID_CHAIN_SUBLKS, TRUE); |
| 268 | if (!fltr) |
| 269 | return -1; |
| 270 | |
| 271 | chn = &pr->chains[BLKID_CHAIN_SUBLKS]; |
| 272 | |
| 273 | for (i = 0; i < chn->driver->nidinfos; i++) { |
| 274 | const struct blkid_idinfo *id = chn->driver->idinfos[i]; |
| 275 | |
| 276 | if (id->usage & usage) { |
| 277 | if (flag & BLKID_FLTR_NOTIN) |
| 278 | blkid_bmp_set_item(chn->fltr, i); |
| 279 | } else if (flag & BLKID_FLTR_ONLYIN) |
| 280 | blkid_bmp_set_item(chn->fltr, i); |
| 281 | } |
| 282 | DBG(DEBUG_LOWPROBE, printf("a new probing usage-filter initialized\n")); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * blkid_known_fstype: |
| 288 | * @fstype: filesystem name |
| 289 | * |
| 290 | * Returns: 1 for known filesytems, or 0 for unknown filesystem. |
| 291 | */ |
| 292 | int blkid_known_fstype(const char *fstype) |
| 293 | { |
| 294 | size_t i; |
| 295 | |
| 296 | if (!fstype) |
| 297 | return 0; |
| 298 | |
| 299 | for (i = 0; i < ARRAY_SIZE(idinfos); i++) { |
| 300 | const struct blkid_idinfo *id = idinfos[i]; |
| 301 | if (strcmp(id->name, fstype) == 0) |
| 302 | return 1; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * blkid_superblocks_get_name: |
| 309 | * @idx: number >= 0 |
| 310 | * @name: returns name of supported filesystem/raid (optional) |
| 311 | * @usage: returns BLKID_USAGE_* flags, (optional) |
| 312 | * |
| 313 | * Returns: -1 if @idx is out of range, or 0 on success. |
| 314 | */ |
| 315 | int blkid_superblocks_get_name(size_t idx, const char **name, int *usage) |
| 316 | { |
| 317 | if (idx < ARRAY_SIZE(idinfos)) { |
| 318 | if (name) |
| 319 | *name = idinfos[idx]->name; |
| 320 | if (usage) |
| 321 | *usage = idinfos[idx]->usage; |
| 322 | return 0; |
| 323 | } |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * The blkid_do_probe() backend. |
| 329 | */ |
| 330 | static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn) |
| 331 | { |
| 332 | size_t i; |
| 333 | |
| 334 | if (!pr || chn->idx < -1) |
| 335 | return -1; |
| 336 | blkid_probe_chain_reset_vals(pr, chn); |
| 337 | |
| 338 | DBG(DEBUG_LOWPROBE, |
| 339 | printf("--> starting probing loop [SUBLKS idx=%d]\n", |
| 340 | chn->idx)); |
| 341 | |
| 342 | if (pr->size <= 0 || (pr->size <= 1024 && !S_ISCHR(pr->mode))) |
| 343 | /* Ignore very very small block devices or regular files (e.g. |
| 344 | * extended partitions). Note that size of the UBI char devices |
| 345 | * is 1 byte */ |
| 346 | goto nothing; |
| 347 | |
| 348 | i = chn->idx < 0 ? 0 : chn->idx + 1U; |
| 349 | |
| 350 | for ( ; i < ARRAY_SIZE(idinfos); i++) { |
| 351 | const struct blkid_idinfo *id; |
| 352 | const struct blkid_idmag *mag = NULL; |
| 353 | blkid_loff_t off = 0; |
| 354 | int rc = 0; |
| 355 | |
| 356 | chn->idx = i; |
| 357 | id = idinfos[i]; |
| 358 | |
| 359 | if (chn->fltr && blkid_bmp_get_item(chn->fltr, i)) { |
| 360 | DBG(DEBUG_LOWPROBE, printf("filter out: %s\n", id->name)); |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | if (id->minsz && id->minsz > pr->size) |
| 365 | continue; /* the device is too small */ |
| 366 | |
| 367 | /* don't probe for RAIDs, swap or journal on CD/DVDs */ |
| 368 | if ((id->usage & (BLKID_USAGE_RAID | BLKID_USAGE_OTHER)) && |
| 369 | blkid_probe_is_cdrom(pr)) |
| 370 | continue; |
| 371 | |
| 372 | /* don't probe for RAIDs on floppies */ |
| 373 | if ((id->usage & BLKID_USAGE_RAID) && blkid_probe_is_tiny(pr)) |
| 374 | continue; |
| 375 | |
| 376 | DBG(DEBUG_LOWPROBE, printf("[%zd] %s:\n", i, id->name)); |
| 377 | |
| 378 | if (blkid_probe_get_idmag(pr, id, &off, &mag)) |
| 379 | continue; |
| 380 | |
| 381 | /* final check by probing function */ |
| 382 | if (id->probefunc) { |
| 383 | DBG(DEBUG_LOWPROBE, printf("\tcall probefunc()\n")); |
| 384 | if (id->probefunc(pr, mag) != 0) { |
| 385 | blkid_probe_chain_reset_vals(pr, chn); |
| 386 | continue; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* all cheks passed */ |
| 391 | if (chn->flags & BLKID_SUBLKS_TYPE) |
| 392 | rc = blkid_probe_set_value(pr, "TYPE", |
| 393 | (unsigned char *) id->name, |
| 394 | strlen(id->name) + 1); |
| 395 | |
| 396 | if (!rc) |
| 397 | rc = blkid_probe_set_usage(pr, id->usage); |
| 398 | |
| 399 | if (!rc && mag) |
| 400 | rc = blkid_probe_set_magic(pr, off, mag->len, |
| 401 | (unsigned char *) mag->magic); |
| 402 | if (rc) { |
| 403 | blkid_probe_chain_reset_vals(pr, chn); |
| 404 | DBG(DEBUG_LOWPROBE, printf("failed to set result -- ingnore\n")); |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | DBG(DEBUG_LOWPROBE, |
| 409 | printf("<-- leaving probing loop (type=%s) [SUBLKS idx=%d]\n", |
| 410 | id->name, chn->idx)); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | nothing: |
| 415 | DBG(DEBUG_LOWPROBE, |
| 416 | printf("<-- leaving probing loop (failed) [SUBLKS idx=%d]\n", |
| 417 | chn->idx)); |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * This is the same function as blkid_do_probe(), but returns only one result |
| 423 | * (cannot be used in while()) and checks for ambivalen results (more |
| 424 | * filesystems on the device) -- in such case returns -2. |
| 425 | * |
| 426 | * The function does not check for filesystems when a RAID or crypto signature |
| 427 | * is detected. The function also does not check for collision between RAIDs |
| 428 | * and crypto devices. The first detected RAID or crypto device is returned. |
| 429 | * |
| 430 | * The function does not probe for ambivalent results on very small devices |
| 431 | * (e.g. floppies), on small devices the first detected filesystem is returned. |
| 432 | */ |
| 433 | static int superblocks_safeprobe(blkid_probe pr, struct blkid_chain *chn) |
| 434 | { |
| 435 | struct blkid_prval vals[BLKID_NVALS_SUBLKS]; |
| 436 | int nvals = BLKID_NVALS_SUBLKS; |
| 437 | int idx = -1; |
| 438 | int count = 0; |
| 439 | int intol = 0; |
| 440 | int rc; |
| 441 | |
| 442 | while ((rc = superblocks_probe(pr, chn)) == 0) { |
| 443 | |
| 444 | if (blkid_probe_is_tiny(pr) && !count) |
| 445 | /* floppy or so -- returns the first result. */ |
| 446 | return 0; |
| 447 | |
| 448 | count++; |
| 449 | |
| 450 | if (chn->idx >= 0 && |
| 451 | idinfos[chn->idx]->usage & (BLKID_USAGE_RAID | BLKID_USAGE_CRYPTO)) |
| 452 | break; |
| 453 | |
| 454 | if (chn->idx >= 0 && |
| 455 | !(idinfos[chn->idx]->flags & BLKID_IDINFO_TOLERANT)) |
| 456 | intol++; |
| 457 | |
| 458 | if (count == 1) { |
| 459 | /* save the first result */ |
| 460 | nvals = blkid_probe_chain_copy_vals(pr, chn, vals, nvals); |
| 461 | idx = chn->idx; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (rc < 0) |
| 466 | return rc; /* error */ |
| 467 | |
| 468 | if (count > 1 && intol) { |
| 469 | DBG(DEBUG_LOWPROBE, |
| 470 | printf("ERROR: superblocks chain: " |
| 471 | "ambivalent result detected (%d filesystems)!\n", |
| 472 | count)); |
| 473 | return -2; /* error, ambivalent result (more FS) */ |
| 474 | } |
| 475 | if (!count) |
| 476 | return 1; /* nothing detected */ |
| 477 | |
| 478 | if (idx != -1) { |
| 479 | /* restore the first result */ |
| 480 | blkid_probe_chain_reset_vals(pr, chn); |
| 481 | blkid_probe_append_vals(pr, vals, nvals); |
| 482 | chn->idx = idx; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * The RAID device could be partitioned. The problem are RAID1 devices |
| 487 | * where the partition table is visible from underlaying devices. We |
| 488 | * have to ignore such partition tables. |
| 489 | */ |
| 490 | if (chn->idx >= 0 && idinfos[chn->idx]->usage & BLKID_USAGE_RAID) |
| 491 | pr->prob_flags |= BLKID_PROBE_FL_IGNORE_PT; |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | int blkid_probe_set_version(blkid_probe pr, const char *version) |
| 497 | { |
| 498 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 499 | |
| 500 | if (chn->flags & BLKID_SUBLKS_VERSION) |
| 501 | return blkid_probe_set_value(pr, "VERSION", |
| 502 | (unsigned char *) version, strlen(version) + 1); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...) |
| 508 | { |
| 509 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 510 | int rc = 0; |
| 511 | |
| 512 | if (chn->flags & BLKID_SUBLKS_VERSION) { |
| 513 | va_list ap; |
| 514 | |
| 515 | va_start(ap, fmt); |
| 516 | rc = blkid_probe_vsprintf_value(pr, "VERSION", fmt, ap); |
| 517 | va_end(ap); |
| 518 | } |
| 519 | return rc; |
| 520 | } |
| 521 | |
| 522 | static int blkid_probe_set_usage(blkid_probe pr, int usage) |
| 523 | { |
| 524 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 525 | char *u = NULL; |
| 526 | |
| 527 | if (!(chn->flags & BLKID_SUBLKS_USAGE)) |
| 528 | return 0; |
| 529 | |
| 530 | if (usage & BLKID_USAGE_FILESYSTEM) |
| 531 | u = "filesystem"; |
| 532 | else if (usage & BLKID_USAGE_RAID) |
| 533 | u = "raid"; |
| 534 | else if (usage & BLKID_USAGE_CRYPTO) |
| 535 | u = "crypto"; |
| 536 | else if (usage & BLKID_USAGE_OTHER) |
| 537 | u = "other"; |
| 538 | else |
| 539 | u = "unknown"; |
| 540 | |
| 541 | return blkid_probe_set_value(pr, "USAGE", (unsigned char *) u, strlen(u) + 1); |
| 542 | } |
| 543 | |
| 544 | int blkid_probe_set_id_label(blkid_probe pr, const char *name, |
| 545 | unsigned char *data, size_t len) |
| 546 | { |
| 547 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 548 | struct blkid_prval *v; |
| 549 | |
| 550 | if (!(chn->flags & BLKID_SUBLKS_LABEL)) |
| 551 | return 0; |
| 552 | |
| 553 | v = blkid_probe_assign_value(pr, name); |
| 554 | if (!v) |
| 555 | return -1; |
| 556 | |
| 557 | if (len >= BLKID_PROBVAL_BUFSIZ) |
| 558 | len = BLKID_PROBVAL_BUFSIZ - 1; /* make a space for \0 */ |
| 559 | |
| 560 | memcpy(v->data, data, len); |
| 561 | v->data[len] = '\0'; |
| 562 | |
| 563 | /* remove white spaces */ |
| 564 | v->len = blkid_rtrim_whitespace(v->data) + 1; |
| 565 | if (v->len > 1) |
| 566 | v->len = blkid_ltrim_whitespace(v->data) + 1; |
| 567 | |
| 568 | if (v->len <= 1) |
| 569 | blkid_probe_reset_last_value(pr); /* ignore empty */ |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len) |
| 574 | { |
| 575 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 576 | struct blkid_prval *v; |
| 577 | if (len > BLKID_PROBVAL_BUFSIZ) |
| 578 | len = BLKID_PROBVAL_BUFSIZ; |
| 579 | |
| 580 | if ((chn->flags & BLKID_SUBLKS_LABELRAW) && |
| 581 | blkid_probe_set_value(pr, "LABEL_RAW", label, len) < 0) |
| 582 | return -1; |
| 583 | if (!(chn->flags & BLKID_SUBLKS_LABEL)) |
| 584 | return 0; |
| 585 | v = blkid_probe_assign_value(pr, "LABEL"); |
| 586 | if (!v) |
| 587 | return -1; |
| 588 | |
| 589 | if (len == BLKID_PROBVAL_BUFSIZ) |
| 590 | len--; /* make a space for \0 */ |
| 591 | |
| 592 | memcpy(v->data, label, len); |
| 593 | v->data[len] = '\0'; |
| 594 | |
| 595 | v->len = blkid_rtrim_whitespace(v->data) + 1; |
| 596 | if (v->len == 1) |
| 597 | blkid_probe_reset_last_value(pr); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label, |
| 602 | size_t len, int enc) |
| 603 | { |
| 604 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 605 | struct blkid_prval *v; |
| 606 | |
| 607 | if ((chn->flags & BLKID_SUBLKS_LABELRAW) && |
| 608 | blkid_probe_set_value(pr, "LABEL_RAW", label, len) < 0) |
| 609 | return -1; |
| 610 | if (!(chn->flags & BLKID_SUBLKS_LABEL)) |
| 611 | return 0; |
| 612 | v = blkid_probe_assign_value(pr, "LABEL"); |
| 613 | if (!v) |
| 614 | return -1; |
| 615 | |
| 616 | blkid_encode_to_utf8(enc, v->data, sizeof(v->data), label, len); |
| 617 | v->len = blkid_rtrim_whitespace(v->data) + 1; |
| 618 | if (v->len == 1) |
| 619 | blkid_probe_reset_last_value(pr); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */ |
| 624 | static int uuid_is_empty(const unsigned char *buf, size_t len) |
| 625 | { |
| 626 | size_t i; |
| 627 | |
| 628 | for (i = 0; i < len; i++) |
| 629 | if (buf[i]) |
| 630 | return 0; |
| 631 | return 1; |
| 632 | } |
| 633 | |
| 634 | int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid, |
| 635 | size_t len, const char *fmt, ...) |
| 636 | { |
| 637 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 638 | int rc = -1; |
| 639 | va_list ap; |
| 640 | |
| 641 | if (len > BLKID_PROBVAL_BUFSIZ) |
| 642 | len = BLKID_PROBVAL_BUFSIZ; |
| 643 | |
| 644 | if (uuid_is_empty(uuid, len)) |
| 645 | return 0; |
| 646 | |
| 647 | if ((chn->flags & BLKID_SUBLKS_UUIDRAW) && |
| 648 | blkid_probe_set_value(pr, "UUID_RAW", uuid, len) < 0) |
| 649 | return -1; |
| 650 | if (!(chn->flags & BLKID_SUBLKS_UUID)) |
| 651 | return 0; |
| 652 | |
| 653 | va_start(ap, fmt); |
| 654 | rc = blkid_probe_vsprintf_value(pr, "UUID", fmt, ap); |
| 655 | va_end(ap); |
| 656 | |
| 657 | /* convert to lower case (..be paranoid) */ |
| 658 | if (!rc) { |
| 659 | size_t i; |
| 660 | struct blkid_prval *v = __blkid_probe_get_value(pr, |
| 661 | blkid_probe_numof_values(pr)); |
| 662 | if (v) { |
| 663 | for (i = 0; i < v->len; i++) |
| 664 | if (v->data[i] >= 'A' && v->data[i] <= 'F') |
| 665 | v->data[i] = (v->data[i] - 'A') + 'a'; |
| 666 | } |
| 667 | } |
| 668 | return rc; |
| 669 | } |
| 670 | |
| 671 | /* function to set UUIDs that are in suberblocks stored as strings */ |
| 672 | int blkid_probe_strncpy_uuid(blkid_probe pr, unsigned char *str, size_t len) |
| 673 | { |
| 674 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 675 | struct blkid_prval *v; |
| 676 | |
| 677 | if (str == NULL || *str == '\0') |
| 678 | return -1; |
| 679 | if (!len) |
| 680 | len = strlen((char *) str); |
| 681 | if (len > BLKID_PROBVAL_BUFSIZ) |
| 682 | len = BLKID_PROBVAL_BUFSIZ; |
| 683 | |
| 684 | if ((chn->flags & BLKID_SUBLKS_UUIDRAW) && |
| 685 | blkid_probe_set_value(pr, "UUID_RAW", str, len) < 0) |
| 686 | return -1; |
| 687 | if (!(chn->flags & BLKID_SUBLKS_UUID)) |
| 688 | return 0; |
| 689 | |
| 690 | v = blkid_probe_assign_value(pr, "UUID"); |
| 691 | if (v) { |
| 692 | if (len == BLKID_PROBVAL_BUFSIZ) |
| 693 | len--; /* make a space for \0 */ |
| 694 | |
| 695 | memcpy((char *) v->data, str, len); |
| 696 | v->data[len] = '\0'; |
| 697 | v->len = len + 1; |
| 698 | return 0; |
| 699 | } |
| 700 | return -1; |
| 701 | } |
| 702 | |
| 703 | /* default _set_uuid function to set DCE UUIDs */ |
| 704 | int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name) |
| 705 | { |
| 706 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 707 | struct blkid_prval *v; |
| 708 | |
| 709 | if (uuid_is_empty(uuid, 16)) |
| 710 | return 0; |
| 711 | |
| 712 | if (!name) { |
| 713 | if ((chn->flags & BLKID_SUBLKS_UUIDRAW) && |
| 714 | blkid_probe_set_value(pr, "UUID_RAW", uuid, 16) < 0) |
| 715 | return -1; |
| 716 | if (!(chn->flags & BLKID_SUBLKS_UUID)) |
| 717 | return 0; |
| 718 | |
| 719 | v = blkid_probe_assign_value(pr, "UUID"); |
| 720 | } else |
| 721 | v = blkid_probe_assign_value(pr, name); |
| 722 | |
| 723 | blkid_unparse_uuid(uuid, (char *) v->data, sizeof(v->data)); |
| 724 | v->len = 37; |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | int blkid_probe_set_uuid(blkid_probe pr, unsigned char *uuid) |
| 730 | { |
| 731 | return blkid_probe_set_uuid_as(pr, uuid, NULL); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * blkid_probe_set_request: |
| 736 | * @pr: probe |
| 737 | * @flags: BLKID_PROBREQ_* (deprecated) or BLKID_SUBLKS_* flags |
| 738 | * |
| 739 | * Returns: 0 on success, or -1 in case of error. |
| 740 | * |
| 741 | * Deprecated: Use blkid_probe_set_superblocks_flags(). |
| 742 | */ |
| 743 | int blkid_probe_set_request(blkid_probe pr, int flags) |
| 744 | { |
| 745 | return blkid_probe_set_superblocks_flags(pr, flags); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * blkid_probe_reset_filter: |
| 750 | * @pr: prober |
| 751 | * |
| 752 | * Returns: 0 on success, or -1 in case of error. |
| 753 | * |
| 754 | * Deprecated: Use blkid_probe_reset_superblocks_filter(). |
| 755 | */ |
| 756 | int blkid_probe_reset_filter(blkid_probe pr) |
| 757 | { |
| 758 | return __blkid_probe_reset_filter(pr, BLKID_CHAIN_SUBLKS); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * blkid_probe_invert_filter: |
| 763 | * @pr: prober |
| 764 | * |
| 765 | * Returns: 0 on success, or -1 in case of error. |
| 766 | * |
| 767 | * Deprecated: Use blkid_probe_invert_superblocks_filter(). |
| 768 | */ |
| 769 | int blkid_probe_invert_filter(blkid_probe pr) |
| 770 | { |
| 771 | return __blkid_probe_invert_filter(pr, BLKID_CHAIN_SUBLKS); |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * blkid_probe_filter_types |
| 776 | * @pr: prober |
| 777 | * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag |
| 778 | * @names: NULL terminated array of probing function names (e.g. "vfat"). |
| 779 | * |
| 780 | * Returns: 0 on success, or -1 in case of error. |
| 781 | * |
| 782 | * Deprecated: Use blkid_probe_filter_superblocks_types(). |
| 783 | */ |
| 784 | int blkid_probe_filter_types(blkid_probe pr, int flag, char *names[]) |
| 785 | { |
| 786 | return __blkid_probe_filter_types(pr, BLKID_CHAIN_SUBLKS, flag, names); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * blkid_probe_filter_usage |
| 791 | * @pr: prober |
| 792 | * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag |
| 793 | * @usage: BLKID_USAGE_* flags |
| 794 | * |
| 795 | * Returns: 0 on success, or -1 in case of error. |
| 796 | * |
| 797 | * Deprecated: Use blkid_probe_filter_superblocks_usage(). |
| 798 | */ |
| 799 | int blkid_probe_filter_usage(blkid_probe pr, int flag, int usage) |
| 800 | { |
| 801 | return blkid_probe_filter_superblocks_usage(pr, flag, usage); |
| 802 | } |
| 803 | |
| 804 | |