Boost logo

Boost :

From: Walter Landry (wlandry_at_[hidden])
Date: 2004-01-16 23:17:08


Greetings,

I have found a need to copy directories, both a straightforward copy
and a recursive copy. So I implemented copy_directory and copy
functions. Since I still haven't learned any Windows programming, it
only work with POSIX systems. I have attached a patch. It modifies
operations.hpp and operations_posix_windows.cpp. The recursive copy()
function might be better in convenience.hpp, but I think
copy_directory should go in operations.hpp.

Alternately, we could have just one copy function, with an optional
flag to indicate that it should be recursive. That seems like a
cleaner interface. If there is any interest in that, I can probably
make a patch.

Let me know what you think.

Regards,
Walter Landry
wlandry_at_[hidden]

--- /home/boo/arx/arx/src/boost/{arch}/++pristine-trees/unlocked/boost/boost--arx/boost--arx--1.0/wlandry_at_ucsd.edu--arx/boost--arx--1.0--patch-22/boost/filesystem/operations.hpp 2003-11-20 14:02:56.000000000 -0500
+++ /home/boo/arx/arx/src/boost/boost/filesystem/operations.hpp 2004-01-16 17:21:52.000000000 -0500
@@ -72,6 +72,12 @@
     void copy_file( const path & from_file_ph,
                     const path & to_file_ph );
 
+ void copy_directory( const path & from_dir_ph,
+ const path & to_dir_ph );
+
+ void copy( const path & from_file_ph,
+ const path & to_file_ph );
+
     path current_path();
     void current_path( const path & new_path );
     const path & initial_path();

--- /home/boo/arx/arx/src/boost/{arch}/++pristine-trees/unlocked/boost/boost--arx/boost--arx--1.0/wlandry_at_ucsd.edu--arx/boost--arx--1.0--patch-22/libs/filesystem/src/operations_posix_windows.cpp 2003-11-20 14:03:07.000000000 -0500
+++ /home/boo/arx/arx/src/boost/libs/filesystem/src/operations_posix_windows.cpp 2004-01-16 23:13:38.000000000 -0500
@@ -400,6 +400,69 @@
           from_file_ph, to_file_ph, fs::detail::system_error_code() ) );
       }
 
+
+ // this function copies files and directories. If copying a
+ // directory to a directory, it copies recursively.
+ void copy( const path & from_ph,
+ const path & to_ph )
+ {
+ // Make sure that the destination, if it exists, is a directory
+ if((exists(to_ph) && !is_directory(to_ph)) || (!exists(from_ph)))
+ boost::throw_exception( filesystem_error(
+ "boost::filesystem::copy",
+ from_ph, to_ph, fs::detail::system_error_code() ) );
+ if(!is_directory(from_ph))
+ {
+ if(exists(to_ph))
+ {
+ copy_file(from_ph,to_ph/from_ph.leaf());
+ }
+ else
+ {
+ copy_file(from_ph,to_ph);
+ }
+ }
+ else
+ {
+ path destination;
+ if(!exists(to_ph))
+ {
+ destination=to_ph;
+ }
+ else
+ {
+ destination=to_ph/from_ph.leaf();
+ }
+ copy_directory(from_ph,destination);
+
+ for(directory_iterator i(from_ph); i!=directory_iterator(); ++i)
+ {
+ copy(*i,destination/i->leaf());
+ }
+ }
+ }
+
+ void copy_directory( const path &from_dir_ph,
+ const path &to_dir_ph)
+ {
+ if(!exists(from_dir_ph) || !is_directory(from_dir_ph)
+ || exists(to_dir_ph))
+ boost::throw_exception( filesystem_error(
+ "boost::filesystem::copy_directory",
+ from_dir_ph, to_dir_ph, fs::detail::system_error_code()));
+
+# ifdef BOOST_POSIX
+ struct stat from_stat;
+ if ( (::stat( from_dir_ph.string().c_str(), &from_stat ) != 0)
+ || ::mkdir(to_dir_ph.native_directory_string().c_str(),
+ from_stat.st_mode)!=0)
+# endif
+ boost::throw_exception( filesystem_error(
+ "boost::filesystem::copy_directory",
+ from_dir_ph, to_dir_ph, fs::detail::system_error_code()));
+ }
+
+
     void copy_file( const path & from_file_ph,
                     const path & to_file_ph )
     {


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