Branch
Hash :
4aa26bff
Author :
Date :
2025-08-18T15:44:48
sys_stat: Fix namespace pollution on native Windows. * lib/issymlink.h: New file, extracted from lib/sys_stat.in.h. * lib/issymlink.c: Include issymlink.h instead of <sys/stat.h>. * lib/issymlinkat.c: Likewise. * modules/issymlink (Files): Add lib/issymlink.h. (Depends-on): Add extern-inline. (configure.ac): Use gl_MODULE_INDICATOR. (Include): Set to "issymlink.h". * modules/issymlinkat (Files): Add lib/issymlink.h. (Depends-on): Add extern-inline. (configure.ac): Use gl_MODULE_INDICATOR. (Include): Set to "issymlink.h". * lib/sys_stat.in.h: Don't include <errno.h>, <unistd.h>. (_GL_ISSYMLINK_INLINE, _GL_ISSYMLINKAT_INLINE): Remove macros. (issymlink, issymlinkat): Remove functions. * m4/sys_stat_h.m4 (gl_SYS_STAT_H_REQUIRE_DEFAULTS): Don't initialize GNULIB_ISSYMLINK, GNULIB_ISSYMLINKAT. * modules/sys_stat-h (Depends-on): Remove extern-inline. (Makefile.am): Don't substitute GNULIB_ISSYMLINK, GNULIB_ISSYMLINKAT. * lib/chown.c: Include issymlink.h. * lib/lchown.c: Likewise. * lib/lchmod.c: Likewise. * lib/fchmodat.c: Likewise. * lib/rename.c: Likewise. * lib/renameatu.c: Likewise. * lib/unlink.c: Likewise. * lib/unlinkat.c: Likewise. * lib/utimens.c: Likewise.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/* Implement lchmod on platforms where it does not work correctly.
Copyright 2020-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* written by Paul Eggert */
#include <config.h>
/* Specification. */
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <intprops.h>
#include "issymlink.h"
/* Work like chmod, except when FILE is a symbolic link.
In that case, on systems where permissions on symbolic links are unsupported
(such as Linux), set errno to EOPNOTSUPP and return -1. */
int
lchmod (char const *file, mode_t mode)
{
#ifdef O_PATH
/* Open a file descriptor with O_NOFOLLOW, to make sure we don't
follow symbolic links, if /proc is mounted. O_PATH is used to
avoid a failure if the file is not readable.
Cf. <https://sourceware.org/PR14578> */
int fd = open (file, O_PATH | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0)
return fd;
int err;
{
int ret = issymlinkat (fd, "");
if (ret > 0)
err = EOPNOTSUPP;
else if (ret == 0)
{
static char const fmt[] = "/proc/self/fd/%d";
char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
sprintf (buf, fmt, fd);
err = chmod (buf, mode) == 0 ? 0 : errno == ENOENT ? -1 : errno;
}
else
err = errno == ENOENT ? -1 : errno;
}
close (fd);
errno = err;
if (0 <= err)
return err == 0 ? 0 : -1;
#endif
size_t len = strlen (file);
if (len && file[len - 1] == '/')
{
struct stat st;
if (lstat (file, &st) < 0)
return -1;
if (!S_ISDIR (st.st_mode))
{
errno = ENOTDIR;
return -1;
}
}
/* O_PATH + /proc is not supported. */
if (issymlink (file) > 0)
{
errno = EOPNOTSUPP;
return -1;
}
/* Fall back on chmod, despite a possible race. */
return chmod (file, mode);
}