Thomas S.
Thomas S.

Reputation: 6345

Find root directory for a given file on Linux using Java

I'm trying to implement a trash support on Linux according to specifications.freedesktop.org/trash-spec. It states

It MAY also choose to provide trashing in the “top directories” of some or all mounted resources. This trashing is done in two ways, described below as (1) and (2).

(1) An administrator can create an $topdir/.Trash directory. The permissions on this directories should permit all users who can trash files at all to write in it.; and the “sticky bit” in the permissions must be set, if the file system supports it.

When trashing a file from a non-home partition/device4 , an implementation (if it supports trashing in top directories) MUST check for the presence of $topdir/.Trash.

If I understand it correctly, I will need to find the $topdir which, according to my understanding, is the root of the mounted file system/partition.

Using

FileStore store = FileSystems.getDefault().getFileStores();

I can get all FileStore that seem to be the mount-points, but I don't see a reliable way to get the mount-point as File, Path or String.

How to find out the mount-points from (plain) Java?

Upvotes: 1

Views: 659

Answers (1)

Souperman
Souperman

Reputation: 9866

From the link you provided, this is the exact description of $topdir

Top directory , $topdir — the directory where a file system is mounted. “/” is the top directory for the root file system, but not for the other mounted file systems. For example, separate file systems might be mounted on “/home”, “/media/flash”, etc. In this text, the designation “$topdir” is used for “any top directory”.

The answers to this question suggest ways to get the mount information although I think it is rather roundabout. Hope this helps!

Upvotes: 1

Related Questions