Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

Problem when recursing through directories

In my previous post I explained how you could build a class that uses recursion to scan through directories. On a similar article I wrote for CodeProject.com, I was brought to the attention of a possible issue that might occur when you do this.

It is possible to create an infinitely recursive directory tree on an NTFS drive. So if you don't provide a solution for this, you will create infinite loops in your application without being aware of the problem. An article explaining this issue can be found here. It's also where I found a solution, but you can simply try the following:

  1. Create a directory in the root of your C: drive, for example AAA.
  2. Right-click My Computer and select Manage.
  3. Click on Disk Management.
  4. In Disk Management, right-click the C drive and select "Change Drive Letter and Paths...".
  5. From the "Change Drive Letter and Paths for C:" dialog, click "Add"
  6. Where it says "Mount in the following empty NTFS folder", enter "C:\AAA". Click OK.

Congratulations, you just created an infinitely recursive directory. Just open op the Windows Explorer and select the AAA directory to see what happens. To solve this, you will need to check the attributes of the directory for the ReparsePoint attribute as in the following example:

if ((subDirectory.Attributes & FileAttributes.ReparsePoint) != 0)
{
    // This directory has the FileAttributes.ReparsePoint attribute set
} 

This fix is obviously available in the code that you can download in my post on How to: Scan directories using recursion

Update June 4, 2006. I closed comments on this post.

Comments

Lennard said:

Not only on NTFS is it possible to create a loop. There are more filesystems on this world.

I havn't test your code if it works on shares (attached to a drive letter or directly).
In that case this solution will not work.
# June 1, 2006 4:38 AM

Jan Schreuder said:

Lennard, if you'd like to send me an example scenario I would be more than willing to see if it will or will not work. And if it won't work, I'll try and provide a working solution.
# June 1, 2006 5:01 AM

Lennard said:

Get yourself a linux box with samba.
Create a windows share with samba.

In the folder you did share create a loop:
$ mkdir loop
$ cd loop
$ ln -s ../loop loop

Now you will have a folder which is looping.
Mount the share to a drive letter or use the UNC path. The application will loop.
# June 4, 2006 5:01 AM

Lennard said:

There is also an problem with deep folders which will give a path name longer than 255 chars.

Steps:
1: C:
2: mkdir C:\long
3: cd C:\long
4: mkdir 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5: share this folder with a sortname like 'l1' and make the share writeable
6: mount the share to a driveletter: 'net use G: \\winxp01\l1' (winxp01 is my machine name)
7: repeat step 4 on G:
8: create a dummy file inside the long folder on G:

Use the example application and let it scan C:\long
The second long folder will not be found, also the create file will not be found.

This is a result that windows cannot search thru paths which are longer than 255 chars.
# June 4, 2006 5:22 AM

Jan Schreuder said:

Interesting scenario's, but those I will not even attempt to fix.

To be honest, if it's a problem with Linux shares on windows, then I will leave solving that problem to people who want that solved.

Windows has a problem with pathnames longer then 255 bytes. All windows developers (should) know that. Windows explorer doesn´t even handle that situation properly, so I can´t be bothered.
# June 4, 2006 11:50 AM