Blame lib/lstat.c

Packit 8f70b4
/* Work around a bug of lstat on some systems
Packit 8f70b4
Packit 8f70b4
   Copyright (C) 1997-2006, 2008-2018 Free Software 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
/* If the user's config.h happens to include <sys/stat.h>, let it include only
Packit 8f70b4
   the system's <sys/stat.h> here, so that orig_lstat doesn't recurse to
Packit 8f70b4
   rpl_lstat.  */
Packit 8f70b4
#define __need_system_sys_stat_h
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
#if !HAVE_LSTAT
Packit 8f70b4
/* On systems that lack symlinks, our replacement <sys/stat.h> already
Packit 8f70b4
   defined lstat as stat, so there is nothing further to do other than
Packit 8f70b4
   avoid an empty file.  */
Packit 8f70b4
typedef int dummy;
Packit 8f70b4
#else /* HAVE_LSTAT */
Packit 8f70b4
Packit 8f70b4
/* Get the original definition of lstat.  It might be defined as a macro.  */
Packit 8f70b4
# include <sys/types.h>
Packit 8f70b4
# include <sys/stat.h>
Packit 8f70b4
# undef __need_system_sys_stat_h
Packit 8f70b4
Packit 8f70b4
static int
Packit 8f70b4
orig_lstat (const char *filename, struct stat *buf)
Packit 8f70b4
{
Packit 8f70b4
  return lstat (filename, buf);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
Packit 8f70b4
   eliminates this include because of the preliminary #include <sys/stat.h>
Packit 8f70b4
   above.  */
Packit 8f70b4
# include "sys/stat.h"
Packit 8f70b4
Packit 8f70b4
# include "stat-time.h"
Packit 8f70b4
Packit 8f70b4
# include <string.h>
Packit 8f70b4
# include <errno.h>
Packit 8f70b4
Packit 8f70b4
/* lstat works differently on Linux and Solaris systems.  POSIX (see
Packit 8f70b4
   "pathname resolution" in the glossary) requires that programs like
Packit 8f70b4
   'ls' take into consideration the fact that FILE has a trailing slash
Packit 8f70b4
   when FILE is a symbolic link.  On Linux and Solaris 10 systems, the
Packit 8f70b4
   lstat function already has the desired semantics (in treating
Packit 8f70b4
   'lstat ("symlink/", sbuf)' just like 'lstat ("symlink/.", sbuf)',
Packit 8f70b4
   but on Solaris 9 and earlier it does not.
Packit 8f70b4
Packit 8f70b4
   If FILE has a trailing slash and specifies a symbolic link,
Packit 8f70b4
   then use stat() to get more info on the referent of FILE.
Packit 8f70b4
   If the referent is a non-directory, then set errno to ENOTDIR
Packit 8f70b4
   and return -1.  Otherwise, return stat's result.  */
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
rpl_lstat (const char *file, struct stat *sbuf)
Packit 8f70b4
{
Packit 8f70b4
  int result = orig_lstat (file, sbuf);
Packit 8f70b4
Packit 8f70b4
  /* This replacement file can blindly check against '/' rather than
Packit 8f70b4
     using the ISSLASH macro, because all platforms with '\\' either
Packit 8f70b4
     lack symlinks (mingw) or have working lstat (cygwin) and thus do
Packit 8f70b4
     not compile this file.  0 len should have already been filtered
Packit 8f70b4
     out above, with a failure return of ENOENT.  */
Packit 8f70b4
  if (result == 0)
Packit 8f70b4
    {
Packit 8f70b4
      if (S_ISDIR (sbuf->st_mode) || file[strlen (file) - 1] != '/')
Packit 8f70b4
        result = stat_time_normalize (result, sbuf);
Packit 8f70b4
      else
Packit 8f70b4
        {
Packit 8f70b4
          /* At this point, a trailing slash is permitted only on
Packit 8f70b4
             symlink-to-dir; but it should have found information on the
Packit 8f70b4
             directory, not the symlink.  Call 'stat' to get info about the
Packit 8f70b4
             link's referent.  Our replacement stat guarantees valid results,
Packit 8f70b4
             even if the symlink is not pointing to a directory.  */
Packit 8f70b4
          if (!S_ISLNK (sbuf->st_mode))
Packit 8f70b4
            {
Packit 8f70b4
              errno = ENOTDIR;
Packit 8f70b4
              return -1;
Packit 8f70b4
            }
Packit 8f70b4
          result = stat (file, sbuf);
Packit 8f70b4
        }
Packit 8f70b4
    }
Packit 8f70b4
  return result;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#endif /* HAVE_LSTAT */