Skip to content

[rcore] Fix FileCopy() and FileMove() reporting success when the copy failed - #6364

Open
b84330808 wants to merge 1 commit into
raysan5:masterfrom
b84330808:fix-filecopy-result
Open

b84330808 wants to merge 1 commit into
raysan5:masterfrom
b84330808:fix-filecopy-result

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Since #5920, FileCopy() sets result = 0 once the destination directory exists and never resets it, so it returns 0 when the source doesn't exist or is empty, or when the destination can't be written (read-only file, a directory, ...).

FileMove() relies on that result and then only checks that the destination exists with the same size. When the destination is an existing read-only file of the same size, nothing is copied but the source file is removed.

SaveFileText("a.txt", "AAAAA");
SaveFileText("b.txt", "BBBBB");   // then mark b.txt read-only
FileMove("a.txt", "b.txt");       // returns 0, a.txt is gone, b.txt still has BBBBB

FileCopy() now returns 0 only when the data was saved, and FileMove() returns -1 when the copied file doesn't check out.

…copy failed

Since raysan5#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.
Comment thread src/rcore.c
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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If FileCopy() returns result == 0, this point should never be reached, why check it?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants