From 49aec72c5b5ff46606a52d2976321662c8f060ce Mon Sep 17 00:00:00 2001 From: Mikael Grankvist Date: Thu, 13 Jan 2022 10:30:11 +0200 Subject: [PATCH] fix: Remove read-only flag on copy When copying the local frontend files remove any read-only flags from the copies. fixes #12711 --- .../frontend/TaskCopyLocalFrontendFiles.java | 5 ++ .../TaskCopyLocalFrontendFilesTest.java | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFilesTest.java diff --git a/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFiles.java b/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFiles.java index 098ca5e0eb5..3abaeda7922 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFiles.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFiles.java @@ -18,6 +18,8 @@ import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Objects; import org.apache.commons.io.FileUtils; @@ -72,6 +74,9 @@ static void copyLocalResources(File source, File target) { } try { FileUtils.copyDirectory(source, target); + Files.walk(Paths.get(target.getPath())) + .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'", diff --git a/flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFilesTest.java b/flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFilesTest.java new file mode 100644 index 00000000000..ebb5b65c3ed --- /dev/null +++ b/flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskCopyLocalFrontendFilesTest.java @@ -0,0 +1,55 @@ +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()); + + Assert.assertFalse("Should not be able to write read-only file", + readOnly.canWrite()); + return sourceFolder; + } +}