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
|
|
|
*
|
2023-01-02 21:00:37 +01:00
|
|
|
* Portions Copyright (c) 1996-2023, 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
|
2022-08-30 02:52:58 +02:00
|
|
|
* Windows.
|
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 <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
|
2017-02-26 00:34:09 +01:00
|
|
|
#include <windows.h>
|
2004-08-29 04:58:50 +02:00
|
|
|
#include <w32api/winioctl.h>
|
|
|
|
#endif
|
2004-10-29 00:09:31 +02:00
|
|
|
#endif
|
|
|
|
|
2022-10-25 04:26:03 +02:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
#include "port/win32ntdll.h"
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int loops = 0;
|
|
|
|
|
2005-08-10 21:52:37 +02:00
|
|
|
/*
|
2006-11-08 21:12:05 +01:00
|
|
|
* We need to loop because even though PostgreSQL uses flags that allow
|
2005-08-10 21:52:37 +02:00
|
|
|
* rename while the file is open, other applications might have the file
|
2006-11-08 21:12:05 +01:00
|
|
|
* 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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modern NT-based Windows versions return ERROR_SHARING_VIOLATION if
|
|
|
|
* another process has the file open without FILE_SHARE_DELETE.
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:26:03 +02:00
|
|
|
/*
|
|
|
|
* Check if _pglstat64()'s reason for failure was STATUS_DELETE_PENDING.
|
|
|
|
* This doesn't apply to Cygwin, which has its own lstat() that would report
|
|
|
|
* the case as EACCES.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
lstat_error_was_status_delete_pending(void)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return false;
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
if (pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2003-04-22 04:18:48 +02:00
|
|
|
|
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
|
|
|
{
|
2022-10-25 04:26:03 +02:00
|
|
|
bool is_lnk;
|
2003-04-22 04:18:48 +02:00
|
|
|
int loops = 0;
|
2022-08-06 02:01:42 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function might be called for a regular file or for a junction
|
|
|
|
* point (which we use to emulate symlinks). The latter must be unlinked
|
|
|
|
* with rmdir() on Windows. Before we worry about any of that, let's see
|
|
|
|
* if we can unlink directly, since that's expected to be the most common
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
if (unlink(path) == 0)
|
|
|
|
return 0;
|
|
|
|
if (errno != EACCES)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EACCES is reported for many reasons including unlink() of a junction
|
|
|
|
* point. Check if that's the case so we can redirect to rmdir().
|
|
|
|
*
|
|
|
|
* Note that by checking only once, we can't cope with a path that changes
|
|
|
|
* from regular file to junction point underneath us while we're retrying
|
|
|
|
* due to sharing violations, but that seems unlikely. We could perhaps
|
|
|
|
* prevent that by holding a file handle ourselves across the lstat() and
|
|
|
|
* the retry loop, but that seems like over-engineering for now.
|
2022-10-25 04:26:03 +02:00
|
|
|
*
|
|
|
|
* In the special case of a STATUS_DELETE_PENDING error (file already
|
|
|
|
* unlinked, but someone still has it open), we don't want to report ENOENT
|
|
|
|
* to the caller immediately, because rmdir(parent) would probably fail.
|
|
|
|
* We want to wait until the file truly goes away so that simple recursive
|
|
|
|
* directory unlink algorithms work.
|
2022-08-06 02:01:42 +02:00
|
|
|
*/
|
|
|
|
if (lstat(path, &st) < 0)
|
2022-10-25 04:26:03 +02:00
|
|
|
{
|
|
|
|
if (lstat_error_was_status_delete_pending())
|
|
|
|
is_lnk = false;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
is_lnk = S_ISLNK(st.st_mode);
|
2003-04-22 04:18:48 +02:00
|
|
|
|
2005-08-10 21:52:37 +02:00
|
|
|
/*
|
2006-11-08 21:12:05 +01:00
|
|
|
* We need to loop because even though PostgreSQL uses flags that allow
|
2005-08-10 21:52:37 +02:00
|
|
|
* unlink while the file is open, other applications might have the file
|
2006-11-08 21:12:05 +01:00
|
|
|
* 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
|
|
|
*/
|
2022-10-25 04:26:03 +02:00
|
|
|
while ((is_lnk ? rmdir(path) : unlink(path)) < 0)
|
2003-04-22 04:18:48 +02: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
|
|
|
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-07-13 01:28:49 +02: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
|
|
|
|
{
|
|
|
|
DWORD ReparseTag;
|
|
|
|
WORD ReparseDataLength;
|
|
|
|
WORD Reserved;
|
|
|
|
/* SymbolicLinkReparseBuffer */
|
|
|
|
WORD SubstituteNameOffset;
|
|
|
|
WORD SubstituteNameLength;
|
|
|
|
WORD PrintNameOffset;
|
|
|
|
WORD PrintNameLength;
|
2015-02-21 22:49:28 +01:00
|
|
|
WCHAR PathBuffer[FLEXIBLE_ARRAY_MEMBER];
|
2004-10-12 00:50:48 +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)
|
|
|
|
{
|
|
|
|
HANDLE dirhandle;
|
|
|
|
DWORD len;
|
2015-02-21 22:49:28 +01:00
|
|
|
char buffer[MAX_PATH * sizeof(WCHAR) + offsetof(REPARSE_JUNCTION_DATA_BUFFER, PathBuffer)];
|
2004-08-07 23:48:09 +02:00
|
|
|
char nativeTarget[MAX_PATH];
|
2004-08-08 03:31:15 +02:00
|
|
|
char *p = nativeTarget;
|
2004-08-07 23:48:09 +02:00
|
|
|
REPARSE_JUNCTION_DATA_BUFFER *reparseBuf = (REPARSE_JUNCTION_DATA_BUFFER *) buffer;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
CreateDirectory(newpath, 0);
|
|
|
|
dirhandle = CreateFile(newpath, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0, 0, OPEN_EXISTING,
|
|
|
|
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)
|
2022-10-25 04:10:49 +02:00
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
2004-08-07 23:48:09 +02:00
|
|
|
return -1;
|
2022-10-25 04:10:49 +02:00
|
|
|
}
|
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 */
|
2015-02-21 22:49:28 +01:00
|
|
|
if (memcmp("\\??\\", oldpath, 4) != 0)
|
|
|
|
snprintf(nativeTarget, sizeof(nativeTarget), "\\??\\%s", oldpath);
|
2004-08-07 23:48:09 +02:00
|
|
|
else
|
2015-02-21 22:49:28 +01:00
|
|
|
strlcpy(nativeTarget, oldpath, sizeof(nativeTarget));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2010-10-29 21:51:52 +02:00
|
|
|
while ((p = strchr(p, '/')) != NULL)
|
2004-08-07 23:48:09 +02:00
|
|
|
*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;
|
|
|
|
reparseBuf->PrintNameOffset = len + sizeof(WCHAR);
|
|
|
|
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
|
|
|
/*
|
|
|
|
* FSCTL_SET_REPARSE_POINT is coded differently depending on SDK version;
|
|
|
|
* we use our own definition
|
|
|
|
*/
|
|
|
|
if (!DeviceIoControl(dirhandle,
|
|
|
|
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_ANY_ACCESS),
|
|
|
|
reparseBuf,
|
|
|
|
reparseBuf->ReparseDataLength + REPARSE_JUNCTION_DATA_BUFFER_HEADER_SIZE,
|
|
|
|
0, 0, &len, 0))
|
|
|
|
{
|
|
|
|
LPSTR msg;
|
2022-10-25 04:10:49 +02:00
|
|
|
int save_errno;
|
|
|
|
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
save_errno = errno;
|
2004-08-07 23:48:09 +02:00
|
|
|
|
2016-03-29 17:54:57 +02:00
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM,
|
2004-08-07 23:48:09 +02:00
|
|
|
NULL, GetLastError(),
|
2006-12-04 23:23:40 +01:00
|
|
|
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
|
2004-08-07 23:48:09 +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);
|
2022-10-25 04:10:49 +02:00
|
|
|
|
|
|
|
errno = save_errno;
|
|
|
|
|
2004-08-07 23:48:09 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(dirhandle);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-09 15:06:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pgreadlink - uses Win32 junction points
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pgreadlink(const char *path, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
DWORD attr;
|
|
|
|
HANDLE h;
|
2015-02-21 22:49:28 +01:00
|
|
|
char buffer[MAX_PATH * sizeof(WCHAR) + offsetof(REPARSE_JUNCTION_DATA_BUFFER, PathBuffer)];
|
2011-01-09 15:06:55 +01:00
|
|
|
REPARSE_JUNCTION_DATA_BUFFER *reparseBuf = (REPARSE_JUNCTION_DATA_BUFFER *) buffer;
|
|
|
|
DWORD len;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
attr = GetFileAttributes(path);
|
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = CreateFile(path,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
0);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DeviceIoControl(h,
|
|
|
|
FSCTL_GET_REPARSE_POINT,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
(LPVOID) reparseBuf,
|
|
|
|
sizeof(buffer),
|
|
|
|
&len,
|
|
|
|
NULL))
|
|
|
|
{
|
|
|
|
LPSTR msg;
|
|
|
|
|
|
|
|
errno = 0;
|
2016-03-29 17:54:57 +02:00
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM,
|
2011-01-09 15:06:55 +01:00
|
|
|
NULL, GetLastError(),
|
|
|
|
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
|
|
|
|
(LPSTR) &msg, 0, NULL);
|
|
|
|
#ifndef FRONTEND
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not get junction for \"%s\": %s",
|
|
|
|
path, msg)));
|
|
|
|
#else
|
|
|
|
fprintf(stderr, _("could not get junction for \"%s\": %s\n"),
|
|
|
|
path, msg);
|
|
|
|
#endif
|
|
|
|
LocalFree(msg);
|
|
|
|
CloseHandle(h);
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle(h);
|
|
|
|
|
|
|
|
/* Got it, let's get some results from this */
|
|
|
|
if (reparseBuf->ReparseTag != IO_REPARSE_TAG_MOUNT_POINT)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = WideCharToMultiByte(CP_ACP, 0,
|
|
|
|
reparseBuf->PathBuffer, -1,
|
|
|
|
buf,
|
|
|
|
size,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
if (r <= 0)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:13:52 +02:00
|
|
|
/* r includes the null terminator */
|
|
|
|
r -= 1;
|
|
|
|
|
2011-01-09 15:06:55 +01:00
|
|
|
/*
|
2022-10-25 04:21:42 +02:00
|
|
|
* If the path starts with "\??\" followed by a "drive absolute" path
|
|
|
|
* (known to Windows APIs as RtlPathTypeDriveAbsolute), then strip that
|
|
|
|
* prefix. This undoes some of the transformation performed by
|
2023-04-18 03:23:23 +02:00
|
|
|
* pgsymlink(), to get back to a format that users are used to seeing. We
|
2022-10-25 04:21:42 +02:00
|
|
|
* don't know how to transform other path types that might be encountered
|
|
|
|
* outside PGDATA, so we just return them directly.
|
2011-01-09 15:06:55 +01:00
|
|
|
*/
|
2022-10-25 04:21:42 +02:00
|
|
|
if (r >= 7 &&
|
|
|
|
buf[0] == '\\' &&
|
|
|
|
buf[1] == '?' &&
|
|
|
|
buf[2] == '?' &&
|
|
|
|
buf[3] == '\\' &&
|
|
|
|
isalpha(buf[4]) &&
|
|
|
|
buf[5] == ':' &&
|
|
|
|
buf[6] == '\\')
|
2011-01-09 15:06:55 +01:00
|
|
|
{
|
|
|
|
memmove(buf, buf + 4, strlen(buf + 4) + 1);
|
|
|
|
r -= 4;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-07-13 01:28:49 +02:00
|
|
|
#endif /* defined(WIN32) && !defined(__CYGWIN__) */
|