/* Implement lchmod on platforms where it does not work correctly. Copyright 2020 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 . */ /* written by Paul Eggert */ #include #include #include #include #include #include #include /* Work like lchmod, except when FILE is a symbolic link. In that case, set errno to EOPNOTSUPP and return -1. */ int lchmod (char const *file, mode_t mode) { #if HAVE_FCHMODAT return fchmodat (AT_FDCWD, file, mode, AT_SYMLINK_NOFOLLOW); #elif defined O_PATH && defined AT_FDCWD int fd = openat (AT_FDCWD, file, O_PATH | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) return fd; static char const fmt[] = "/proc/self/fd/%d"; char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)]; sprintf (buf, fmt, fd); int chmod_result = chmod (buf, mode); int chmod_errno = errno; close (fd); if (chmod_result == 0) return chmod_result; if (chmod_errno != ENOENT) { errno = chmod_errno; return chmod_result; } /* /proc is not mounted; fall back on racy implementation. */ #endif #if HAVE_LSTAT struct stat st; int lstat_result = lstat (file, &st); if (lstat_result != 0) return lstat_result; if (S_ISLNK (st.st_mode)) { errno = EOPNOTSUPP; return -1; } #endif return chmod (file, mode); }