Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two paths is on the same mount point?

I need to move a file from one directory to another in the android filesystem. How can I programmatically check if the two paths is on the same mountpoint?

The reason I want to know this is because if they are, we need to copy the bits instead of using File.rename(newPath).

Examples when the paths is on different mountpoints:

  • The user wants to move a file from the internal to external storage.

  • The user wants to move a file from /sdcard/files to /sdcard/external_sd/files on a samsungdevice.

like image 321
Jakob Eriksson Avatar asked Nov 13 '11 11:11

Jakob Eriksson


2 Answers

Call File.rename. If it succeeds, they're on the same mountpoint.

like image 103
David Schwartz Avatar answered Nov 11 '22 19:11

David Schwartz


One way to determine which filesystem a file resides on from within an Android app:

  • get the file's canonical path by calling File.getCanonicalPath() on it.

  • then get the list of currently mounted filesystems & their mount point paths from /proc/mounts

  • and find which mount point path is the most complete string match for the canonical path of the file in question, this should give you the mount point / filesystem of the file.

Compare results from the two files.

like image 23
Jimmy Avatar answered Nov 11 '22 21:11

Jimmy