Blame lib/fstat.c

Packit 33f14e
/* fstat() replacement.
Packit 33f14e
   Copyright (C) 2011-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
/* If the user's config.h happens to include <sys/stat.h>, let it include only
Packit 33f14e
   the system's <sys/stat.h> here, so that orig_fstat doesn't recurse to
Packit 33f14e
   rpl_fstat.  */
Packit 33f14e
#define __need_system_sys_stat_h
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
/* Get the original definition of fstat.  It might be defined as a macro.  */
Packit 33f14e
#include <sys/types.h>
Packit 33f14e
#include <sys/stat.h>
Packit 33f14e
#undef __need_system_sys_stat_h
Packit 33f14e
Packit 33f14e
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 33f14e
# define WINDOWS_NATIVE
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#if !defined WINDOWS_NATIVE
Packit 33f14e
Packit 33f14e
static int
Packit 33f14e
orig_fstat (int fd, struct stat *buf)
Packit 33f14e
{
Packit 33f14e
  return fstat (fd, buf);
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
/* Specification.  */
Packit 33f14e
/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
Packit 33f14e
   eliminates this include because of the preliminary #include <sys/stat.h>
Packit 33f14e
   above.  */
Packit 33f14e
#include "sys/stat.h"
Packit 33f14e
Packit 33f14e
#include <errno.h>
Packit 33f14e
#include <unistd.h>
Packit 33f14e
#ifdef WINDOWS_NATIVE
Packit 33f14e
# define WIN32_LEAN_AND_MEAN
Packit 33f14e
# include <windows.h>
Packit 33f14e
# if GNULIB_MSVC_NOTHROW
Packit 33f14e
#  include "msvc-nothrow.h"
Packit 33f14e
# else
Packit 33f14e
#  include <io.h>
Packit 33f14e
# endif
Packit 33f14e
# include "stat-w32.h"
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
rpl_fstat (int fd, struct stat *buf)
Packit 33f14e
{
Packit 33f14e
#if REPLACE_FCHDIR && REPLACE_OPEN_DIRECTORY
Packit 33f14e
  /* Handle the case when rpl_open() used a dummy file descriptor to work
Packit 33f14e
     around an open() that can't normally visit directories.  */
Packit 33f14e
  const char *name = _gl_directory_name (fd);
Packit 33f14e
  if (name != NULL)
Packit 33f14e
    return stat (name, buf);
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#ifdef WINDOWS_NATIVE
Packit 33f14e
  /* Fill the fields ourselves, because the original fstat function returns
Packit 33f14e
     values for st_atime, st_mtime, st_ctime that depend on the current time
Packit 33f14e
     zone.  See
Packit 33f14e
     <https://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00134.html>  */
Packit 33f14e
  HANDLE h = (HANDLE) _get_osfhandle (fd);
Packit 33f14e
Packit 33f14e
  if (h == INVALID_HANDLE_VALUE)
Packit 33f14e
    {
Packit 33f14e
      errno = EBADF;
Packit 33f14e
      return -1;
Packit 33f14e
    }
Packit 33f14e
  return _gl_fstat_by_handle (h, NULL, buf);
Packit 33f14e
#else
Packit 33f14e
  return orig_fstat (fd, buf);
Packit 33f14e
#endif
Packit 33f14e
}