1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-26 07:33:47 +00:00
emacs/lib/stat-time.h

246 lines
7.9 KiB
C
Raw Permalink Normal View History

/* stat-related time functions.
2024-01-02 01:47:10 +00:00
Copyright (C) 2005, 2007, 2009-2024 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Paul Eggert. */
#ifndef STAT_TIME_H
#define STAT_TIME_H 1
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_UNUSED,
_GL_ATTRIBUTE_PURE, HAVE_STRUCT_STAT_*. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#include <errno.h>
#include <stdckdint.h>
#include <stddef.h>
#include <sys/stat.h>
#include <time.h>
_GL_INLINE_HEADER_BEGIN
#ifndef _GL_STAT_TIME_INLINE
# define _GL_STAT_TIME_INLINE _GL_INLINE
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
for access, status change, data modification, or birth (creation)
time respectively.
These macros are private to stat-time.h. */
#if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
# if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
# define STAT_TIMESPEC_OFFSETOF(st_xtim) offsetof (struct stat, st_xtim)
# else
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
# endif
#elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
# define STAT_TIMESPEC_OFFSETOF(st_xtim) offsetof (struct stat, st_xtim##espec)
#elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
#elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
#endif
/* Return the nanosecond component of *ST's access time. */
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
get_stat_atime_ns (struct stat const *st)
{
# if defined STAT_TIMESPEC
return STAT_TIMESPEC (st, st_atim).tv_nsec;
# elif defined STAT_TIMESPEC_NS
return STAT_TIMESPEC_NS (st, st_atim);
# else
return 0;
# endif
}
/* Return the nanosecond component of *ST's status change time. */
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
get_stat_ctime_ns (struct stat const *st)
{
# if defined STAT_TIMESPEC
return STAT_TIMESPEC (st, st_ctim).tv_nsec;
# elif defined STAT_TIMESPEC_NS
return STAT_TIMESPEC_NS (st, st_ctim);
# else
return 0;
# endif
}
/* Return the nanosecond component of *ST's data modification time. */
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
get_stat_mtime_ns (struct stat const *st)
{
# if defined STAT_TIMESPEC
return STAT_TIMESPEC (st, st_mtim).tv_nsec;
# elif defined STAT_TIMESPEC_NS
return STAT_TIMESPEC_NS (st, st_mtim);
# else
return 0;
# endif
}
/* Return the nanosecond component of *ST's birth time. */
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
get_stat_birthtime_ns (_GL_UNUSED struct stat const *st)
{
# if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
# elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
return STAT_TIMESPEC_NS (st, st_birthtim);
# else
return 0;
# endif
}
/* Return *ST's access time. */
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
get_stat_atime (struct stat const *st)
{
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_atim);
#else
return (struct timespec) { .tv_sec = st->st_atime,
.tv_nsec = get_stat_atime_ns (st) };
#endif
}
/* Return *ST's status change time. */
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
get_stat_ctime (struct stat const *st)
{
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_ctim);
#else
return (struct timespec) { .tv_sec = st->st_ctime,
.tv_nsec = get_stat_ctime_ns (st) };
#endif
}
/* Return *ST's data modification time. */
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
get_stat_mtime (struct stat const *st)
{
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_mtim);
#else
return (struct timespec) { .tv_sec = st->st_mtime,
.tv_nsec = get_stat_mtime_ns (st) };
#endif
}
/* Return *ST's birth time, if available; otherwise return a value
with tv_sec and tv_nsec both equal to -1. */
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
get_stat_birthtime (_GL_UNUSED struct stat const *st)
{
struct timespec t;
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
t = STAT_TIMESPEC (st, st_birthtim);
#elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
t = (struct timespec) { .tv_sec = st->st_birthtime,
.tv_nsec = st->st_birthtimensec };
Update from Gnulib This incorporates: 2018-05-21 crypto: omit stream ops Emacs doesn’t need 2018-05-13 truncate: Fix compilation error on Android 2018-05-13 imaxdiv: Fix compilation error on Android 2018-05-13 Support selective inclusion of recent mingw.org headers 2018-05-13 Add cross-compilation guesses for Linux systems sans glibc 2018-05-13 stdioext: Fix compilation errors with newer Android headers 2018-05-07 af_alg: Pacify --enable-gcc-warnings 2018-05-06 af_alg: Fix bug with streams that are not at position 0 2018-05-06 Followup to 'af_alg: New module' 2018-05-05 crypto/{md5,sha1,sha256,sha512}: simplify 2018-05-05 af_alg: New module 2018-05-05 af_alg: Improve function signature 2018-04-28 md5sum: Use AF_ALG when available 2018-04-28 sha512sum: Use AF_ALG when available 2018-04-28 sha256sum: Use AF_ALG when available 2018-04-28 sha1sum: Use AF_ALG when available 2018-05-05 all: Replace more http URLs by https URLs 2018-05-03 maint: port more modules to GCC 8 2018-05-03 Simplify code; drop support for Borland C++ on Windows * admin/merge-gnulib (GNULIB_MODULES): Use crypto/md5-buffer rather than crypto/md5, since Emacs doesn’t use the stream operations that in recent Gnulib pull in other stuff Emacs doesn’t need. Similarly for crypto/sha1-buffer, crypto/sha256-buffer, crypto/sha512-buffer. * build-aux/config.guess, build-aux/config.sub, lib/dosname.h: * lib/dup2.c, lib/errno.in.h, lib/euidaccess.c, lib/fcntl.c: * lib/fcntl.in.h, lib/fpending.c, lib/fsync.c, lib/getdtablesize.c: * lib/getopt.c, lib/gettimeofday.c, lib/inttypes.in.h, lib/md5.c: * lib/md5.h, lib/open.c, lib/pipe2.c, lib/putenv.c, lib/sha1.c: * lib/sha1.h, lib/sha256.c, lib/sha256.h, lib/sha512.c: * lib/sha512.h, lib/stat-time.h, lib/stdio-impl.h, lib/stdio.in.h: * lib/stdlib.in.h, lib/sys_stat.in.h, lib/sys_types.in.h: * lib/timespec.h, lib/unistd.in.h, lib/utimens.c, m4/c-strtod.m4: * m4/gnulib-common.m4, m4/inttypes.m4, m4/lstat.m4, m4/nocrash.m4: * m4/pselect.m4, m4/readlink.m4, m4/stdio_h.m4, m4/symlink.m4: * m4/unistd_h.m4, m4/utimens.m4: Copy from Gnulib. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2018-05-21 18:42:18 +00:00
#elif defined _WIN32 && ! defined __CYGWIN__
/* Native Windows platforms (but not Cygwin) put the "file creation
time" in st_ctime (!). See
<https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions>. */
# if _GL_WINDOWS_STAT_TIMESPEC
t = st->st_ctim;
# else
t = (struct timespec) { .tv_sec = st->st_ctime };
# endif
#else
/* Birth time is not supported. */
t = (struct timespec) { .tv_sec = -1, .tv_nsec = -1 };
#endif
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
/* FreeBSD and NetBSD sometimes signal the absence of knowledge by
using zero. Attempt to work around this problem. Alas, this can
report failure even for valid timestamps. Also, NetBSD
sometimes returns junk in the birth time fields; work around this
bug if it is detected. */
if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
t = (struct timespec) { .tv_sec = -1, .tv_nsec = -1 };
#endif
return t;
}
/* If a stat-like function returned RESULT, normalize the timestamps
in *ST, if this platform suffers from a macOS and Solaris bug where
tv_nsec might be negative. Return the adjusted RESULT, setting
errno to EOVERFLOW if normalization overflowed. This function
is intended to be private to this .h file. */
_GL_STAT_TIME_INLINE int
stat_time_normalize (int result, _GL_UNUSED struct stat *st)
{
#if (((defined __APPLE__ && defined __MACH__) || defined __sun) \
&& defined STAT_TIMESPEC_OFFSETOF)
if (result == 0)
{
long int timespec_hz = 1000000000;
short int const ts_off[] = { STAT_TIMESPEC_OFFSETOF (st_atim),
STAT_TIMESPEC_OFFSETOF (st_mtim),
STAT_TIMESPEC_OFFSETOF (st_ctim) };
int i;
for (i = 0; i < sizeof ts_off / sizeof *ts_off; i++)
{
struct timespec *ts = (struct timespec *) ((char *) st + ts_off[i]);
long int q = ts->tv_nsec / timespec_hz;
long int r = ts->tv_nsec % timespec_hz;
if (r < 0)
{
r += timespec_hz;
q--;
}
ts->tv_nsec = r;
/* Overflow is possible, as Solaris 11 stat can yield
tv_sec == TYPE_MINIMUM (time_t) && tv_nsec == -1000000000. */
if (ckd_add (&ts->tv_sec, q, ts->tv_sec))
{
errno = EOVERFLOW;
return -1;
}
}
}
#endif
return result;
}
#ifdef __cplusplus
}
#endif
_GL_INLINE_HEADER_END
#endif