Blame gl/fstat.c

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