Back to index

glibc  2.9
tst-futimesat.c
Go to the documentation of this file.
00001 #include <dirent.h>
00002 #include <fcntl.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 #include <sys/time.h>
00008 
00009 
00010 static void prepare (void);
00011 #define PREPARE(argc, argv) prepare ()
00012 
00013 static int do_test (void);
00014 #define TEST_FUNCTION do_test ()
00015 
00016 #include "../test-skeleton.c"
00017 
00018 static int dir_fd;
00019 
00020 static void
00021 prepare (void)
00022 {
00023   size_t test_dir_len = strlen (test_dir);
00024   static const char dir_name[] = "/tst-futimesat.XXXXXX";
00025 
00026   size_t dirbuflen = test_dir_len + sizeof (dir_name);
00027   char *dirbuf = malloc (dirbuflen);
00028   if (dirbuf == NULL)
00029     {
00030       puts ("out of memory");
00031       exit (1);
00032     }
00033 
00034   snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
00035   if (mkdtemp (dirbuf) == NULL)
00036     {
00037       puts ("cannot create temporary directory");
00038       exit (1);
00039     }
00040 
00041   add_temp_file (dirbuf);
00042 
00043   dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
00044   if (dir_fd == -1)
00045     {
00046       puts ("cannot open directory");
00047       exit (1);
00048     }
00049 }
00050 
00051 
00052 static int
00053 do_test (void)
00054 {
00055   /* fdopendir takes over the descriptor, make a copy.  */
00056   int dupfd = dup (dir_fd);
00057   if (dupfd == -1)
00058     {
00059       puts ("dup failed");
00060       return 1;
00061     }
00062   if (lseek (dupfd, 0, SEEK_SET) != 0)
00063     {
00064       puts ("1st lseek failed");
00065       return 1;
00066     }
00067 
00068   /* The directory should be empty safe the . and .. files.  */
00069   DIR *dir = fdopendir (dupfd);
00070   if (dir == NULL)
00071     {
00072       puts ("fdopendir failed");
00073       return 1;
00074     }
00075   struct dirent64 *d;
00076   while ((d = readdir64 (dir)) != NULL)
00077     if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
00078       {
00079        printf ("temp directory contains file \"%s\"\n", d->d_name);
00080        return 1;
00081       }
00082   closedir (dir);
00083 
00084   /* Try to create a file.  */
00085   int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
00086   if (fd == -1)
00087     {
00088       if (errno == ENOSYS)
00089        {
00090          puts ("*at functions not supported");
00091          return 0;
00092        }
00093 
00094       puts ("file creation failed");
00095       return 1;
00096     }
00097   write (fd, "hello", 5);
00098   puts ("file created");
00099 
00100   struct stat64 st1;
00101   if (fstat64 (fd, &st1) != 0)
00102     {
00103       puts ("fstat64 failed");
00104       return 1;
00105     }
00106 
00107   close (fd);
00108 
00109   struct timeval tv[2];
00110   tv[0].tv_sec = st1.st_atime + 1;
00111   tv[0].tv_usec = 0;
00112   tv[1].tv_sec = st1.st_mtime + 1;
00113   tv[1].tv_usec = 0;
00114   if (futimesat (dir_fd, "some-file", tv) != 0)
00115     {
00116       puts ("futimesat failed");
00117       return 1;
00118     }
00119 
00120   struct stat64 st2;
00121   if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
00122     {
00123       puts ("fstatat64 failed");
00124       return 1;
00125     }
00126 
00127   if (st2.st_mtime != tv[1].tv_sec
00128 #ifdef _STATBUF_ST_NSEC
00129       || st2.st_mtim.tv_nsec != 0
00130 #endif
00131       )
00132     {
00133       puts ("stat shows different mtime");
00134       return 1;
00135     }
00136 
00137 
00138   if (unlinkat (dir_fd, "some-file", 0) != 0)
00139     {
00140       puts ("unlinkat failed");
00141       return 1;
00142     }
00143 
00144   close (dir_fd);
00145 
00146   return 0;
00147 }