How to manage Linux file permissions in Java? - java

Is there any mechanism to get and set the file/directory permissions?
For example, I want to show the permisssions of a file in a shell way:
-rwxr-xr--
Is it possible to do this using Java?
I know that there are some methods in the File class to know if the file canExecute, canRead and canWrite, but AFAIK this info is for the current user only. I need to know the whole octal number, for example 755, so I need to get it from the user, from group and from others.
I know that Java7 brings Posix operations, but how could do this using a smaller JRE?
I would like not to use a command like ls, or chmod.

If you can use external libraries, there are several:
JPosix
Posix for java
jnr-posix
If an entire library seems a hassle, creating a JNI wrapper that calls the lstat C function and returns the access mode takes you about 10 minutes. Here's a tutorial that creates such a wrapper for the isatty and ttyname functions.

As you say, in Java7, the JVM supports it, so you have a guarantee that this can be done portably in all OSs (because the JVM implementation takes care of it). Under Java7, you'd have to use a native library per OS you want to support. This is potentially even dirtier than executing chmod

Related

Calling WSH from Java

I have some wscript / vbscript based code that calls a certain COM API. This code needs to be run periodically with lots of other Java based code that we run in order to gather metrics.
I know that I could take this code and translate it to something that uses Com4J, JNA or alike to mimic the behaviour in Java. But the vbscript code is "certified" by the vendor of the application we measure and I want to avoid debates that when I translated the code I changed the validity of the measurements (we had a comparable debate before)
A second alternative would be to simply call wscript.exe as a subprocess, have it write the results to stdout and parse that. But I would rather avoid that because then I would have to check for stalled subprocesses etc.
Is there a different way to call WSH from Java ? Perhaps by calling the Windows Scripting Host as a COM Object and passing it a pointer to the sourcecode or the source in a String ?

Processing unix paths in Windows

What's the best way of processing *ix file path strings when running on Windows?
If I just use Paths.get() it invokes Filesystem.getDefault() which ends up processing it like a Windows path. The parsing seems to work in my tests but they're pretty rudimentary, and of course toString uses the wrong path separator.
Can I manually load up the LinuxFileSystem somehow? Or should I use the commons-io parser instead?
Can I manually load up the LinuxFileSystem somehow?
I doubt it, because one JDK distribution is oriented either to Unix or either to Windows. The only chance I think of is that you look for an open source distribution of some UnixFileSystemProvider and import into your application's runtime.
But even in that case, I have my doubts it will work 100%, because a FileSystemProvider class deals with low-level details from the underlying OS - not just path separators, so there is a risk that it won't be compatible over a Windows filesystem.
Looks like #Little Santi's comment on the question is in fact the answer. If I run:
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
System.out.println(provider.getClass().getName());
}
I get:
sun.nio.fs.WindowsFileSystemProvider
com.sun.nio.zipfs.ZipFileSystemProvider
If I interpret this correctly it means I can't use the LinuxFileSystem path methods under Windows but should use commons-io instead.

JRuby DSL encapsulation, exclude standard library

I'm trying to make a Java program that allows users to do some limited scripting with a Ruby DSL that I've written. The script the user writes is saved to a Proc object in JRuby. The problem arises in that the user can still access methods that are standard to Ruby, such as File.new, or creating classes, or basically messing with other internal logic of the program or computer.
Is there a way to limit the user's script to only the constraints of the DSL, using JRuby or Ruby or even Java? Or at least to remove the user's access to certain classes?
Since you're running under JRuby, you can use a Java security policy (policy file documentation) to prevent users from being able to do things like file or network I/O. Of course, this will keep your code from having those capabilities, too! You can whitelist code by jar URI or by jar signature, so one tactic is to create a "hull" of trusted code that strongly validates its input, package it in its own jar, trust it, and use it exclusively for your own code. Doing this right gets complicated fast (have an extensive test suite!), but it can be done.
To have explicit control over the namespace available to your DSL, you can use BasicObject. It doesn't mix in Kernel or any of the other things available in the standard Ruby namespace. This doesn't give you security, though, because users can still use ::File directly or include ::Kernel to get it all back!

Windows - get warned when a file is created

I'm not sure if what I'm asking is possible, but I would like to do the following:
When a file is created in a certain folder (Windows), my program should respond. I'd like to let Windows call a callback method when a file is created.
Another option is of course just use a loop and constantly check if a new file is in the folder, but I'd like to know it instantly, so a callback method would be much more efficient.
Is this possible? The language is not important, although Java is preferred.
With Java nio 2 (available in Java 1.7 +), you can "watch" a directory and get notified when that directory changes.
The method proposed in the tutorial linked above uses the WatchService API.
Commons IO contains a FileAlterationListener wich has a onDirectoryChangemethod. Can be an alternative if Java 1.7 is not available.
If you are not bound to Java, then you could use very convenient FileSystemWatcher in C# or VisualBasic. It will allow you to watch all kinds of events which can occur in folder and it's quite easy to implement it.

behavior of external executable

I am currently writing a program in JAVA that examines the behavior of external executable. One of the requirements is to observe the file operations of the external executable in real time (check if the executable creates/ deletes/modifies any file). I tried to find a suitable API in java to help me do this though it was not possible to find one. I have found the Class FileAlterationObserver which is not suitable for my program since you have to specify manually all the directories you want to monitor.
I was wondering if any of you knows a good API to use?
Thanks for your time in advance.
Without java, you could use the linux lsof command to list the open files in the system. Alternatively, and with Java, you can use libnotify, but you will need to specify the folders. I can't see any other way of doing this with pure java.
EDIT #Keppil linked you to the file change notification API that looks way more suitable than libjnotify. I wasn't aware it existed!

Categories