Skip to content

Commit

Permalink
fix: Remove read-only flag on copy (#12715) (#12735)
Browse files Browse the repository at this point in the history
When copying the local frontend files
remove any read-only flags from the
copies.

fixes #12711

Co-authored-by: caalador <[email protected]>
  • Loading branch information
vaadin-bot and caalador authored Jan 17, 2022
1 parent bbfd1b0 commit 41e5f87
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
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 @@ -74,6 +78,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;
}
}

0 comments on commit 41e5f87

Please sign in to comment.