summaryrefslogtreecommitdiff
path: root/src/port/copydir.c
blob: 68959971fd34f67e76cda6c856c616e9225363e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 *	While "xcopy /e /i /q" works fine for copying directories, on Windows XP
 *	it requires an Window handle which prevents it from working when invoked
 *	as a service.
 */

#include "postgres.h"

#undef mkdir					/* no reason to use that macro because we
								 * ignore the 2nd arg */

#include <dirent.h>


int
copydir(char *fromdir, char *todir)
{
	DIR		   *xldir;
	struct dirent *xlde;
	char		fromfl[MAXPGPATH];
	char		tofl[MAXPGPATH];

	if (mkdir(todir) != 0)
	{
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not create directory \"%s\": %m", todir)));
		return 1;
	}
	xldir = opendir(fromdir);
	if (xldir == NULL)
	{
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not open directory \"%s\": %m", fromdir)));
		return 1;
	}

	while ((xlde = readdir(xldir)) != NULL)
	{
		snprintf(fromfl, MAXPGPATH, "%s/%s", fromdir, xlde->d_name);
		snprintf(tofl, MAXPGPATH, "%s/%s", todir, xlde->d_name);
		if (CopyFile(fromfl, tofl, TRUE) < 0)
		{
			int			save_errno = errno;

			closedir(xldir);
			errno = save_errno;
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not copy file \"%s\": %m", fromfl)));
			return 1;
		}
	}

	closedir(xldir);
	return 0;
}