Unhandled exception using directory_iterator

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.

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
On Tue, Apr 15, 2008 at 1:19 PM, 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. 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.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Tuesday 15 April 2008 14:19:51 Silvio Reis Junior wrote:
Hello friends!
Hi
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.
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() ); } } }
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: dizzy@roedu.net "Linux is obsolete" -- AST

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
On Tue, Apr 15, 2008 at 2:35 PM, dizzy
On Tuesday 15 April 2008 14:19:51 Silvio Reis Junior wrote:
Hello friends!
Hi
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.
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() ); } } }
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: dizzy@roedu.net "Linux is obsolete" -- AST _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Tuesday 15 April 2008 15:44:49 Ovanes Markarian wrote:
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: dizzy@roedu.net "Linux is obsolete" -- AST

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
On Wed, Apr 16, 2008 at 5:32 AM, dizzy
On Tuesday 15 April 2008 15:44:49 Ovanes Markarian wrote:
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: dizzy@roedu.net "Linux is obsolete" -- AST _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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

I haven't seen it. Just unhandled exception.
On Wed, Apr 16, 2008 at 11:25 AM, Igor R.
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 Learn more!http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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.
On Wed, Apr 16, 2008 at 6:33 PM, Silvio Reis Junior
I haven't seen it. Just unhandled exception.
On Wed, Apr 16, 2008 at 11:25 AM, Igor R.
wrote: The exception doesn't have some message saying "access denied"?
participants (4)
-
dizzy
-
Igor R.
-
Ovanes Markarian
-
Silvio Reis Junior