1

When invoking Files.getFileStore() on a substed drive (on Windows), this results in following error:

The directory is not a subdirectory of the root directory

For example with:

subst P: C:\temp

running:

public static void main(String[] args) throws IOException {
    final Path dir = Paths.get("P:/sub");
    final FileStore fileStore = Files.getFileStore(dir);
    fileStore.isReadOnly();
}

results in:

Exception in thread "main" java.nio.file.FileSystemException: P:\sub: The directory is not a subdirectory of the root directory.

    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileStore.create(WindowsFileStore.java:92)
    at sun.nio.fs.WindowsFileSystemProvider.getFileStore(WindowsFileSystemProvider.java:482)
    at java.nio.file.Files.getFileStore(Files.java:1411)
    at utils.FileStoreMain.main(FileStoreMain.java:16)

How to fix this problem and receive the appropriate FileStore for P:?

mstrap
  • 16,808
  • 10
  • 56
  • 86
  • Why use `File` when you use JSR 203? Just use `Paths.get("P:/sub")` – fge Nov 26 '15 at 13:35
  • 1
    I've updated the example. However, this does not solve the problem. – mstrap Nov 26 '15 at 13:38
  • Sure it doesn't; it's just good practice, however. – fge Nov 26 '15 at 13:38
  • Also, can you please paste the _full_ exception, not just the exception message? And what are you trying to achieve? – fge Nov 26 '15 at 13:39
  • I've added the full exception. – mstrap Nov 26 '15 at 13:42
  • Does `p:\sub` exist when you perform this operation? Also, and again, what are you trying to achieve? – fge Nov 26 '15 at 13:42
  • Try [Path.toRealPath](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#toRealPath%28java.nio.file.LinkOption...%29) or File.getCanonicaPath.first. – Joop Eggen Nov 26 '15 at 13:52
  • @fge I'm trying to get the `FileStore` representing this location and checking e.g. whether it's read-only. Yes, `P:\sub` exists. – mstrap Nov 26 '15 at 14:05
  • @JoopEggen `Path.toRealPath()` returns `P:/sub`, so doesn't help. – mstrap Nov 26 '15 at 14:06
  • I'm really not sure why you are getting this error; is P: mapped to a "real" filestore? Isn't that a "virtual" drive? It looks to me that there is simply no filestore for P:. Try and see the list of filestores for the filesystem for this path by iterating over `path.getFileSystem().getFileStores()`. – fge Nov 26 '15 at 14:12
  • @fge, you are right, it's more or less a virtual drive and my goal is to get e.g. `isReadOnly()` information for the real drive to which the virtual drive maps. Hence, if will be possible to resolve `P:\` to `C:\Temp` using Java API, that would already solve my problem. I'm pretty sure that there is some API for that, but I can't find it. – mstrap Nov 26 '15 at 14:25

2 Answers2

2

Have a look at this bug report JDK-8034057 and at a related answer from Alan Bateman.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

The problem is that a "substed drive" is not a file store; it just associates a drive letter with a path on an existing drive.

You did:

subst p: c:\temp

which means, in fact, that the real filestore of your p:\sub is the drive associated with c:.

Note: that's just a hypothesis, I don't actually run windows. But if you try and iterate over the filestores (ie, by calling .getFileSystem().getFileStores() on your Path instance) then P: will not appear.

Now, the question remains as to how to obtain the real filestore, if it's possible at all. Maybe a FileAttributeView exists which can provide you with this information; try and see what attribute views are available to you, and their parameters, by using this code:

// using some Path instance named path...
final FileSystem fs = path.getFileSystem();
final Set<String> viewNames = fs.supportedFileAttributesView();

for (final String viewName: viewNames) {
    System.out.println("View " + viewName + ':');
    System.out.println(Files.readAttributes(path, viewName + ":*"));
}

Maybe there exists a view with the information you are looking for... No guarantee though.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thanks for this idea, unfortunately the views don't give any helpful results. – mstrap Nov 26 '15 at 14:47
  • @mstrap it is strange though that `.toRealPath()` will not even "desubst" the path... I'd have expected that it would in this situation. But then again I don't do Windows, so... – fge Nov 26 '15 at 14:55
  • @mstrap note that if there exists a command that lists the "substitutions" and that it is easily parseable then maybe you can obtain this information – fge Nov 26 '15 at 14:57
  • You mean calling the Windows command `subst`? This would be my last resort. Still hoping for a pure Java solution ... though it's becoming more and more unlikely. – mstrap Nov 26 '15 at 15:01
  • Maybe the filestores have an attribute which can help? Paths are not the only things with attributes – fge Nov 26 '15 at 15:06