Skip to content

Commit

Permalink
[core] Set executable bit when unpacking Zip files. Fixes #752
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Feb 15, 2022
1 parent fa31343 commit cd40677
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum FileType {
TAR_XZ("tar.xz"),
TBZ2("tbz2"),
TGZ("tgz"),
TXZ("tXz"),
TXZ("txz"),
ZIP("zip");

private final String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private static void unpackArchive(String basename, File destinationDir, ArchiveI
try (OutputStream o = Files.newOutputStream(file.toPath())) {
IOUtils.copy(in, o);
Files.setLastModifiedTime(file.toPath(), FileTime.from(entry.getLastModifiedDate().toInstant()));
chmod(file, getEntryMode(entry));
chmod(file, getEntryMode(entry, file));
}
}
}
Expand Down Expand Up @@ -358,18 +358,22 @@ private static String getLinkName(ArchiveInputStream in, ArchiveEntry entry) thr
return "";
}

private static int getEntryMode(ArchiveEntry entry) {
private static int getEntryMode(ArchiveEntry entry, File file) {
if (entry instanceof TarArchiveEntry) {
return getEntryMode(entry, ((TarArchiveEntry) entry).getMode());
return getEntryMode(entry, ((TarArchiveEntry) entry).getMode(), file);
}
return getEntryMode(entry, ((ZipArchiveEntry) entry).getUnixMode());
return getEntryMode(entry, ((ZipArchiveEntry) entry).getUnixMode(), file);
}

private static int getEntryMode(ArchiveEntry entry, int mode) {
private static int getEntryMode(ArchiveEntry entry, int mode, File file) {
int unixMode = mode & 0777;
if (unixMode == 0) {
if (entry.isDirectory()) {
unixMode = 0755;
} else if ("bin".equalsIgnoreCase(file.getParentFile().getName())) {
// zipEntry.unixMode returns 0 most times even if the entry is executable
// force executable bit only if parent dir == 'bin'
unixMode = 0777;
} else {
unixMode = 0644;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* 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
*
* https://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.
*/
package org.jreleaser.test;

import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* @author Andres Almiray
* @since 1.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(PlatformCondition.class)
public @interface Platform {
boolean match() default true;

String platform() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* 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
*
* https://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.
*/
package org.jreleaser.test;

import org.jreleaser.util.PlatformUtils;
import org.jreleaser.util.SimpleJReleaserLoggerAdapter;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.Optional;

import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;

/**
* @author Andres Almiray
* @since 1.0.0
*/
public class PlatformCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<Platform> optional = findAnnotation(context.getElement(), Platform.class);
if (optional.isPresent()) {
PlatformUtils.resolveCurrentPlatform(new SimpleJReleaserLoggerAdapter());
Platform annotation = optional.get();
boolean match = annotation.match();
String platform = annotation.platform();
boolean compatible = PlatformUtils.isCompatible(platform, PlatformUtils.getCurrentFull());
boolean result = match == compatible;
if (result) {
return ConditionEvaluationResult.enabled("Platform " + platform + " matches");
} else {
return ConditionEvaluationResult.disabled("Platform " + platform + " did not match");
}
}
return ConditionEvaluationResult.enabled("Nothing to match");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* 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
*
* https://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.
*/
package org.jreleaser.util;

import org.jreleaser.test.Platform;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Andres Almiray
* @since 1.0.0
*/
public class FileUtilsTests {
@ParameterizedTest
@EnumSource(value = FileType.class,
names = {"TAR", "TAR_BZ2", "TAR_GZ", "TAR_XZ", "TBZ2", "TGZ", "TXZ", "ZIP"})
@Platform(platform = "windows", match = false)
public void unpackArchiveWithExecutable(FileType fileType) throws IOException {
// given:
Path resourcesDir = Paths.get(".")
.resolve("src/test/resources")
.normalize();
Path archive = resourcesDir.resolve("app-1.0.0" + fileType.extension());
Path tmp = Files.createTempDirectory(fileType.name());

// when:
FileUtils.unpackArchive(archive, tmp, false);

// then:
Path license = tmp.resolve("app-1.0.0").resolve("LICENSE");
assertTrue(() -> Files.exists(license), "LICENSE exists");
Path executable = tmp.resolve("app-1.0.0").resolve("bin/executable");
assertTrue(() -> Files.exists(executable), "executable exists");
assertTrue(() -> Files.isExecutable(executable), "executable has executable bit set");
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit cd40677

Please sign in to comment.