Repository navigation
Conversation
…same file FileCopyUtils.copy(File, File) opens the input stream and then the output stream. When both refer to the same file (same path, or a different path to it), opening the output truncates the file before anything is read: the method returns 0 and the file is left empty. Detect this case with Files.isSameFile() and leave the file unchanged, which is what Files.copy() does for the same source and target. Signed-off-by: Martin Mirchev <mmirchev@comp.nus.edu.sg>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FileCopyUtils.copy(File, File)opens the input stream and then the output stream. When both arguments refer to the same file (the same path, or another path to it), opening the output truncates the file before anything is read, so the method returns 0 and the file is left empty:This change detects the case with
Files.isSameFile()and leaves the file unchanged, which is whatFiles.copy()does when the source and target are the same file. A new test,FileCopyUtilsTests.copyFileToSameFile, covers the same path and asub/../srcpath to the same file; it fails before the change and passes after it.Testing:
FileCopyUtilsTests(10 tests),:spring-core:checkstyleMainandcheckstyleTestpass. 7.0.x and 6.2.x have the same code.The issue was found by an LLM-assisted tool and verified manually.