2003-11-12 00:52:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* dirmod.c
|
2009-05-04 11:30:06 +02:00
|
|
|
* directory handling functions
|
2003-11-12 00:52:45 +01:00
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2003-11-12 00:52:45 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2009-05-04 11:30:06 +02:00
|
|
|
* This includes replacement versions of functions that work on
|
|
|
|
* Win32 (NT4 and newer).
|
2003-11-12 00:52:45 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/port/dirmod.c
|
2003-11-12 00:52:45 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
2003-04-24 18:40:17 +02:00
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2004-02-02 23:20:33 +01:00
|
|
|
#ifndef FRONTEND
|
2003-05-15 18:35:30 +02:00
|
|
|
#include "postgres.h"
|
2004-02-02 23:20:33 +01:00
|
|
|
#else
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#endif
|
2004-08-01 08:19:26 +02:00
|
|
|
|
2004-09-10 11:53:08 +02:00
|
|
|
/* Don't modify declarations in system headers */
|
|
|
|
#if defined(WIN32) || defined(__CYGWIN__)
|
|
|
|
#undef rename
|
|
|
|
#undef unlink
|
|
|
|
#endif
|
|
|
|
|
2004-08-01 08:19:26 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(__CYGWIN__)
|
2004-09-10 04:49:37 +02:00
|
|
|
#ifndef __CYGWIN__
|
2004-08-29 04:58:50 +02:00
|
|
|
#include <winioctl.h>
|
|
|
|
#else
|
|
|
|
#include <windows.h>
|
|
|
|
#include <w32api/winioctl.h>
|
|
|
|
#endif
|
2004-10-29 00:09:31 +02:00
|
|
|
#endif
|
|
|
|
|
2005-02-22 05:43:23 +01:00
|
|
|
|
2004-08-08 08:44:36 +02:00
|
|
|
#ifndef FRONTEND
|
2004-10-29 00:09:31 +02:00
|
|
|
|
2004-08-29 03:44:02 +02:00
|
|
|
/*
|
2004-10-29 00:09:31 +02:00
|
|
|
* On Windows, call non-macro versions of palloc; we can't reference
|
2007-07-25 14:22:54 +02:00
|
|
|
* CurrentMemoryContext in this file because of PGDLLIMPORT conflict.
|
2004-08-29 03:44:02 +02:00
|
|
|
*/
|
2004-10-29 00:09:31 +02:00
|
|
|
#if defined(WIN32) || defined(__CYGWIN__)
|
2004-08-29 04:58:50 +02:00
|
|
|
#undef palloc
|
|
|
|
#undef pstrdup
|
2004-08-08 08:44:36 +02:00
|
|
|
#define palloc(sz) pgport_palloc(sz)
|
|
|
|
#define pstrdup(str) pgport_pstrdup(str)
|
|
|
|
#endif
|
2005-10-15 04:49:52 +02:00
|
|
|
#else /* FRONTEND */
|
2004-10-29 00:09:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In frontend, fake palloc behavior with these
|
|
|
|
*/
|
|
|
|
#undef palloc
|
|
|
|
#undef pstrdup
|
|
|
|
#define palloc(sz) fe_palloc(sz)
|
|
|
|
#define pstrdup(str) fe_pstrdup(str)
|
|
|
|
#define repalloc(pointer,sz) fe_repalloc(pointer,sz)
|
|
|
|
#define pfree(pointer) free(pointer)
|
|
|
|
|
|
|
|
static void *
|
|
|
|
fe_palloc(Size size)
|
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
if ((res = malloc(size)) == NULL)
|
|
|
|
{
|
2005-02-22 05:43:23 +01:00
|
|
|
fprintf(stderr, _("out of memory\n"));
|
2004-10-29 00:09:31 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
fe_pstrdup(const char *string)
|
|
|
|
{
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
if ((res = strdup(string)) == NULL)
|
|
|
|
{
|
2005-02-22 05:43:23 +01:00
|
|
|
fprintf(stderr, _("out of memory\n"));
|
2004-10-29 00:09:31 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
fe_repalloc(void *pointer, Size size)
|
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
if ((res = realloc(pointer, size)) == NULL)
|
|
|
|
{
|
2005-02-22 05:43:23 +01:00
|
|
|
fprintf(stderr, _("out of memory\n"));
|
2004-10-29 00:09:31 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2005-10-15 04:49:52 +02:00
|
|
|
#endif /* FRONTEND */
|
2004-10-29 00:09:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(__CYGWIN__)
|
2004-08-08 08:44:36 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
/*
|
|
|
|
* pgrename
|
|
|
|
*/
|
2003-07-27 19:10:07 +02:00
|
|
|
int
|
|
|
|
pgrename(const char *from, const char *to)
|
2003-04-22 04:18:48 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int loops = 0;
|
2003-04-22 04:18:48 +02:00
|
|
|
|
2005-08-10 21:52:37 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* We need to loop because even though PostgreSQL uses flags that allow
|
|
|
|
* rename while the file is open, other applications might have the file
|
|
|
|
* open without those flags. However, we won't wait indefinitely for
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
* someone else to close the file, as the caller might be holding locks
|
|
|
|
* and blocking other backends.
|
2005-08-10 21:52:37 +02:00
|
|
|
*/
|
2004-09-09 02:59:49 +02:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2003-04-22 04:18:48 +02:00
|
|
|
while (!MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING))
|
|
|
|
#else
|
2006-11-08 21:12:05 +01:00
|
|
|
while (rename(from, to) < 0)
|
2003-04-22 04:18:48 +02:00
|
|
|
#endif
|
2006-11-08 21:12:05 +01:00
|
|
|
{
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
|
|
|
|
_dosmaperr(err);
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Modern NT-based Windows versions return ERROR_SHARING_VIOLATION if
|
|
|
|
* another process has the file open without FILE_SHARE_DELETE.
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
* ERROR_LOCK_VIOLATION has also been seen with some anti-virus
|
|
|
|
* software. This used to check for just ERROR_ACCESS_DENIED, so
|
|
|
|
* presumably you can get that too with some OS versions. We don't
|
|
|
|
* expect real permission errors where we currently use rename().
|
|
|
|
*/
|
|
|
|
if (err != ERROR_ACCESS_DENIED &&
|
|
|
|
err != ERROR_SHARING_VIOLATION &&
|
|
|
|
err != ERROR_LOCK_VIOLATION)
|
|
|
|
return -1;
|
2003-04-22 04:18:48 +02:00
|
|
|
#else
|
2006-11-08 21:12:05 +01:00
|
|
|
if (errno != EACCES)
|
|
|
|
return -1;
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (++loops > 100) /* time out after 10 sec */
|
2006-11-08 21:12:05 +01:00
|
|
|
return -1;
|
|
|
|
pg_usleep(100000); /* us */
|
|
|
|
}
|
2003-04-22 04:18:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
/*
|
|
|
|
* pgunlink
|
|
|
|
*/
|
2003-07-27 19:10:07 +02:00
|
|
|
int
|
|
|
|
pgunlink(const char *path)
|
2003-04-22 04:18:48 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int loops = 0;
|
2003-04-22 04:18:48 +02:00
|
|
|
|
2005-08-10 21:52:37 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* We need to loop because even though PostgreSQL uses flags that allow
|
|
|
|
* unlink while the file is open, other applications might have the file
|
|
|
|
* open without those flags. However, we won't wait indefinitely for
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
* someone else to close the file, as the caller might be holding locks
|
|
|
|
* and blocking other backends.
|
2005-08-10 21:52:37 +02:00
|
|
|
*/
|
2003-04-22 04:18:48 +02:00
|
|
|
while (unlink(path))
|
|
|
|
{
|
|
|
|
if (errno != EACCES)
|
|
|
|
return -1;
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
if (++loops > 100) /* time out after 10 sec */
|
2006-11-08 21:12:05 +01:00
|
|
|
return -1;
|
2004-08-01 08:19:26 +02:00
|
|
|
pg_usleep(100000); /* us */
|
2003-04-22 04:18:48 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-13 01:28:49 +02:00
|
|
|
/* We undefined these above; now redefine for possible use below */
|
|
|
|
#define rename(from, to) pgrename(from, to)
|
|
|
|
#define unlink(path) pgunlink(path)
|
|
|
|
#endif /* defined(WIN32) || defined(__CYGWIN__) */
|
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__) /* Cygwin has its own symlinks */
|
2004-10-29 00:09:31 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
/*
|
|
|
|
* pgsymlink support:
|
|
|
|
*
|
|
|
|
* This struct is a replacement for REPARSE_DATA_BUFFER which is defined in VC6 winnt.h
|
|
|
|
* but omitted in later SDK functions.
|
|
|
|
* We only need the SymbolicLinkReparseBuffer part of the original struct's union.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
DWORD ReparseTag;
|
|
|
|
WORD ReparseDataLength;
|
|
|
|
WORD Reserved;
|
|
|
|
/* SymbolicLinkReparseBuffer */
|
|
|
|
WORD SubstituteNameOffset;
|
|
|
|
WORD SubstituteNameLength;
|
|
|
|
WORD PrintNameOffset;
|
|
|
|
WORD PrintNameLength;
|
|
|
|
WCHAR PathBuffer[1];
|
2010-07-06 21:19:02 +02:00
|
|
|
} REPARSE_JUNCTION_DATA_BUFFER;
|
2004-08-07 23:48:09 +02:00
|
|
|
|
|
|
|
#define REPARSE_JUNCTION_DATA_BUFFER_HEADER_SIZE \
|
|
|
|
FIELD_OFFSET(REPARSE_JUNCTION_DATA_BUFFER, SubstituteNameOffset)
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgsymlink - uses Win32 junction points
|
|
|
|
*
|
2010-04-02 17:21:20 +02:00
|
|
|
* For reference: http://www.codeproject.com/KB/winsdk/junctionpoints.aspx
|
2004-08-07 23:48:09 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
pgsymlink(const char *oldpath, const char *newpath)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
HANDLE dirhandle;
|
|
|
|
DWORD len;
|
|
|
|
char buffer[MAX_PATH * sizeof(WCHAR) + sizeof(REPARSE_JUNCTION_DATA_BUFFER)];
|
|
|
|
char nativeTarget[MAX_PATH];
|
|
|
|
char *p = nativeTarget;
|
|
|
|
REPARSE_JUNCTION_DATA_BUFFER *reparseBuf = (REPARSE_JUNCTION_DATA_BUFFER *) buffer;
|
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
CreateDirectory(newpath, 0);
|
2004-08-29 07:07:03 +02:00
|
|
|
dirhandle = CreateFile(newpath, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0, 0, OPEN_EXISTING,
|
2005-10-15 04:49:52 +02:00
|
|
|
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, 0);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
if (dirhandle == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
/* make sure we have an unparsed native win32 path */
|
|
|
|
if (memcmp("\\??\\", oldpath, 4))
|
|
|
|
sprintf(nativeTarget, "\\??\\%s", oldpath);
|
|
|
|
else
|
|
|
|
strcpy(nativeTarget, oldpath);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
while ((p = strchr(p, '/')) != 0)
|
|
|
|
*p++ = '\\';
|
|
|
|
|
|
|
|
len = strlen(nativeTarget) * sizeof(WCHAR);
|
|
|
|
reparseBuf->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
|
|
|
|
reparseBuf->ReparseDataLength = len + 12;
|
|
|
|
reparseBuf->Reserved = 0;
|
|
|
|
reparseBuf->SubstituteNameOffset = 0;
|
|
|
|
reparseBuf->SubstituteNameLength = len;
|
2004-08-29 07:07:03 +02:00
|
|
|
reparseBuf->PrintNameOffset = len + sizeof(WCHAR);
|
2004-08-07 23:48:09 +02:00
|
|
|
reparseBuf->PrintNameLength = 0;
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, nativeTarget, -1,
|
|
|
|
reparseBuf->PathBuffer, MAX_PATH);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* FSCTL_SET_REPARSE_POINT is coded differently depending on SDK version;
|
|
|
|
* we use our own definition
|
2004-08-07 23:48:09 +02:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
if (!DeviceIoControl(dirhandle,
|
2005-10-15 04:49:52 +02:00
|
|
|
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_ANY_ACCESS),
|
2004-08-29 07:07:03 +02:00
|
|
|
reparseBuf,
|
2005-10-15 04:49:52 +02:00
|
|
|
reparseBuf->ReparseDataLength + REPARSE_JUNCTION_DATA_BUFFER_HEADER_SIZE,
|
2004-08-29 07:07:03 +02:00
|
|
|
0, 0, &len, 0))
|
2004-08-07 23:48:09 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
LPSTR msg;
|
2004-08-07 23:48:09 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
errno = 0;
|
2004-08-07 23:48:09 +02:00
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
2004-08-29 07:07:03 +02:00
|
|
|
NULL, GetLastError(),
|
2006-12-04 23:23:40 +01:00
|
|
|
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
|
2009-06-11 16:49:15 +02:00
|
|
|
(LPSTR) &msg, 0, NULL);
|
2004-08-08 07:04:41 +02:00
|
|
|
#ifndef FRONTEND
|
2004-10-29 00:09:31 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2005-08-02 17:14:47 +02:00
|
|
|
errmsg("could not set junction for \"%s\": %s",
|
2004-10-29 00:09:31 +02:00
|
|
|
nativeTarget, msg)));
|
2004-08-08 07:04:41 +02:00
|
|
|
#else
|
2005-08-02 17:14:47 +02:00
|
|
|
fprintf(stderr, _("could not set junction for \"%s\": %s\n"),
|
2004-10-29 00:09:31 +02:00
|
|
|
nativeTarget, msg);
|
2004-08-08 03:31:15 +02:00
|
|
|
#endif
|
2004-08-07 23:48:09 +02:00
|
|
|
LocalFree(msg);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
CloseHandle(dirhandle);
|
|
|
|
RemoveDirectory(newpath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(dirhandle);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-15 22:14:46 +01:00
|
|
|
#endif /* defined(WIN32) && !defined(__CYGWIN__) */
|
2004-08-01 08:19:26 +02:00
|
|
|
|
2004-10-29 00:09:31 +02:00
|
|
|
|
2004-08-01 08:19:26 +02:00
|
|
|
/*
|
2007-01-19 17:42:24 +01:00
|
|
|
* pgfnames
|
2004-10-29 00:09:31 +02:00
|
|
|
*
|
2007-01-19 17:42:24 +01:00
|
|
|
* return a list of the names of objects in the argument directory. Caller
|
|
|
|
* must call pgfnames_cleanup later to free the memory allocated by this
|
|
|
|
* function.
|
2004-10-29 00:09:31 +02:00
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
char **
|
2008-04-18 19:05:45 +02:00
|
|
|
pgfnames(const char *path)
|
2004-10-29 00:09:31 +02:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *file;
|
|
|
|
char **filenames;
|
|
|
|
int numnames = 0;
|
|
|
|
int fnsize = 200; /* enough for many small dbs */
|
|
|
|
|
|
|
|
dir = opendir(path);
|
|
|
|
if (dir == NULL)
|
2005-03-24 03:11:20 +01:00
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
elog(WARNING, "could not open directory \"%s\": %m", path);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, _("could not open directory \"%s\": %s\n"),
|
|
|
|
path, strerror(errno));
|
|
|
|
#endif
|
2004-10-29 00:09:31 +02:00
|
|
|
return NULL;
|
2005-03-24 03:11:20 +01:00
|
|
|
}
|
2004-10-29 00:09:31 +02:00
|
|
|
|
|
|
|
filenames = (char **) palloc(fnsize * sizeof(char *));
|
|
|
|
|
2005-03-24 03:11:20 +01:00
|
|
|
errno = 0;
|
2004-10-29 00:09:31 +02:00
|
|
|
while ((file = readdir(dir)) != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
if (numnames + 1 >= fnsize)
|
2004-10-29 00:09:31 +02:00
|
|
|
{
|
|
|
|
fnsize *= 2;
|
|
|
|
filenames = (char **) repalloc(filenames,
|
|
|
|
fnsize * sizeof(char *));
|
|
|
|
}
|
|
|
|
filenames[numnames++] = pstrdup(file->d_name);
|
|
|
|
}
|
2005-03-24 03:11:20 +01:00
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
|
|
|
* released version
|
2005-03-24 03:11:20 +01:00
|
|
|
*/
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
|
|
|
errno = 0;
|
|
|
|
#endif
|
|
|
|
if (errno)
|
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
elog(WARNING, "could not read directory \"%s\": %m", path);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, _("could not read directory \"%s\": %s\n"),
|
|
|
|
path, strerror(errno));
|
|
|
|
#endif
|
2004-10-29 00:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
filenames[numnames] = NULL;
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return filenames;
|
|
|
|
}
|
|
|
|
|
2005-02-13 17:50:44 +01:00
|
|
|
|
2004-10-29 00:09:31 +02:00
|
|
|
/*
|
2007-01-19 17:42:24 +01:00
|
|
|
* pgfnames_cleanup
|
2004-08-08 05:51:20 +02:00
|
|
|
*
|
|
|
|
* deallocate memory used for filenames
|
2004-08-01 08:19:26 +02:00
|
|
|
*/
|
2007-01-19 17:42:24 +01:00
|
|
|
void
|
|
|
|
pgfnames_cleanup(char **filenames)
|
2004-08-01 08:19:26 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char **fn;
|
2004-08-01 08:19:26 +02:00
|
|
|
|
|
|
|
for (fn = filenames; *fn; fn++)
|
2004-08-08 05:51:20 +02:00
|
|
|
pfree(*fn);
|
|
|
|
|
|
|
|
pfree(filenames);
|
2004-08-01 08:19:26 +02:00
|
|
|
}
|
|
|
|
|
2005-02-13 17:50:44 +01:00
|
|
|
|
2004-08-01 08:19:26 +02:00
|
|
|
/*
|
2004-08-07 23:48:09 +02:00
|
|
|
* rmtree
|
|
|
|
*
|
|
|
|
* Delete a directory tree recursively.
|
|
|
|
* Assumes path points to a valid directory.
|
|
|
|
* Deletes everything under path.
|
|
|
|
* If rmtopdir is true deletes the directory too.
|
2008-04-18 19:05:45 +02:00
|
|
|
* Returns true if successful, false if there was any problem.
|
|
|
|
* (The details of the problem are reported already, so caller
|
|
|
|
* doesn't really have to say anything more, but most do.)
|
2004-08-01 08:19:26 +02:00
|
|
|
*/
|
|
|
|
bool
|
2008-04-18 19:05:45 +02:00
|
|
|
rmtree(const char *path, bool rmtopdir)
|
2004-08-01 08:19:26 +02:00
|
|
|
{
|
2008-04-18 19:05:45 +02:00
|
|
|
bool result = true;
|
2005-08-02 17:14:47 +02:00
|
|
|
char pathbuf[MAXPGPATH];
|
2004-08-01 08:19:26 +02:00
|
|
|
char **filenames;
|
|
|
|
char **filename;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* we copy all the names out of the directory before we start modifying
|
|
|
|
* it.
|
2004-08-01 08:19:26 +02:00
|
|
|
*/
|
2007-01-19 17:42:24 +01:00
|
|
|
filenames = pgfnames(path);
|
2004-08-01 08:19:26 +02:00
|
|
|
|
2004-10-29 00:09:31 +02:00
|
|
|
if (filenames == NULL)
|
2004-08-01 08:19:26 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* now we have the names we can start removing things */
|
|
|
|
for (filename = filenames; *filename; filename++)
|
|
|
|
{
|
2008-04-18 19:05:45 +02:00
|
|
|
snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
|
2004-08-01 08:19:26 +02:00
|
|
|
|
2008-04-18 08:48:38 +02:00
|
|
|
/*
|
|
|
|
* It's ok if the file is not there anymore; we were just about to
|
|
|
|
* delete it anyway.
|
|
|
|
*
|
|
|
|
* This is not an academic possibility. One scenario where this
|
2009-06-11 16:49:15 +02:00
|
|
|
* happens is when bgwriter has a pending unlink request for a file in
|
|
|
|
* a database that's being dropped. In dropdb(), we call
|
2008-04-18 08:48:38 +02:00
|
|
|
* ForgetDatabaseFsyncRequests() to flush out any such pending unlink
|
2009-06-11 16:49:15 +02:00
|
|
|
* requests, but because that's asynchronous, it's not guaranteed that
|
|
|
|
* the bgwriter receives the message in time.
|
2008-04-18 08:48:38 +02:00
|
|
|
*/
|
2008-04-18 19:05:45 +02:00
|
|
|
if (lstat(pathbuf, &statbuf) != 0)
|
2008-04-18 08:48:38 +02:00
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2008-04-18 19:05:45 +02:00
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
elog(WARNING, "could not stat file or directory \"%s\": %m",
|
|
|
|
pathbuf);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, _("could not stat file or directory \"%s\": %s\n"),
|
|
|
|
pathbuf, strerror(errno));
|
|
|
|
#endif
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
continue;
|
2008-04-18 08:48:38 +02:00
|
|
|
}
|
2004-08-01 08:19:26 +02:00
|
|
|
|
|
|
|
if (S_ISDIR(statbuf.st_mode))
|
|
|
|
{
|
|
|
|
/* call ourselves recursively for a directory */
|
2008-04-18 19:05:45 +02:00
|
|
|
if (!rmtree(pathbuf, true))
|
2004-08-01 08:19:26 +02:00
|
|
|
{
|
2005-02-13 17:50:44 +01:00
|
|
|
/* we already reported the error */
|
2008-04-18 19:05:45 +02:00
|
|
|
result = false;
|
2004-08-01 08:19:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-18 19:05:45 +02:00
|
|
|
if (unlink(pathbuf) != 0)
|
2008-04-18 08:48:38 +02:00
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2008-04-18 19:05:45 +02:00
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
elog(WARNING, "could not remove file or directory \"%s\": %m",
|
|
|
|
pathbuf);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
|
|
|
|
pathbuf, strerror(errno));
|
|
|
|
#endif
|
|
|
|
result = false;
|
|
|
|
}
|
2008-04-18 08:48:38 +02:00
|
|
|
}
|
2004-08-01 08:19:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rmtopdir)
|
|
|
|
{
|
2008-04-18 19:05:45 +02:00
|
|
|
if (rmdir(path) != 0)
|
|
|
|
{
|
2005-02-13 17:50:44 +01:00
|
|
|
#ifndef FRONTEND
|
2008-04-18 19:05:45 +02:00
|
|
|
elog(WARNING, "could not remove file or directory \"%s\": %m",
|
|
|
|
path);
|
2005-02-13 17:50:44 +01:00
|
|
|
#else
|
2008-04-18 19:05:45 +02:00
|
|
|
fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
|
|
|
|
path, strerror(errno));
|
2005-02-13 17:50:44 +01:00
|
|
|
#endif
|
2008-04-18 19:05:45 +02:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-19 17:42:24 +01:00
|
|
|
pgfnames_cleanup(filenames);
|
2008-04-18 19:05:45 +02:00
|
|
|
|
|
|
|
return result;
|
2004-08-01 08:19:26 +02:00
|
|
|
}
|
2008-04-10 18:58:51 +02:00
|
|
|
|
|
|
|
|
2008-04-12 01:53:00 +02:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
|
|
|
|
#undef stat
|
|
|
|
|
2008-04-10 18:58:51 +02:00
|
|
|
/*
|
|
|
|
* The stat() function in win32 is not guaranteed to update the st_size
|
|
|
|
* field when run. So we define our own version that uses the Win32 API
|
|
|
|
* to update this field.
|
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
int
|
|
|
|
pgwin32_safestat(const char *path, struct stat * buf)
|
2008-04-10 18:58:51 +02:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
int r;
|
2008-04-10 18:58:51 +02:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attr;
|
|
|
|
|
|
|
|
r = stat(path, buf);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!GetFileAttributesEx(path, GetFileExInfoStandard, &attr))
|
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* XXX no support for large files here, but we don't do that in general on
|
|
|
|
* Win32 yet.
|
2008-04-10 18:58:51 +02:00
|
|
|
*/
|
|
|
|
buf->st_size = attr.nFileSizeLow;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-04-12 01:53:00 +02:00
|
|
|
|
2008-04-10 18:58:51 +02:00
|
|
|
#endif
|