Fix file.move failing on 2nd move when createParentDirectories=true and overwrite=true - #300
Open
GDLMadushanka wants to merge 1 commit into
Open
Fix file.move failing on 2nd move when createParentDirectories=true and overwrite=true#300GDLMadushanka wants to merge 1 commit into
GDLMadushanka wants to merge 1 commit into
Conversation
…nd overwrite=true MoveFiles.moveFile() was calling createFolder() on the destination FILE path instead of the destination's parent directory. On the first move this worked by accident (VFS created the path as a folder and moveTo() overwrote it); on the second move the destination already existed as a regular file, so VFS threw "Could not create folder ... because it already exists and is a file" at MoveFiles.java:289 — before moveTo() was ever reached, making overwrite=true silently ineffective. Fix: resolve destinationFile.getParent() and call createFolder() only when the parent is non-null and does not yet exist. The !overWrite && exists() guard and moveTo() call are unchanged; the folder-move path (moveFolder) is untouched. Adds MoveFilesParentDirectoryTest (5 new unit tests) covering the regression, parent-dir creation, overwrite=false negative path, and renameTo + createParentDirectories. Full unit suite: 15 run, 0 failures, 1 skip (pre-existing smb2 case). Fixes: wso2/product-integrator-mi#4934 Patch: wso2-enterprise/wso2-integration-internal#5082
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.
Summary
Fixes wso2/product-integrator-mi#4934
The
file.moveoperation fails withFileSystemException: Could not create folder "...abc.txt" because it already exists and is a fileon every move after the first, whencreateParentDirectories=trueandoverwrite=trueare both set.Root Cause
MoveFiles.moveFile()(line 289) was callingcreateFolder()on the full destination file path instead of the destination's parent directory:/target/abc.txtdoes not exist → VFS creates it as a folder, thenmoveTo()overwrites it with the file. Works by accident./target/abc.txtalready exists as a regular file → VFS refuses to create a folder where a file already exists →FileSystemException. Because this exception is thrown beforemoveTo(),overwrite=trueis silently ineffective.Fix
Resolve
destinationFile.getParent()and callcreateFolder()only when the parent is non-null and does not yet exist:The
!overWrite && destinationFile.exists()guard, theelse/debug-log branch, andsrcFile.moveTo(destinationFile)are unchanged. ThemoveFolderpath is untouched.Files Changed
src/main/java/org/wso2/carbon/connector/operations/MoveFiles.javadestinationFile.createFolder()with guarded parent-directory creationsrc/test/java/org/wso2/carbon/connector/operations/MoveFilesParentDirectoryTest.javaMoveFiles.moveFile()(see below)src/test/resources/testng-unit.xmlMoveFilesParentDirectoryTestin the unit suiteTests Added
MoveFilespreviously had zero automated tests. AddedMoveFilesParentDirectoryTest(5 tests) using a realStandardFileSystemManageragainst a temp directory — no MI server or carbon zip required:createParentDirectories=true+overwrite=true, move TWICE (the reported regression)createFolder-on-destination logiccreateParentDirectories=trueinto a missing target dircreateParentDirectories=true+overwrite=false, dest presentFileAlreadyExistsException; dest untouched; no folder-creation exceptionrenameTo+createParentDirectories=trueinto missing dirFull unit suite result: Tests run: 15, Failures: 0, Skips: 1 (9 pre-existing
WriteFileTempFileResolutionTestcases + 5 new cases; 1 skip is the pre-existing smb2 case).Verification
Independently verified on a freshly extracted WSO2 MI 4.4.0 pack:
fix/issue-5082working tree (mvn clean package -DskipTests).FILE_CONN(LOCAL,workingDir=/tmp/file5082) +MoveTestAPI(overwrite=true) +MoveNoOverwriteAPI(overwrite=false).{"success":true}— file moved,/targetcreated as directory, file inside it.{"success":true}— destination overwritten, source removed. Zero "Could not create folder" errors in the full server log./targetrecreated, file inside.FileAlreadyExistsException; destination untouched; no folder-creation exception.grep -c "Could not create folder"across the entire verify session = 0.