2003-11-12 00:52:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* copydir.c
|
|
|
|
* copies a directory
|
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2003-11-12 00:52:45 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2003-05-15 19:59:17 +02:00
|
|
|
* While "xcopy /e /i /q" works fine for copying directories, on Windows XP
|
2003-09-10 22:12:01 +02:00
|
|
|
* it requires a Window handle which prevents it from working when invoked
|
2003-05-15 19:59:17 +02:00
|
|
|
* as a service.
|
2003-09-10 22:12:01 +02:00
|
|
|
*
|
2003-11-12 00:52:45 +01:00
|
|
|
* IDENTIFICATION
|
2005-03-24 03:11:20 +01:00
|
|
|
* $PostgreSQL: pgsql/src/port/copydir.c,v 1.11 2005/03/24 02:11:20 tgl Exp $
|
2003-11-12 00:52:45 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
2003-05-15 19:59:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-02-24 00:03:10 +01:00
|
|
|
#include "storage/fd.h"
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
#undef mkdir /* no reason to use that macro because we
|
|
|
|
* ignore the 2nd arg */
|
2003-05-16 03:57:52 +02:00
|
|
|
|
|
|
|
|
2003-09-10 22:12:01 +02:00
|
|
|
/*
|
|
|
|
* copydir: copy a directory (we only need to go one level deep)
|
|
|
|
*
|
|
|
|
* Return 0 on success, nonzero on failure.
|
|
|
|
*
|
|
|
|
* NB: do not elog(ERROR) on failure. Return to caller so it can try to
|
|
|
|
* clean up.
|
|
|
|
*/
|
2003-05-15 19:59:17 +02:00
|
|
|
int
|
2003-08-04 02:43:34 +02:00
|
|
|
copydir(char *fromdir, char *todir)
|
2003-05-15 19:59:17 +02:00
|
|
|
{
|
|
|
|
DIR *xldir;
|
|
|
|
struct dirent *xlde;
|
|
|
|
char fromfl[MAXPGPATH];
|
|
|
|
char tofl[MAXPGPATH];
|
|
|
|
|
|
|
|
if (mkdir(todir) != 0)
|
|
|
|
{
|
2003-09-10 22:12:01 +02:00
|
|
|
ereport(WARNING,
|
2003-07-27 19:10:07 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not create directory \"%s\": %m", todir)));
|
2003-09-10 22:12:01 +02:00
|
|
|
return -1;
|
2003-05-15 19:59:17 +02:00
|
|
|
}
|
2004-02-24 00:03:10 +01:00
|
|
|
xldir = AllocateDir(fromdir);
|
2003-05-15 19:59:17 +02:00
|
|
|
if (xldir == NULL)
|
|
|
|
{
|
2003-09-10 22:12:01 +02:00
|
|
|
ereport(WARNING,
|
2003-07-27 19:10:07 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open directory \"%s\": %m", fromdir)));
|
2003-09-10 22:12:01 +02:00
|
|
|
return -1;
|
2003-05-15 19:59:17 +02:00
|
|
|
}
|
|
|
|
|
2005-03-24 03:11:20 +01:00
|
|
|
errno = 0;
|
2003-05-15 19:59:17 +02:00
|
|
|
while ((xlde = readdir(xldir)) != NULL)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
snprintf(fromfl, MAXPGPATH, "%s/%s", fromdir, xlde->d_name);
|
|
|
|
snprintf(tofl, MAXPGPATH, "%s/%s", todir, xlde->d_name);
|
|
|
|
if (CopyFile(fromfl, tofl, TRUE) < 0)
|
|
|
|
{
|
2003-09-10 22:12:01 +02:00
|
|
|
ereport(WARNING,
|
2003-08-04 02:43:34 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not copy file \"%s\": %m", fromfl)));
|
2004-02-24 00:03:10 +01:00
|
|
|
FreeDir(xldir);
|
2003-09-10 22:12:01 +02:00
|
|
|
return -1;
|
2003-08-04 02:43:34 +02:00
|
|
|
}
|
2005-03-24 03:11:20 +01:00
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
|
|
|
|
* not in released version
|
|
|
|
*/
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
|
|
|
errno = 0;
|
|
|
|
#endif
|
|
|
|
if (errno)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read directory \"%s\": %m", fromdir)));
|
|
|
|
FreeDir(xldir);
|
|
|
|
return -1;
|
2003-05-15 19:59:17 +02:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:03:10 +01:00
|
|
|
FreeDir(xldir);
|
2003-05-15 19:59:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|