5

The Files class introduced in Java 7 has methods for handling links and symlinks but only as optional operations.

Is there any way of determining at runtime if a file system supports these operations before actually invoking the respective methods or do I need to call them and then catch the exception?

Classes like FileSystem or FileStore do not seem to contain anything in that regard (or I overlooked it).

shutefan
  • 6,156
  • 8
  • 23
  • 23
  • The documentation says: UnsupportedOperationException - if the implementation does not support symbolic links or the array contains an attribute that cannot be set atomically when creating the symbolic link – Akira Nov 13 '13 at 21:24
  • Maybe this one can help you: http://stackoverflow.com/questions/9441258/identify-file-system-format-of-a-disk-type-in-java-like-ntfs-fat16-32-or-ext – Akira Nov 13 '13 at 21:26
  • 1
    @Akira that's what I meant by catching the exception ;) – shutefan Nov 13 '13 at 21:30

1 Answers1

4

I don't see any general approach that will work without relying on an UnsupportedOperationException or some other exception.

You could use a heuristic that assumes that only subclasses of BasicFileAttributesView support symbolic linking.


Note: The approach below will not work because FileAttributeViews and file attributes are not the same concept:

I did not get isSymbolicLink as one of the supported attributes with the following code on OS X 10.8.4:

package com.mlbam.internal;

import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainClass {
    private static final Logger LOG = LoggerFactory.getLogger(MainClass.class);
    public static void main(String[] args) {
        try {
            System.out.println("FileStore.supportsFileAttributeView('isSymbolicLink'): " 
                + Files.getFileStore(Paths.get("/")).supportsFileAttributeView("isSymbolicLink"));
            // Got: FileStore.supportsFileAttributeView('isSymbolicLink'): false
            System.out.println(FileSystems.getDefault().supportedFileAttributeViews());
            // Got: [basic, owner, unix, posix]
        } catch (Exception e) {}
    }
}

Original Answer:

If you have an instance of FileStore, you can use FileStore.supportsFileAttributeView("isSymbolicLink")

Or, if you have an instance of FileSystem, you can check that resulting Set<String> from FileSystem.supportedFileAttributeViews() contains the String "isSymbolicLink".


You can get the FileStore associated with a Path using Files.getFileStore(Path)

One way of getting the FileSystem is via FileSystems.getDefault()


yegeniy
  • 1,272
  • 13
  • 28
  • 1
    Hm, surprisingly this does not seem to work on a win-machine with NTFS - thought it supported symlinks? Gonna try it on linux later... – shutefan Nov 13 '13 at 21:52
  • @shutefan, it looks like you're correct and my answer might not be. It didn't work with OS X either. Not sure how to address this. Do I flag the answer? I'd rather not delete so others can see this approach doesn't work. – yegeniy Nov 13 '13 at 22:16
  • thanks for trying out! I'd rather keep the answer for a reference so that others don't try the same in vain. – shutefan Nov 13 '13 at 22:23
  • Agreed. In any case, for most practical purposes I bet people can use [`Files.isSymbolicLink(Path)`](http://docs.oracle.com/javase/tutorial/essential/io/links.html#detect) (please note that this is not documented to throw an `UnsupportedOperationException`) – yegeniy Nov 13 '13 at 22:27
  • 2
    The `supportsFileAttributeView` method takes the name of a FileAttributeView, *not* the name of a file attribute. That's why it always returns false for you. Similarly, `FileSystem.supportsFileAttributeViews` returns the names of FileAttributeViews, not the names of file attributes. See [`Files.getAttribute`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#getAttribute%28java.nio.file.Path,%20java.lang.String,%20java.nio.file.LinkOption...%29) for a description of the two types of names. – VGR Nov 13 '13 at 23:54
  • +1 @VGR, I'll update the answer tomorrow (or feel free to edit for me). – yegeniy Nov 14 '13 at 05:08