
"Peter Dimov" <pdimov@mmltd.net> wrote:
Walter Landry wrote:
Greetings,
I have discovered a bug in the implementation of remove_all. remove_all works by reading through a directory and deleting entries along the way. If it comes across a subdirectory, it immediately recurses into that subdirectory. However, boost::filesystem uses readdir, which on some platforms (including OS X, apparently) uses a static data area that can be overwritten on each call to readdir. This messes up the original directory iteration, ending up with some entries not being deleted.
A solution is to not recurse the directory immediately, but to make a list of them. Once you have gone through all of the entries in a directory, then you recurse into the subdirectories. A patch is below, although it could probably be improved.
This patch would only fix remove_all, but the general case remains broken. A filesystem iterator should not be affected by another filesystem iterator.
The only fix for that is readdir_r. There is a comment in the docs that readdir_r might not always be available. This is one of those cases where an autoconf macro would come in handy.
By the way, POSIX says that readdir can only overwrite the result of readdir on the same directory stream, so OS X seems to be buggy in this regard.
I agree. Cheers, Walter Landry wlandry@ucsd.edu