Back to index

glibc  2.9
tst-renameat.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 
00008 
00009 static void prepare (void);
00010 #define PREPARE(argc, argv) prepare ()
00011 
00012 static int do_test (void);
00013 #define TEST_FUNCTION do_test ()
00014 
00015 #include "../test-skeleton.c"
00016 
00017 static int dir_fd;
00018 
00019 static void
00020 prepare (void)
00021 {
00022   size_t test_dir_len = strlen (test_dir);
00023   static const char dir_name[] = "/tst-renameat.XXXXXX";
00024 
00025   size_t dirbuflen = test_dir_len + sizeof (dir_name);
00026   char *dirbuf = malloc (dirbuflen);
00027   if (dirbuf == NULL)
00028     {
00029       puts ("out of memory");
00030       exit (1);
00031     }
00032 
00033   snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
00034   if (mkdtemp (dirbuf) == NULL)
00035     {
00036       puts ("cannot create temporary directory");
00037       exit (1);
00038     }
00039 
00040   add_temp_file (dirbuf);
00041 
00042   dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
00043   if (dir_fd == -1)
00044     {
00045       puts ("cannot open directory");
00046       exit (1);
00047     }
00048 }
00049 
00050 
00051 static int
00052 do_test (void)
00053 {
00054   /* fdopendir takes over the descriptor, make a copy.  */
00055   int dupfd = dup (dir_fd);
00056   if (dupfd == -1)
00057     {
00058       puts ("dup failed");
00059       return 1;
00060     }
00061   if (lseek (dupfd, 0, SEEK_SET) != 0)
00062     {
00063       puts ("1st lseek failed");
00064       return 1;
00065     }
00066 
00067   /* The directory should be empty safe the . and .. files.  */
00068   DIR *dir = fdopendir (dupfd);
00069   if (dir == NULL)
00070     {
00071       puts ("fdopendir failed");
00072       return 1;
00073     }
00074   struct dirent64 *d;
00075   while ((d = readdir64 (dir)) != NULL)
00076     if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
00077       {
00078        printf ("temp directory contains file \"%s\"\n", d->d_name);
00079        return 1;
00080       }
00081   closedir (dir);
00082 
00083   /* Try to create a file.  */
00084   int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
00085   if (fd == -1)
00086     {
00087       if (errno == ENOSYS)
00088        {
00089          puts ("*at functions not supported");
00090          return 0;
00091        }
00092 
00093       puts ("file creation failed");
00094       return 1;
00095     }
00096   write (fd, "hello", 5);
00097   puts ("file created");
00098 
00099   struct stat64 st1;
00100   if (fstat64 (fd, &st1) != 0)
00101     {
00102       puts ("fstat64 failed");
00103       return 1;
00104     }
00105 
00106   /* Using a descriptor for a normal file must fail.  */
00107   if (renameat (fd, "some-file", dir_fd, "another-file") == 0)
00108     {
00109       puts ("renameat with normal file descriptor succeeded");
00110       return 1;
00111     }
00112   if (errno != ENOTDIR)
00113     {
00114       puts ("error for renameat with normal file descriptor not ENOTDIR");
00115       return 1;
00116     }
00117 
00118   if (renameat (dir_fd, "some-file", fd, "another-file") == 0)
00119     {
00120       puts ("2nd renameat with normal file descriptor succeeded");
00121       return 1;
00122     }
00123   if (errno != ENOTDIR)
00124     {
00125       puts ("error for 2nd renameat with normal file descriptor not ENOTDIR");
00126       return 1;
00127     }
00128 
00129   close (fd);
00130 
00131   if (renameat (dir_fd, "some-file", dir_fd, "another-file") != 0)
00132     {
00133       puts ("renameat failed");
00134       return 1;
00135     }
00136 
00137   struct stat64 st2;
00138   if (fstatat64 (dir_fd, "some-file", &st2, 0) == 0)
00139     {
00140       puts ("fstatat64 succeeded");
00141       return 1;
00142     }
00143   if (errno != ENOENT)
00144     {
00145       puts ("fstatat64 did not fail with ENOENT");
00146       return 1;
00147     }
00148 
00149   if (fstatat64 (dir_fd, "another-file", &st2, 0) != 0)
00150     {
00151       puts ("2nd fstatat64 failed");
00152       return 1;
00153     }
00154 
00155   if (st1.st_dev != st2.st_dev
00156       || st1.st_ino != st2.st_ino
00157       || st1.st_size != st2.st_size)
00158     {
00159       puts ("stat results do not match");
00160       return 1;
00161     }
00162 
00163   /* Create a file descriptor which is closed again right away.  */
00164   int dir_fd2 = dup (dir_fd);
00165   if (dir_fd2 == -1)
00166     {
00167       puts ("dup failed");
00168       return 1;
00169     }
00170   close (dir_fd2);
00171 
00172   if (renameat (dir_fd2, "another-file", dir_fd, "some-file") == 0)
00173     {
00174       puts ("renameat with closed file descriptor succeeded");
00175       return 1;
00176     }
00177   if (errno != EBADF)
00178     {
00179       puts ("error for renameat with closed file descriptor not EBADF");
00180       return 1;
00181     }
00182 
00183   if (renameat (dir_fd, "another-file", dir_fd2, "some-file") == 0)
00184     {
00185       puts ("2nd renameat with closed file descriptor succeeded");
00186       return 1;
00187     }
00188   if (errno != EBADF)
00189     {
00190       puts ("error for 2nd renameat with closed file descriptor not EBADF");
00191       return 1;
00192     }
00193 
00194   if (unlinkat (dir_fd, "another-file", 0) != 0)
00195     {
00196       puts ("unlinkat failed");
00197       return 1;
00198     }
00199 
00200   if (renameat (-1, "another-file", dir_fd, "some-file") == 0)
00201     {
00202       puts ("renameat with invalid file descriptor succeeded");
00203       return 1;
00204     }
00205   if (errno != EBADF)
00206     {
00207       puts ("error for renameat with invalid file descriptor not EBADF");
00208       return 1;
00209     }
00210 
00211   if (renameat (dir_fd, "another-file", -1, "some-file") == 0)
00212     {
00213       puts ("2nd renameat with invalid file descriptor succeeded");
00214       return 1;
00215     }
00216   if (errno != EBADF)
00217     {
00218       puts ("error for 2nd renameat with invalid file descriptor not EBADF");
00219       return 1;
00220     }
00221 
00222   close (dir_fd);
00223 
00224   return 0;
00225 }