OtaPalStatus_t otaPal_SetPlatformImageState()

in platform/posix/ota_pal/source/ota_pal_posix.c [590:661]


OtaPalStatus_t otaPal_SetPlatformImageState( OtaFileContext_t * const C,
                                             OtaImageState_t eState )
{
    OtaPalMainStatus_t mainErr = OtaPalBadImageState;
    OtaPalPathGenStatus_t status = OtaPalFileGenSuccess;
    int32_t subErr = 0;
    FILE * pPlatformImageState = NULL;
    char imageStateFile[ OTA_FILE_PATH_LENGTH_MAX ] = { 0 };

    ( void ) C;

    if( ( eState != OtaImageStateUnknown ) && ( eState <= OtaLastImageState ) )
    {
        /* Get file path for the image state file. */
        status = getFilePathFromCWD( imageStateFile, OTA_PLATFORM_IMAGE_STATE_FILE );

        if( status == OtaPalFileGenSuccess )
        {
            /* POSIX port using standard library */
            /* coverity[misra_c_2012_rule_21_6_violation] */
            pPlatformImageState = fopen( imageStateFile, "w+b" );
        }
        else
        {
            LogError( ( "Could not generate the absolute path for the file" ) );
        }

        if( pPlatformImageState != NULL )
        {
            /* Write the image state to PlatformImageState.txt. */
            /* POSIX port using standard library */
            /* coverity[misra_c_2012_rule_21_6_violation] */
            if( 1UL == fwrite( &eState, sizeof( OtaImageState_t ), 1, pPlatformImageState ) )
            {
                /* Close PlatformImageState.txt. */
                /* POSIX port using standard library */
                /* coverity[misra_c_2012_rule_21_6_violation] */
                if( 0 == fclose( pPlatformImageState ) )
                {
                    mainErr = OtaPalSuccess;
                }
                else
                {
                    LogError( ( "Unable to close image state file." ) );
                    subErr = errno;
                }
            }
            else
            {
                LogError( ( "Unable to write to image state file. error-- %d", errno ) );
                subErr = errno;

                /* The file should be closed, but errno passed out is fwrite error */
                /* POSIX port using standard library */
                /* coverity[misra_c_2012_rule_21_6_violation] */
                ( void ) fclose( pPlatformImageState );
            }
        }
        else
        {
            LogError( ( "Unable to open image state file. Path: %s error: %s", imageStateFile, strerror( errno ) ) );
            subErr = errno;
        }
    }
    else /* Image state invalid. */
    {
        LogError( ( "Invalid image state provided." ) );
    }

    /* Allow calls to fopen and fclose in this context. */
    return OTA_PAL_COMBINE_ERR( mainErr, subErr );
}