Beman Dawes wrote:

> >6. Would it be reasonable to introduce a function "create_directories",
> > similiar in spirit to "remove_all"? That function would create
> > intermediate directories, not only the leaf one.
>
> Yes, that would be both reasonable and useful. The
> filesystem/convenience.hpp header we've discussed would be a good place.

Agreed. BTW, where would that header live? In CVS tree or in sandbox? And
what would be the access rules?

>
> Care to contribute it?

See attachment. A pretty trivial excersice, of course, except for one
point. The create_directories function as written by me does nothing (not throws) if you call it on existing directory. And explicit "exists" all is
certainly needed for parent directories, and I'm not sure what to do with
the full directory.

Allowing it to exist seems in spirit of this function --- after all it automatically creates needed directories and does nothing with already existing.

OTOH, this behaviour is different from 'create_directory', which throws if
directory does not exist.

Lastly, we probably can't change create_directory, at least for one reason.
On Linux, this operation is atomic, and can be used to make locks. I guess,
the same is true on Windows. If 'create_directory' checks for existence, you cannot use it for locking any more.

So, what's the right interface?

- Volodya


#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

namespace boost { namespace filesystem {

    /** Creates directory 'ph' and all necessary parent directories.
        @post exists(directory_ph) && is_directory(directory_ph) && is_empty(directory_ph)
     */
    void create_directories(const path& ph);

}}
#include <boost/filesystem/convenience.hpp>

namespace boost { namespace filesystem {

    void create_directories(const path& ph)
    {
        if (ph.empty())
            return;
    
        // First create branch, by calling ourself recursively
        create_directories(ph.branch_path());
        // Now that parent's path exists, create the directory
        if (!exists(ph))            
            create_directory(ph);                
    }


}}
#include <boost/filesystem/convenience.hpp>

using namespace boost::filesystem;

#include <iostream>
using namespace std;

int main()
{
    try {
        create_directories("a");
        create_directories("1/2/3");
        create_directories("1/2/3");
    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
    }
}
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to