Skip to content
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
@@ -0,0 +1,18 @@
#
# Copyright (C) 2023 Ashley Scopes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

invoker.goals=clean generate-sources
invoker.buildResult = success
90 changes: 90 additions & 0 deletions protobuf-maven-plugin/src/it/gh-1053-make-proto-writable/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) 2023 Ashley Scopes

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>@project.groupId@.it</groupId>
<artifactId>integration-test-parent</artifactId>
<version>@project.version@</version>
<relativePath>../setup/pom.xml</relativePath>
</parent>

<groupId>gh-1022-url-plugin-pathext-windows</groupId>
<artifactId>gh-1022-url-plugin-pathext-windows</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-jsr223</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>make-proto-read-only</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<script language="groovy"><![CDATA[
def protoDirectory = new File(project.getBaseDir(), "src/main/protobuf")
if (protoDirectory.exists()) {
protoDirectory.eachFileRecurse(groovy.io.FileType.FILES) { file ->
if (file.name.endsWith('.proto')) {
println "Making proto file read-only: ${file}"
file.setWritable(false, true)
}
}
}
]]></script>
</target>
</configuration>
</execution>
</executions>

</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright (C) 2023 Ashley Scopes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

syntax = "proto3";

option java_multiple_files = true;
option java_package = "org.example.message";

message Message {
string id = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 Ashley Scopes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.nio.file.Path

import static org.assertj.core.api.Assertions.assertThat

Path baseDirectory = basedir.toPath().toAbsolutePath()
Path protoFile = baseDirectory.resolve("target/classes/message.proto")
assertThat(protoFile).isWritable();
return true
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
Expand All @@ -39,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -154,13 +157,39 @@ public static List<Path> rebaseFileTree(
);

if (!Files.isDirectory(newPath)) {
setWritable(newPath);
newPaths.add(newPath);
}
}

return Collections.unmodifiableList(newPaths);
}

private static void setWritable(Path path) {
log.trace("Making \"{}\" writable", path);
DosFileAttributeView dosView =
Files.getFileAttributeView(path, DosFileAttributeView.class);
if (dosView != null) {
try {
dosView.setReadOnly(false);
} catch (IOException e) {
log.warn("Failed to set dos read-only attribute of \"{}\"", path, e);
}
}

PosixFileAttributeView posixView =
Files.getFileAttributeView(path, PosixFileAttributeView.class);
if (posixView != null) {
try {
Set<PosixFilePermission> perms = posixView.readAttributes().permissions();
perms.add(PosixFilePermission.OWNER_WRITE);
posixView.setPermissions(perms);
} catch (IOException e) {
log.warn("Failed to set POSIX permissions of \"{}\"", path, e);
}
}
}

public static InputStream newBufferedInputStream(
Path path,
OpenOption... options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.jspecify.annotations.Nullable;
Expand All @@ -38,6 +40,7 @@
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;


/**
Expand Down Expand Up @@ -256,6 +259,39 @@ void rebaseFileTreeCopiesAllNodesBetweenFileSystems() throws IOException {
}
}

@DisplayName(".rebaseFileTree(...) makes target file writable by owner.")
@ParameterizedTest(name = "{0}")
@ValueSource(strings = {"windows", "linux"})
void rebaseFileTreeMakesTargetFileWritableByOwner(String os) throws IOException {
// Given
try (
var sourceFs = "windows".equals(os) ? TestFileSystem.windows() : TestFileSystem.linux();
var targetFs = "windows".equals(os) ? TestFileSystem.windows() : TestFileSystem.linux()
) {
var sourceDir = sourceFs.getRoot().resolve("base").resolve("dir");
Files.createDirectories(sourceDir);
Path sourceFile = sourceDir.resolve("foo.txt");
Files.writeString(sourceFile, "foo");

if (os.equals("windows")) {
Files.setAttribute(sourceFile, "dos:readonly", true);
} else {
Files.setAttribute(sourceFile, "posix:permissions", Set.of(PosixFilePermission.OWNER_READ));
}
assertThat(Files.isWritable(sourceFile)).isFalse();

var targetDir = targetFs.getRoot().resolve("target").resolve("directory");

// When
try (var files = Files.walk(sourceDir)) {
FileUtils.rebaseFileTree(sourceDir, targetDir, files);
}

// Then
assertThat(targetDir.resolve("foo.txt")).isWritable();
}
}

@DisplayName(".newBufferedInputStream(Path, OpenOption... reads a file with a buffer")
@Test
void newBufferedInputStreamReadsFileWithBuffer(@TempDir Path dir) throws IOException {
Expand Down
Loading