Blame io/test-utime.c

Packit 6c4009
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <utime.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  char file[] = "/tmp/test-utime.XXXXXX";
Packit 6c4009
  struct utimbuf ut;
Packit 6c4009
  struct stat st;
Packit 6c4009
  struct stat stnow;
Packit 6c4009
  time_t now1, now2;
Packit 6c4009
  int fd;
Packit 6c4009
Packit 6c4009
  fd = mkstemp (file);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("mkstemp");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  /* Test utime with arg */
Packit 6c4009
  ut.actime = 500000000;
Packit 6c4009
  ut.modtime = 500000001;
Packit 6c4009
  if (utime (file, &ut))
Packit 6c4009
    {
Packit 6c4009
      perror ("utime");
Packit 6c4009
      remove (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (stat (file, &st))
Packit 6c4009
    {
Packit 6c4009
      perror ("stat");
Packit 6c4009
      remove (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test utime with NULL.
Packit 6c4009
     Since there's a race condition possible here, we check
Packit 6c4009
     the time before and after the call to utime.  */
Packit 6c4009
  now1 = time (NULL);
Packit 6c4009
  if (now1 == (time_t)-1)
Packit 6c4009
    {
Packit 6c4009
      perror ("time");
Packit 6c4009
      remove (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The clocks used to set the modification time and that used in the
Packit 6c4009
     time() call need not be the same.  They need not have the same
Packit 6c4009
     precision.  Therefore we delay the following operation by one
Packit 6c4009
     second which makes sure we can compare with second precision.  */
Packit 6c4009
  sleep (1);
Packit 6c4009
Packit 6c4009
  if (utime (file, NULL))
Packit 6c4009
    {
Packit 6c4009
      perror ("utime NULL");
Packit 6c4009
      remove (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  sleep (1);
Packit 6c4009
Packit 6c4009
  now2 = time (NULL);
Packit 6c4009
  if (now2 == (time_t)-1)
Packit 6c4009
    {
Packit 6c4009
      perror ("time");
Packit 6c4009
      remove (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (stat (file, &stnow))
Packit 6c4009
    {
Packit 6c4009
      perror ("stat");
Packit 6c4009
      remove (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  remove (file);
Packit 6c4009
Packit 6c4009
  if (st.st_mtime != ut.modtime)
Packit 6c4009
    {
Packit 6c4009
      printf ("modtime %jd != %jd\n",
Packit 6c4009
	      (intmax_t) st.st_mtime, (intmax_t) ut.modtime);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (st.st_atime != ut.actime)
Packit 6c4009
    {
Packit 6c4009
      printf ("actime %jd != %jd\n",
Packit 6c4009
	      (intmax_t) st.st_atime, (intmax_t) ut.actime);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (stnow.st_mtime < now1 || stnow.st_mtime > now2)
Packit 6c4009
    {
Packit 6c4009
      printf ("modtime %jd <%jd >%jd\n",
Packit 6c4009
	      (intmax_t) stnow.st_mtime, (intmax_t) now1, (intmax_t) now2);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (stnow.st_atime < now1 || stnow.st_atime > now2)
Packit 6c4009
    {
Packit 6c4009
      printf ("actime %jd <%jd >%jd\n",
Packit 6c4009
	      (intmax_t) stnow.st_atime, (intmax_t) now1, (intmax_t) now2);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("Test succeeded.");
Packit 6c4009
  return 0;
Packit 6c4009
}