Frage

I am developing file manager application in Android. In my app, I have one button which shows number of files and number of folders for every folders. I use recursive function and my function works except proc folder. Functions goes into an infinite loop for this folder.

I guess there are temporary files in this folder but I do not know how to handle this condition.

I don't get any warning in log but I see this path in the log.

proc/self/task/20561/cwd/sys/devices/platform/omap/omap_l3_noc.0/subsystem/devices/power.0

How can I handle this temporarily files?

My function:

private void iterate(String directory) {
        File dir = new File(directory);

        File listFile[] = dir.listFiles();
        if (listFile != null && listFile.length > 0) {
            for (int i = 0; i < listFile.length; i++) {


                size = size + listFile[i].length();

                if (listFile[i].isDirectory()) {

                    numberOfFolders++;
                    iterate(listFile[i].getAbsolutePath());

                } else {

                    numberOfFiles++;

                }
            }


        }
    }
War es hilfreich?

Lösung

Avoid /proc /dev (sometimes) and /sys and you'll be fine. /proc and /sys definitely have recursions, and I've seen them in /dev too.

Basically avoid all fake (what's the correct word?) filesystems.

You can't detect this recursion (simply anyway) because as you've seen the path is recursive, it is not like following .. then going back into a child of the parent, the paths for two different levels in the recursion are DIFFERENT, where as ./A/B/C and ./A/../A/../A/B/../B/./././C are technically the same.

BTW why are you scanning these files? They're not real! What use could you have to be scanning them?

Put that in the question please!

Addendum

Run mount in a terminal and do not touch anything that isn't from a device. That should certainly help. 'cept /dev/pts.

For example, I could use sshfs to mount my other laptop's FS to this laptops, and I could mount this laptop's fs within my computers, and my computer's fs to my other laptops, now if I scan without caution recursion across 3 computers!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top