问题
Is it possible in Android to have a SymLink (Symbolic Link) that leads to a point higher in the directory tree than the initial directory, so leading to a loop? For example:
/
Parent
Child
SymLink to directory '/Parent'
This would loop if you try to crawl it with File.listFiles()
.
If so, how can one avoid it, other than not following SymLinks?
Sure, we could check if a link points to a parent directory, but more complicated structures would break that:
/
Parent1
SymLink to /Parent2
Parent2
SymLink to /Parent1
Right now, the best idea I have is to track all canonical directories already crawled, and refuse to crawl a directory if it has already been crawled, which seems excessive.
Does anyone have any better solutions?
NB: this is part of my attempt to find a solution to Crawling Android File System gets stuck in possible SymLink loop, so can be seen in that context if needed, and more code can be found there.
来源:https://stackoverflow.com/questions/59678418/avoiding-android-symbolic-link-loop