From a6df290392a20141bdd0ac023742290a8c539dad Mon Sep 17 00:00:00 2001 From: Paul Bartell Date: Fri, 14 Jun 2024 12:01:56 -0700 Subject: [PATCH] Add missing call to fclose --- .../fleet_provisioning_csr/CMakeLists.txt | 3 -- .../fleet_provisioning_with_csr_demo.c | 42 ++++--------------- 2 files changed, 8 insertions(+), 37 deletions(-) diff --git a/demos/fleet_provisioning/fleet_provisioning_csr/CMakeLists.txt b/demos/fleet_provisioning/fleet_provisioning_csr/CMakeLists.txt index a15e8bfa37..1dd9f41f52 100644 --- a/demos/fleet_provisioning/fleet_provisioning_csr/CMakeLists.txt +++ b/demos/fleet_provisioning/fleet_provisioning_csr/CMakeLists.txt @@ -38,9 +38,6 @@ target_include_directories( ${DEMO_NAME} ${CMAKE_CURRENT_LIST_DIR} ) set_macro_definitions(TARGETS ${DEMO_NAME} - OPTIONAL - "DOWNLOADED_CERT_WRITE_PATH" - "GENERATED_PRIVATE_KEY_WRITE_PATH" REQUIRED "AWS_IOT_ENDPOINT" "ROOT_CA_CERT_PATH" diff --git a/demos/fleet_provisioning/fleet_provisioning_csr/fleet_provisioning_with_csr_demo.c b/demos/fleet_provisioning/fleet_provisioning_csr/fleet_provisioning_with_csr_demo.c index d392611226..0cab93fba4 100644 --- a/demos/fleet_provisioning/fleet_provisioning_csr/fleet_provisioning_with_csr_demo.c +++ b/demos/fleet_provisioning/fleet_provisioning_csr/fleet_provisioning_with_csr_demo.c @@ -58,10 +58,6 @@ #include #include -#if defined( DOWNLOADED_CERT_WRITE_PATH ) - #include -#endif // DOWNLOADED_CERT_WRITE_PATH - /* Demo config. */ #include "demo_config.h" @@ -614,11 +610,17 @@ int main( int argc, } else { - size_t itemsWritten = fwrite( certificate, certificateLength, 1, pFile ); - if( itemsWritten != 1 ) + size_t bytesWritten = fwrite( certificate, 1, certificateLength, pFile ); + if( bytesWritten != certificateLength ) { status = false; + LogError( ( "Failed to write device certificate to file %s.", CLIENT_CERT_PATH ) ); + } + else + { + LogInfo( ( "Wrote client certificate to path: %s, length: %lu", CLIENT_CERT_PATH, ( unsigned long ) certificateLength ) ); } + fclose( pFile ); } } @@ -765,34 +767,6 @@ int main( int argc, if( status == true ) { LogInfo( ( "Demo completed successfully." ) ); - - #if defined( DOWNLOADED_CERT_WRITE_PATH ) - { - int fd = open( DOWNLOADED_CERT_WRITE_PATH, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR ); - - if( -1 != fd ) - { - const ssize_t writtenBytes = write( fd, certificate, certificateLength ); - - if( writtenBytes == certificateLength ) - { - LogInfo( ( "Written %s successfully.", DOWNLOADED_CERT_WRITE_PATH ) ); - } - else - { - LogError( ( "Could not write to %s. Error: %s.", DOWNLOADED_CERT_WRITE_PATH, strerror( errno ) ) ); - } - - close( fd ); - } - else - { - LogError( ( "Could not open %s. Error: %s.", DOWNLOADED_CERT_WRITE_PATH, strerror( errno ) ) ); - } - } - #else /* if defined( DOWNLOADED_CERT_WRITE_PATH ) */ - LogInfo( ( "NOTE: define DOWNLOADED_CERT_WRITE_PATH in order to have the certificate written to disk." ) ); - #endif // DOWNLOADED_CERT_WRITE_PATH } return ( status == true ) ? EXIT_SUCCESS : EXIT_FAILURE;