Boost logo

Boost :

From: Martin (adrianm_at_[hidden])
Date: 2004-09-09 04:12:51


The filesystem status operations exists & is_directory will both throw if the
status of the file can't be determined (access problems or race conditions).

I think this makes the operations overly complicated to use since you need to
enclose them in try catch blocks. You normally guard operations that use
files in some way but it is easy to forget try/catch around exists /
is_directory.

(Noticed that exists has been updated in CVS to return true on error which is
probably ok but I think is_directory need to be updated as well.)

I think a good solution would be to have a non-throwing overload where the
return value in case of error is specified.

bool is_directory(const tdFS::path& aPath, bool aErrorRet) {
  try { return tdFS::is_directory(aPath); } catch (...) { return aErrorRet; }
}

Another way could be to have a "is_accessable" operation that determines if
the status of the entry can be determined but it will not solve the racing
problem.

usage:

for (directory_iterator itr(mypath); itr != directory_iterator(); ++itr)
  if (is_directory(*itr, true)) continue; // skip directories

or

for (directory_iterator itr(mypath); itr != directory_iterator(); ++itr)
  if (!is_accessable(*itr) || is_directory(*itr)) continue;

instead of

for (directory_iterator itr(mypath); itr != directory_iterator(); ++itr)
  try { if (is_directory(*itr)) continue; } catch (...) { continue; }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk