(root)/
coreutils-9.4/
lib/
utimecmp.c
       1  /* utimecmp.c -- compare file timestamps
       2  
       3     Copyright (C) 2004-2007, 2009-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation, either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by Paul Eggert.  */
      19  
      20  #include <config.h>
      21  
      22  #include "utimecmp.h"
      23  
      24  #include <fcntl.h>
      25  #include <limits.h>
      26  #include <stdint.h>
      27  #include <stdlib.h>
      28  #include <sys/stat.h>
      29  #include <time.h>
      30  #include <unistd.h>
      31  
      32  #include "dirname.h"
      33  #include "hash.h"
      34  #include "intprops.h"
      35  #include "stat-time.h"
      36  
      37  #ifndef MAX
      38  # define MAX(a, b) ((a) > (b) ? (a) : (b))
      39  #endif
      40  
      41  #define BILLION (1000 * 1000 * 1000)
      42  
      43  /* Best possible resolution that utimens can set and stat can return,
      44     due to system-call limitations.  It must be a power of 10 that is
      45     no greater than 1 billion.  */
      46  #if HAVE_UTIMENSAT
      47  enum { SYSCALL_RESOLUTION = 1 };
      48  #elif defined _WIN32 && ! defined __CYGWIN__
      49  /* On native Windows, file times have 100 ns resolution. See
      50     <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime>  */
      51  enum { SYSCALL_RESOLUTION = 100 };
      52  #elif ((HAVE_FUTIMESAT || HAVE_WORKING_UTIMES)                  \
      53         && (defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC             \
      54             || defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC     \
      55             || defined HAVE_STRUCT_STAT_ST_ATIMENSEC             \
      56             || defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC))
      57  enum { SYSCALL_RESOLUTION = 1000 };
      58  #else
      59  enum { SYSCALL_RESOLUTION = BILLION };
      60  #endif
      61  
      62  /* Describe a file system and its timestamp resolution in nanoseconds.  */
      63  struct fs_res
      64  {
      65    /* Device number of file system.  */
      66    dev_t dev;
      67  
      68    /* An upper bound on the timestamp resolution of this file system,
      69       ignoring any resolution that cannot be set via utimens.  It is
      70       represented by an integer count of nanoseconds.  It must be
      71       either 2 billion, or a power of 10 that is no greater than a
      72       billion and is no less than SYSCALL_RESOLUTION.  */
      73    int resolution;
      74  
      75    /* True if RESOLUTION is known to be exact, and is not merely an
      76       upper bound on the true resolution.  */
      77    bool exact;
      78  };
      79  
      80  /* Hash some device info.  */
      81  static size_t
      82  dev_info_hash (void const *x, size_t table_size)
      83  {
      84    struct fs_res const *p = x;
      85  
      86    /* Beware signed arithmetic gotchas.  */
      87    if (TYPE_SIGNED (dev_t) && SIZE_MAX < MAX (INT_MAX, TYPE_MAXIMUM (dev_t)))
      88      {
      89        uintmax_t dev = p->dev;
      90        return dev % table_size;
      91      }
      92  
      93    return p->dev % table_size;
      94  }
      95  
      96  /* Compare two dev_info structs.  */
      97  static bool
      98  dev_info_compare (void const *x, void const *y)
      99  {
     100    struct fs_res const *a = x;
     101    struct fs_res const *b = y;
     102    return a->dev == b->dev;
     103  }
     104  
     105  /* Return -1, 0, 1 based on whether the destination file (relative
     106     to openat-like directory file descriptor DFD with name
     107     DST_NAME and status DST_STAT) is older than SRC_STAT, the same age
     108     as SRC_STAT, or newer than SRC_STAT, respectively.
     109  
     110     DST_NAME may be NULL if OPTIONS is 0.
     111  
     112     If OPTIONS & UTIMECMP_TRUNCATE_SOURCE, do the comparison after SRC is
     113     converted to the destination's timestamp resolution as filtered through
     114     utimens.  In this case, return -2 if the exact answer cannot be
     115     determined; this can happen only if the timestamps are very close and
     116     there is some trouble accessing the file system (e.g., the user does not
     117     have permission to futz with the destination's timestamps).  */
     118  
     119  int
     120  utimecmp (char const *dst_name,
     121            struct stat const *dst_stat,
     122            struct stat const *src_stat,
     123            int options)
     124  {
     125    return utimecmpat (AT_FDCWD, dst_name, dst_stat, src_stat, options);
     126  }
     127  
     128  int
     129  utimecmpat (int dfd, char const *dst_name,
     130              struct stat const *dst_stat,
     131              struct stat const *src_stat,
     132              int options)
     133  {
     134    /* Things to watch out for:
     135  
     136       The code uses a static hash table internally and is not safe in the
     137       presence of signals, multiple threads, etc.  However, memory pressure
     138       that prevents use of the hash table is not fatal - we just fall back
     139       to redoing the computations on every call in that case.
     140  
     141       int and long int might be 32 bits.  Many of the calculations store
     142       numbers up to 2 billion, and multiply by 10; they have to avoid
     143       multiplying 2 billion by 10, as this exceeds 32-bit capabilities.
     144  
     145       time_t might be unsigned.  */
     146  
     147    static_assert (TYPE_IS_INTEGER (time_t));
     148  
     149    /* Destination and source timestamps.  */
     150    time_t dst_s = dst_stat->st_mtime;
     151    time_t src_s = src_stat->st_mtime;
     152    int dst_ns = get_stat_mtime_ns (dst_stat);
     153    int src_ns = get_stat_mtime_ns (src_stat);
     154  
     155    if (options & UTIMECMP_TRUNCATE_SOURCE)
     156      {
     157  #if defined _AIX
     158        /* On AIX 7.2, on a jfs2 file system, the times may differ by up to
     159           0.01 seconds in either direction.  But it does not seem to come
     160           from clock ticks of 0.01 seconds each.  */
     161        long long difference =
     162          ((long long) dst_s - (long long) src_s) * BILLION
     163          + ((long long) dst_ns - (long long) src_ns);
     164        if (difference < 10000000 && difference > -10000000)
     165          return 0;
     166  #endif
     167  
     168        /* Look up the timestamp resolution for the destination device.  */
     169  
     170        /* Hash table for caching information learned about devices.  */
     171        static Hash_table *ht;
     172  
     173        /* Information about the destination file system.  */
     174        static struct fs_res *new_dst_res;
     175        struct fs_res *dst_res = NULL;
     176        struct fs_res tmp_dst_res;
     177  
     178        /* timestamp resolution in nanoseconds.  */
     179        int res;
     180  
     181        /* Quick exit, if possible.  Since the worst resolution is 2
     182           seconds, anything that differs by more than that does not
     183           needs source truncation.  */
     184        if (dst_s == src_s && dst_ns == src_ns)
     185          return 0;
     186        if (dst_s <= src_s - 2)
     187          return -1;
     188        if (src_s <= dst_s - 2)
     189          return 1;
     190  
     191        /* Try to do a hash lookup, but fall back to stack variables and
     192           recomputation on low memory situations.  */
     193        if (! ht)
     194          ht = hash_initialize (16, NULL, dev_info_hash, dev_info_compare, free);
     195        if (ht)
     196          {
     197            if (! new_dst_res)
     198              {
     199                new_dst_res = malloc (sizeof *new_dst_res);
     200                if (!new_dst_res)
     201                  goto low_memory;
     202                new_dst_res->resolution = 2 * BILLION;
     203                new_dst_res->exact = false;
     204              }
     205            new_dst_res->dev = dst_stat->st_dev;
     206            dst_res = hash_insert (ht, new_dst_res);
     207            if (! dst_res)
     208              goto low_memory;
     209  
     210            if (dst_res == new_dst_res)
     211              {
     212                /* NEW_DST_RES is now in use in the hash table, so allocate a
     213                   new entry next time.  */
     214                new_dst_res = NULL;
     215              }
     216          }
     217        else
     218          {
     219          low_memory:
     220            if (ht)
     221              {
     222                tmp_dst_res.dev = dst_stat->st_dev;
     223                dst_res = hash_lookup (ht, &tmp_dst_res);
     224              }
     225            if (!dst_res)
     226              {
     227                dst_res = &tmp_dst_res;
     228                dst_res->resolution = 2 * BILLION;
     229                dst_res->exact = false;
     230              }
     231          }
     232  
     233        res = dst_res->resolution;
     234  
     235  #ifdef _PC_TIMESTAMP_RESOLUTION
     236        /* If the system will tell us the resolution, we're set!  */
     237        if (! dst_res->exact)
     238          {
     239            res = -1;
     240            if (dfd == AT_FDCWD)
     241              res = pathconf (dst_name, _PC_TIMESTAMP_RESOLUTION);
     242            else
     243              {
     244                char *dstdir = mdir_name (dst_name);
     245                if (dstdir)
     246                  {
     247                    int destdirfd = openat (dfd, dstdir,
     248                                            O_SEARCH | O_CLOEXEC | O_DIRECTORY);
     249                    if (0 <= destdirfd)
     250                      {
     251                        res = fpathconf (destdirfd, _PC_TIMESTAMP_RESOLUTION);
     252                        close (destdirfd);
     253                      }
     254                    free (dstdir);
     255                  }
     256              }
     257            if (0 < res)
     258              {
     259                dst_res->resolution = res;
     260                dst_res->exact = true;
     261              }
     262          }
     263  #endif
     264  
     265        if (! dst_res->exact)
     266          {
     267            /* This file system's resolution is not known exactly.
     268               Deduce it, and store the result in the hash table.  */
     269  
     270            time_t dst_a_s = dst_stat->st_atime;
     271            time_t dst_c_s = dst_stat->st_ctime;
     272            time_t dst_m_s = dst_s;
     273            int dst_a_ns = get_stat_atime_ns (dst_stat);
     274            int dst_c_ns = get_stat_ctime_ns (dst_stat);
     275            int dst_m_ns = dst_ns;
     276  
     277            /* Set RES to an upper bound on the file system resolution
     278               (after truncation due to SYSCALL_RESOLUTION) by inspecting
     279               the atime, ctime and mtime of the existing destination.
     280               We don't know of any file system that stores atime or
     281               ctime with a higher precision than mtime, so it's valid to
     282               look at them too.  */
     283            {
     284              bool odd_second = (dst_a_s | dst_c_s | dst_m_s) & 1;
     285  
     286              if (SYSCALL_RESOLUTION == BILLION)
     287                {
     288                  if (odd_second | dst_a_ns | dst_c_ns | dst_m_ns)
     289                    res = BILLION;
     290                }
     291              else
     292                {
     293                  int a = dst_a_ns;
     294                  int c = dst_c_ns;
     295                  int m = dst_m_ns;
     296  
     297                  /* Write it this way to avoid mistaken GCC warning
     298                     about integer overflow in constant expression.  */
     299                  int SR10 = SYSCALL_RESOLUTION;  SR10 *= 10;
     300  
     301                  if ((a % SR10 | c % SR10 | m % SR10) != 0)
     302                    res = SYSCALL_RESOLUTION;
     303                  else
     304                    for (res = SR10, a /= SR10, c /= SR10, m /= SR10;
     305                         (res < dst_res->resolution
     306                          && (a % 10 | c % 10 | m % 10) == 0);
     307                         res *= 10, a /= 10, c /= 10, m /= 10)
     308                      if (res == BILLION)
     309                        {
     310                          if (! odd_second)
     311                            res *= 2;
     312                          break;
     313                        }
     314                }
     315  
     316              dst_res->resolution = res;
     317            }
     318  
     319            if (SYSCALL_RESOLUTION < res)
     320              {
     321                struct stat dst_status;
     322  
     323                /* Ignore source timestamp information that must necessarily
     324                   be lost when filtered through utimens.  */
     325                src_ns -= src_ns % SYSCALL_RESOLUTION;
     326  
     327                /* If the timestamps disagree widely enough, there's no need
     328                   to interrogate the file system to deduce the exact
     329                   timestamp resolution; return the answer directly.  */
     330                {
     331                  time_t s = src_s & ~ (res == 2 * BILLION ? 1 : 0);
     332                  if (src_s < dst_s || (src_s == dst_s && src_ns <= dst_ns))
     333                    return 1;
     334                  if (dst_s < s
     335                      || (dst_s == s && dst_ns < src_ns - src_ns % res))
     336                    return -1;
     337                }
     338  
     339                /* Determine the actual timestamp resolution for the
     340                   destination file system (after truncation due to
     341                   SYSCALL_RESOLUTION) by setting the access timestamp of the
     342                   destination to the existing access time, except with
     343                   trailing nonzero digits.  */
     344  
     345                struct timespec timespec[2] = {
     346                  [0].tv_sec = dst_a_s,
     347                  [0].tv_nsec = dst_a_ns,
     348                  [1].tv_sec = dst_m_s | (res == 2 * BILLION),
     349                  [1].tv_nsec = dst_m_ns + res / 9
     350                };
     351  
     352                if (utimensat (dfd, dst_name, timespec, AT_SYMLINK_NOFOLLOW))
     353                  return -2;
     354  
     355                /* Read the modification time that was set.  */
     356                {
     357                  int stat_result
     358                    = fstatat (dfd, dst_name, &dst_status, AT_SYMLINK_NOFOLLOW);
     359  
     360                  if (stat_result
     361                      | (dst_status.st_mtime ^ dst_m_s)
     362                      | (get_stat_mtime_ns (&dst_status) ^ dst_m_ns))
     363                    {
     364                      /* The modification time changed, or we can't tell whether
     365                         it changed.  Change it back as best we can.  */
     366                      timespec[1].tv_sec = dst_m_s;
     367                      timespec[1].tv_nsec = dst_m_ns;
     368                      utimensat (dfd, dst_name, timespec, AT_SYMLINK_NOFOLLOW);
     369                    }
     370  
     371                  if (stat_result != 0)
     372                    return -2;
     373                }
     374  
     375                /* Determine the exact resolution from the modification time
     376                   that was read back.  */
     377                {
     378                  int old_res = res;
     379                  int a = (BILLION * (dst_status.st_mtime & 1)
     380                           + get_stat_mtime_ns (&dst_status));
     381  
     382                  res = SYSCALL_RESOLUTION;
     383  
     384                  for (a /= res; a % 10 == 0; a /= 10)
     385                    {
     386                      if (res == BILLION)
     387                        {
     388                          res *= 2;
     389                          break;
     390                        }
     391                      res *= 10;
     392                      if (res == old_res)
     393                        break;
     394                    }
     395                }
     396              }
     397  
     398            dst_res->resolution = res;
     399            dst_res->exact = true;
     400          }
     401  
     402        /* Truncate the source's timestamp according to the resolution.  */
     403        src_s &= ~ (res == 2 * BILLION ? 1 : 0);
     404        src_ns -= src_ns % res;
     405      }
     406  
     407    /* Compare the timestamps and return -1, 0, 1 accordingly.  */
     408    return (_GL_CMP (dst_s, src_s)
     409            + ((dst_s == src_s ? ~0 : 0) & _GL_CMP (dst_ns, src_ns)));
     410  }