Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f357233

Browse files
committed
Make unlink() work for junction points on Windows.
To support harmonization of Windows and Unix code, teach our unlink() wrapper that junction points need to be unlinked with rmdir() on Windows. Tested-by: Andrew Dunstan <andrew@dunslane.net> Discussion: https://postgr.es/m/CA%2BhUKGLfOOeyZpm5ByVcAt7x5Pn-%3DxGRNCvgiUPVVzjFLtnY0w%40mail.gmail.com
1 parent c5cb8f3 commit f357233

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/port/dirmod.c

+27-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ int
9999
pgunlink(const char *path)
100100
{
101101
int loops = 0;
102+
struct stat st;
103+
104+
/*
105+
* This function might be called for a regular file or for a junction
106+
* point (which we use to emulate symlinks). The latter must be unlinked
107+
* with rmdir() on Windows. Before we worry about any of that, let's see
108+
* if we can unlink directly, since that's expected to be the most common
109+
* case.
110+
*/
111+
if (unlink(path) == 0)
112+
return 0;
113+
if (errno != EACCES)
114+
return -1;
115+
116+
/*
117+
* EACCES is reported for many reasons including unlink() of a junction
118+
* point. Check if that's the case so we can redirect to rmdir().
119+
*
120+
* Note that by checking only once, we can't cope with a path that changes
121+
* from regular file to junction point underneath us while we're retrying
122+
* due to sharing violations, but that seems unlikely. We could perhaps
123+
* prevent that by holding a file handle ourselves across the lstat() and
124+
* the retry loop, but that seems like over-engineering for now.
125+
*/
126+
if (lstat(path, &st) < 0)
127+
return -1;
102128

103129
/*
104130
* We need to loop because even though PostgreSQL uses flags that allow
@@ -107,7 +133,7 @@ pgunlink(const char *path)
107133
* someone else to close the file, as the caller might be holding locks
108134
* and blocking other backends.
109135
*/
110-
while (unlink(path))
136+
while ((S_ISLNK(st.st_mode) ? rmdir(path) : unlink(path)) < 0)
111137
{
112138
if (errno != EACCES)
113139
return -1;

0 commit comments

Comments
 (0)