Skip to content

Commit

Permalink
attempting to fix the symlink issue on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Jun 11, 2024
1 parent d2dceb6 commit 800eaf9
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/src/main/java/com/commonwealthrobotics/JvmManager.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.commonwealthrobotics;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -21,13 +24,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.compress.archivers.examples.Archiver;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarUtils;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -87,16 +92,16 @@ public static String getCommandString(String project, String repo, String versio
jvmargs = new ArrayList<String>();
String jvmURL = baseURL + name + "." + type;
File jvmArchive = download("", jvmURL, 185000000, progress, bindir, name + "." + type);
File dest = new File(bindir+name);
if(!dest.exists()) {
File dest = new File(bindir + name);
if (!dest.exists()) {
if (type.toLowerCase().contains("zip")) {
unzip(jvmArchive, bindir);
}
if (type.toLowerCase().contains("tar.gz")) {
untar(jvmArchive, bindir);
}
}else {
System.out.println("Not extraction, VM exists "+dest.getAbsolutePath());
} else {
System.out.println("Not extraction, VM exists " + dest.getAbsolutePath());
}
String cmd = bindir + name + "/bin/java" + (LatestFromGithubLaunchUI.isWin() ? ".exe" : "") + " ";
for (String s : jvmargs) {
Expand All @@ -120,6 +125,18 @@ private static void unzip(File path, String dir) throws Exception {
} else {
Files.createDirectories(entryPath.getParent());
try (InputStream in = zipFile.getInputStream(entry)) {
try {
ZipArchiveEntry ar = new ZipArchiveEntry( entry);
//ar.setExternalAttributes(entry.extraAttributes);
if (ar.isUnixSymlink()) {
String text = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
.lines().collect(Collectors.joining("\n"));
Files.createSymbolicLink(entryPath, Paths.get(".", text), null);
continue;
}
} catch (java.lang.ClassCastException ex) {
ex.printStackTrace();
}
try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
IOUtils.copy(in, out);
}
Expand All @@ -141,7 +158,7 @@ private static void untar(File tarFile, String dir) throws Exception {
// tarIn is a TarArchiveInputStream
while (tarEntry != null) {// create a file with the same name as the tarEntry
File destPath = new File(dest.toString() + System.getProperty("file.separator") + tarEntry.getName());
//System.out.println("working: " + destPath.getCanonicalPath());
// System.out.println("working: " + destPath.getCanonicalPath());
if (tarEntry.isDirectory()) {
destPath.mkdirs();
} else {
Expand All @@ -152,16 +169,17 @@ private static void untar(File tarFile, String dir) throws Exception {
fout.write(b);
fout.close();
int mode = tarEntry.getMode();
b= new byte[5];
b = new byte[5];
TarUtils.formatUnsignedOctalString(mode, b, 0, 4);
if(bits(b[1]).endsWith("1")) {
if (bits(b[1]).endsWith("1")) {
destPath.setExecutable(true);
}
}
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}

private static String bits(byte b) {
return String.format("%6s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
}
Expand Down

0 comments on commit 800eaf9

Please sign in to comment.