Blame lib/lchown.c

Packit 8f70b4
/* Provide a stub lchown function for systems that lack it.
Packit 8f70b4
Packit 8f70b4
   Copyright (C) 1998-1999, 2002, 2004, 2006-2007, 2009-2018 Free Software
Packit 8f70b4
   Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
/* written by Jim Meyering */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
#include <stdbool.h>
Packit 8f70b4
#include <string.h>
Packit 8f70b4
#include <sys/stat.h>
Packit 8f70b4
Packit 8f70b4
#if !HAVE_LCHOWN
Packit 8f70b4
Packit 8f70b4
/* If the system chown does not follow symlinks, we don't want it
Packit 8f70b4
   replaced by gnulib's chown, which does follow symlinks.  */
Packit 8f70b4
# if CHOWN_MODIFIES_SYMLINK
Packit 8f70b4
#  undef chown
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
/* Work just like chown, except when FILE is a symbolic link.
Packit 8f70b4
   In that case, set errno to EOPNOTSUPP and return -1.
Packit 8f70b4
   But if autoconf tests determined that chown modifies
Packit 8f70b4
   symlinks, then just call chown.  */
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
lchown (const char *file, uid_t uid, gid_t gid)
Packit 8f70b4
{
Packit 8f70b4
# if HAVE_CHOWN
Packit 8f70b4
#  if ! CHOWN_MODIFIES_SYMLINK
Packit 8f70b4
  struct stat stats;
Packit 8f70b4
Packit 8f70b4
  if (lstat (file, &stats) == 0 && S_ISLNK (stats.st_mode))
Packit 8f70b4
    {
Packit 8f70b4
      errno = EOPNOTSUPP;
Packit 8f70b4
      return -1;
Packit 8f70b4
    }
Packit 8f70b4
#  endif
Packit 8f70b4
Packit 8f70b4
  return chown (file, uid, gid);
Packit 8f70b4
Packit 8f70b4
# else /* !HAVE_CHOWN */
Packit 8f70b4
  errno = ENOSYS;
Packit 8f70b4
  return -1;
Packit 8f70b4
# endif
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#else /* HAVE_LCHOWN */
Packit 8f70b4
Packit 8f70b4
# undef lchown
Packit 8f70b4
Packit 8f70b4
/* Work around trailing slash bugs in lchown.  */
Packit 8f70b4
int
Packit 8f70b4
rpl_lchown (const char *file, uid_t uid, gid_t gid)
Packit 8f70b4
{
Packit 8f70b4
  bool stat_valid = false;
Packit 8f70b4
  int result;
Packit 8f70b4
Packit 8f70b4
# if CHOWN_CHANGE_TIME_BUG
Packit 8f70b4
  struct stat st;
Packit 8f70b4
Packit 8f70b4
  if (gid != (gid_t) -1 || uid != (uid_t) -1)
Packit 8f70b4
    {
Packit 8f70b4
      if (lstat (file, &st))
Packit 8f70b4
        return -1;
Packit 8f70b4
      stat_valid = true;
Packit 8f70b4
      if (!S_ISLNK (st.st_mode))
Packit 8f70b4
        return chown (file, uid, gid);
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
# if CHOWN_TRAILING_SLASH_BUG
Packit 8f70b4
  if (!stat_valid)
Packit 8f70b4
    {
Packit 8f70b4
      size_t len = strlen (file);
Packit 8f70b4
      if (len && file[len - 1] == '/')
Packit 8f70b4
        return chown (file, uid, gid);
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
  result = lchown (file, uid, gid);
Packit 8f70b4
Packit 8f70b4
# if CHOWN_CHANGE_TIME_BUG && HAVE_LCHMOD
Packit 8f70b4
  if (result == 0 && stat_valid
Packit 8f70b4
      && (uid == st.st_uid || uid == (uid_t) -1)
Packit 8f70b4
      && (gid == st.st_gid || gid == (gid_t) -1))
Packit 8f70b4
    {
Packit 8f70b4
      /* No change in ownership, but at least one argument was not -1,
Packit 8f70b4
         so we are required to update ctime.  Since lchown succeeded,
Packit 8f70b4
         we assume that lchmod will do likewise.  But if the system
Packit 8f70b4
         lacks lchmod and lutimes, we are out of luck.  Oh well.  */
Packit 8f70b4
      result = lchmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
Packit 8f70b4
                                           | S_ISUID | S_ISGID | S_ISVTX));
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
  return result;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#endif /* HAVE_LCHOWN */