December 27th, 2004

You can create an infinitely recursive directory tree

It is possible to create an infinitely recursive directory tree. This throws many recursive directory-traversal functions into disarray. Here’s how you do it. (Note: Requires NTFS.)

Create a directory in the root of your C: drive, call it C:\C, for lack of a more creative name. Right-click My Computer and select Manage. click on the Disk Management snap-in.

From the Disk Management snap-in, right-click the C drive and select “Change Drive Letter and Paths…”.

From the “Change Drive Letter and Paths for C:” dialog, click “Add”, then where it says “Mount in the following empty NTFS folder”, enter “C:\C”. Click OK.

Congratulations, you just created an infinitely recursive directory.

C:\> dir

Volume in drive has no label Volume Serial Number is A035-E01D

Directory of C:\

08/19/2001 08:43 PM 0 AUTOEXEC.BAT 12/23/2004 09:43 PM <JUNCTION> C 05/05/2001 04:09 PM 0 CONFIG.SYS 12/16/2001 04:34 PM <DIR> Documents and Settings 08/10/2004 12:00 AM <DIR> Program Files 08/28/2004 01:08 PM <DIR> WINDOWS 2 File(s) 0 bytes 4 Dir(s) 2,602,899,968 bytes free

C:\> dir C:\C

Volume in drive has no label Volume Serial Number is A035-E01D

Directory of C:\C

08/19/2001 08:43 PM 0 AUTOEXEC.BAT 12/23/2004 09:43 PM <JUNCTION> C 05/05/2001 04:09 PM 0 CONFIG.SYS 12/16/2001 04:34 PM <DIR> Documents and Settings 08/10/2004 12:00 AM <DIR> Program Files 08/28/2004 01:08 PM <DIR> WINDOWS 2 File(s) 0 bytes 4 Dir(s) 2,602,899,968 bytes free

C:\> dir C:\C\C\C\C\C\C

Volume in drive has no label Volume Serial Number is A035-E01D

Directory of C:\C\C\C\C\C\C

08/19/2001 08:43 PM 0 AUTOEXEC.BAT 12/23/2004 09:43 PM <JUNCTION> C 05/05/2001 04:09 PM 0 CONFIG.SYS 12/16/2001 04:34 PM <DIR> Documents and Settings 08/10/2004 12:00 AM <DIR> Program Files 08/28/2004 01:08 PM <DIR> WINDOWS 2 File(s) 0 bytes 4 Dir(s) 2,602,899,968 bytes free

Go ahead and add as many “\C”s as you like. You’ll just get your own C drive back again.

Okay, now that you’ve had your fun, go back to the “Change Drive Letter and Paths for C:” dialog and Remove the “C:\C” entry. Do this before you create some real havoc.

Now imagine what happens if you had tried a recursive treecopy from that mysterious C:\C directory. Or if you ran a program that did some sort of recursive operation starting from C:\C, like, say, trying to add up the sizes of all the files in it.

If you’re writing such a program, you need to be aware of reparse points (that thing that shows up as <JUNCTION> in the directory listing). You can identify them because their file attributes include the FILE_ATTRIBUTE_REPARSE_POINT flag. Of course, what you do when you find one of these is up to you. I’m just warning you that these strange things exist and if you aren’t careful, your program can go into an infinite loop.

Topics
Other

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.