Crawling Android File System gets stuck in possible SymLink loop

会有一股神秘感。 提交于 2020-01-28 12:32:01

问题


I'm trying to crawl the entire file system of an android device, both directories and files, without the benefit of NIO, to build a tree of it. If I had NIO then I could use WalkTree or similar, but I don't.

The problem I am having (on the Nexus 5 API 23 x86 emulator) is in /sys/bus/pci/devices and possibly other directories (eg /proc/self) - it doesn't complete before the app times out/quits/crashes (unknown which), possibly getting into some kind of loop or something (the path may change in a repetitive fashion but the canonical path varies little or not at all) .

However if I rule out Symbolic links then that problem goes away but I get what is only some of the files on the device rather than all - for example lacking files under /data (or /data/media/0) and those files not showing up elsewhere - not to mention it looks completely different from the file system that most file managers show. The former is strange as I'd understood Symbolic Links pointed to files and folders that were still present in the file system, but just made them look as if they were elsewhere.

What's the solution? Do I have to code exceptions or special handling for /sys/bus/pci/devices, /proc/self and others? I'd prefer to keep Symbolic Links being followed if I can, and I'd prefer to crawl as many files and folders as I can (so starting in a sub-folder is not preferred).

And a few related questions that might affect the approach I eventually take - if I DO keep SymLinks then does that mean that some things will be crawled twice or more? Is there a way to avoid that? Is there a way to detect when something is the TARGET of a SymLink, other than following the SymLink and checking the CanonicalPath?

Here's my code:

I get the root (I understand that in Android, the first and likely only root is the valid one):

File[] roots = File.listRoots();
String rootPath = "";
try {
    rootPath = roots[0].getCanonicalPath();
} catch (IOException e) {
    // do something
}

Then I start the crawl (note the boolean to choose whether to ignore simlinks or not):

try {
    // check if the rootPath is null or empty, and then...
    File rootFile = new File(rootPath);
    rootNode = new FileFolderNode(rootFile, null, true, false);   // last param may be true to ignore sim links
    //FileFolderNode(String filePath, FileFolderNode parent, boolean addChildren, boolean ignoreSimLinks)
} catch (Exception e) {
    // do something
}

That uses the FileFolderNode, which has constructor:

    public FileFolderNode(File file, FileFolderNode parent, boolean addChildren, boolean ignoreSimLinks) throws IOException {
    if (file == null)
        throw new IOException("File is null in new FileFolderNode");

    if (!file.exists())
        throw new IOException("File '" + file.getName() + "' does not exist in new FileFolderNode");

    // for now this uses isSymLink() from https://svn.apache.org/repos/asf/commons/_moved_to_git/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java adjusted a bit to remove Java 7 and Windows mentions
    if (!ignoreSimLinks)
       if (FileUtils.isSymlink(file))
          return;

    this.name = file.getName();
    if (this.name.equals("") && ! file.getCanonicalPath().equals("/"))
        throw new IOException("Name is empty in new FileFolderNode");

    this.isDirectory = file.isDirectory();
    if (this.isDirectory) {
        this.children = new ArrayList<FileFolderNode>();

        if (addChildren) {
            File[] files = file.listFiles();
            if (files == null) {
                // do something
            } else {
                // add in children
                for (File f : files) {
                    FileFolderNode child = null;
                    try {
                        child = new FileFolderNode(f, this, addChildren, ignoreSimLinks);
                    } catch (Exception e) {
                        child = null;
                    }
                    if (child != null)
                        children.add(child);
                }
            }
        }
    }
}

Given the lack of answers here, I've broken this question down into areas needing clarification, and am trying to get answers to those - please do see if you can help with those:

Get Android Filing System root

Android SymLinks to hidden or separate locations or partitions

Avoiding Android Symbolic Link loop

来源:https://stackoverflow.com/questions/59559016/crawling-android-file-system-gets-stuck-in-possible-symlink-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!