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

fix: Remove read-only flag on copy #12715

Merged
merged 3 commits into from
Jan 17, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -72,6 +76,12 @@ static void copyLocalResources(File source, File target) {
}
try {
FileUtils.copyDirectory(source, target);
try (Stream<Path> fileStream = Files
.walk(Paths.get(target.getPath()))) {
// used with try-with-resources as defined in walk API note
fileStream.filter(file -> !Files.isWritable(file)).forEach(
filePath -> filePath.toFile().setWritable(true));
}
} catch (IOException e) {
throw new UncheckedIOException(String.format(
"Failed to copy project frontend resources from '%s' to '%s'",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.vaadin.flow.server.frontend;

import java.io.File;
import java.io.IOException;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class TaskCopyLocalFrontendFilesTest {

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void directoryWithReadOnlyFile_copyIsNotReadOnly()
throws IOException {
final File sourceFolder = createReadOnlySource();

final File outFolder = temporaryFolder.newFolder("out");

TaskCopyLocalFrontendFiles.copyLocalResources(sourceFolder, outFolder);

final File copiedReadOnly = new File(outFolder, "readOnly.txt");
Assert.assertTrue(
"Copied files should be writable even when source is readOnly",
copiedReadOnly.canWrite());

}

@Test
public void directoryWithReadOnlyFile_canCopyMultipleTimesToSource()
throws IOException {
final File sourceFolder = createReadOnlySource();

final File outFolder = temporaryFolder.newFolder("out");

TaskCopyLocalFrontendFiles.copyLocalResources(sourceFolder, outFolder);

TaskCopyLocalFrontendFiles.copyLocalResources(sourceFolder, outFolder);
}

private File createReadOnlySource() throws IOException {
final File sourceFolder = temporaryFolder.newFolder("source");
File readOnly = new File(sourceFolder, "readOnly.txt");
readOnly.createNewFile();
Assert.assertTrue("Could not make file read-only",
readOnly.setReadOnly());

return sourceFolder;
}
}