Added FileNativeUtils class (for Linux, Windows and Mac)

Provides the following native methods:
- chmod
- link
- symlink
This commit is contained in:
Cristian Maglie 2014-05-16 00:58:27 +02:00 committed by Federico Fissore
parent 38aefb2ab4
commit 7d5d7a8b31
5 changed files with 288 additions and 0 deletions

View File

@ -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 <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
* @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 <b>oldFile</b> to <b>newFile</b>. 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);
}
}

View File

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

View File

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

View File

@ -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
}

View File

@ -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
}
}