openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/util-linux/2.33.2-r0/util-linux-2.33.2/lib/sysfs.c
int sysfs_blkdev_get_wholedisk(struct path_cxt* pc, char* diskname, size_t len,
                               dev_t* diskdevno)
{
    int is_part = 0;

    if (!pc)
        return -1;

    is_part = ul_path_access(pc, F_OK, "partition") == 0;
    if (!is_part)
    {
        /*
         * Extra case for partitions mapped by device-mapper.
         *
         * All regular partitions (added by BLKPG ioctl or kernel PT
         * parser) have the /sys/.../partition file. The partitions
         * mapped by DM don't have such file, but they have "part"
         * prefix in DM UUID.
         */
        char *uuid = NULL, *tmp, *prefix;

        ul_path_read_string(pc, &uuid, "dm/uuid");
        tmp = uuid;
        prefix = uuid ? strsep(&tmp, "-") : NULL;

        if (prefix && strncasecmp(prefix, "part", 4) == 0)
            is_part = 1;
        free(uuid);

        if (is_part && get_dm_wholedisk(pc, diskname, len, diskdevno) == 0)
            /*
             * partitioned device, mapped by DM
             */
            goto done;

        is_part = 0;
    }

    if (!is_part)
    {
        /*
         * unpartitioned device
         */
        if (diskname && !sysfs_blkdev_get_name(pc, diskname, len))
            goto err;
        if (diskdevno)
            *diskdevno = sysfs_blkdev_get_devno(pc);
    }
    else
    {
        /*
         * partitioned device
         *  - readlink /sys/dev/block/8:1   = ../../block/sda/sda1
         *  - dirname  ../../block/sda/sda1 = ../../block/sda
         *  - basename ../../block/sda      = sda
         */
        char linkpath[PATH_MAX];
        char* name;
        ssize_t linklen;

        linklen = ul_path_readlink(pc, linkpath, sizeof(linkpath) - 1, NULL);
        if (linklen < 0)
            goto err;
        linkpath[linklen] = '\0';

        stripoff_last_component(linkpath);        /* dirname */
        name = stripoff_last_component(linkpath); /* basename */
        if (!name)
            goto err;

        sysfs_devname_sys_to_dev(name);
        if (diskname && len)
        {
            strncpy(diskname, name, len);
            diskname[len - 1] = '\0';
        }

        if (diskdevno)
        {
            *diskdevno =
                __sysfs_devname_to_devno(ul_path_get_prefix(pc), name, NULL);
            if (!*diskdevno)
                goto err;
        }
    }

done:
    return 0;
err:
    return -1;
}
/*
 * Returns by @diskdevno whole disk device devno and (optionally) by
 * @diskname the whole disk device name.
 */
Returns by @diskdevno whole disk device devno and (optionally) by
@diskname the whole disk device name.
Returns by @diskdevno whole disk device devno and ( optionally ) by @diskname the whole disk device name .
int sysfs_blkdev_get_wholedisk(struct path_cxt* pc, char* diskname, size_t len,
                               dev_t* diskdevno)
{
    int is_part = 0;
    if (!pc)
        return -1;
    is_part = ul_path_access(pc, F_OK, "partition") == 0;
    if (!is_part)
    {
        char *uuid = NULL, *tmp, *prefix;
        ul_path_read_string(pc, &uuid, "dm/uuid");
        tmp = uuid;
        prefix = uuid ? strsep(&tmp, "-") : NULL;
        if (prefix && strncasecmp(prefix, "part", 4) == 0)
            is_part = 1;
        free(uuid);
        if (is_part && get_dm_wholedisk(pc, diskname, len, diskdevno) == 0)
            goto done;
        is_part = 0;
    }
    if (!is_part)
    {
        if (diskname && !sysfs_blkdev_get_name(pc, diskname, len))
            goto err;
        if (diskdevno)
            *diskdevno = sysfs_blkdev_get_devno(pc);
    }
    else
    {
        char linkpath[PATH_MAX];
        char* name;
        ssize_t linklen;
        linklen = ul_path_readlink(pc, linkpath, sizeof(linkpath) - 1, NULL);
        if (linklen < 0)
            goto err;
        linkpath[linklen] = '\0';
        stripoff_last_component(linkpath);        
        name = stripoff_last_component(linkpath); 
        if (!name)
            goto err;
        sysfs_devname_sys_to_dev(name);
        if (diskname && len)
        {
            strncpy(diskname, name, len);
            diskname[len - 1] = '\0';
        }
        if (diskdevno)
        {
            *diskdevno =
                __sysfs_devname_to_devno(ul_path_get_prefix(pc), name, NULL);
            if (!*diskdevno)
                goto err;
        }
    }
done:
    return 0;
err:
    return -1;
}
int sysfs_blkdev_get_wholedisk ( struct path_cxt * pc , char * diskname , size_t len , dev_t * diskdevno ) { int is_part = 0 ; if ( ! pc ) return -1 ; is_part = ul_path_access ( pc , F_OK , " " ) == 0 ; if ( ! is_part ) { char * uuid = NULL , * tmp , * prefix ; ul_path_read_string ( pc , & uuid , " " ) ; tmp = uuid ; prefix = uuid ? strsep ( & tmp , " " ) : NULL ; if ( prefix && strncasecmp ( prefix , " " , 4 ) == 0 ) is_part = 1 ; free ( uuid ) ; if ( is_part && get_dm_wholedisk ( pc , diskname , len , diskdevno ) == 0 ) goto done ; is_part = 0 ; } if ( ! is_part ) { if ( diskname && ! sysfs_blkdev_get_name ( pc , diskname , len ) ) goto err ; if ( diskdevno ) * diskdevno = sysfs_blkdev_get_devno ( pc ) ; } else { char linkpath [ PATH_MAX ] ; char * name ; ssize_t linklen ; linklen = ul_path_readlink ( pc , linkpath , sizeof ( linkpath ) - 1 , NULL ) ; if ( linklen < 0 ) goto err ; linkpath [ linklen ] = ' \0 ' ; stripoff_last_component ( linkpath ) ; name = stripoff_last_component ( linkpath ) ; if ( ! name ) goto err ; sysfs_devname_sys_to_dev ( name ) ; if ( diskname && len ) { strncpy ( diskname , name , len ) ; diskname [ len - 1 ] = ' \0 ' ; } if ( diskdevno ) { * diskdevno = __sysfs_devname_to_devno ( ul_path_get_prefix ( pc ) , name , NULL ) ; if ( ! * diskdevno ) goto err ; } } done : return 0 ; err : return -1 ; }
Returns by @diskdevno whole disk device devno and (optionally) by
 @diskname the whole disk device name.
Returns by @diskdevno whole disk device devno and ( optionally ) by @diskname the whole disk device name .
/*
         * Extra case for partitions mapped by device-mapper.
         *
         * All regular partitions (added by BLKPG ioctl or kernel PT
         * parser) have the /sys/.../partition file. The partitions
         * mapped by DM don't have such file, but they have "part"
         * prefix in DM UUID.
         */ /*
             * partitioned device, mapped by DM
             */ /*
         * unpartitioned device
         */ /*
         * partitioned device
         *  - readlink /sys/dev/block/8:1   = ../../block/sda/sda1
         *  - dirname  ../../block/sda/sda1 = ../../block/sda
         *  - basename ../../block/sda      = sda
         */ /* dirname */ /* basename */