Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-01-28 05:58:24


On Fri, Jan 28, 2005 at 10:17:54AM +0100, Lars Gullik Bj?nnes wrote:

> Anyway the problem with copy_file is that the requirement on having
> target non-existant is not possible to turn off.

Sounds like there should be a copy_file() overload (or a default
argument) with a "no clobber" flag saying whether or not to overwrite.

This would be analoguous to cp(1)'s -f switch and sh(1)'s >| redirection
operator, which give you the option of clobbering an existing file even
when in safe-by-default mode (i.e. using "cp -i" or "set -o noclobber")

Looking at the implementation of copy_file() I'm wondering why it isn't
done like this (this provides noclobber support too):

    std::fstream out;
    if (noclobber)
    {
        out.open(to_file_ph.string().c_str(), std::ios::in);
        if (out.is_open())
        {
            // file exists
            boost::throw_exception( filesystem_error(
                "boost::filesystem::copy_file",
                from_file_ph, to_file_ph, fs::detail::system_error_code() ) );
        }
    }
    std::ifstream in(from_file_ph.string().c_str());
    out.open(to_file_ph.string().c_str(), std::ios::out);
    out << in.rdbuf();

It may not be blindingly fast on all systems (though there's no reason
it can't be, with a good filebuf) but it's a lot simpler than a loop
with read(2)/write(2) and file descriptors that have to be closed
manually, and would work on all platforms.

jon

-- 
"In times like these, it helps to recall that there have always been
 times like these."
	- Paul Harvey

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