Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can there be a (Java 7) FileSystem for which a Path .isAbsolute() but has a null root?

The javadoc for .isAbsolute() says:

Tells whether or not this path is absolute.
An absolute path is complete in that it doesn't need to be combined with other path information in order to locate a file.

Returns: true if, and only if, this path is absolute

The javadoc for .getRoot() says:

Returns the root component of this path as a Path object, or null if this path does not have a root component.

Returns: a path representing the root component of this path, or null

OK, so, I am at a loss here; are there any filesystems out there for which a path may be absolute without a root at all?


EDIT: note that there CAN be paths which have a root but are NOT absolute. For instance, these on Windows systems:

  • C:foo;
  • \foo\bar.

But I am asking for the reverse here: no root and absolute.

like image 672
fge Avatar asked Nov 29 '14 12:11

fge


People also ask

What is the difference between file and path in Java?

In Java, Path, and File both are classes. They belong to different packages but perform the same mechanism. We can say that the Java Path class is the advanced version of the File class. We use both the classes for the File I/O operations.

How do you pass an absolute path in Java?

io. File. getAbsolutePath() is used to obtain the absolute path of a file in the form of a string. This method requires no parameters.

How do file paths work in Java?

A path can point to either a file or a directory. A path can be absolute or relative. An absolute path contains the full path from the root of the file system down to the file or directory it points to. A relative path contains the path to the file or directory relative to some other path.

What is path object in Java?

A Path object contains the file name and directory list used to construct the path and is used to examine, locate, and manipulate files. The helper class, java. nio. file. Paths (in plural form) is the formal way of creating Path objects.


2 Answers

Well, there are some obscure things with file systems. I made a few enterprise search crawlers, and somewhere down the road you will notice some strange file system things going on with paths. BTW: these are all implementations of custom (overridden) file systems, so no standard ones, and you can definitely argue for hours on what of those things are good ideas and what are not... Still, I don't think you'll encounter any of these cases with the standard file systems.

Here goes a few examples of strange things:

Files in container file systems (OLE2, ZIP, TAR, etc): c:\foo\bar\blah.zip\myfile

In this case, you can decide what item is 'the root':

  • 'c:\' ? That's not the root of the zip file containing the file...
  • 'c:\foo\bar\blah.zip' ? It might be the root of the file, but by doing that it might break your application.
  • 'blah.zip' ? Might be the root of the zip file - but regardless this might probably break your application as well.
  • '/' ? As in the '/' folder in the zip file? It might be possible, but that will give you a serious headache in the long run.

'graph' like structures like HTTP:

  • The fact that you have '/foo/bar' doesn't imply that '/foo' or even '/' exists. (Suppose that meets your criterium). The only thing you can do is walk the graph...
  • Note that protocols like WebDav are HTTP based and can give you a similar headache. I have some examples here of custom webdav file systems that don't have a 'root' folder, but do have absolute paths.

Still, you can argue that the top-most common path (if that exists...) that you can reach is the root or that there is a root - but you simply cannot reach it (even though it's really non-existent).

Samba/netbios

If you see a complete Samba (windows networking) network as a single file system, then you basically end up with a 'root' containing all workgroups, a workgroup containing all computers, a computer containing all shares, and then the files in the share.

However... the root and the workgroups don't really exist. They are things that are made up from a broadcast protocol (which is also quite unreliable if you have a network of over 1000 computers). From a crawler perspective, it makes all the sense in the world to treat the 'root' and 'workgroup' directories completely different from the (reliable) rest.

However

These scenario's describe only paths where the root is unreachable, unreliable or something else. Theoretically, I suppose that in any URL you can think of, there is always a root. After all, it's made up as a string of characters defining a hierarchy, which therefore by definition has a start.

like image 133
atlaste Avatar answered Oct 02 '22 18:10

atlaste


A Question of Semantics

From my understanding of the subject, an absolute path can only be absolute if it can be traced back to it's root. As such there should never be an absolute path without a root. Ultimately this just comes down to semantics and although we can find definitions that define the absolute path such (egs below);

  • "the absolute path contains the root directory and all other subdirectories"
  • "absolute path is a path that points to the same location on one file system regardless of the present working directory or combined paths. As such it must always contain the root directory."

The only real question left after this point is whether the definition by the Java API follows suit. The only place I can find reference to the definition of an absolute path (with reference to the root element) from an official Oracle source is from inside the official Java tutorial. The official Java tutorials say

An absolute path always contains the root element

If this statement is to be believed, then no file system (no matter how obscure) can contain a Path that the Java API will consider absolute, unless it also considers it to contain a root.

You could argue that in some non-heirarchical file systems you might fall into some issues deciding whether a file can be it's own root. However, by this definition in the Path API (emphasis mine), a path should not represent a non-hierarchical element;

A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements

like image 41
Rudi Kershaw Avatar answered Oct 02 '22 16:10

Rudi Kershaw