AzureSphere/AzureSphereSampleDevice/main.c
static ExitCode ReadIoTEdgeCaCertContent(void)
{
    int certFd = -1;
    off_t fileSize = 0;

    certFd = Storage_OpenFileInImagePackage(iotEdgeRootCAPath);
    if (certFd == -1) {
        Log_Debug("ERROR: Storage_OpenFileInImagePackage failed with error code: %d (%s).\n", errno,
                  strerror(errno));
        return ExitCode_IoTEdgeRootCa_Open_Failed;
    }

    // Get the file size.
    fileSize = lseek(certFd, 0, SEEK_END);
    if (fileSize == -1) {
        Log_Debug("ERROR: lseek SEEK_END: %d (%s)\n", errno, strerror(errno));
        close(certFd);
        return ExitCode_IoTEdgeRootCa_LSeek_Failed;
    }

    // Reset the pointer to start of the file.
    if (lseek(certFd, 0, SEEK_SET) < 0) {
        Log_Debug("ERROR: lseek SEEK_SET: %d (%s)\n", errno, strerror(errno));
        close(certFd);
        return ExitCode_IoTEdgeRootCa_LSeek_Failed;
    }

    if (fileSize == 0) {
        Log_Debug("File size invalid for %s\r\n", iotEdgeRootCAPath);
        close(certFd);
        return ExitCode_IoTEdgeRootCa_FileSize_Invalid;
    }

    if (fileSize > MAX_ROOT_CA_CERT_CONTENT_SIZE) {
        Log_Debug("File size for %s is %lld bytes. Max file size supported is %d bytes.\r\n",
                  iotEdgeRootCAPath, fileSize, MAX_ROOT_CA_CERT_CONTENT_SIZE);
        close(certFd);
        return ExitCode_IoTEdgeRootCa_FileSize_TooLarge;
    }

    // Copy the file into the buffer.
    ssize_t read_size = read(certFd, &iotEdgeRootCACertContent, (size_t)fileSize);
    if (read_size != (size_t)fileSize) {
        Log_Debug("Error reading file %s\r\n", iotEdgeRootCAPath);
        close(certFd);
        return ExitCode_IoTEdgeRootCa_FileRead_Failed;
    }

    // Add the null terminator at the end.
    iotEdgeRootCACertContent[fileSize] = '\0';

    close(certFd);
    return ExitCode_Success;
}
/// <summary>
///     Read the certificate file and provide a null terminated string containing the certificate.
///     The function logs an error and returns an error code if it cannot allocate enough memory to
///     hold the certificate content.
/// </summary>
/// <returns>ExitCode_Success on success, any other exit code on error</returns>

Read the certificate file and provide a null terminated string containing the certificate.
The function logs an error and returns an error code if it cannot allocate enough memory to
hold the certificate content.

ExitCode_Success on success, any other exit code on error
Read the certificate file and provide a null terminated string containing the certificate . The function logs an error and returns an error code if it cannot allocate enough memory to hold the certificate content . ExitCode_Success on success any other exit code on error
static ExitCode ReadIoTEdgeCaCertContent(void)
{
    int certFd = -1;
    off_t fileSize = 0;
    certFd = Storage_OpenFileInImagePackage(iotEdgeRootCAPath);
    if (certFd == -1) {
        Log_Debug("ERROR: Storage_OpenFileInImagePackage failed with error code: %d (%s).\n", errno,
                  strerror(errno));
        return ExitCode_IoTEdgeRootCa_Open_Failed;
    }
    fileSize = lseek(certFd, 0, SEEK_END);
    if (fileSize == -1) {
        Log_Debug("ERROR: lseek SEEK_END: %d (%s)\n", errno, strerror(errno));
        close(certFd);
        return ExitCode_IoTEdgeRootCa_LSeek_Failed;
    }
    if (lseek(certFd, 0, SEEK_SET) < 0) {
        Log_Debug("ERROR: lseek SEEK_SET: %d (%s)\n", errno, strerror(errno));
        close(certFd);
        return ExitCode_IoTEdgeRootCa_LSeek_Failed;
    }
    if (fileSize == 0) {
        Log_Debug("File size invalid for %s\r\n", iotEdgeRootCAPath);
        close(certFd);
        return ExitCode_IoTEdgeRootCa_FileSize_Invalid;
    }
    if (fileSize > MAX_ROOT_CA_CERT_CONTENT_SIZE) {
        Log_Debug("File size for %s is %lld bytes. Max file size supported is %d bytes.\r\n",
                  iotEdgeRootCAPath, fileSize, MAX_ROOT_CA_CERT_CONTENT_SIZE);
        close(certFd);
        return ExitCode_IoTEdgeRootCa_FileSize_TooLarge;
    }
    ssize_t read_size = read(certFd, &iotEdgeRootCACertContent, (size_t)fileSize);
    if (read_size != (size_t)fileSize) {
        Log_Debug("Error reading file %s\r\n", iotEdgeRootCAPath);
        close(certFd);
        return ExitCode_IoTEdgeRootCa_FileRead_Failed;
    }
    iotEdgeRootCACertContent[fileSize] = '\0';
    close(certFd);
    return ExitCode_Success;
}
static ExitCode ReadIoTEdgeCaCertContent ( void ) { int certFd = -1 ; off_t fileSize = 0 ; certFd = Storage_OpenFileInImagePackage ( iotEdgeRootCAPath ) ; if ( certFd == -1 ) { Log_Debug ( " \n " , errno , strerror ( errno ) ) ; return ExitCode_IoTEdgeRootCa_Open_Failed ; } fileSize = lseek ( certFd , 0 , SEEK_END ) ; if ( fileSize == -1 ) { Log_Debug ( " \n " , errno , strerror ( errno ) ) ; close ( certFd ) ; return ExitCode_IoTEdgeRootCa_LSeek_Failed ; } if ( lseek ( certFd , 0 , SEEK_SET ) < 0 ) { Log_Debug ( " \n " , errno , strerror ( errno ) ) ; close ( certFd ) ; return ExitCode_IoTEdgeRootCa_LSeek_Failed ; } if ( fileSize == 0 ) { Log_Debug ( " \r \n " , iotEdgeRootCAPath ) ; close ( certFd ) ; return ExitCode_IoTEdgeRootCa_FileSize_Invalid ; } if ( fileSize > MAX_ROOT_CA_CERT_CONTENT_SIZE ) { Log_Debug ( " \r \n " , iotEdgeRootCAPath , fileSize , MAX_ROOT_CA_CERT_CONTENT_SIZE ) ; close ( certFd ) ; return ExitCode_IoTEdgeRootCa_FileSize_TooLarge ; } ssize_t read_size = read ( certFd , & iotEdgeRootCACertContent , ( size_t ) fileSize ) ; if ( read_size != ( size_t ) fileSize ) { Log_Debug ( " \r \n " , iotEdgeRootCAPath ) ; close ( certFd ) ; return ExitCode_IoTEdgeRootCa_FileRead_Failed ; } iotEdgeRootCACertContent [ fileSize ] = ' \0 ' ; close ( certFd ) ; return ExitCode_Success ; }
<summary>
Read the certificate file and provide a null terminated string containing the certificate.
<summary > Read the certificate file and provide a null terminated string containing the certificate .
// Get the file size. // Reset the pointer to start of the file. // Copy the file into the buffer. // Add the null terminator at the end.