A program that recursively traverses a directory tree, needs to figure out what to do about symlinks (and/or junction points on Windows), to avoid going into an infinite loop when a symlink points to a parent directory. The two obvious options are:

  1. Skip symlinks completely.

  2. Go ahead and follow them, but keep track of all the directories visited, and skip whenever it's about to hit an infinite loop.

Which is the most common/expected/preferred choice?

link|improve this question

80% accept rate
2  
That depends what it's doing it for. If it's building a directory tree to enumerate files or build a visual representation, the second one, if it's totalling up filesizes, the first. But either way, make sure you tell me which way you're doing it. – Phoshi Sep 11 '11 at 11:38
Or if the API implements it, realize that it is a symlink . . . – surfasb Sep 11 '11 at 13:07
feedback

1 Answer

If you are just browsing, follow.

But, you specified that you don't want infinite loops...

to avoid going into an infinite loop when a symlink points to a parent directory

If you are searching recursively in a Breadth-First manner, follow. In a Depth-First manner, skip.

If you are counting up file sizes, skip.

Keeping track is often an optimization, rather than an implementation. Of course, you better have it.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.