From b467934fb762bf2de27e37e28ac02d42cae7f9a2 Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Wed, 10 Apr 2019 15:37:06 +0100 Subject: [PATCH 1/3] Use Windows-specific renaming function On Windows, rename() fails if the new filename already exists. Use the Windows specific function MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag set instead to do renames. --- library/psa_its_file.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/psa_its_file.c b/library/psa_its_file.c index de60ecfc9..bf55ed3f7 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -33,6 +33,10 @@ #define mbedtls_snprintf snprintf #endif +#if defined(_WIN32) +#include +#endif + #include "psa_crypto_its.h" #include @@ -209,7 +213,12 @@ exit: } if( status == PSA_SUCCESS ) { +#if defined(_WIN32) + if( MoveFileExA( PSA_ITS_STORAGE_TEMP, filename, + MOVEFILE_REPLACE_EXISTING ) == 0 ) +#else if( rename( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) +#endif status = PSA_ERROR_STORAGE_FAILURE; } remove( PSA_ITS_STORAGE_TEMP ); From fdda7de048ff2050e3ae9f1adc52e9a7d65d1817 Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Thu, 11 Apr 2019 12:54:02 +0100 Subject: [PATCH 2/3] Use function-like macro for Windows renaming --- library/psa_its_file.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/library/psa_its_file.c b/library/psa_its_file.c index bf55ed3f7..bc0f84cae 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -62,6 +62,13 @@ #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0" #define PSA_ITS_MAGIC_LENGTH 8 +#if defined(_WIN32) +#define rename_replace_existing( oldpath, newpath ) \ + (!MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING )) +#else +#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath ) +#endif + typedef struct { uint8_t magic[PSA_ITS_MAGIC_LENGTH]; @@ -213,12 +220,7 @@ exit: } if( status == PSA_SUCCESS ) { -#if defined(_WIN32) - if( MoveFileExA( PSA_ITS_STORAGE_TEMP, filename, - MOVEFILE_REPLACE_EXISTING ) == 0 ) -#else - if( rename( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) -#endif + if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) status = PSA_ERROR_STORAGE_FAILURE; } remove( PSA_ITS_STORAGE_TEMP ); From 86095bcaa8c54177b10afdb5bf40beacff707dd8 Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Thu, 11 Apr 2019 14:21:14 +0100 Subject: [PATCH 3/3] Document rename_replace_existing macro --- library/psa_its_file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/psa_its_file.c b/library/psa_its_file.c index bc0f84cae..8cdf783a7 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -62,9 +62,12 @@ #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0" #define PSA_ITS_MAGIC_LENGTH 8 +/* As rename fails on Windows if the new filepath already exists, + * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead. + * Returns 0 on success, nonzero on failure. */ #if defined(_WIN32) #define rename_replace_existing( oldpath, newpath ) \ - (!MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING )) + ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) ) #else #define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath ) #endif