Home | History | Annotate | Download | only in rename
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      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 2 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
     13  *   the 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, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  *	rename13
     23  *
     24  * DESCRIPTION
     25  *	Verify rename() return successfully and performs no other action
     26  *      when "old" file and "new" file link to the same file.
     27  *
     28  * ALGORITHM
     29  *	Setup:
     30  *		Setup signal handling.
     31  *		Create temporary directory.
     32  *		Pause for SIGUSR1 if option specified.
     33  *
     34  *	Test:
     35  *		Loop if the proper options are given.
     36  *                  create the "old" file
     37  *                  link the "new" file to the "old" file
     38  *                  rename the "old" to the "new" file
     39  *                  verify the "new" file points to the original file
     40  *                  verify the "old" file exists and points to
     41  *                         the original file
     42  *	Cleanup:
     43  *		Print errno log and/or timing stats if options given
     44  *		Delete the temporary directory created.
     45  *
     46  * USAGE
     47  *	rename13 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
     48  *	where,  -c n : Run n copies concurrently.
     49  *		-f   : Turn off functionality Testing.
     50  *		-i n : Execute test n times.
     51  *		-I x : Execute test for x seconds.
     52  *		-P x : Pause for x seconds between iterations.
     53  *		-t   : Turn on syscall timing.
     54  *
     55  * HISTORY
     56  *	07/2001 Ported by Wayne Boyer
     57  *
     58  * RESTRICTIONS
     59  *	None.
     60  */
     61 #include <sys/types.h>
     62 #include <fcntl.h>
     63 #include <sys/stat.h>
     64 #include <unistd.h>
     65 #include <errno.h>
     66 
     67 #include "test.h"
     68 #include "safe_macros.h"
     69 
     70 void setup();
     71 void cleanup();
     72 
     73 char *TCID = "rename13";
     74 int TST_TOTAL = 1;
     75 
     76 int fd;
     77 char fname[255], mname[255];
     78 struct stat buf1, buf2;
     79 dev_t olddev;
     80 ino_t oldino;
     81 
     82 int main(int ac, char **av)
     83 {
     84 	int lc;
     85 
     86 	/*
     87 	 * parse standard options
     88 	 */
     89 	tst_parse_opts(ac, av, NULL, NULL);
     90 
     91 	/*
     92 	 * perform global setup for test
     93 	 */
     94 	setup();
     95 
     96 	/*
     97 	 * check looping state if -i option given
     98 	 */
     99 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    100 
    101 		tst_count = 0;
    102 
    103 		/*
    104 		 * TEST rename()works when
    105 		 * both old file and new file link to the same file
    106 		 */
    107 
    108 		/* Call rename(2) */
    109 		TEST(rename(fname, mname));
    110 
    111 		if (TEST_RETURN == -1) {
    112 			tst_resm(TFAIL, "rename(%s, %s) failed", fname, mname);
    113 			continue;
    114 		}
    115 
    116 		/* check the existence of "new", and get the status */
    117 		SAFE_STAT(cleanup, mname, &buf2);
    118 
    119 		/* check the existence of "old", and get the status */
    120 		SAFE_STAT(cleanup, fname, &buf1);
    121 
    122 		/* verify the new file is the same as the original */
    123 		if (buf2.st_dev != olddev || buf2.st_ino != oldino) {
    124 			tst_resm(TFAIL,
    125 				 "rename() failed: new file does "
    126 				 "not point to the same file as old "
    127 				 "file");
    128 			continue;
    129 		}
    130 
    131 		/* verify the old file is unchanged */
    132 		if (buf1.st_dev != olddev || buf1.st_ino != oldino) {
    133 			tst_resm(TFAIL,
    134 				 "rename() failed: old file does "
    135 				 "not point to the original file");
    136 			continue;
    137 		}
    138 
    139 		tst_resm(TPASS, "functionality of rename() is correct");
    140 	}
    141 
    142 	cleanup();
    143 	tst_exit();
    144 }
    145 
    146 /*
    147  * setup() - performs all ONE TIME setup for this test.
    148  */
    149 void setup(void)
    150 {
    151 
    152 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    153 
    154 	TEST_PAUSE;
    155 
    156 	/* Create a temporary directory and make it current. */
    157 	tst_tmpdir();
    158 
    159 	sprintf(fname, "./tfile_%d", getpid());
    160 	sprintf(mname, "./rnfile_%d", getpid());
    161 
    162 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
    163 
    164 	SAFE_STAT(cleanup, fname, &buf1);
    165 
    166 	/* save the dev and inode */
    167 	olddev = buf1.st_dev;
    168 	oldino = buf1.st_ino;
    169 
    170 	/* link the "new" file to the "old" file */
    171 	SAFE_LINK(cleanup, fname, mname);
    172 }
    173 
    174 /*
    175  * cleanup() - performs all ONE TIME cleanup for this test at
    176  *             completion or premature exit.
    177  */
    178 void cleanup(void)
    179 {
    180 
    181 	/*
    182 	 * Remove the temporary directory.
    183 	 */
    184 	tst_rmdir();
    185 
    186 }
    187