From c34c5e6e8f44e1486344304070474476c80e9d94 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Wed, 4 Nov 2026 23:16:56 +0900 Subject: [PATCH 1/2] [rcore] Fix `FileCopy()` and `FileMove()` reporting success when the copy failed Since #5920, `FileCopy()` sets `result` to 0 once the destination directory exists and never resets it, so it returns 0 when the source is missing or empty or when the destination can't be written. `FileMove()` trusts that result. If the destination is an existing file of the same size that can't be overwritten (e.g. read-only), the length check passes and the source file is removed although nothing was copied. Only return 0 from `FileCopy()` when the data was saved, and make `FileMove()` return -1 when the copied file does not check out. --- src/rcore.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index cbb2761d5a97..5670bcae5f78 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2243,13 +2243,12 @@ int FileCopy(const char *srcPath, const char *dstPath) int srcDataSize = 0; unsigned char *srcFileData = LoadFileData(srcPath, &srcDataSize); - // Create required paths if they do not exist - if (DirectoryExists(GetDirectoryPath(dstPath))) result = 0; // Already exists - else result = MakeDirectory(GetDirectoryPath(dstPath)); - - if (result == 0) // Directory created successfully or already exists + if ((srcFileData != NULL) && (srcDataSize > 0)) { - if ((srcFileData != NULL) && (srcDataSize > 0)) + // Create required paths if they do not exist + const char *dstDirPath = GetDirectoryPath(dstPath); + + if (DirectoryExists(dstDirPath) || (MakeDirectory(dstDirPath) == 0)) // Directory created successfully or already exists { bool saved = SaveFileData(dstPath, srcFileData, srcDataSize); if (saved) result = 0; @@ -2279,7 +2278,11 @@ int FileMove(const char *srcPath, const char *dstPath) result = FileRemove(srcPath); if (result != 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to remove source file after copy", srcPath); } - else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to copy file to [%s]", srcPath, dstPath); + else + { + TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to copy file to [%s]", srcPath, dstPath); + result = -1; + } } } else TRACELOG(LOG_WARNING, "FILEIO: [%s] Source file does not exist", srcPath); From 018a432745aa9df7adea49b0638c9bc31b556591 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Tue, 10 Nov 2026 10:56:56 +0900 Subject: [PATCH 2/2] FileMove: drop the extra failure result, FileCopy already reports it --- src/rcore.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index 5670bcae5f78..26392c3d5a39 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2278,11 +2278,7 @@ int FileMove(const char *srcPath, const char *dstPath) result = FileRemove(srcPath); if (result != 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to remove source file after copy", srcPath); } - else - { - TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to copy file to [%s]", srcPath, dstPath); - result = -1; - } + else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to copy file to [%s]", srcPath, dstPath); } } else TRACELOG(LOG_WARNING, "FILEIO: [%s] Source file does not exist", srcPath);