Boost logo

Boost Users :

From: Rush Manbert (rush_at_[hidden])
Date: 2006-10-26 14:03:42


Nick Dimiduk wrote:
> As per the subject line, I have a rather noobish question about using the
> boost::filesystem tools, specifically the path object. The idea here is
> that I want to start my program by removing the "tmp" directory in the
> current process context if it exists to ensure a clean working environment
> for the remainder of the process. To the code:
>
> ...
> boost::filesystem::path tmp_dir ("tmp", boost::filesystem::native); // get
> a native handle for "./tmp"
> boost::filesystem::remove_all (tmp_dir); // clean out this tree in the fs
> ...
>
> This code acts as expected on linux. On windows, it causes a crash. The
> debugger shows the tmp_dir.m_path._Bx is a bad pointer, indicating to me
> that the constructor failed somewhere along the way. My questions are
> these:
>
> (1) Is there any error checking I can perform to verify that the path()
> constructor succeeds, such as a null pointer value or a boolean flag within
> the data type?
> (2) Is there something about my code which is linux/POSIX specific?
> (3) Am I missing something else entirely?
>
> I'm using boost 1.33.0 and have a mess of other functioning code which
> suggests that my configuration/installation is correct on both platforms.
>

Hi Nick,

Errors cause a throw of boost::filesystem::filesystem_error, so you need
to catch it.

I think you also might need to check that boost::filesystem::exists
(tmp_dir) is true before you call remove_all.

Note also that remove_all throws if tmp_dir is empty, so you need to
handle that case as well.

So I guess your code should look something like this:

using boost::filesystem; // Just to save me some typing
try
{
     path tmp_dir ("tmp", boost::filesystem::native);
     if (exists (tmp_dir))
     {
         if (!is_empty (tmp_dir))
         { // Directory has content
             remove_all (tmp_dir);
         }
         else
         { // Directory is empty
             remove (tmp_dir);
         }
     }
}
catch (filesystem_error &er)
{ // Do something useful here
}

- Rush


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net