Blame lib/stat-time.h

Packit 33f14e
/* stat-related time functions.
Packit 33f14e
Packit 33f14e
   Copyright (C) 2005, 2007, 2009-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
/* Written by Paul Eggert.  */
Packit 33f14e
Packit 33f14e
#ifndef STAT_TIME_H
Packit 33f14e
#define STAT_TIME_H 1
Packit 33f14e
Packit 33f14e
#include <sys/stat.h>
Packit 33f14e
#include <time.h>
Packit 33f14e
Packit 33f14e
#ifndef _GL_INLINE_HEADER_BEGIN
Packit 33f14e
 #error "Please include config.h first."
Packit 33f14e
#endif
Packit 33f14e
_GL_INLINE_HEADER_BEGIN
Packit 33f14e
#ifndef _GL_STAT_TIME_INLINE
Packit 33f14e
# define _GL_STAT_TIME_INLINE _GL_INLINE
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#ifdef __cplusplus
Packit 33f14e
extern "C" {
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
/* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
Packit 33f14e
   struct timespec, if available.  If not, then STAT_TIMESPEC_NS (ST,
Packit 33f14e
   ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
Packit 33f14e
   if available.  ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
Packit 33f14e
   for access, status change, data modification, or birth (creation)
Packit 33f14e
   time respectively.
Packit 33f14e
Packit 33f14e
   These macros are private to stat-time.h.  */
Packit 33f14e
#if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
Packit 33f14e
# if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
Packit 33f14e
#  define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
Packit 33f14e
# else
Packit 33f14e
#  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
Packit 33f14e
# endif
Packit 33f14e
#elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
Packit 33f14e
# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
Packit 33f14e
#elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
Packit 33f14e
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
Packit 33f14e
#elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
Packit 33f14e
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
/* Return the nanosecond component of *ST's access time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_atime_ns (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
# if defined STAT_TIMESPEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_atim).tv_nsec;
Packit 33f14e
# elif defined STAT_TIMESPEC_NS
Packit 33f14e
  return STAT_TIMESPEC_NS (st, st_atim);
Packit 33f14e
# else
Packit 33f14e
  return 0;
Packit 33f14e
# endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return the nanosecond component of *ST's status change time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_ctime_ns (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
# if defined STAT_TIMESPEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_ctim).tv_nsec;
Packit 33f14e
# elif defined STAT_TIMESPEC_NS
Packit 33f14e
  return STAT_TIMESPEC_NS (st, st_ctim);
Packit 33f14e
# else
Packit 33f14e
  return 0;
Packit 33f14e
# endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return the nanosecond component of *ST's data modification time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_mtime_ns (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
# if defined STAT_TIMESPEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_mtim).tv_nsec;
Packit 33f14e
# elif defined STAT_TIMESPEC_NS
Packit 33f14e
  return STAT_TIMESPEC_NS (st, st_mtim);
Packit 33f14e
# else
Packit 33f14e
  return 0;
Packit 33f14e
# endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return the nanosecond component of *ST's birth time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_birthtime_ns (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
# if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
Packit 33f14e
# elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
Packit 33f14e
  return STAT_TIMESPEC_NS (st, st_birthtim);
Packit 33f14e
# else
Packit 33f14e
  /* Avoid a "parameter unused" warning.  */
Packit 33f14e
  (void) st;
Packit 33f14e
  return 0;
Packit 33f14e
# endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return *ST's access time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_atime (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
#ifdef STAT_TIMESPEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_atim);
Packit 33f14e
#else
Packit 33f14e
  struct timespec t;
Packit 33f14e
  t.tv_sec = st->st_atime;
Packit 33f14e
  t.tv_nsec = get_stat_atime_ns (st);
Packit 33f14e
  return t;
Packit 33f14e
#endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return *ST's status change time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_ctime (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
#ifdef STAT_TIMESPEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_ctim);
Packit 33f14e
#else
Packit 33f14e
  struct timespec t;
Packit 33f14e
  t.tv_sec = st->st_ctime;
Packit 33f14e
  t.tv_nsec = get_stat_ctime_ns (st);
Packit 33f14e
  return t;
Packit 33f14e
#endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return *ST's data modification time.  */
Packit 33f14e
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_mtime (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
#ifdef STAT_TIMESPEC
Packit 33f14e
  return STAT_TIMESPEC (st, st_mtim);
Packit 33f14e
#else
Packit 33f14e
  struct timespec t;
Packit 33f14e
  t.tv_sec = st->st_mtime;
Packit 33f14e
  t.tv_nsec = get_stat_mtime_ns (st);
Packit 33f14e
  return t;
Packit 33f14e
#endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return *ST's birth time, if available; otherwise return a value
Packit 33f14e
   with tv_sec and tv_nsec both equal to -1.  */
Packit 33f14e
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit 33f14e
get_stat_birthtime (struct stat const *st)
Packit 33f14e
{
Packit 33f14e
  struct timespec t;
Packit 33f14e
Packit 33f14e
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
Packit 33f14e
     || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
Packit 33f14e
  t = STAT_TIMESPEC (st, st_birthtim);
Packit 33f14e
#elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
Packit 33f14e
  t.tv_sec = st->st_birthtime;
Packit 33f14e
  t.tv_nsec = st->st_birthtimensec;
Packit 33f14e
#elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 33f14e
  /* Native Windows platforms (but not Cygwin) put the "file creation
Packit 33f14e
     time" in st_ctime (!).  See
Packit 33f14e
     <https://msdn.microsoft.com/en-us/library/14h5k7ff(VS.80).aspx>.  */
Packit 33f14e
# if _GL_WINDOWS_STAT_TIMESPEC
Packit 33f14e
  t = st->st_ctim;
Packit 33f14e
# else
Packit 33f14e
  t.tv_sec = st->st_ctime;
Packit 33f14e
  t.tv_nsec = 0;
Packit 33f14e
# endif
Packit 33f14e
#else
Packit 33f14e
  /* Birth time is not supported.  */
Packit 33f14e
  t.tv_sec = -1;
Packit 33f14e
  t.tv_nsec = -1;
Packit 33f14e
  /* Avoid a "parameter unused" warning.  */
Packit 33f14e
  (void) st;
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
Packit 33f14e
     || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
Packit 33f14e
     || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
Packit 33f14e
  /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
Packit 33f14e
     using zero.  Attempt to work around this problem.  Alas, this can
Packit 33f14e
     report failure even for valid timestamps.  Also, NetBSD
Packit 33f14e
     sometimes returns junk in the birth time fields; work around this
Packit 33f14e
     bug if it is detected.  */
Packit 33f14e
  if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
Packit 33f14e
    {
Packit 33f14e
      t.tv_sec = -1;
Packit 33f14e
      t.tv_nsec = -1;
Packit 33f14e
    }
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
  return t;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
#ifdef __cplusplus
Packit 33f14e
}
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
_GL_INLINE_HEADER_END
Packit 33f14e
Packit 33f14e
#endif