OtaPalImageState_t otaPal_GetPlatformImageState()

in platform/posix/ota_pal/source/ota_pal_posix.c [683:749]


OtaPalImageState_t otaPal_GetPlatformImageState( OtaFileContext_t * const C )
{
    FILE * pPlatformImageState = NULL;
    OtaImageState_t eSavedAgentState = OtaImageStateUnknown;
    OtaPalImageState_t ePalState = OtaPalImageStateUnknown;
    OtaPalPathGenStatus_t status = OtaPalFileGenSuccess;
    char imageStateFile[ OTA_FILE_PATH_LENGTH_MAX ] = { 0 };

    ( void ) C;

    /* Get file path for the image state file. */
    status = getFilePathFromCWD( imageStateFile, OTA_PLATFORM_IMAGE_STATE_FILE );

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

        if( pPlatformImageState != NULL )
        {
            /* POSIX port using standard library */
            /* coverity[misra_c_2012_rule_21_6_violation] */
            if( 1U != fread( &eSavedAgentState, sizeof( OtaImageState_t ), 1, pPlatformImageState ) )
            {
                /* If an error occurred reading the file, mark the state as aborted. */
                LogError( ( "Failed to read image state file." ) );
                ePalState = OtaPalImageStateInvalid;
            }
            else
            {
                if( eSavedAgentState == OtaImageStateTesting )
                {
                    ePalState = OtaPalImageStatePendingCommit;
                }
                else if( eSavedAgentState == OtaImageStateAccepted )
                {
                    ePalState = OtaPalImageStateValid;
                }
                else
                {
                    ePalState = OtaPalImageStateInvalid;
                }
            }

            /* POSIX port using standard library */
            /* coverity[misra_c_2012_rule_21_6_violation] */
            if( 0 != fclose( pPlatformImageState ) )
            {
                LogError( ( "Failed to close image state file." ) );
                ePalState = OtaPalImageStateInvalid;
            }
        }
        else
        {
            /* If no image state file exists, assume a factory image. */
            ePalState = OtaPalImageStateValid; /*lint !e64 Allow assignment. */
        }
    }

    return ePalState;
}