Fixed missing symlinks after extraction

This commit is contained in:
Federico Fissore 2015-03-19 14:03:49 +01:00
parent fc4179f1f7
commit 29d20f297c
5 changed files with 45 additions and 44 deletions

View File

@ -45,8 +45,6 @@ public abstract class HostDependentDownloadableContribution extends Downloadable
Properties prop = System.getProperties();
String osName = prop.getProperty("os.name");
String osArch = prop.getProperty("os.arch");
// for (Object k : properties.keySet())
// System.out.println(k + " = " + properties.get(k));
String host = getHost();

View File

@ -59,33 +59,33 @@ public class FileNativeUtils {
* Create a hard link from <b>oldFile</b> to <b>newFile</b>. If the underlying
* filesystem doesn't support hard links then the command is ignored.
*
* @param oldFile
* @param newFile
* @param something
* @param somewhere
* @throws IOException
*/
public static void link(File oldFile, File newFile) throws IOException {
public static void link(File something, File somewhere) throws IOException {
if (OSUtils.isLinux())
LinuxFileNativeUtils.link(oldFile, newFile);
LinuxFileNativeUtils.link(something, somewhere);
if (OSUtils.isMacOS())
MacOSFileNativeUtils.link(oldFile, newFile);
MacOSFileNativeUtils.link(something, somewhere);
if (OSUtils.isWindows())
WindowsFileNativeUtils.link(oldFile, newFile);
WindowsFileNativeUtils.link(something, somewhere);
}
/**
* Create a symlink link from <b>oldFile</b> to <b>newFile</b>. If the
* underlying filesystem doesn't support symlinks then the command is ignored.
*
* @param oldFile
* @param newFile
* @param something
* @param somewhere
* @throws IOException
*/
public static void symlink(File oldFile, File newFile) throws IOException {
public static void symlink(File something, File somewhere) throws IOException {
if (OSUtils.isLinux())
LinuxFileNativeUtils.symlink(oldFile, newFile);
LinuxFileNativeUtils.symlink(something, somewhere);
if (OSUtils.isMacOS())
MacOSFileNativeUtils.symlink(oldFile, newFile);
MacOSFileNativeUtils.symlink(something, somewhere);
if (OSUtils.isWindows())
WindowsFileNativeUtils.symlink(oldFile, newFile);
WindowsFileNativeUtils.symlink(something, somewhere);
}
}

View File

@ -35,18 +35,16 @@ import com.sun.jna.Pointer;
public interface LibCNative extends Library {
static LibCNative libc = (LibCNative) Native.loadLibrary("c",
LibCNative.class);
LibCNative libc = (LibCNative) Native.loadLibrary("c", LibCNative.class);
Pointer errno = NativeLibrary.getInstance("c")
.getGlobalVariableAddress("errno");
Pointer errno = NativeLibrary.getInstance("c").getGlobalVariableAddress("errno");
public int chmod(String path, int mode);
int chmod(String path, int mode);
public int link(String oldpath, String newpath);
int link(String something, String somewhere);
public int symlink(String oldpath, String newpath);
int symlink(String something, String somewhere);
public String strerror(int errno);
String strerror(int errno);
}

View File

@ -37,20 +37,23 @@ public class LinuxFileNativeUtils {
public static void chmod(File file, int mode) throws IOException {
int res = libc.chmod(file.getAbsolutePath(), mode);
if (res == -1)
if (res == -1) {
throw new IOException("Could not change file permission: " + strerror());
}
}
public static void link(File file, File link) throws IOException {
int res = libc.link(file.getAbsolutePath(), link.getAbsolutePath());
if (res == -1)
throw new IOException("Could not create hard link to " + file + " from " + link + ": " + strerror());
public static void link(File something, File somewhere) throws IOException {
int res = libc.link(something.getAbsolutePath(), somewhere.getAbsolutePath());
if (res == -1) {
throw new IOException("Could not create hard link to " + somewhere + " from " + something + ": " + strerror());
}
}
public static void symlink(File file, File link) throws IOException {
int res = libc.symlink(file.getPath(), link.getAbsolutePath());
if (res == -1)
throw new IOException("Could not create symlink: " + strerror());
public static void symlink(File something, File somewhere) throws IOException {
int res = libc.symlink(something.getPath(), somewhere.getAbsolutePath());
if (res == -1) {
throw new IOException("Could not create symlink to " + somewhere + " from " + something + ": " + strerror());
}
}
private static String strerror() {

View File

@ -174,21 +174,23 @@ public class ArchiveExtractor {
}
File outputFile = new File(destFolder, name);
File outputLinkFile = null;
File outputLinkedFile = null;
if (isLink) {
if (!linkName.startsWith(pathPrefix)) {
throw new IOException("Invalid archive: it must contains a single root folder while file " + linkName + " is outside " + pathPrefix);
}
linkName = linkName.substring(pathPrefix.length());
outputLinkFile = new File(destFolder, linkName);
outputLinkedFile = new File(destFolder, linkName);
}
if (isSymLink) {
// Symbolic links are referenced with relative paths
outputLinkFile = new File(linkName);
if (outputLinkFile.isAbsolute()) {
System.err.println(I18n.format(_("Warning: file {0} links to an absolute path {1}, changing it to {2}"), outputFile, outputLinkFile, new File(outputLinkFile.getName())));
outputLinkedFile = new File(linkName);
if (outputLinkedFile.isAbsolute()) {
System.err.println(I18n.format(_("Warning: file {0} links to an absolute path {1}, changing it to {2}"), outputFile, outputLinkedFile, new File(outputLinkedFile.getName())));
System.err.println();
outputLinkFile = new File(outputLinkFile.getName());
outputLinkedFile = new File(outputLinkedFile.getName());
} else {
outputLinkedFile = new File(outputFile.getParent(), linkName);
}
}
@ -213,10 +215,10 @@ public class ArchiveExtractor {
}
foldersTimestamps.put(outputFile, modifiedTime);
} else if (isLink) {
hardLinks.put(outputLinkFile, outputFile);
hardLinks.put(outputFile, outputLinkedFile);
hardLinksMode.put(outputFile, mode);
} else if (isSymLink) {
symLinks.put(outputLinkFile, outputFile);
symLinks.put(outputFile, outputLinkedFile);
symLinksModifiedTimes.put(outputFile, modifiedTime);
} else {
// Create the containing folder if not exists
@ -234,16 +236,16 @@ public class ArchiveExtractor {
}
for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
FileNativeUtils.link(entry.getKey(), entry.getValue());
Integer mode = hardLinksMode.get(entry.getValue());
FileNativeUtils.link(entry.getValue(), entry.getKey());
Integer mode = hardLinksMode.get(entry.getKey());
if (mode != null) {
FileNativeUtils.chmod(entry.getValue(), mode);
FileNativeUtils.chmod(entry.getKey(), mode);
}
}
for (Map.Entry<File, File> entry : symLinks.entrySet()) {
FileNativeUtils.symlink(entry.getKey(), entry.getValue());
entry.getValue().setLastModified(symLinksModifiedTimes.get(entry.getValue()));
FileNativeUtils.symlink(entry.getValue(), entry.getKey());
entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()));
}
} finally {