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/libfdisk/src/context.c
int fdisk_assign_device(struct fdisk_context* cxt, const char* fname,
                        int readonly)
{
    int fd;

    DBG(CXT, ul_debugobj(cxt, "assigning device %s", fname));
    assert(cxt);

    /* redirect request to parent */
    if (cxt->parent)
    {
        int rc, org = fdisk_is_listonly(cxt->parent);

        /* assign_device() is sensitive to "listonly" mode, so let's
         * follow the current context setting for the parent to avoid
         * unwanted extra warnings. */
        fdisk_enable_listonly(cxt->parent, fdisk_is_listonly(cxt));

        rc = fdisk_assign_device(cxt->parent, fname, readonly);
        fdisk_enable_listonly(cxt->parent, org);

        if (!rc)
            rc = init_nested_from_parent(cxt, 0);
        if (!rc)
            fdisk_probe_labels(cxt);
        return rc;
    }

    reset_context(cxt);

    fd = open(fname, (readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
    if (fd < 0)
        goto fail;

    if (fstat(fd, &cxt->dev_st) != 0)
        goto fail;

    cxt->readonly = readonly;
    cxt->dev_fd = fd;
    cxt->dev_path = strdup(fname);
    if (!cxt->dev_path)
        goto fail;

    fdisk_discover_topology(cxt);
    fdisk_discover_geometry(cxt);

    fdisk_apply_user_device_properties(cxt);

    if (fdisk_read_firstsector(cxt) < 0)
        goto fail;

    fdisk_probe_labels(cxt);

    fdisk_apply_label_device_properties(cxt);

    /* warn about obsolete stuff on the device if we aren't in
     * list-only mode and there is not PT yet */
    if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt) &&
        fdisk_check_collisions(cxt) < 0)
        goto fail;

    DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s]", fname,
                         readonly ? "READ-ONLY" : "READ-WRITE"));
    return 0;
fail:
{
    int rc = -errno;
    if (fd >= 0)
        close(fd);
    DBG(CXT, ul_debugobj(cxt, "failed to assign device [rc=%d]", rc));
    return rc;
}
}
/**
 * fdisk_assign_device:
 * @cxt: context
 * @fname: path to the device to be handled
 * @readonly: how to open the device
 *
 * Open the device, discovery topology, geometry, detect disklabel and switch
 * the current label driver to reflect the probing result.
 *
 * Note that this function resets all generic setting in context. If the @cxt
 * is nested context then the device is assigned to the parental context and
 * necessary properties are copied to the @cxt. The change is propagated in
 * child->parent direction only. It's impossible to use a different device for
 * primary and nested contexts.
 *
 * Returns: 0 on success, < 0 on error.
 */
@cxt: context
@fname: path to the device to be handled
@readonly: how to open the device

Open the device, discovery topology, geometry, detect disklabel and switch
the current label driver to reflect the probing result.

Note that this function resets all generic setting in context. If the @cxt
is nested context then the device is assigned to the parental context and
necessary properties are copied to the @cxt. The change is propagated in
child->parent direction only. It's impossible to use a different device for
primary and nested contexts.

0 on success, < 0 on error.
@cxt : context @fname : path to the device to be handled @readonly : how to open the device Open the device discovery topology geometry detect disklabel and switch the current label driver to reflect the probing result . Note that this function resets all generic setting in context . If the @cxt is nested context then the device is assigned to the parental context and necessary properties are copied to the @cxt . The change is propagated in child - > parent direction only . It ' s impossible to use a different device for primary and nested contexts . 0 on success < 0 on error .
int fdisk_assign_device(struct fdisk_context* cxt, const char* fname,
                        int readonly)
{
    int fd;
    DBG(CXT, ul_debugobj(cxt, "assigning device %s", fname));
    assert(cxt);
    if (cxt->parent)
    {
        int rc, org = fdisk_is_listonly(cxt->parent);
        fdisk_enable_listonly(cxt->parent, fdisk_is_listonly(cxt));
        rc = fdisk_assign_device(cxt->parent, fname, readonly);
        fdisk_enable_listonly(cxt->parent, org);
        if (!rc)
            rc = init_nested_from_parent(cxt, 0);
        if (!rc)
            fdisk_probe_labels(cxt);
        return rc;
    }
    reset_context(cxt);
    fd = open(fname, (readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
    if (fd < 0)
        goto fail;
    if (fstat(fd, &cxt->dev_st) != 0)
        goto fail;
    cxt->readonly = readonly;
    cxt->dev_fd = fd;
    cxt->dev_path = strdup(fname);
    if (!cxt->dev_path)
        goto fail;
    fdisk_discover_topology(cxt);
    fdisk_discover_geometry(cxt);
    fdisk_apply_user_device_properties(cxt);
    if (fdisk_read_firstsector(cxt) < 0)
        goto fail;
    fdisk_probe_labels(cxt);
    fdisk_apply_label_device_properties(cxt);
    if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt) &&
        fdisk_check_collisions(cxt) < 0)
        goto fail;
    DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s]", fname,
                         readonly ? "READ-ONLY" : "READ-WRITE"));
    return 0;
fail:
{
    int rc = -errno;
    if (fd >= 0)
        close(fd);
    DBG(CXT, ul_debugobj(cxt, "failed to assign device [rc=%d]", rc));
    return rc;
}
}
int fdisk_assign_device ( struct fdisk_context * cxt , const char * fname , int readonly ) { int fd ; DBG ( CXT , ul_debugobj ( cxt , " " , fname ) ) ; assert ( cxt ) ; if ( cxt -> parent ) { int rc , org = fdisk_is_listonly ( cxt -> parent ) ; fdisk_enable_listonly ( cxt -> parent , fdisk_is_listonly ( cxt ) ) ; rc = fdisk_assign_device ( cxt -> parent , fname , readonly ) ; fdisk_enable_listonly ( cxt -> parent , org ) ; if ( ! rc ) rc = init_nested_from_parent ( cxt , 0 ) ; if ( ! rc ) fdisk_probe_labels ( cxt ) ; return rc ; } reset_context ( cxt ) ; fd = open ( fname , ( readonly ? O_RDONLY : O_RDWR ) | O_CLOEXEC ) ; if ( fd < 0 ) goto fail ; if ( fstat ( fd , & cxt -> dev_st ) != 0 ) goto fail ; cxt -> readonly = readonly ; cxt -> dev_fd = fd ; cxt -> dev_path = strdup ( fname ) ; if ( ! cxt -> dev_path ) goto fail ; fdisk_discover_topology ( cxt ) ; fdisk_discover_geometry ( cxt ) ; fdisk_apply_user_device_properties ( cxt ) ; if ( fdisk_read_firstsector ( cxt ) < 0 ) goto fail ; fdisk_probe_labels ( cxt ) ; fdisk_apply_label_device_properties ( cxt ) ; if ( ! fdisk_is_listonly ( cxt ) && ! fdisk_has_label ( cxt ) && fdisk_check_collisions ( cxt ) < 0 ) goto fail ; DBG ( CXT , ul_debugobj ( cxt , " " , fname , readonly ? " " : " " ) ) ; return 0 ; fail : { int rc = - errno ; if ( fd >= 0 ) close ( fd ) ; DBG ( CXT , ul_debugobj ( cxt , " " , rc ) ) ; return rc ; } }
fdisk_assign_device:
 @cxt: context
 @fname: path to the device to be handled
 @readonly: how to open the device
fdisk_assign_device : @cxt : context @fname : path to the device to be handled @readonly : how to open the device
/* redirect request to parent */ /* assign_device() is sensitive to "listonly" mode, so let's
         * follow the current context setting for the parent to avoid
         * unwanted extra warnings. */ /* warn about obsolete stuff on the device if we aren't in
     * list-only mode and there is not PT yet */