Skip to content

Commit a780f24

Browse files
Merge remote-tracking branch 'upstream-crypto/development' into psa-api-beta2-merge-development
2 parents 2b522db + 125a1e9 commit a780f24

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

library/psa_its_file.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#define mbedtls_snprintf snprintf
3434
#endif
3535

36+
#if defined(_WIN32)
37+
#include <windows.h>
38+
#endif
39+
3640
#include "psa_crypto_its.h"
3741

3842
#include <limits.h>
@@ -58,6 +62,16 @@
5862
#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
5963
#define PSA_ITS_MAGIC_LENGTH 8
6064

65+
/* As rename fails on Windows if the new filepath already exists,
66+
* use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
67+
* Returns 0 on success, nonzero on failure. */
68+
#if defined(_WIN32)
69+
#define rename_replace_existing( oldpath, newpath ) \
70+
( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
71+
#else
72+
#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
73+
#endif
74+
6175
typedef struct
6276
{
6377
uint8_t magic[PSA_ITS_MAGIC_LENGTH];
@@ -209,7 +223,7 @@ psa_status_t psa_its_set( psa_storage_uid_t uid,
209223
}
210224
if( status == PSA_SUCCESS )
211225
{
212-
if( rename( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
226+
if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
213227
status = PSA_ERROR_STORAGE_FAILURE;
214228
}
215229
remove( PSA_ITS_STORAGE_TEMP );

0 commit comments

Comments
 (0)