Skip to content

Test case and proposed fix for #38 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ limitations under the License.
</distributionManagement>

<properties>
<javaVersion>6</javaVersion>
<javaVersion>7</javaVersion>
</properties>

<dependencies>
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import java.io.Writer;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.attribute.DosFileAttributes;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -1094,6 +1096,26 @@ public static void copyFile( final File source, final File destination )
private static void doCopyFile( File source, File destination )
throws IOException
{
// If the source file is read-only on windows, that's probably not what we want in the destination. e.g.
// jetty won't start if the web.xml or jetty-web.xml is read-only, for some reason. So after we copy,
// clear the read-only flag.
boolean setWriteable = Os.isFamily(Os.FAMILY_WINDOWS)
Copy link
Member

Choose a reason for hiding this comment

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

I believe this is the wrong place for this. It must be in NioFiles.

&& source.exists()
&& !source.canWrite()
&& Files.readAttributes(source.toPath(), DosFileAttributes.class)
.isReadOnly();

// Special-case for Windows read-only file attribute on the destination file -- if it's set, unset it so
// that replacement of the destination doesn't fail. Files.copy will apparently not REPLACE_EXISTING in
// that case, at least on Windows.
if (Os.isFamily(Os.FAMILY_WINDOWS)
&& destination.exists()
&& !destination.canWrite()
&& Files.readAttributes(destination.toPath(), DosFileAttributes.class)
.isReadOnly()) {
destination.setWritable(true);
}

// offload to operating system if supported
if ( Java7Detector.isJava7() )
{
Expand All @@ -1103,6 +1125,10 @@ private static void doCopyFile( File source, File destination )
{
doCopyFileUsingLegacyIO( source, destination );
}

if (setWriteable) {
destination.setWritable(true);
}
}

private static void doCopyFileUsingLegacyIO( File source, File destination )
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,21 @@ public void testCopyFile3()
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Check Full copy", destination.length() == testFile2Size );
}

public void testCopyOverReadOnlyFile()
throws IOException
{
final File destination = new File( getTestDirectory(), "copy2.txt" );

// Make sure file exists and is read-only
assertTrue(destination.createNewFile());
assertTrue(destination.setReadOnly());;

// Copy
FileUtils.copyFile( testFile1, destination );
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Check Full copy", destination.length() == testFile2Size );
}

// copyFileIfModified

Expand Down