示例#1
0
int
main (void)
{
  int result1; /* Skip because of no symlink support.  */
  int result2; /* Skip because of no futimens support.  */
  int result3; /* Skip because of no lutimens support.  */
  int fd;

  /* Clean up any trash from prior testsuite runs.  */
  ignore_value (system ("rm -rf " BASE "*"));

  /* Basic tests.  */
  result1 = test_utimens (do_utimens, true);
  ASSERT (test_utimens (do_fdutimens, false) == result1);
  result2 = test_futimens (do_futimens, result1 == 0);
  result3 = test_lutimens (do_lutimens, (result1 + result2) == 0);
  /* We expect 0/0, 0/77, or 77/77, but not 77/0.  */
  ASSERT (result1 <= result3);
  ASSERT (test_lutimens (do_lutimens1, (result1 + result2) == 0) == result3);
  dfd = open (".", O_RDONLY);
  ASSERT (0 <= dfd);
  ASSERT (test_utimens (do_utimens, false) == result1);
  ASSERT (test_utimens (do_fdutimens, false) == result1);
  ASSERT (test_futimens (do_futimens, false) == result2);
  ASSERT (test_lutimens (do_lutimens, false) == result3);
  ASSERT (test_lutimens (do_lutimens1, false) == result3);

  /* Directory relative tests.  */
  ASSERT (mkdir (BASE "dir", 0700) == 0);
  ASSERT (chdir (BASE "dir") == 0);
  fd = creat ("file", 0600);
  ASSERT (0 <= fd);
  errno = 0;
  ASSERT (fdutimensat (AT_FDCWD, fd, ".", NULL, 0) == -1);
  ASSERT (errno == ENOTDIR);
  {
    struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
    struct stat st;
    ASSERT (fdutimensat (fd, dfd, BASE "dir/file", ts, 0) == 0);
    ASSERT (stat ("file", &st) == 0);
    ASSERT (st.st_atime == Y2K);
    ASSERT (get_stat_atime_ns (&st) == 0);
    ASSERT (st.st_mtime == Y2K);
    ASSERT (get_stat_mtime_ns (&st) == 0);
  }
  ASSERT (close (fd) == 0);
  ASSERT (close (dfd) == 0);
  errno = 0;
  ASSERT (fdutimensat (-1, dfd, ".", NULL, 0) == -1);
  ASSERT (errno == EBADF);

  /* Cleanup.  */
  ASSERT (chdir ("..") == 0);
  ASSERT (unlink (BASE "dir/file") == 0);
  ASSERT (rmdir (BASE "dir") == 0);
  return result1 | result2 | result3;
}
示例#2
0
int
fdutimens (int fd, char const *file, struct timespec const timespec[2])
{
    struct timespec adjusted_timespec[2];
    struct timespec *ts = timespec ? adjusted_timespec : NULL;
    int adjustment_needed = 0;
    struct stat st;

    if (ts)
    {
        adjusted_timespec[0] = timespec[0];
        adjusted_timespec[1] = timespec[1];
        adjustment_needed = validate_timespec (ts);
    }
    if (adjustment_needed < 0)
        return -1;

    /* Require that at least one of FD or FILE are potentially valid, to avoid
       a Linux bug where futimens (AT_FDCWD, NULL) changes "." rather
       than failing.  */
    if (fd < 0 && !file)
    {
        errno = EBADF;
        return -1;
    }

    /* Some Linux-based NFS clients are buggy, and mishandle time stamps
       of files in NFS file systems in some cases.  We have no
       configure-time test for this, but please see
       <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
       some of the problems with Linux 2.6.16.  If this affects you,
       compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
       help in some cases, albeit at a cost in performance.  But you
       really should upgrade your kernel to a fixed version, since the
       problem affects many applications.  */

#if HAVE_BUGGY_NFS_TIME_STAMPS
    if (fd < 0)
        sync ();
    else
        fsync (fd);
#endif

    /* POSIX 2008 added two interfaces to set file timestamps with
       nanosecond resolution; newer Linux implements both functions via
       a single syscall.  We provide a fallback for ENOSYS (for example,
       compiling against Linux 2.6.25 kernel headers and glibc 2.7, but
       running on Linux 2.6.18 kernel).  */
#if HAVE_UTIMENSAT || HAVE_FUTIMENS
    if (0 <= utimensat_works_really)
    {
        int result;
# if __linux__
        /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
           systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
           but work if both times are either explicitly specified or
           UTIME_NOW.  Work around it with a preparatory [f]stat prior
           to calling futimens/utimensat; fortunately, there is not much
           timing impact due to the extra syscall even on file systems
           where UTIME_OMIT would have worked.  FIXME: Simplify this in
           2012, when file system bugs are no longer common.  */
        if (adjustment_needed == 2)
        {
            if (fd < 0 ? stat (file, &st) : fstat (fd, &st))
                return -1;
            if (ts[0].tv_nsec == UTIME_OMIT)
                ts[0] = get_stat_atime (&st);
            else if (ts[1].tv_nsec == UTIME_OMIT)
                ts[1] = get_stat_mtime (&st);
            /* Note that st is good, in case utimensat gives ENOSYS.  */
            adjustment_needed++;
        }
# endif /* __linux__ */
# if HAVE_UTIMENSAT
        if (fd < 0)
        {
            result = utimensat (AT_FDCWD, file, ts, 0);
#  ifdef __linux__
            /* Work around a kernel bug:
               http://bugzilla.redhat.com/442352
               http://bugzilla.redhat.com/449910
               It appears that utimensat can mistakenly return 280 rather
               than -1 upon ENOSYS failure.
               FIXME: remove in 2010 or whenever the offending kernels
               are no longer in common use.  */
            if (0 < result)
                errno = ENOSYS;
#  endif /* __linux__ */
            if (result == 0 || errno != ENOSYS)
            {
                utimensat_works_really = 1;
                return result;
            }
        }
# endif /* HAVE_UTIMENSAT */
# if HAVE_FUTIMENS
        if (0 <= fd)
        {
            result = futimens (fd, ts);
#  ifdef __linux__
            /* Work around the same bug as above.  */
            if (0 < result)
                errno = ENOSYS;
#  endif /* __linux__ */
            if (result == 0 || errno != ENOSYS)
            {
                utimensat_works_really = 1;
                return result;
            }
        }
# endif /* HAVE_FUTIMENS */
    }
    utimensat_works_really = -1;
    lutimensat_works_really = -1;
#endif /* HAVE_UTIMENSAT || HAVE_FUTIMENS */

    /* The platform lacks an interface to set file timestamps with
       nanosecond resolution, so do the best we can, discarding any
       fractional part of the timestamp.  */

    if (adjustment_needed || (REPLACE_FUNC_STAT_FILE && fd < 0))
    {
        if (adjustment_needed != 3
                && (fd < 0 ? stat (file, &st) : fstat (fd, &st)))
            return -1;
        if (ts && update_timespec (&st, &ts))
            return 0;
    }

    {
#if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
        struct timeval timeval[2];
        struct timeval *t;
        if (ts)
        {
            timeval[0].tv_sec = ts[0].tv_sec;
            timeval[0].tv_usec = ts[0].tv_nsec / 1000;
            timeval[1].tv_sec = ts[1].tv_sec;
            timeval[1].tv_usec = ts[1].tv_nsec / 1000;
            t = timeval;
        }
        else
            t = NULL;

        if (fd < 0)
        {
# if HAVE_FUTIMESAT
            return futimesat (AT_FDCWD, file, t);
# endif
        }
        else
        {
            /* If futimesat or futimes fails here, don't try to speed things
               up by returning right away.  glibc can incorrectly fail with
               errno == ENOENT if /proc isn't mounted.  Also, Mandrake 10.0
               in high security mode doesn't allow ordinary users to read
               /proc/self, so glibc incorrectly fails with errno == EACCES.
               If errno == EIO, EPERM, or EROFS, it's probably safe to fail
               right away, but these cases are rare enough that they're not
               worth optimizing, and who knows what other messed-up systems
               are out there?  So play it safe and fall back on the code
               below.  */

# if (HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG) || HAVE_FUTIMES
#  if HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG
#   undef futimes
#   define futimes(fd, t) futimesat (fd, NULL, t)
#  endif
            if (futimes (fd, t) == 0)
            {
#  if __linux__ && __GLIBC__
                /* Work around a longstanding glibc bug, still present as
                   of 2010-12-27.  On older Linux kernels that lack both
                   utimensat and utimes, glibc's futimes rounds instead of
                   truncating when falling back on utime.  The same bug
                   occurs in futimesat with a null 2nd arg.  */
                if (t)
                {
                    bool abig = 500000 <= t[0].tv_usec;
                    bool mbig = 500000 <= t[1].tv_usec;
                    if ((abig | mbig) && fstat (fd, &st) == 0)
                    {
                        /* If these two subtractions overflow, they'll
                           track the overflows inside the buggy glibc.  */
                        time_t adiff = st.st_atime - t[0].tv_sec;
                        time_t mdiff = st.st_mtime - t[1].tv_sec;

                        struct timeval *tt = NULL;
                        struct timeval truncated_timeval[2];
                        truncated_timeval[0] = t[0];
                        truncated_timeval[1] = t[1];
                        if (abig && adiff == 1 && get_stat_atime_ns (&st) == 0)
                        {
                            tt = truncated_timeval;
                            tt[0].tv_usec = 0;
                        }
                        if (mbig && mdiff == 1 && get_stat_mtime_ns (&st) == 0)
                        {
                            tt = truncated_timeval;
                            tt[1].tv_usec = 0;
                        }
                        if (tt)
                            futimes (fd, tt);
                    }
                }
#  endif

                return 0;
            }
# endif
        }
#endif /* HAVE_FUTIMESAT || HAVE_WORKING_UTIMES */

        if (!file)
        {
#if ! ((HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG)          \
        || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
            errno = ENOSYS;
#endif
            return -1;
        }

#if HAVE_WORKING_UTIMES
        return utimes (file, t);
#else
        {
            struct utimbuf utimbuf;
            struct utimbuf *ut;
            if (ts)
            {
                utimbuf.actime = ts[0].tv_sec;
                utimbuf.modtime = ts[1].tv_sec;
                ut = &utimbuf;
            }
            else
                ut = NULL;

            return utime (file, ut);
        }
#endif /* !HAVE_WORKING_UTIMES */
    }
}
int
utimecmp (char const *dst_name,
	  struct stat const *dst_stat,
	  struct stat const *src_stat,
	  int options)
{
  /* Things to watch out for:

     The code uses a static hash table internally and is not safe in the
     presence of signals, multiple threads, etc.

     int and long int might be 32 bits.  Many of the calculations store
     numbers up to 2 billion, and multiply by 10; they have to avoid
     multiplying 2 billion by 10, as this exceeds 32-bit capabilities.

     time_t might be unsigned.  */

  verify (TYPE_IS_INTEGER (time_t));
  verify (TYPE_TWOS_COMPLEMENT (int));

  /* Destination and source time stamps.  */
  time_t dst_s = dst_stat->st_mtime;
  time_t src_s = src_stat->st_mtime;
  int dst_ns = get_stat_mtime_ns (dst_stat);
  int src_ns = get_stat_mtime_ns (src_stat);

  if (options & UTIMECMP_TRUNCATE_SOURCE)
    {
      /* Look up the time stamp resolution for the destination device.  */

      /* Hash table for devices.  */
      static Hash_table *ht;

      /* Information about the destination file system.  */
      static struct fs_res *new_dst_res;
      struct fs_res *dst_res;

      /* Time stamp resolution in nanoseconds.  */
      int res;

      if (! ht)
	ht = hash_initialize (16, NULL, dev_info_hash, dev_info_compare, free);
      if (! new_dst_res)
	{
	  new_dst_res = xmalloc (sizeof *new_dst_res);
	  new_dst_res->resolution = 2 * BILLION;
	  new_dst_res->exact = false;
	}
      new_dst_res->dev = dst_stat->st_dev;
      dst_res = hash_insert (ht, new_dst_res);
      if (! dst_res)
	xalloc_die ();

      if (dst_res == new_dst_res)
	{
	  /* NEW_DST_RES is now in use in the hash table, so allocate a
	     new entry next time.  */
	  new_dst_res = NULL;
	}

      res = dst_res->resolution;

      if (! dst_res->exact)
	{
	  /* This file system's resolution is not known exactly.
	     Deduce it, and store the result in the hash table.  */

	  time_t dst_a_s = dst_stat->st_atime;
	  time_t dst_c_s = dst_stat->st_ctime;
	  time_t dst_m_s = dst_s;
	  int dst_a_ns = get_stat_atime_ns (dst_stat);
	  int dst_c_ns = get_stat_ctime_ns (dst_stat);
	  int dst_m_ns = dst_ns;

	  /* Set RES to an upper bound on the file system resolution
	     (after truncation due to SYSCALL_RESOLUTION) by inspecting
	     the atime, ctime and mtime of the existing destination.
	     We don't know of any file system that stores atime or
	     ctime with a higher precision than mtime, so it's valid to
	     look at them too.  */
	  {
	    bool odd_second = (dst_a_s | dst_c_s | dst_m_s) & 1;

	    if (SYSCALL_RESOLUTION == BILLION)
	      {
		if (odd_second | dst_a_ns | dst_c_ns | dst_m_ns)
		  res = BILLION;
	      }
	    else
	      {
		int a = dst_a_ns;
		int c = dst_c_ns;
		int m = dst_m_ns;

		/* Write it this way to avoid mistaken GCC warning
		   about integer overflow in constant expression.  */
		int SR10 = SYSCALL_RESOLUTION;  SR10 *= 10;

		if ((a % SR10 | c % SR10 | m % SR10) != 0)
		  res = SYSCALL_RESOLUTION;
		else
		  for (res = SR10, a /= SR10, c /= SR10, m /= SR10;
		       (res < dst_res->resolution
			&& (a % 10 | c % 10 | m % 10) == 0);
		       res *= 10, a /= 10, c /= 10, m /= 10)
		    if (res == BILLION)
		      {
			if (! odd_second)
			  res *= 2;
			break;
		      }
	      }

	    dst_res->resolution = res;
	  }

	  if (SYSCALL_RESOLUTION < res)
	    {
	      struct timespec timespec[2];
	      struct stat dst_status;

	      /* Ignore source time stamp information that must necessarily
		 be lost when filtered through utimens.  */
	      src_ns -= src_ns % SYSCALL_RESOLUTION;

	      /* If the time stamps disagree widely enough, there's no need
		 to interrogate the file system to deduce the exact time
		 stamp resolution; return the answer directly.  */
	      {
		time_t s = src_s & ~ (res == 2 * BILLION);
		if (src_s < dst_s || (src_s == dst_s && src_ns <= dst_ns))
		  return 1;
		if (dst_s < s
		    || (dst_s == s && dst_ns < src_ns - src_ns % res))
		  return -1;
	      }

	      /* Determine the actual time stamp resolution for the
		 destination file system (after truncation due to
		 SYSCALL_RESOLUTION) by setting the access time stamp of the
		 destination to the existing access time, except with
		 trailing nonzero digits.  */

	      timespec[0].tv_sec = dst_a_s;
	      timespec[0].tv_nsec = dst_a_ns;
	      timespec[1].tv_sec = dst_m_s | (res == 2 * BILLION);
	      timespec[1].tv_nsec = dst_m_ns + res / 9;

	      /* Set the modification time.  But don't try to set the
		 modification time of symbolic links; on many hosts this sets
		 the time of the pointed-to file.  */
	      if (S_ISLNK (dst_stat->st_mode)
		  || utimens (dst_name, timespec) != 0)
		return -2;

	      /* Read the modification time that was set.  It's safe to call
		 'stat' here instead of worrying about 'lstat'; either the
		 caller used 'stat', or the caller used 'lstat' and found
		 something other than a symbolic link.  */
	      {
		int stat_result = stat (dst_name, &dst_status);

		if (stat_result
		    | (dst_status.st_mtime ^ dst_m_s)
		    | (get_stat_mtime_ns (&dst_status) ^ dst_m_ns))
		  {
		    /* The modification time changed, or we can't tell whether
		       it changed.  Change it back as best we can.  */
		    timespec[1].tv_sec = dst_m_s;
		    timespec[1].tv_nsec = dst_m_ns;
		    utimens (dst_name, timespec);
		  }

		if (stat_result != 0)
		  return -2;
	      }

	      /* Determine the exact resolution from the modification time
		 that was read back.  */
	      {
		int old_res = res;
		int a = (BILLION * (dst_status.st_mtime & 1)
			 + get_stat_mtime_ns (&dst_status));

		res = SYSCALL_RESOLUTION;

		for (a /= res; a % 10 != 0; a /= 10)
		  {
		    if (res == BILLION)
		      {
			res *= 2;
			break;
		      }
		    res *= 10;
		    if (res == old_res)
		      break;
		  }
	      }
	    }

	  dst_res->resolution = res;
	  dst_res->exact = true;
	}

      /* Truncate the source's time stamp according to the resolution.  */
      src_s &= ~ (res == 2 * BILLION);
      src_ns -= src_ns % res;
    }

  /* Compare the time stamps and return -1, 0, 1 accordingly.  */
  return (dst_s < src_s ? -1
	  : dst_s > src_s ? 1
	  : dst_ns < src_ns ? -1
	  : dst_ns > src_ns);
}
示例#4
0
int
main (void)
{
  int result1; /* Skip because of no symlink support.  */
  int result2; /* Skip because of no lutimens support.  */
  int fd;

  /* Clean up any trash from prior testsuite runs.  */
  ignore_value (system ("rm -rf " BASE "*"));

  /* Test behaviour for invalid file descriptors.  */
  {
    errno = 0;
    ASSERT (utimensat (-1, "foo", NULL, 0) == -1);
    ASSERT (errno == EBADF);
  }
  {
    close (99);
    errno = 0;
    ASSERT (utimensat (99, "foo", NULL, 0) == -1);
    ASSERT (errno == EBADF);
  }

  /* Basic tests.  */
  result1 = test_utimens (do_utimensat, true);
  result2 = test_lutimens (do_lutimensat, result1 == 0);
  dfd = open (".", O_RDONLY);
  ASSERT (0 <= dfd);
  ASSERT (test_utimens (do_utimensat, false) == result1);
  ASSERT (test_lutimens (do_lutimensat, false) == result2);
  /* We expect 0/0, 0/77, or 77/77, but not 77/0.  */
  ASSERT (result1 <= result2);

  /* Directory-relative tests.  */
  ASSERT (mkdir (BASE "dir", 0700) == 0);
  ASSERT (chdir (BASE "dir") == 0);
  fd = creat ("file", 0600);
  ASSERT (0 <= fd);
  errno = 0;
  ASSERT (utimensat (fd, ".", NULL, 0) == -1);
  ASSERT (errno == ENOTDIR);
  {
    struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
    struct stat st;
    ASSERT (utimensat (dfd, BASE "dir/file", ts, AT_SYMLINK_NOFOLLOW) == 0);
    ASSERT (stat ("file", &st) == 0);
    ASSERT (st.st_atime == Y2K);
    ASSERT (get_stat_atime_ns (&st) == 0);
    ASSERT (st.st_mtime == Y2K);
    ASSERT (get_stat_mtime_ns (&st) == 0);
  }
  ASSERT (close (fd) == 0);
  ASSERT (close (dfd) == 0);
  errno = 0;
  ASSERT (utimensat (dfd, ".", NULL, 0) == -1);
  ASSERT (errno == EBADF);

  /* Cleanup.  */
  ASSERT (chdir ("..") == 0);
  ASSERT (unlink (BASE "dir/file") == 0);
  ASSERT (rmdir (BASE "dir") == 0);
  return result1 | result2;
}