Skip to content

Commit

Permalink
Fixing permissions for macos / linux - 2.3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcsakany committed Aug 13, 2021
1 parent 79016a2 commit 79b7f3c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<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>
<groupId>hu.blackbelt.judo</groupId>
<groupId>hu.blackbelt</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>flutter-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -107,6 +108,12 @@ private boolean flutterIsAlreadyInstalled() {
try {
FlutterExecutorConfig executorConfig = new DefaultFlutterExecutorConfig(this.config);
File flutterFile = executorConfig.getFlutterPath();
try {
setExecutablePermission();
} catch (InstallationException | IOException e) {
logger.error("Could not set permissions");
}

if (flutterFile.exists()) {
final String version =
new FlutterExecutor(executorConfig, Arrays.asList("--version"), null).executeAndGetResult(logger);
Expand Down Expand Up @@ -263,10 +270,7 @@ private void installFlutterDefaultFromArchive() throws InstallationException {
throw new InstallationException("Could not install Flutter: Was not allowed to rename "
+ extractedDirectory + " to " + destinationDirectory);
}
setExecutable(new File(destinationDirectory, "bin" + File.separator + "flutter"));
setExecutable(new File(destinationDirectory, "bin" + File.separator + "dart"));
setExecutable(new File(destinationDirectory, "bin" + File.separator + "internal" + File.separator + "shared.sh"));
setExecutable(new File(destinationDirectory, "bin" + File.separator + "internal" + File.separator + "update_dart_sdk.sh"));
setExecutablePermission();

deleteTempDirectory(tmpDirectory);

Expand All @@ -290,6 +294,16 @@ private void installFlutterDefaultFromArchive() throws InstallationException {
}
}

private void setExecutablePermission() throws InstallationException, IOException {
File destinationDirectory = getInstallDirectory();

addExecPermission(new File(destinationDirectory, "bin" + File.separator + "flutter"));
addExecPermission(new File(destinationDirectory, "bin" + File.separator + "dart"));
addExecPermission(new File(destinationDirectory, "bin" + File.separator + "internal" + File.separator + "shared.sh"));
addExecPermission(new File(destinationDirectory, "bin" + File.separator + "internal" + File.separator + "update_dart_sdk.sh"));
addExecPermission(new File(destinationDirectory, "bin" + File.separator + "cache" + File.separator + "dart-sdk" + File.separator + "bin"));
}

private void installFlutterDefaultFromGit() throws InstallationException {
try {
setupGitProxy();
Expand Down Expand Up @@ -337,6 +351,9 @@ private void installFlutterDefaultFromGit() throws InstallationException {
}

private void setExecutable(File destinationBinary) throws InstallationException {
if (!destinationBinary.exists()) {
return;
}
if (!destinationBinary.setExecutable(true, false)) {
throw new InstallationException(
"Could not install Flutter: Was not allowed to make " + destinationBinary + " executable.");
Expand Down Expand Up @@ -431,4 +448,44 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
}

private void addExecPermission(final File file) throws IOException {
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
Files.walk(file.toPath()).filter(path -> !Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)).forEach(f -> {
try {
addExecPermission(f.toFile());
} catch (IOException e) {
}
});
}
Path path = file.toPath();

Set<String> fileAttributeView = FileSystems.getDefault().supportedFileAttributeViews();

if (fileAttributeView.contains("posix")) {
final Set<PosixFilePermission> permissions;
try {
permissions = Files.getPosixFilePermissions(path);
} catch (UnsupportedOperationException e) {
logger.debug("Exec file permission is not set", e);
return;
}
permissions.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(path, permissions);

} else if (fileAttributeView.contains("acl")) {
String username = System.getProperty("user.name");
UserPrincipal userPrincipal = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(username);
AclEntry aclEntry = AclEntry.newBuilder().setPermissions(AclEntryPermission.EXECUTE).setType(AclEntryType.ALLOW).setPrincipal(userPrincipal).build();

AclFileAttributeView acl = Files.getFileAttributeView(path, AclFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
List<AclEntry> aclEntries = acl.getAcl();
aclEntries.add(aclEntry);
acl.setAcl(aclEntries);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class InstallFlutterMojo extends AbstractFlutterMojo {
/**
* Where to download Flutter binary from. Defaults to https://storage.googleapis.com/flutter_infra/releases/
*/
@Parameter(property = "flutter-download-root", required = false, defaultValue = "https://storage.googleapis.com/flutter_infra/releases/")
@Parameter(property = "flutter-download-root", required = false, defaultValue = "https://storage.googleapis.com/flutter_infra_release/releases/")
private String flutterDownloadRoot;

/**
Expand Down

0 comments on commit 79b7f3c

Please sign in to comment.