int get_device_info(int fd, struct device_info *info) { blkid_probe probe; struct stat stat; int ret; *info = device_info_clueless; ret = fstat(fd, &stat); if (ret < 0) { perror("fstat on target failed"); return -1; } if (S_ISREG(stat.st_mode)) { /* there is nothing more to discover for an image file */ info->type = TYPE_FILE; info->partition = 0; info->size = stat.st_size; return 0; } if (!S_ISBLK(stat.st_mode)) { /* neither regular file nor block device? not usable */ info->type = TYPE_BAD; return 0; } probe = blkid_new_probe(); if (!probe) { return -1; } if (blkid_probe_set_device(probe, fd, 0, 0)) { blkid_free_probe(probe); return -1; } get_block_device_size(info, fd); get_block_geometry(info, fd); info->sector_size = blkid_probe_get_sectorsize(probe); /* use udev information if available, fall back to blkid probing */ if (udev_fill_info(info, &stat)) blkid_fill_info(info, probe); blkid_free_probe(probe); return 0; }
static char* get_blkid(char *device) { int fd; blkid_probe pr = NULL; uint64_t size; const char *label; char *ret; char path[PATH_MAX]; if(!device || !strlen(device)) return NULL; snprintf(path, PATH_MAX, "/dev/%s", device); fd = open(path, O_RDONLY); if(fd<0) return NULL; pr = blkid_new_probe (); blkid_probe_set_request (pr, BLKID_PROBREQ_LABEL); ioctl(fd, BLKGETSIZE64, &size); blkid_probe_set_device (pr, fd, 0, size); blkid_do_safeprobe (pr); blkid_probe_lookup_value(pr, "LABEL", &label, NULL); ret = strdup(label); blkid_free_probe (pr); close(fd); return ret; }
static int check_partition_table(const char *device) { blkid_probe pr; const char *value; int ret; pr = blkid_new_probe_from_filename(device); if (!pr) return -1; ret = blkid_probe_enable_partitions(pr, 1); if (ret < 0) goto errout; ret = blkid_probe_enable_superblocks(pr, 0); if (ret < 0) goto errout; ret = blkid_do_fullprobe(pr); if (ret < 0) goto errout; ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL); if (ret == 0) fprintf(stderr, _("Found a %s partition table in %s\n"), value, device); else ret = 1; errout: blkid_free_probe(pr); return ret; }
static int _wipe_known_signatures_with_blkid(struct device *dev, const char *name, uint32_t types_to_exclude, uint32_t types_no_prompt, int yes, force_t force, int *wiped) { blkid_probe probe = NULL; int found = 0, left = 0, wiped_tmp; int r_wipe; int r = 0; if (!wiped) wiped = &wiped_tmp; *wiped = 0; /* TODO: Should we check for valid dev - _dev_is_valid(dev)? */ if (!(probe = blkid_new_probe_from_filename(dev_name(dev)))) { log_error("Failed to create a new blkid probe for device %s.", dev_name(dev)); goto out; } blkid_probe_enable_partitions(probe, 1); blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC); blkid_probe_enable_superblocks(probe, 1); blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION | BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM); while (!blkid_do_probe(probe)) { if ((r_wipe = _blkid_wipe(probe, dev, name, types_to_exclude, types_no_prompt, yes, force)) == 1) { (*wiped)++; if (blkid_probe_step_back(probe)) { log_error("Failed to step back blkid probe to check just wiped signature."); goto out; } } /* do not count excluded types */ if (r_wipe != 2) found++; } if (!found) r = 1; left = found - *wiped; if (!left) r = 1; else log_warn("%d existing signature%s left on the device.", left, left > 1 ? "s" : ""); out: if (probe) blkid_free_probe(probe); return r; }
/* * returns (in @res) blkid prober, the @cache argument is optional */ static int cache_get_probe(struct libmnt_cache *cache, const char *devname, blkid_probe *res) { blkid_probe pr = cache ? cache->pr : NULL; assert(devname); if (cache && cache->pr && (!cache->filename || strcmp(devname, cache->filename))) { blkid_free_probe(cache->pr); free(cache->filename); cache->filename = NULL; pr = cache->pr = NULL; } if (!pr) { pr = blkid_new_probe_from_filename(devname); if (!pr) return -1; if (cache) { cache->pr = pr; cache->filename = strdup(devname); if (!cache->filename) return -ENOMEM; } } if (res) *res = pr; return 0; }
/** * mnt_get_fstype: * @devname: device name * @ambi: returns TRUE if probing result is ambivalent (optional argument) * @cache: cache for results or NULL * * Returns: filesystem type or NULL in case of error. The result has to be * deallocated by free() if @cache is NULL. */ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache) { blkid_probe pr; const char *data; char *type = NULL; int rc; DBG(CACHE, mnt_debug_h(cache, "get %s FS type", devname)); if (cache) return mnt_cache_find_tag_value(cache, devname, "TYPE"); if (cache_get_probe(NULL, devname, &pr)) return NULL; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); rc = blkid_do_safeprobe(pr); if (!rc && !blkid_probe_lookup_value(pr, "TYPE", &data, NULL)) type = strdup(data); if (ambi) *ambi = rc == -2 ? TRUE : FALSE; blkid_free_probe(pr); return type; }
static char* get_uuid(char *device) { char path[PATH_MAX]; int fd; blkid_probe pr = NULL; uint64_t size; const char *uuid; char *ret; if(!device || !strlen(device)) return NULL; fd = open(device, O_RDONLY); if (fd < 0) return NULL; pr = blkid_new_probe(); blkid_probe_set_request (pr, BLKID_PROBREQ_UUID); ioctl(fd, BLKGETSIZE64, &size); blkid_probe_set_device(pr, fd, 0, size); blkid_do_safeprobe(pr); blkid_probe_lookup_value(pr, "UUID", &uuid, NULL); snprintf(path, PATH_MAX, "/dev/disk/by-uuid/%s", uuid); ret = strdup(path); blkid_free_probe(pr); close(fd); return ret; }
gboolean type_by_device(const gchar* device, gchar** type) { gboolean result = false; const char *devtype = NULL; blkid_probe probe = NULL; *type = NULL; probe = blkid_new_probe_from_filename(device); if(!probe) { LOG(MOD "Probe from filename failed!\n"); return false; } if (blkid_probe_enable_partitions(probe, true) != 0) { LOG(MOD "Enable partitions failed!\n"); goto fail; } if (blkid_do_fullprobe(probe) != 0) { LOG(MOD "Fullprobe failed!\n"); goto fail; } if (blkid_probe_lookup_value(probe, "TYPE", &devtype, NULL) != 0) { LOG(MOD "Lookup value failed!\n"); goto fail; } *type = g_strdup(devtype); result = true; fail: blkid_free_probe(probe); return result; }
int main(int argc, char *argv[]) { int rc; char *devname; blkid_probe pr; blkid_topology tp; if (argc < 2) { fprintf(stderr, "usage: %s <device> " "-- checks based on libblkid for mkfs-like programs.\n", program_invocation_short_name); return EXIT_FAILURE; } devname = argv[1]; pr = blkid_new_probe_from_filename(devname); if (!pr) err(EXIT_FAILURE, "%s: faild to create a new libblkid probe", devname); /* * check Filesystems / Partitions overwrite */ /* enable partitions probing (superblocks are enabled by default) */ blkid_probe_enable_partitions(pr, TRUE); rc = blkid_do_fullprobe(pr); if (rc == -1) errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname); else if (rc == 0) { const char *type; if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) errx(EXIT_FAILURE, "%s: appears to contain an existing " "%s superblock", devname, type); if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) errx(EXIT_FAILURE, "%s: appears to contain an partition " "table (%s)", devname, type); } /* * get topology details */ tp = blkid_probe_get_topology(pr); if (!tp) errx(EXIT_FAILURE, "%s: failed to read topology", devname); /* ... your mkfs.<type> code or so ... off = blkid_topology_get_alignment_offset(tp); */ blkid_free_probe(pr); return EXIT_SUCCESS; }
static int is_ssd(const char *file) { blkid_probe probe; char wholedisk[32]; char sysfs_path[PATH_MAX]; dev_t devno; int fd; char rotational; int ret; probe = blkid_new_probe_from_filename(file); if (!probe) return 0; /* Device number of this disk (possibly a partition) */ devno = blkid_probe_get_devno(probe); if (!devno) { blkid_free_probe(probe); return 0; } /* Get whole disk name (not full path) for this devno */ ret = blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL); if (ret) { blkid_free_probe(probe); return 0; } snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational", wholedisk); blkid_free_probe(probe); fd = open(sysfs_path, O_RDONLY); if (fd < 0) { return 0; } if (read(fd, &rotational, sizeof(char)) < sizeof(char)) { close(fd); return 0; } close(fd); return !atoi((const char *)&rotational); }
void fsprobe_exit(void) { if (blprobe) blkid_free_probe(blprobe); if (blcache) blkid_put_cache(blcache); }
int main(int argc, char *argv[]) { blkid_probe pr = NULL; char *uuid = NULL, *label = NULL, *devname; int c, rc = -1; static const struct option longopts[] = { { "help", 0, 0, 'h' }, { "version", 0, 0, 'V' }, { "label", 1, 0, 'L' }, { "uuid", 1, 0, 'U' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "hVL:U:", longopts, NULL)) != -1) { switch (c) { case 'h': usage(stdout); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'L': label = optarg; break; case 'U': #ifdef HAVE_LIBUUID uuid = optarg; #else warnx(_("ignore -U (UUIDs are unsupported)")); #endif break; default: usage(stderr); break; } } if (optind == argc) usage(stderr); devname = argv[optind]; pr = get_swap_prober(devname); if (pr) { if (uuid || label) rc = change_info(devname, label, uuid); else rc = print_info(pr); blkid_free_probe(pr); } return rc ? EXIT_FAILURE : EXIT_SUCCESS; }
static char * _get_uuid_from_device( const char * device ) { char * r = NULL ; const char * e = NULL ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"UUID",&e,NULL ) ; r = StringCopy_2( e ) ; blkid_free_probe( blkid ) ; } return r ; }
string_t zuluCryptGetFileSystemFromDevice( const char * device ) { string_t st = StringVoid ; const char * cf = NULL ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"TYPE",&cf,NULL ) ; st = String( cf ) ; blkid_free_probe( blkid ) ; } return st ; }
static struct wipe_desc * do_wipe(struct wipe_desc *wp, const char *devname, int noact, int all, int quiet, int force) { int flags; blkid_probe pr; struct wipe_desc *w, *wp0 = clone_offset(wp); int zap = all ? 1 : wp->zap; flags = O_RDWR; if (!force) flags |= O_EXCL; pr = new_probe(devname, flags); if (!pr) return NULL; while (blkid_do_probe(pr) == 0) { wp = get_desc_for_probe(wp, pr); if (!wp) break; /* Check if offset is in provided list */ w = wp0; while(w && w->offset != wp->offset) w = w->next; if (wp0 && !w) continue; /* Mark done if found in provided list */ if (w) w->on_disk = wp->on_disk; if (!wp->on_disk) continue; if (zap) do_wipe_real(pr, devname, wp, noact, quiet); } for (w = wp0; w != NULL; w = w->next) { if (!w->on_disk && !quiet) warnx(_("%s: offset 0x%jx not found"), devname, w->offset); } fsync(blkid_probe_get_fd(pr)); close(blkid_probe_get_fd(pr)); blkid_free_probe(pr); free_wipe(wp0); return wp; }
static int set_disk_properties(struct sync_disk *disk) { blkid_probe probe; blkid_topology topo; uint32_t sector_size, ss_logical, ss_physical; probe = blkid_new_probe_from_filename(disk->path); if (!probe) { log_error("cannot get blkid probe %s", disk->path); return -1; } topo = blkid_probe_get_topology(probe); if (!topo) { log_error("cannot get blkid topology %s", disk->path); blkid_free_probe(probe); return -1; } sector_size = blkid_probe_get_sectorsize(probe); ss_logical = blkid_topology_get_logical_sector_size(topo); ss_physical = blkid_topology_get_physical_sector_size(topo); blkid_free_probe(probe); if ((sector_size != ss_logical) || (sector_size != ss_physical) || (sector_size % 512)) { log_error("invalid disk sector size %u logical %u " "physical %u %s", sector_size, ss_logical, ss_physical, disk->path); return -1; } disk->sector_size = sector_size; return 0; }
static struct wipe_desc * read_offsets(struct wipe_desc *wp, const char *devname) { blkid_probe pr = new_probe(devname, 0); if (!pr) return NULL; while (blkid_do_probe(pr) == 0) { wp = get_desc_for_probe(wp, pr); if (!wp) break; } blkid_free_probe(pr); return wp; }
/* * This function prints a warning if the device is not wiped (e.g. wipefs(8). * Please don't call this function if there is already a PT. * * Returns: 0 if nothing found, < 0 on error, 1 if found a signature */ static int warn_wipe(struct fdisk_context *cxt) { #ifdef HAVE_LIBBLKID blkid_probe pr; #endif int rc = 0; assert(cxt); if (fdisk_dev_has_disklabel(cxt) || cxt->dev_fd < 0) return -EINVAL; #ifdef HAVE_LIBBLKID DBG(LABEL, dbgprint("wipe check: initialize libblkid prober")); pr = blkid_new_probe(); if (!pr) return -ENOMEM; rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0); if (rc) return rc; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); blkid_probe_enable_partitions(pr, 1); /* we care about the first found FS/raid, so don't call blkid_do_probe() * in loop or don't use blkid_do_fullprobe() ... */ rc = blkid_do_probe(pr); if (rc == 0) { const char *name = NULL; if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 || blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) { fdisk_warnx(cxt, _( "%s: device contains a valid '%s' signature, it's " "strongly recommended to wipe the device by command wipefs(8) " "if this setup is unexpected to avoid " "possible collisions."), cxt->dev_path, name); rc = 1; } } blkid_free_probe(pr); #endif return rc; }
static lgfs2_rgrps_t rgrps_init(struct gfs2_sbd *sdp) { int ret; int error; uint64_t al_base = 0; uint64_t al_off = 0; struct stat st; blkid_probe pr = blkid_new_probe(); if (pr == NULL || blkid_probe_set_device(pr, sdp->device_fd, 0, 0) != 0 || blkid_probe_enable_superblocks(pr, TRUE) != 0 || blkid_probe_enable_partitions(pr, TRUE) != 0) { fprintf(stderr, _("Failed to create probe\n")); return NULL; } error = fstat(sdp->device_fd, &st); if (error < 0) { fprintf(stderr, _("fstat failed\n")); return NULL; } if (!S_ISREG(st.st_mode) && blkid_probe_enable_topology(pr, TRUE) != 0) { fprintf(stderr, _("Failed to create probe\n")); return NULL; } ret = blkid_do_fullprobe(pr); if (ret == 0 && !S_ISREG(st.st_mode)) { blkid_topology tp = blkid_probe_get_topology(pr); if (tp != NULL) { unsigned long min_io_sz = blkid_topology_get_minimum_io_size(tp); unsigned long opt_io_sz = blkid_topology_get_optimal_io_size(tp); unsigned long phy_sector_sz = blkid_topology_get_physical_sector_size(tp); if ((min_io_sz > phy_sector_sz) && (opt_io_sz > phy_sector_sz)) { al_base = opt_io_sz / sdp->bsize; al_off = min_io_sz / sdp->bsize; } } } blkid_free_probe(pr); return lgfs2_rgrps_init(sdp, al_base, al_off); }
/* * This function works like mnt_resolve_tag(), but it's able to read UUiD/LABEL * from regular swap files too (according to entries in /proc/swaps). Note that * mnt_resolve_tag() and mnt_resolve_spec() works with system visible block * devices only. */ static char *swapoff_resolve_tag(const char *name, const char *value, struct libmnt_cache *cache) { char *path; struct libmnt_table *tb; struct libmnt_iter *itr; struct libmnt_fs *fs; /* this is usual case for block devices (and it's really fast as it uses * udev /dev/disk/by-* symlinks by default */ path = mnt_resolve_tag(name, value, cache); if (path) return path; /* try regular files from /proc/swaps */ tb = get_swaps(); if (!tb) return NULL; itr = mnt_new_iter(MNT_ITER_BACKWARD); if (!itr) err(EXIT_FAILURE, _("failed to initialize libmount iterator")); while (tb && mnt_table_next_fs(tb, itr, &fs) == 0) { blkid_probe pr = NULL; const char *src = mnt_fs_get_source(fs); const char *type = mnt_fs_get_swaptype(fs); const char *data = NULL; if (!src || !type || strcmp(type, "file") != 0) continue; pr = get_swap_prober(src); if (!pr) continue; blkid_probe_lookup_value(pr, name, &data, NULL); if (data && strcmp(data, value) == 0) path = xstrdup(src); blkid_free_probe(pr); if (path) break; } mnt_free_iter(itr); return path; }
static inline void probe_filesystem(struct format *target) { blkid_probe probe = 0; static char *filesystems[] = { "ext2", "ext3", "ext4", "reiserfs", "jfs", "xfs", "btrfs", "swap", 0 }; const char *filesystem = "unknown"; const char *result = 0; if((probe = blkid_new_probe_from_filename(target->devicepath)) == 0) goto bail; if(blkid_probe_enable_superblocks(probe,true) == -1) goto bail; if(blkid_probe_filter_superblocks_type(probe,BLKID_FLTR_ONLYIN,filesystems) == -1) goto bail; if(blkid_probe_set_superblocks_flags(probe,BLKID_SUBLKS_TYPE) == -1) goto bail; if(blkid_do_probe(probe) == -1) goto bail; if(blkid_probe_lookup_value(probe,"TYPE",&result,0) == -1) goto bail; filesystem = result; bail: if(probe != 0) blkid_free_probe(probe); target->filesystem = strdup(filesystem); }
int zuluCryptFileSystemIsFUSEbased( const char * device ) { const char * cf = NULL ; int st ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"TYPE",&cf,NULL ) ; #if 1 st = StringAtLeastOneMatch_1( cf,"ntfs","exfat",NULL ) ; #else st = StringAtLeastOneMatch_1( cf,"ntfs",NULL ) ; #endif blkid_free_probe( blkid ) ; return st ; }else{ return 0 ; } }
static blkid_probe new_probe(const char *devname, int mode) { blkid_probe pr = NULL; if (!devname) return NULL; if (mode) { int fd = open(devname, mode); if (fd < 0) goto error; pr = blkid_new_probe(); if (pr && blkid_probe_set_device(pr, fd, 0, 0)) { close(fd); goto error; } } else pr = blkid_new_probe_from_filename(devname); if (!pr) goto error; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC | /* return magic string and offset */ BLKID_SUBLKS_TYPE | /* return superblock type */ BLKID_SUBLKS_USAGE | /* return USAGE= */ BLKID_SUBLKS_LABEL | /* return LABEL= */ BLKID_SUBLKS_UUID | /* return UUID= */ BLKID_SUBLKS_BADCSUM); /* accept bad checksums */ blkid_probe_enable_partitions(pr, 1); blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC); return pr; error: blkid_free_probe(pr); err(EXIT_FAILURE, _("error: %s: probing initialization failed"), devname); return NULL; }
/* * Returns new libblkid prober. This function call exit() on error. */ static blkid_probe get_swap_prober(const char *devname) { blkid_probe pr; int rc; const char *version = NULL; char *swap_filter[] = { "swap", NULL }; pr = blkid_new_probe_from_filename(devname); if (!pr) { warn(_("%s: unable to probe device"), devname); return NULL; } blkid_probe_enable_superblocks(pr, TRUE); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | BLKID_SUBLKS_VERSION); blkid_probe_filter_superblocks_type(pr, BLKID_FLTR_ONLYIN, swap_filter); rc = blkid_do_safeprobe(pr); if (rc == -1) warn(_("%s: unable to probe device"), devname); else if (rc == -2) warnx(_("%s: ambivalent probing result, use wipefs(8)"), devname); else if (rc == 1) warnx(_("%s: not a valid swap partition"), devname); if (rc == 0) { /* supported is SWAPSPACE2 only */ if (blkid_probe_lookup_value(pr, "VERSION", &version, NULL) == 0 && version && strcmp(version, "2")) warnx(_("%s: unsupported swap version '%s'"), devname, version); else return pr; } blkid_free_probe(pr); return NULL; }
/* returns zero when the device has NAME=value (LABEL/UUID) */ static int verify_tag(const char *devname, const char *name, const char *value) { blkid_probe pr; int fd = -1, rc = -1; size_t len; const char *data; int errsv = 0; pr = blkid_new_probe(); if (!pr) return -1; blkid_probe_enable_superblocks(pr, TRUE); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID); blkid_probe_enable_partitions(pr, TRUE); blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS); fd = open(devname, O_RDONLY|O_CLOEXEC); if (fd < 0) { errsv = errno; goto done; } if (blkid_probe_set_device(pr, fd, 0, 0)) goto done; rc = blkid_do_safeprobe(pr); if (rc) goto done; rc = blkid_probe_lookup_value(pr, name, &data, &len); if (!rc) rc = memcmp(value, data, len); done: DBG(EVALUATE, ul_debug("%s: %s verification %s", devname, name, rc == 0 ? "PASS" : "FAILED")); if (fd >= 0) close(fd); blkid_free_probe(pr); /* for non-root users we use unverified udev links */ return errsv == EACCES ? 0 : rc; }
/* * This function prints a warning if the device is not wiped (e.g. wipefs(8). * Please don't call this function if there is already a PT. * * Returns: 0 if nothing found, < 0 on error, 1 if found a signature */ static int check_collisions(struct fdisk_context *cxt) { #ifdef HAVE_LIBBLKID int rc = 0; blkid_probe pr; assert(cxt); assert(cxt->dev_fd >= 0); DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober")); pr = blkid_new_probe(); if (!pr) return -ENOMEM; rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0); if (rc) return rc; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); blkid_probe_enable_partitions(pr, 1); /* we care about the first found FS/raid, so don't call blkid_do_probe() * in loop or don't use blkid_do_fullprobe() ... */ rc = blkid_do_probe(pr); if (rc == 0) { const char *name = NULL; if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 || blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) { cxt->collision = strdup(name); if (!cxt->collision) rc = -ENOMEM; } } blkid_free_probe(pr); return rc; #else return 0; #endif }
/** * mnt_get_fstype: * @devname: device name * @ambi: returns TRUE if probing result is ambivalent (optional argument) * @cache: cache for results or NULL * * Returns: filesystem type or NULL in case of error. The result has to be * deallocated by free() if @cache is NULL. */ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache) { blkid_probe pr; const char *data; char *type = NULL; int rc; DBG(CACHE, ul_debugobj(cache, "get %s FS type", devname)); if (cache) { char *val = NULL; rc = __mnt_cache_find_tag_value(cache, devname, "TYPE", &val); if (ambi) *ambi = rc == -2 ? TRUE : FALSE; return rc ? NULL : val; } /* * no cache, probe directly */ pr = blkid_new_probe_from_filename(devname); if (!pr) return NULL; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); rc = blkid_do_safeprobe(pr); DBG(CACHE, ul_debugobj(cache, "libblkid rc=%d", rc)); if (!rc && !blkid_probe_lookup_value(pr, "TYPE", &data, NULL)) type = strdup(data); if (ambi) *ambi = rc == -2 ? TRUE : FALSE; blkid_free_probe(pr); return type; }
static struct wipe_desc * read_offsets(struct wipe_desc *wp, const char *fname, int zap) { blkid_probe pr; int rc; if (!fname) return NULL; pr = blkid_new_probe_from_filename(fname); if (!pr) errx(EXIT_FAILURE, _("error: %s: probing initialization failed"), fname); blkid_probe_enable_superblocks(pr, 0); /* enabled by default ;-( */ blkid_probe_enable_partitions(pr, 1); rc = blkid_do_fullprobe(pr); blkid_probe_enable_partitions(pr, 0); if (rc == 0) { const char *type = NULL; blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL); warnx(_("WARNING: %s: appears to contain '%s' " "partition table"), fname, type); } blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID); while (blkid_do_probe(pr) == 0) { wp = get_offset_from_probe(wp, pr, zap); if (!wp) break; } blkid_free_probe(pr); return wp; }
/** * mnt_free_cache: * @cache: pointer to struct libmnt_cache instance * * Deallocates the cache. */ void mnt_free_cache(struct libmnt_cache *cache) { size_t i; if (!cache) return; DBG(CACHE, mnt_debug_h(cache, "free")); for (i = 0; i < cache->nents; i++) { struct mnt_cache_entry *e = &cache->ents[i]; if (e->value != e->key) free(e->value); free(e->key); } free(cache->ents); free(cache->filename); if (cache->bc) blkid_put_cache(cache->bc); blkid_free_probe(cache->pr); free(cache); }
static blkid_probe new_probe(const char *devname, int mode) { blkid_probe pr = NULL; if (!devname) return NULL; if (mode) { int fd = open(devname, mode); if (fd < 0) goto error; pr = blkid_new_probe(); if (pr && blkid_probe_set_device(pr, fd, 0, 0)) { close(fd); goto error; } } else pr = blkid_new_probe_from_filename(devname); if (!pr) goto error; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID); blkid_probe_enable_partitions(pr, 1); blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC); return pr; error: blkid_free_probe(pr); err(EXIT_FAILURE, _("error: %s: probing initialization failed"), devname); return NULL; }