Infinite Recursion in Linux file system

I抦 working on some code that will recursively search through a directory and enumerate a list of files. However I抦 having trouble with infinite loops. When my code reaches a link (shortcut) in the file system it will register as a directory and start from there over and over again until memory is full. Anyone have any thoughts or recommendations?

Here抯 a sample of the code:

publicstaticvoid enumerateFiles(String parentDir)

{

File searchDir =new File(parentDir);

File[] list = searchDir.listFiles();

if(list !=null)

{

for(int i=0;i<list.length;i++)

{

if(list[i].isDirectory())

enumerateFiles(list[i].toString());

else

superList.file.add(list[i]);// public static ArrayList

}

}

}

Thanks in advance.>

[1338 byte] By [sisslacka] at [2007-10-3 4:28:02]
# 1
use getCanonicalFile() or getCanonicalPath(), they resolve symlinks
jsalonena at 2007-7-14 22:30:57 > top of Java-index,Java Essentials,Java Programming...
# 2

> use getCanonicalFile() or getCanonicalPath(), they

> resolve symlinks

Well the problem I had there was that it would still resolve to a directory and still loop infinitely.

But thanks to your post, I played around with the File class a little more and I noticed if you compare getCanonicalFile() or getCanonicalPath() to getAbsoluteFile() or getAbsolutePath() they are only equivalent when it is a normal file and they are different when it is a symlink.

Here is some output from a test app I made:

file.exists() = true

file.getAbsolutePath() = /sys/block/hdc/device/driver/1.0

file.getCanonicalPath() = /sys/devices/pci0000:00/0000:00:1f.1/ide1/1.0

file.getName() = 1.0

file.getParent() = /sys/block/hdc/device/driver

file.getPath() = /sys/block/hdc/device/driver/1.0

file.getAbsoluteFile() = /sys/block/hdc/device/driver/1.0

file.getCanonicalFile() = /sys/devices/pci0000:00/0000:00:1f.1/ide1/1.0

file.getClass() = class java.io.File

file.getParentFile() = /sys/block/hdc/device/driver

file.isAbsolute() = true

file.isDirectory() = true

file.isFile() = false

file.isHidden() = false

file.toURI() = file:/sys/block/hdc/device/driver/1.0/

file.toURL() = file:/sys/block/hdc/device/driver/1.0/

Since my app my have to enumerate all the files in / , I basically just coded the logic to throw out the symlinks and only pass the true files back into enumerateFiles(). Now I'm only enumerating the real files and not getting stuck in infinite loops.

sisslacka at 2007-7-14 22:30:57 > top of Java-index,Java Essentials,Java Programming...