15

I am creating an app in which I would like to make use of hardlinks and symlinks in the Android external memory filesystem. I have tried using the commands

Os.link("oldpath", "newpath");
Os.link("oldpath", "newpath");

However, when I try this, I get this error:

link failed: EPERM (Operation not permitted)

This makes me think that you need root access, although I have seen other people do this same thing, and I would not think that they would have these commands if they needed root. Any ideas?

Ashwin Kudva
  • 325
  • 2
  • 16

2 Answers2

13

Call to Os.link is failing because Android uses FAT32 file system by default for external storage. FAT32 file system does not support hard links and soft links that is why you are getting operation not permitted error.

EPERM The filesystem containing oldpath and newpath does not support the creation of hard links.

You can read more information about link system call here

Furthermore you cannot fake hard links or soft links on FAT32 accurately. And also note that for creating hard link in Android requires root permission.

Kamil Mahmood
  • 527
  • 9
  • 22
  • So is there any way I can make a hardlink or something similar for my use, or is my case rendered moot? @KamilMahmood – Ashwin Kudva Jul 18 '17 at 20:10
  • Can you clarify why the last sentence is true? You normally should only need rwx perms on the directory and rw on the file. Is it because you need root to access any non-FAT32 filesystem? – Michael May 30 '19 at 22:57
  • @Michael You can access non-FAT32 filesystem without root. Most of the time `/data/data/com.yourapp/` is on non-FAT32 (ext4) filesystem but still you cannot create hardlink with in it even after having `rwx` on directory and `rw` on file. – Kamil Mahmood May 31 '19 at 02:15
1

android Oreo(API 26) add Files.createLink and FileSystemProvider.createLink for hard link.

android lollipop(API 21) add Os.link for hard link

API 26 also add LinkPermission("hard") and LinkPermission("symbolic"), but I do not know how to use them.

jackiszhp
  • 767
  • 1
  • 5
  • 11