Re: Use of 'cp -r' in CREATE DATABASE

Bruce Momjian <pgman@candle.pha.pa.us>

From: Bruce Momjian <pgman@candle.pha.pa.us>
To: "Nigel J. Andrews" <nandrews@investsystems.co.uk>
Cc: Alvaro Herrera <alvherre@dcc.uchile.cl>, PostgreSQL-development <pgsql-hackers@postgreSQL.org>
Date: 2003-12-12T01:05:54Z
Lists: pgsql-hackers
Nigel J. Andrews wrote:
> On Thu, 11 Dec 2003, Alvaro Herrera wrote:
> 
> > On Thu, Dec 11, 2003 at 06:36:05PM -0500, Bruce Momjian wrote:
> > > Our dbcommands.c has for create database:
> > > 
> > >     snprintf(buf, sizeof(buf), "cp -r '%s' '%s'", src_loc, target_dir);
> > > 
> > [...]
> > > 
> > > I think we should switch to -R in our code.
> > 
> > But you will have to write special code for Win32, won't you?
> > Maybe it would be better to avoid using system commands
> > altogether and copy the whole thing using syscalls ...
> 
> That was my immediate thought. Unfortunately that means reinventing the
> wheel; or grabbing it from BSD or somewhere and distributing it with
> postgresql.

We already have in dbcommands.c:
	
	#ifndef WIN32
	    snprintf(buf, sizeof(buf), "cp -r '%s' '%s'", src_loc, target_dir);
	    if (system(buf) != 0)
	    {
	        if (remove_dbdirs(nominal_loc, alt_loc))
	            ereport(ERROR,
	                    (errmsg("could not initialize database directory"),
	                     errdetail("Failing system command was: %s", buf),
	                     errhint("Look in the postmaster's stderr log for more information.")));
	        else
	            ereport(ERROR,
	                    (errmsg("could not initialize database directory; delete failed as well"),
	                     errdetail("Failing system command was: %s", buf),
	                     errhint("Look in the postmaster's stderr log for more information.")));
	    }
	#else   /* WIN32 */
	    if (copydir(src_loc, target_dir) != 0)
	    {
	        /* copydir should already have given details of its troubles */
	        if (remove_dbdirs(nominal_loc, alt_loc))
	            ereport(ERROR,
	                    (errmsg("could not initialize database directory")));
	        else
	            ereport(ERROR,
	                    (errmsg("could not initialize database directory; delete failed as well")));
	    }
	#endif  /* WIN32 */

so Win32 is already handled by copydir.  One reason we didn't implement
this in C is because there might be some OS-specific handling for copy. 
copydir() is in port/copydir.c and is for Win32 alone.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073