Discussion:
Unhandled exception using directory_iterator
Silvio Reis Junior
2008-04-15 11:19:51 UTC
Permalink
Hello friends!
I'm trying to use directory_iterator but when I try to list recursivelly an
entire drive or a big directory I get an unhandled exception.
I'm trying to figure out if I did something wrong but the code is very
similar to the example on the boost web page.
Here is the code:

//-------------------------------------------------------------------
VOID CFileSearch::InternalSearch( path RootDir )
{
directory_iterator EndDirItr;
for ( directory_iterator DirItr( RootDir ); DirItr != EndDirItr;
++DirItr )
{
if ( is_regular( DirItr->status() ) )
{
_tprintf( "FILE: %s\n", DirItr->path().string().c_str() );
}
else if ( is_directory( DirItr->status() ) )
{
_tprintf( "DIR : %s\n", DirItr->path().string().c_str() );
InternalSearch( DirItr->path() );
}
}
}

//-------------------------------------------------------------------
VOID CFileSearch::Search( string RootDir )
{
path TempRootDir = system_complete( path( RootDir, native ) );
if ( exists( TempRootDir ) == true )
{
InternalSearch( TempRootDir );
}
}

//-------------------------------------------------------------------
CFileSearch FileSearch;
FileSearch.Search( "c:\\" );

Is there something wrong in the above code?
Thank you in advance.
Silvio.
Ovanes Markarian
2008-04-15 11:28:08 UTC
Permalink
Hi!

Just a question:
Did you run your code in debugger to see why it fails?


And an idea from the scratch, what happens if the directory found is either
. or ..? May be you should skip them?



Best Regards,
Ovanes
Post by Silvio Reis Junior
Hello friends!
I'm trying to use directory_iterator but when I try to list recursivelly
an entire drive or a big directory I get an unhandled exception.
I'm trying to figure out if I did something wrong but the code is very
similar to the example on the boost web page.
//-------------------------------------------------------------------
VOID CFileSearch::InternalSearch( path RootDir )
{
directory_iterator EndDirItr;
for ( directory_iterator DirItr( RootDir ); DirItr != EndDirItr;
++DirItr )
{
if ( is_regular( DirItr->status() ) )
{
_tprintf( "FILE: %s\n", DirItr->path().string().c_str() );
}
else if ( is_directory( DirItr->status() ) )
{
_tprintf( "DIR : %s\n", DirItr->path().string().c_str() );
InternalSearch( DirItr->path() );
}
}
}
//-------------------------------------------------------------------
VOID CFileSearch::Search( string RootDir )
{
path TempRootDir = system_complete( path( RootDir, native ) );
if ( exists( TempRootDir ) == true )
{
InternalSearch( TempRootDir );
}
}
//-------------------------------------------------------------------
CFileSearch FileSearch;
FileSearch.Search( "c:\\" );
Is there something wrong in the above code?
Thank you in advance.
Silvio.
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
dizzy
2008-04-15 12:35:51 UTC
Permalink
Post by Silvio Reis Junior
Hello friends!
Hi
Post by Silvio Reis Junior
I'm trying to use directory_iterator but when I try to list recursivelly an
entire drive or a big directory I get an unhandled exception.
I'm trying to figure out if I did something wrong but the code is very
similar to the example on the boost web page.
I have used directory_iterator many times for recursive traversals, no issues
so far.
Post by Silvio Reis Junior
//-------------------------------------------------------------------
VOID CFileSearch::InternalSearch( path RootDir )
{
directory_iterator EndDirItr;
for ( directory_iterator DirItr( RootDir ); DirItr != EndDirItr;
++DirItr )
{
if ( is_regular( DirItr->status() ) )
{
_tprintf( "FILE: %s\n", DirItr->path().string().c_str() );
}
else if ( is_directory( DirItr->status() ) )
{
_tprintf( "DIR : %s\n", DirItr->path().string().c_str() );
InternalSearch( DirItr->path() );
}
}
}
So you have a recursive algorithm. One thing that comes straight into my mind
(especially for big structures) is the depth of the calls. Try to rewrite it
iteratively and see if it still happens. Also, not sure on Windows (which you
seem to be using) but on POSIX doing that is surely a problem because
directory_iterator does follow symbolik links and thus one can easily create
an infinite depth loop.

I also sugest that if your intentation is to recurse then just use
recursive_directory_iterator directly (still pay attention to symlinks,
call .no_push() on them so recursive_directory_iterator does not follow them
through).
--
Mihai RUSU Email: ***@roedu.net
"Linux is obsolete" -- AST
Ovanes Markarian
2008-04-15 12:44:49 UTC
Permalink
This is what I was going to mention in my post with . and .. special
directories. With the approach used the infinite loop should happen already
with the first occurence of .-directory, which would recurse in the same
directory and find the first item (.-dir again).


Greetings,
Ovanes
Post by dizzy
Post by Silvio Reis Junior
Hello friends!
Hi
Post by Silvio Reis Junior
I'm trying to use directory_iterator but when I try to list recursivelly
an
Post by Silvio Reis Junior
entire drive or a big directory I get an unhandled exception.
I'm trying to figure out if I did something wrong but the code is very
similar to the example on the boost web page.
I have used directory_iterator many times for recursive traversals, no issues
so far.
Post by Silvio Reis Junior
//-------------------------------------------------------------------
VOID CFileSearch::InternalSearch( path RootDir )
{
directory_iterator EndDirItr;
for ( directory_iterator DirItr( RootDir ); DirItr != EndDirItr;
++DirItr )
{
if ( is_regular( DirItr->status() ) )
{
_tprintf( "FILE: %s\n", DirItr->path().string().c_str() );
}
else if ( is_directory( DirItr->status() ) )
{
_tprintf( "DIR : %s\n", DirItr->path().string().c_str() );
InternalSearch( DirItr->path() );
}
}
}
So you have a recursive algorithm. One thing that comes straight into my mind
(especially for big structures) is the depth of the calls. Try to rewrite it
iteratively and see if it still happens. Also, not sure on Windows (which you
seem to be using) but on POSIX doing that is surely a problem because
directory_iterator does follow symbolik links and thus one can easily create
an infinite depth loop.
I also sugest that if your intentation is to recurse then just use
recursive_directory_iterator directly (still pay attention to symlinks,
call .no_push() on them so recursive_directory_iterator does not follow them
through).
--
"Linux is obsolete" -- AST
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
dizzy
2008-04-16 08:32:38 UTC
Permalink
Post by Ovanes Markarian
This is what I was going to mention in my post with . and .. special
directories. With the approach used the infinite loop should happen already
with the first occurence of .-directory, which would recurse in the same
directory and find the first item (.-dir again).
No because boost::filesystem directory iterators do not expose (ie
automatically skip) "." and "..".
--
Mihai RUSU Email: ***@roedu.net
"Linux is obsolete" -- AST
Silvio Reis Junior
2008-04-16 11:53:49 UTC
Permalink
Hello folks!
Sorry for the late reply. I found what was wrong.
boost::filesystem directory_iterator throws an exception when access is
denied for a folder.
So I need to create a check for this before recursing into any folder.

Thanks for the replies!
Silvio
Post by Ovanes Markarian
Post by Ovanes Markarian
This is what I was going to mention in my post with . and .. special
directories. With the approach used the infinite loop should happen
already
Post by Ovanes Markarian
with the first occurence of .-directory, which would recurse in the same
directory and find the first item (.-dir again).
No because boost::filesystem directory iterators do not expose (ie
automatically skip) "." and "..".
--
"Linux is obsolete" -- AST
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Igor R.
2008-04-16 14:25:02 UTC
Permalink
The exception doesn't have some message saying "access denied"?
Hello folks!> Sorry for the late reply. I found what was wrong. > boost::filesystem directory_iterator throws an exception when access is denied for a folder.> So I need to create a check for this before recursing into any folder.> Thanks for the replies!> Silvio
_________________________________________________________________
Explore the seven wonders of the world
http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE
Silvio Reis Junior
2008-04-16 16:33:14 UTC
Permalink
I haven't seen it. Just unhandled exception.
Post by Igor R.
The exception doesn't have some message saying "access denied"?
------------------------------
Post by Silvio Reis Junior
Hello folks!
Sorry for the late reply. I found what was wrong.
boost::filesystem directory_iterator throws an exception when access is
denied for a folder.
Post by Silvio Reis Junior
So I need to create a check for this before recursing into any folder.
Thanks for the replies!
Silvio
------------------------------
Explore the seven wonders of the world Learn more!<http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE>
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Ovanes Markarian
2008-04-16 17:04:40 UTC
Permalink
Yes, directory iterator does not explain the error cause. I did the same
experience as well. But in some other context. As far as I remember it was
smth like trying to traverse the directory path which did not exist.
Directory iterator also threw an exception with directory iterator string.
Post by Silvio Reis Junior
I haven't seen it. Just unhandled exception.
Post by Igor R.
The exception doesn't have some message saying "access denied"?
Loading...