Skip to content

Commit

Permalink
Fixed missing symlinks after extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Fissore committed Mar 27, 2015
1 parent fc4179f commit 29d20f2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public boolean isCompatible() {
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();

Expand Down
24 changes: 12 additions & 12 deletions arduino-core/src/cc/arduino/os/FileNativeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,33 @@ public static void chmod(File file, int mode) throws IOException {
* 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);
}
}
14 changes: 6 additions & 8 deletions arduino-core/src/cc/arduino/os/linux/LibCNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@

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);

}
21 changes: 12 additions & 9 deletions arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
28 changes: 15 additions & 13 deletions arduino-core/src/cc/arduino/utils/ArchiveExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,23 @@ public static void extract(File archiveFile, File destFolder, int stripPath) thr
}
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);
}
}

Expand All @@ -213,10 +215,10 @@ public static void extract(File archiveFile, File destFolder, int stripPath) thr
}
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
Expand All @@ -234,16 +236,16 @@ public static void extract(File archiveFile, File destFolder, int stripPath) thr
}

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 {
Expand Down

0 comments on commit 29d20f2

Please sign in to comment.