Skip to content
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

[MSHARED-1330] Always overwrite files #97

Merged
merged 5 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -305,6 +307,8 @@ static boolean isEmpty(final String string) {
* @throws IOException if an IO error occurs during copying or filtering
*/
public static void copyFile(File from, File to, String encoding, FilterWrapper[] wrappers) throws IOException {
setAllPermissions(to);

if (wrappers == null || wrappers.length == 0) {
try (OutputStream os = new CachingOutputStream(to.toPath())) {
Files.copy(from.toPath(), os);
Expand Down Expand Up @@ -371,6 +375,23 @@ private static void copyFilePermissions(File source, File destination) throws IO
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static void setAllPermissions(File file) throws IOException {
gnodet marked this conversation as resolved.
Show resolved Hide resolved
if (file.exists()) {
try {
Files.setPosixFilePermissions(
file.toPath(),
EnumSet.of(
PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE,
PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE,
PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_WRITE));
gnodet marked this conversation as resolved.
Show resolved Hide resolved
} catch (UnsupportedOperationException e) {
file.setReadable(true);
file.setWritable(true);
}
}
}

private static Charset charset(String encoding) {
if (encoding == null || encoding.isEmpty()) {
return Charset.defaultCharset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* @author John Casey
Expand Down Expand Up @@ -148,4 +149,20 @@ void escapeWindowsPathNotAtBeginning() throws Exception {
"jdbc:derby:C:\\\\Users\\\\Administrator/test;create=true",
FilteringUtils.escapeWindowsPath("jdbc:derby:C:\\Users\\Administrator/test;create=true"));
}

// MSHARED-1330
@Test
void copyReadOnlyFileTwice() throws Exception {
File temp = File.createTempFile("pre-", ".txt");
temp.setReadOnly();

File out = File.createTempFile("out-", ".txt");
out.delete();

FilteringUtils.copyFile(temp, out, "UTF-8", new FilterWrapper[0]);
assertFalse(out.canWrite());

FilteringUtils.copyFile(temp, out, "UTF-8", new FilterWrapper[0]);
assertFalse(out.canWrite());
}
}