From 7d5d7a8b31723cbc897882ea6f179b557df0ddf6 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 16 May 2014 00:58:27 +0200 Subject: [PATCH] Added FileNativeUtils class (for Linux, Windows and Mac) Provides the following native methods: - chmod - link - symlink --- .../src/cc/arduino/os/FileNativeUtils.java | 91 +++++++++++++++++++ .../src/cc/arduino/os/linux/LibCNative.java | 52 +++++++++++ .../os/linux/LinuxFileNativeUtils.java | 60 ++++++++++++ .../os/macos/MacOSFileNativeUtils.java | 37 ++++++++ .../os/windows/WindowsFileNativeUtils.java | 48 ++++++++++ 5 files changed, 288 insertions(+) create mode 100644 arduino-core/src/cc/arduino/os/FileNativeUtils.java create mode 100644 arduino-core/src/cc/arduino/os/linux/LibCNative.java create mode 100644 arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java create mode 100644 arduino-core/src/cc/arduino/os/macos/MacOSFileNativeUtils.java create mode 100644 arduino-core/src/cc/arduino/os/windows/WindowsFileNativeUtils.java diff --git a/arduino-core/src/cc/arduino/os/FileNativeUtils.java b/arduino-core/src/cc/arduino/os/FileNativeUtils.java new file mode 100644 index 000000000..885729461 --- /dev/null +++ b/arduino-core/src/cc/arduino/os/FileNativeUtils.java @@ -0,0 +1,91 @@ +/* + * 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 oldFile + * @param newFile + * @throws IOException + */ + public static void link(File oldFile, File newFile) throws IOException { + if (OSUtils.isLinux()) + LinuxFileNativeUtils.link(oldFile, newFile); + if (OSUtils.isMacOS()) + MacOSFileNativeUtils.link(oldFile, newFile); + if (OSUtils.isWindows()) + WindowsFileNativeUtils.link(oldFile, newFile); + } + + /** + * Create a symlink link from oldFile to newFile. If the + * underlying filesystem doesn't support symlinks then the command is ignored. + * + * @param oldFile + * @param newFile + * @throws IOException + */ + public static void symlink(File oldFile, File newFile) throws IOException { + if (OSUtils.isLinux()) + LinuxFileNativeUtils.symlink(oldFile, newFile); + if (OSUtils.isMacOS()) + MacOSFileNativeUtils.symlink(oldFile, newFile); + if (OSUtils.isWindows()) + WindowsFileNativeUtils.symlink(oldFile, newFile); + } +} diff --git a/arduino-core/src/cc/arduino/os/linux/LibCNative.java b/arduino-core/src/cc/arduino/os/linux/LibCNative.java new file mode 100644 index 000000000..11739dc79 --- /dev/null +++ b/arduino-core/src/cc/arduino/os/linux/LibCNative.java @@ -0,0 +1,52 @@ +/* + * 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 { + + static LibCNative libc = (LibCNative) Native.loadLibrary("c", + LibCNative.class); + + Pointer errno = NativeLibrary.getInstance("c") + .getGlobalVariableAddress("errno"); + + public int chmod(String path, int mode); + + public int link(String oldpath, String newpath); + + public int symlink(String oldpath, String newpath); + + public 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 new file mode 100644 index 000000000..df1a615d1 --- /dev/null +++ b/arduino-core/src/cc/arduino/os/linux/LinuxFileNativeUtils.java @@ -0,0 +1,60 @@ +/* + * 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 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 file, File link) throws IOException { + int res = libc.link(file.getAbsolutePath(), link.getAbsolutePath()); + if (res == -1) + throw new IOException("Could not create hard link: " + 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()); + } + + 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 new file mode 100644 index 000000000..6232f9f84 --- /dev/null +++ b/arduino-core/src/cc/arduino/os/macos/MacOSFileNativeUtils.java @@ -0,0 +1,37 @@ +/* + * 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 new file mode 100644 index 000000000..5073e7920 --- /dev/null +++ b/arduino-core/src/cc/arduino/os/windows/WindowsFileNativeUtils.java @@ -0,0 +1,48 @@ +/* + * 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; +import java.io.IOException; + +public class WindowsFileNativeUtils { + + public static void chmod(File file, int mode) throws IOException { + // Empty + } + + public static void link(File file, File link) { + // Empty + } + + public static void symlink(File file, File link) { + // Empty + } + +}