Skip to content

Copy a symbolic link even when its target does not exist - #433

Merged
slachiewicz merged 1 commit into
masterfrom
agent/copyfile-dangling-symlink
Sep 11, 2026
Merged

slachiewicz merged 1 commit into
masterfrom
agent/copyfile-dangling-symlink

Conversation

@slachiewicz

@slachiewicz slachiewicz commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

copyFile(File, File) checked source.exists() before its symbolic-link branch, and File.exists() follows links, so a link whose target is missing was rejected as a missing file. The link branch now runs first. Two smaller gaps in the same path surfaced while testing: the link branch did not create the destination directory the way the regular-file path does, and the filtering overload then tried to copy POSIX permissions onto a dangling link, which follows the link and fails. Both are handled in this change.

The two tests follow the scenarios from #110 by @kwin: a dangling link, and a relative link copied into another directory where its target does not resolve. Both are skipped on Windows like the other symlink tests here.

copyFile is deprecated in favour of Files.copy with NOFOLLOW_LINKS, but it still ships, and maven-filtering hit this same path in MSHARED-1112, so the reordered check is worth having.

Fixes #306.

Verified: mvn -B verify on JDK 17 -> Tests run: 793, Failures: 0, Errors: 0; on master the dangling-link test fails with IOException: File .../dangling does not exist.

This change was created with AI assistance.

copyFile(File, File) checked source.exists() before the symbolic-link
branch, and File.exists() follows links, so a dangling link was rejected
as a missing file. The link branch now runs first and creates the
destination directory like the regular-file path does, and the
permission copy is skipped for a link destination because the link has
no permissions of its own and its target may be absent.

Fixes #306
@slachiewicz slachiewicz added this to the 3.5.0 milestone Sep 11, 2026
@slachiewicz slachiewicz added the bug Something isn't working label Sep 11, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review September 11, 2026 07:58
@slachiewicz
slachiewicz requested a review from elharo September 11, 2026 07:58
@slachiewicz
slachiewicz merged commit 23ddeee into master Sep 11, 2026
15 checks passed
@slachiewicz
slachiewicz deleted the agent/copyfile-dangling-symlink branch September 11, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MSHARED-1176] FileUtils.copyFile should support copying symlinks with non-existing targets

2 participants