Skip to content

Commit

Permalink
Test case and proposed fix for codehaus-plexus#38
Browse files Browse the repository at this point in the history
  • Loading branch information
awallgren committed Apr 8, 2018
1 parent ac81ee2 commit 6bcc3d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
26 changes: 13 additions & 13 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,11 @@
import org.codehaus.plexus.util.io.InputStreamFacade;
import org.codehaus.plexus.util.io.URLInputStreamFacade;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.*;

This comment has been minimized.

Copy link
@khmarbaise

khmarbaise Apr 8, 2018

Can you please leave the imports as it....

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 +1084,16 @@ public static void copyFile( final File source, final File destination )
private static void doCopyFile( File source, File destination )
throws IOException
{
// Special-case for Windows read-only file attribute -- 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 Down
29 changes: 19 additions & 10 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
* limitations under the License.
*/

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import org.junit.Assume;

import java.io.*;

This comment has been minimized.

Copy link
@khmarbaise

khmarbaise Apr 8, 2018

Can you please leave the imports as it...

import java.net.URL;
import java.nio.file.Files;
import java.util.Properties;

/**
Expand Down Expand Up @@ -394,6 +388,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

0 comments on commit 6bcc3d2

Please sign in to comment.