1

On android 8(API26), I want to use Files.createLink to create a hard link. I am very sure fExists does indeed exist, and f does not exist. And both of them are in app's data storage area.

fExists: /data/user/0/appPakageName/myFolder/a.png
  f: /data/user/0/appPakageName/myFolder2/a.png

                        Path p0 = fExists.toPath(), p2 = f.toPath();
                        try {
                            done = Files.createLink(p2, p0) != null;
                        } catch (Throwable lp) {
                            lp.printStackTrace();
                        }

I do not have any adhoc FileSystemProvider configured since I do not know how. the above code prints:

W/zygote: Got a deoptimization request on un-deoptimizable method void sun.nio.fs.UnixNativeDispatcher.link0(long, long)
W/System.err: java.nio.file.AccessDeniedException: /data/user/0/appPackageName/myFolder/a.png -> /data/user/0/appPackageName/myFolder2/a.png
W/System.err:     at sun.nio.fs.UnixFileSystemProvider.createLink(UnixFileSystemProvider.java:480)
    at java.nio.file.Files.createLink(Files.java:1086)
W/System.err:     at com.nativeapi.ManagerCache$Item.lambda$ensureCopy$1$ManagerCache$Item(ManagerCache.java:4721)
    at com.nativeapi.-$$Lambda$ManagerCache$Item$iI_UuLtrfZZ1CjaxV7zycT3h6rE.run(Unknown Source:6)
W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)

The AccessDeniedException is of IOException, not of SecurityException. Anyone has any idea how to successfully to create a hard link use the above API(android API 26)? thanks.

By the way, I also tried FileSystemProvider.createLink, I got the same "access denied". And when I forced stepping into the method, I can see the System.getSecurityManager() retuns null.

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

I have not tried Os.link for hard link available since android lollipop(API 21).

relevant link:

Creating Hardlinks and Symlinks in Android

Is Android M not allowing hard links?

jackiszhp
  • 767
  • 1
  • 5
  • 11

0 Answers0