Skip to content

Commit 2f6ec6e

Browse files
committed
Fix the symlinks and permissions in MacOS using unzip
1 parent 800eaf9 commit 2f6ec6e

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

lib/src/main/java/com/commonwealthrobotics/JvmManager.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.stream.Collectors;
28-
import java.util.zip.ZipEntry;
29-
import java.util.zip.ZipFile;
28+
//import java.util.zip.ZipEntry;
29+
//import java.util.zip.ZipFile;
3030

3131
import org.apache.commons.compress.archivers.examples.Archiver;
3232
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
3333
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
3434
import org.apache.commons.compress.archivers.tar.TarUtils;
3535
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
36+
import org.apache.commons.compress.archivers.zip.ZipFile;
3637
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
3738
import org.apache.commons.compress.utils.IOUtils;
3839
import org.apache.commons.io.FilenameUtils;
@@ -110,14 +111,20 @@ public static String getCommandString(String project, String repo, String versio
110111
return cmd + " -jar ";
111112
}
112113

114+
public static boolean isExecutable(ZipArchiveEntry entry) {
115+
int unixMode = entry.getUnixMode();
116+
// Check if any of the executable bits are set for user, group, or others.
117+
// User executable: 0100 (0x40), Group executable: 0010 (0x10), Others executable: 0001 (0x01)
118+
return (unixMode & 0x49) != 0;
119+
}
113120
private static void unzip(File path, String dir) throws Exception {
114121
String fileBaseName = FilenameUtils.getBaseName(path.getName().toString());
115122
Path destFolderPath = new File(dir).toPath();
116123

117-
try (ZipFile zipFile = new ZipFile(path, ZipFile.OPEN_READ, Charset.defaultCharset())) {
118-
Enumeration<? extends ZipEntry> entries = zipFile.entries();
124+
try (ZipFile zipFile = ZipFile.builder().setFile(path).get()) {
125+
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
119126
while (entries.hasMoreElements()) {
120-
ZipEntry entry = entries.nextElement();
127+
ZipArchiveEntry entry = entries.nextElement();
121128
Path entryPath = destFolderPath.resolve(entry.getName());
122129
if (entryPath.normalize().startsWith(destFolderPath.normalize())) {
123130
if (entry.isDirectory()) {
@@ -126,20 +133,25 @@ private static void unzip(File path, String dir) throws Exception {
126133
Files.createDirectories(entryPath.getParent());
127134
try (InputStream in = zipFile.getInputStream(entry)) {
128135
try {
129-
ZipArchiveEntry ar = new ZipArchiveEntry( entry);
130136
//ar.setExternalAttributes(entry.extraAttributes);
131-
if (ar.isUnixSymlink()) {
137+
if (entry.isUnixSymlink()) {
132138
String text = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
133139
.lines().collect(Collectors.joining("\n"));
134-
Files.createSymbolicLink(entryPath, Paths.get(".", text), null);
140+
Path target = Paths.get(".", text);
141+
System.out.println("Creating symlink "+entryPath+" with "+target);
142+
143+
Files.createSymbolicLink(entryPath, target);
135144
continue;
136145
}
137-
} catch (java.lang.ClassCastException ex) {
146+
} catch (Exception ex) {
138147
ex.printStackTrace();
139148
}
140149
try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
141150
IOUtils.copy(in, out);
142151
}
152+
if(isExecutable(entry)) {
153+
entryPath.toFile().setExecutable(true);
154+
}
143155
}
144156
}
145157
}

0 commit comments

Comments
 (0)