From b65b576eb026c6ee5718a8262d9c2b274f6639c3 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 30 Mar 2015 09:30:03 +0200 Subject: [PATCH] Getting rid of native stuff. Avoid native stuff: it will break. Fixes #2828, #2829, #2830 --- .../libraries/ui/LibraryInstaller.java | 3 +- .../packages/ContributionInstaller.java | 4 +- .../src/cc/arduino/os/FileNativeUtils.java | 91 ------------------- .../src/cc/arduino/os/linux/LibCNative.java | 50 ---------- .../os/linux/LinuxFileNativeUtils.java | 63 ------------- .../os/macos/MacOSFileNativeUtils.java | 37 -------- .../os/windows/WindowsFileNativeUtils.java | 47 ---------- .../cc/arduino/utils/ArchiveExtractor.java | 23 +++-- .../src/processing/app/BaseNoGui.java | 2 +- arduino-core/src/processing/app/Platform.java | 15 +++ .../src/processing/app/windows/Platform.java | 9 ++ 11 files changed, 44 insertions(+), 300 deletions(-) delete mode 100644 arduino-core/src/cc/arduino/os/FileNativeUtils.java delete mode 100644 arduino-core/src/cc/arduino/os/linux/LibCNative.java delete mode 100644 arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java delete mode 100644 arduino-core/src/cc/arduino/os/macos/MacOSFileNativeUtils.java delete mode 100644 arduino-core/src/cc/arduino/os/windows/WindowsFileNativeUtils.java diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryInstaller.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryInstaller.java index bd966a23d..970ba2e53 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryInstaller.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryInstaller.java @@ -34,6 +34,7 @@ import java.io.File; import java.io.IOException; import java.net.URL; +import processing.app.BaseNoGui; import processing.app.helpers.FileUtils; import cc.arduino.contributions.libraries.ContributedLibrary; import cc.arduino.contributions.libraries.LibrariesIndexer; @@ -122,7 +123,7 @@ public class LibraryInstaller { File libsFolder = indexer.getSketchbookLibrariesFolder(); File tmpFolder = FileUtils.createTempFolderIn(libsFolder); try { - ArchiveExtractor.extract(lib.getDownloadedFile(), tmpFolder, 1); + new ArchiveExtractor(BaseNoGui.getPlatform()).extract(lib.getDownloadedFile(), tmpFolder, 1); } catch (Exception e) { if (tmpFolder.exists()) FileUtils.recursiveDelete(tmpFolder); diff --git a/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java b/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java index 312595c0b..17b7dacbf 100644 --- a/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java +++ b/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java @@ -139,7 +139,7 @@ public class ContributionInstaller { destFolder.mkdirs(); assert toolContrib.getDownloadedFile() != null; - ArchiveExtractor.extract(toolContrib.getDownloadedFile(), destFolder, 1); + new ArchiveExtractor(BaseNoGui.getPlatform()).extract(toolContrib.getDownloadedFile(), destFolder, 1); executePostInstallScriptIfAny(destFolder); toolContrib.setInstalled(true); toolContrib.setInstalledFolder(destFolder); @@ -152,7 +152,7 @@ public class ContributionInstaller { File platformFolder = new File(packageFolder, "hardware" + File.separator + platform.getArchitecture()); File destFolder = new File(platformFolder, platform.getVersion()); destFolder.mkdirs(); - ArchiveExtractor.extract(platform.getDownloadedFile(), destFolder, 1); + new ArchiveExtractor(BaseNoGui.getPlatform()).extract(platform.getDownloadedFile(), destFolder, 1); platform.setInstalled(true); platform.setInstalledFolder(destFolder); progress.stepDone(); diff --git a/arduino-core/src/cc/arduino/os/FileNativeUtils.java b/arduino-core/src/cc/arduino/os/FileNativeUtils.java deleted file mode 100644 index 25afdaf69..000000000 --- a/arduino-core/src/cc/arduino/os/FileNativeUtils.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2014 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ -package cc.arduino.os; - -import java.io.File; -import java.io.IOException; - -import cc.arduino.os.linux.LinuxFileNativeUtils; -import cc.arduino.os.macos.MacOSFileNativeUtils; -import cc.arduino.os.windows.WindowsFileNativeUtils; -import processing.app.helpers.OSUtils; - -public class FileNativeUtils { - - /** - * Change file access permissions (UNIX). If the underlying filesystem doesn't - * support UNIX permission then the command is ignored. - * - * @param file - * @param mode - * @throws IOException - */ - public static void chmod(File file, int mode) throws IOException { - if (OSUtils.isLinux()) - LinuxFileNativeUtils.chmod(file, mode); - if (OSUtils.isMacOS()) - MacOSFileNativeUtils.chmod(file, mode); - if (OSUtils.isWindows()) - WindowsFileNativeUtils.chmod(file, mode); - } - - /** - * Create a hard link from oldFile to newFile. If the underlying - * filesystem doesn't support hard links then the command is ignored. - * - * @param something - * @param somewhere - * @throws IOException - */ - public static void link(File something, File somewhere) throws IOException { - if (OSUtils.isLinux()) - LinuxFileNativeUtils.link(something, somewhere); - if (OSUtils.isMacOS()) - MacOSFileNativeUtils.link(something, somewhere); - if (OSUtils.isWindows()) - WindowsFileNativeUtils.link(something, somewhere); - } - - /** - * Create a symlink link from oldFile to newFile. If the - * underlying filesystem doesn't support symlinks then the command is ignored. - * - * @param something - * @param somewhere - * @throws IOException - */ - public static void symlink(File something, File somewhere) throws IOException { - if (OSUtils.isLinux()) - LinuxFileNativeUtils.symlink(something, somewhere); - if (OSUtils.isMacOS()) - MacOSFileNativeUtils.symlink(something, somewhere); - if (OSUtils.isWindows()) - WindowsFileNativeUtils.symlink(something, somewhere); - } -} diff --git a/arduino-core/src/cc/arduino/os/linux/LibCNative.java b/arduino-core/src/cc/arduino/os/linux/LibCNative.java deleted file mode 100644 index 73e571834..000000000 --- a/arduino-core/src/cc/arduino/os/linux/LibCNative.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2014 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ -package cc.arduino.os.linux; - -import com.sun.jna.Library; -import com.sun.jna.Native; -import com.sun.jna.NativeLibrary; -import com.sun.jna.Pointer; - -public interface LibCNative extends Library { - - LibCNative libc = (LibCNative) Native.loadLibrary("c", LibCNative.class); - - Pointer errno = NativeLibrary.getInstance("c").getGlobalVariableAddress("errno"); - - int chmod(String path, int mode); - - int link(String something, String somewhere); - - int symlink(String something, String somewhere); - - String strerror(int errno); - -} diff --git a/arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java b/arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java deleted file mode 100644 index d9f6ad2f0..000000000 --- a/arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2014 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ -package cc.arduino.os.linux; - -import java.io.File; -import java.io.IOException; - -public class LinuxFileNativeUtils { - - public static final LibCNative libc = LibCNative.libc; - - public static void chmod(File file, int mode) throws IOException { - int res = libc.chmod(file.getAbsolutePath(), mode); - if (res == -1) { - throw new IOException("Could not change file permission: " + 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 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() { - return libc.strerror(LibCNative.errno.getInt(0)); - } - -} diff --git a/arduino-core/src/cc/arduino/os/macos/MacOSFileNativeUtils.java b/arduino-core/src/cc/arduino/os/macos/MacOSFileNativeUtils.java deleted file mode 100644 index 6232f9f84..000000000 --- a/arduino-core/src/cc/arduino/os/macos/MacOSFileNativeUtils.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2014 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ -package cc.arduino.os.macos; - -import cc.arduino.os.linux.LinuxFileNativeUtils; - -public class MacOSFileNativeUtils extends LinuxFileNativeUtils { - - // OSX and Linux shares the same POSIX API - -} diff --git a/arduino-core/src/cc/arduino/os/windows/WindowsFileNativeUtils.java b/arduino-core/src/cc/arduino/os/windows/WindowsFileNativeUtils.java deleted file mode 100644 index 26b477dd7..000000000 --- a/arduino-core/src/cc/arduino/os/windows/WindowsFileNativeUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2014 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ -package cc.arduino.os.windows; - -import java.io.File; - -public class WindowsFileNativeUtils { - - public static void chmod(File file, int mode) { - // Empty - } - - public static void link(File file, File link) { - // Empty - } - - public static void symlink(File file, File link) { - // Empty - } - -} diff --git a/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java b/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java index 036a3daf4..d8d1c1b07 100644 --- a/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java +++ b/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java @@ -28,7 +28,6 @@ */ package cc.arduino.utils; -import cc.arduino.os.FileNativeUtils; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; @@ -37,6 +36,7 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import processing.app.I18n; +import processing.app.Platform; import java.io.*; import java.util.HashMap; @@ -46,6 +46,13 @@ import static processing.app.I18n._; public class ArchiveExtractor { + private final Platform platform; + + public ArchiveExtractor(Platform platform) { + assert platform != null; + this.platform = platform; + } + /** * Extract source into destFolder. source file archive * format is autodetected from file extension. @@ -54,7 +61,7 @@ public class ArchiveExtractor { * @param destFolder * @throws IOException */ - public static void extract(File archiveFile, File destFolder) throws IOException { + public void extract(File archiveFile, File destFolder) throws IOException, InterruptedException { extract(archiveFile, destFolder, 0); } @@ -68,12 +75,12 @@ public class ArchiveExtractor { * archived files * @throws IOException */ - public static void extract(File archiveFile, File destFolder, int stripPath) throws IOException { + public void extract(File archiveFile, File destFolder, int stripPath) throws IOException, InterruptedException { extract(archiveFile, destFolder, stripPath, false); } - public static void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite) throws IOException { + public void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite) throws IOException, InterruptedException { // Folders timestamps must be set at the end of archive extraction // (because creating a file in a folder alters the folder's timestamp) @@ -233,7 +240,7 @@ public class ArchiveExtractor { // Set file/folder permission if (mode != null && !isSymLink && outputFile.exists()) { - FileNativeUtils.chmod(outputFile, mode); + platform.chmod(outputFile, mode); } } @@ -241,10 +248,10 @@ public class ArchiveExtractor { if (entry.getKey().exists() && overwrite) { entry.getKey().delete(); } - FileNativeUtils.link(entry.getValue(), entry.getKey()); + platform.link(entry.getValue(), entry.getKey()); Integer mode = hardLinksMode.get(entry.getKey()); if (mode != null) { - FileNativeUtils.chmod(entry.getKey(), mode); + platform.chmod(entry.getKey(), mode); } } @@ -252,7 +259,7 @@ public class ArchiveExtractor { if (entry.getKey().exists() && overwrite) { entry.getKey().delete(); } - FileNativeUtils.symlink(entry.getValue(), entry.getKey()); + platform.symlink(entry.getValue(), entry.getKey()); entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey())); } diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index 609a9eb87..16b922caa 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -582,7 +582,7 @@ public class BaseNoGui { if (!indexFile.isFile() || !(avrCoreFolder.exists() && avrCoreFolder.isDirectory())) { File distFile = findDefaultPackageFile(); if (distFile != null) { - ArchiveExtractor.extract(distFile, BaseNoGui.getSettingsFolder(), 0, true); + new ArchiveExtractor(getPlatform()).extract(distFile, BaseNoGui.getSettingsFolder(), 0, true); } else if (!indexFile.isFile()) { // Otherwise create an empty packages index FileOutputStream out = null; diff --git a/arduino-core/src/processing/app/Platform.java b/arduino-core/src/processing/app/Platform.java index 9faf21b41..b6ed59cbf 100644 --- a/arduino-core/src/processing/app/Platform.java +++ b/arduino-core/src/processing/app/Platform.java @@ -244,4 +244,19 @@ public class Platform { public String getOsArch() { return System.getProperty("os.arch"); } + + public void symlink(File something, File somewhere) throws IOException, InterruptedException { + Process process = Runtime.getRuntime().exec(new String[]{"ln", "-s", something.getAbsolutePath(), somewhere.getAbsolutePath()}, null, null); + process.waitFor(); + } + + public void link(File something, File somewhere) throws IOException, InterruptedException { + Process process = Runtime.getRuntime().exec(new String[]{"ln", something.getAbsolutePath(), somewhere.getAbsolutePath()}, null, null); + process.waitFor(); + } + + public void chmod(File file, int mode) throws IOException, InterruptedException { + Process process = Runtime.getRuntime().exec(new String[]{"chmod", Integer.toOctalString(mode), file.getAbsolutePath()}, null, null); + process.waitFor(); + } } diff --git a/arduino-core/src/processing/app/windows/Platform.java b/arduino-core/src/processing/app/windows/Platform.java index 9571a1079..2776fea5c 100644 --- a/arduino-core/src/processing/app/windows/Platform.java +++ b/arduino-core/src/processing/app/windows/Platform.java @@ -362,4 +362,13 @@ public class Platform extends processing.app.Platform { scripts.add(new File(folder, "post_install.bat")); return scripts; } + + public void symlink(File something, File somewhere) throws IOException, InterruptedException { + } + + public void link(File something, File somewhere) throws IOException, InterruptedException { + } + + public void chmod(File file, int mode) throws IOException, InterruptedException { + } }