Skip to content

Commit ca99219

Browse files
macdiceadunstan
authored andcommitted
Replace pgwin32_is_junction() with lstat().
Now that lstat() reports junction points with S_IFLNK/S_ISLINK(), and unlink() can unlink them, there is no need for conditional code for Windows in a few places. That was expressed by testing for WIN32 or S_ISLNK, which we can now constant-fold. The coding around pgwin32_is_junction() was a bit suspect anyway, as we never checked for errors, and we also know that errors can be spuriously reported because of transient sharing violations on this OS. The lstat()-based code has handling for that. This also reverts 4fc6b6e on master only. That was done because lstat() didn't previously work for symlinks (junction points), but now it does. Tested-by: Andrew Dunstan <andrew@dunslane.net> Discussion: https://postgr.es/m/CA%2BhUKGLfOOeyZpm5ByVcAt7x5Pn-%3DxGRNCvgiUPVVzjFLtnY0w%40mail.gmail.com (cherry picked from commit 5fc88c5) Author: Thomas Munro <tmunro@postgresql.org> Author: Alexandra Wang <alexandra.wang.oss@gmail.com>
1 parent 8a5e498 commit ca99219

File tree

10 files changed

+2
-78
lines changed

10 files changed

+2
-78
lines changed

src/backend/commands/tablespace.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
787787
/*
788788
* Try to remove the symlink. We must however deal with the possibility
789789
* that it's a directory instead of a symlink --- this could happen during
790-
* WAL replay (see TablespaceCreateDbspace), and it is also the case on
791-
* Windows where junction points lstat() as directories.
790+
* WAL replay (see TablespaceCreateDbspace).
792791
*
793792
* Note: in the redo case, we'll return true if this final step fails;
794793
* there's no point in retrying it. Also, ENOENT should provoke no more
@@ -818,7 +817,6 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
818817
linkloc)));
819818
}
820819
}
821-
#ifdef S_ISLNK
822820
else if (S_ISLNK(st.st_mode))
823821
{
824822
if (unlink(linkloc) < 0)
@@ -831,7 +829,6 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
831829
linkloc)));
832830
}
833831
}
834-
#endif
835832
else
836833
{
837834
/* Refuse to remove anything that's not a directory or symlink */
@@ -909,7 +906,6 @@ remove_tablespace_symlink(const char *linkloc)
909906
errmsg("could not remove directory \"%s\": %m",
910907
linkloc)));
911908
}
912-
#ifdef S_ISLNK
913909
else if (S_ISLNK(st.st_mode))
914910
{
915911
if (unlink(linkloc) < 0 && errno != ENOENT)
@@ -918,7 +914,6 @@ remove_tablespace_symlink(const char *linkloc)
918914
errmsg("could not remove symbolic link \"%s\": %m",
919915
linkloc)));
920916
}
921-
#endif
922917
else
923918
{
924919
/* Refuse to remove anything that's not a directory or symlink */

src/backend/replication/basebackup.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,13 +1415,7 @@ sendDir(const char *path, int basepathlen, bool sizeonly, List *tablespaces,
14151415
}
14161416

14171417
/* Allow symbolic links in pg_tblspc only */
1418-
if (strcmp(path, "./pg_tblspc") == 0 &&
1419-
#ifndef WIN32
1420-
S_ISLNK(statbuf.st_mode)
1421-
#else
1422-
pgwin32_is_junction(pathbuf)
1423-
#endif
1424-
)
1418+
if (strcmp(path, "./pg_tblspc") == 0 && S_ISLNK(statbuf.st_mode))
14251419
{
14261420
#if defined(HAVE_READLINK) || defined(WIN32)
14271421
char linkpath[MAXPGPATH];
@@ -1884,11 +1878,7 @@ _tarWriteDir(const char *pathbuf, int basepathlen, struct stat *statbuf,
18841878
bool sizeonly)
18851879
{
18861880
/* If symlink, write it as a directory anyway */
1887-
#ifndef WIN32
18881881
if (S_ISLNK(statbuf->st_mode))
1889-
#else
1890-
if (pgwin32_is_junction(pathbuf))
1891-
#endif
18921882
statbuf->st_mode = S_IFDIR | pg_dir_create_mode;
18931883

18941884
return _tarWriteHeader(pathbuf + basepathlen + 1, NULL, statbuf, sizeonly);

src/backend/storage/file/fd.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,7 +3351,6 @@ SyncDataDirectory(void)
33513351
*/
33523352
xlog_is_symlink = false;
33533353

3354-
#ifndef WIN32
33553354
{
33563355
struct stat st;
33573356

@@ -3363,10 +3362,6 @@ SyncDataDirectory(void)
33633362
else if (S_ISLNK(st.st_mode))
33643363
xlog_is_symlink = true;
33653364
}
3366-
#else
3367-
if (pgwin32_is_junction("pg_wal"))
3368-
xlog_is_symlink = true;
3369-
#endif
33703365

33713366
#ifdef HAVE_SYNCFS
33723367
if (recovery_init_sync_method == RECOVERY_INIT_SYNC_METHOD_SYNCFS)

src/backend/utils/adt/misc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
310310
char sourcepath[MAXPGPATH];
311311
char targetpath[MAXPGPATH];
312312
int rllen;
313-
#ifndef WIN32
314313
struct stat st;
315-
#endif
316314

317315
/*
318316
* It's useful to apply this function to pg_class.reltablespace, wherein
@@ -343,10 +341,6 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
343341
* created with allow_in_place_tablespaces enabled. If a directory is
344342
* found, a relative path to the data directory is returned.
345343
*/
346-
#ifdef WIN32
347-
if (!pgwin32_is_junction(sourcepath))
348-
PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
349-
#else
350344
if (lstat(sourcepath, &st) < 0)
351345
{
352346
ereport(ERROR,
@@ -357,7 +351,6 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
357351

358352
if (!S_ISLNK(st.st_mode))
359353
PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
360-
#endif
361354

362355
/*
363356
* In presence of a link or a junction point, return the path pointing to.

src/bin/pg_checksums/pg_checksums.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,7 @@ scan_directory(const char *basedir, const char *subdir, bool sizeonly)
391391
if (!sizeonly)
392392
scan_file(fn, segmentno);
393393
}
394-
#ifndef WIN32
395394
else if (S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
396-
#else
397-
else if (S_ISDIR(st.st_mode) || pgwin32_is_junction(fn))
398-
#endif
399395
{
400396
/*
401397
* If going through the entries of pg_tblspc, we assume to operate

src/bin/pg_rewind/file_ops.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,7 @@ recurse_dir(const char *datadir, const char *parentpath,
431431
/* recurse to handle subdirectories */
432432
recurse_dir(datadir, path, callback);
433433
}
434-
#ifndef WIN32
435434
else if (S_ISLNK(fst.st_mode))
436-
#else
437-
else if (pgwin32_is_junction(fullpath))
438-
#endif
439435
{
440436
#if defined(HAVE_READLINK) || defined(WIN32)
441437
char link_target[MAXPGPATH];

src/common/file_utils.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ fsync_pgdata(const char *pg_data,
7979
*/
8080
xlog_is_symlink = false;
8181

82-
#ifndef WIN32
8382
{
8483
struct stat st;
8584

@@ -88,10 +87,6 @@ fsync_pgdata(const char *pg_data,
8887
else if (S_ISLNK(st.st_mode))
8988
xlog_is_symlink = true;
9089
}
91-
#else
92-
if (pgwin32_is_junction(pg_wal))
93-
xlog_is_symlink = true;
94-
#endif
9590

9691
/*
9792
* If possible, hint to the kernel that we're soon going to fsync the data
@@ -459,27 +454,9 @@ get_dirent_type(const char *path,
459454
result = PGFILETYPE_REG;
460455
else if (S_ISDIR(fst.st_mode))
461456
result = PGFILETYPE_DIR;
462-
#ifdef S_ISLNK
463457
else if (S_ISLNK(fst.st_mode))
464458
result = PGFILETYPE_LNK;
465-
#endif
466459
}
467460

468-
#if defined(WIN32) && !defined(_MSC_VER)
469-
470-
/*
471-
* If we're on native Windows (not Cygwin, which has its own POSIX
472-
* symlinks), but not using the MSVC compiler, then we're using a
473-
* readdir() emulation provided by the MinGW runtime that has no d_type.
474-
* Since the lstat() fallback code reports junction points as directories,
475-
* we need an extra system call to check if we should report them as
476-
* symlinks instead, following our convention.
477-
*/
478-
if (result == PGFILETYPE_DIR &&
479-
!look_through_symlinks &&
480-
pgwin32_is_junction(path))
481-
result = PGFILETYPE_LNK;
482-
#endif
483-
484461
return result;
485462
}

src/include/port.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ extern int pgunlink(const char *path);
284284
#if defined(WIN32) && !defined(__CYGWIN__)
285285
extern int pgsymlink(const char *oldpath, const char *newpath);
286286
extern int pgreadlink(const char *path, char *buf, size_t size);
287-
extern bool pgwin32_is_junction(const char *path);
288287

289288
#define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
290289
#define readlink(path, buf, size) pgreadlink(path, buf, size)

src/include/port/win32_port.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ extern pgoff_t _pgftello64(FILE *stream);
228228
*/
229229
extern int pgsymlink(const char *oldpath, const char *newpath);
230230
extern int pgreadlink(const char *path, char *buf, size_t size);
231-
extern bool pgwin32_is_junction(const char *path);
232231

233232
#define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
234233
#define readlink(path, buf, size) pgreadlink(path, buf, size)

src/port/dirmod.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,4 @@ pgreadlink(const char *path, char *buf, size_t size)
362362
return r;
363363
}
364364

365-
/*
366-
* Assumes the file exists, so will return false if it doesn't
367-
* (since a nonexistent file is not a junction)
368-
*/
369-
bool
370-
pgwin32_is_junction(const char *path)
371-
{
372-
DWORD attr = GetFileAttributes(path);
373-
374-
if (attr == INVALID_FILE_ATTRIBUTES)
375-
{
376-
_dosmaperr(GetLastError());
377-
return false;
378-
}
379-
return ((attr & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT);
380-
}
381365
#endif /* defined(WIN32) && !defined(__CYGWIN__) */

0 commit comments

Comments
 (0)