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 
     69 void setup();
     70 void cleanup();
     71 
     72 char *TCID = "rename13";
     73 int TST_TOTAL = 1;
     74 
     75 int fd;
     76 char fname[255], mname[255];
     77 struct stat buf1, buf2;
     78 dev_t olddev;
     79 ino_t oldino;
     80 
     81 int main(int ac, char **av)
     82 {
     83 	int lc;
     84 
     85 	/*
     86 	 * parse standard options
     87 	 */
     88 	tst_parse_opts(ac, av, NULL, NULL);
     89 
     90 	/*
     91 	 * perform global setup for test
     92 	 */
     93 	setup();
     94 
     95 	/*
     96 	 * check looping state if -i option given
     97 	 */
     98 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     99 
    100 		tst_count = 0;
    101 
    102 		/*
    103 		 * TEST rename()works when
    104 		 * both old file and new file link to the same file
    105 		 */
    106 
    107 		/* Call rename(2) */
    108 		TEST(rename(fname, mname));
    109 
    110 		if (TEST_RETURN == -1) {
    111 			tst_resm(TFAIL, "rename(%s, %s) failed", fname, mname);
    112 			continue;
    113 		}
    114 
    115 		/* check the existence of "new", and get the status */
    116 		if (stat(mname, &buf2) == -1) {
    117 			tst_brkm(TBROK, cleanup, "failed to stat file "
    118 				 "%s in rename()", mname);
    119 
    120 		}
    121 
    122 		/* check the existence of "old", and get the status */
    123 		if (stat(fname, &buf1) == -1) {
    124 			tst_brkm(TBROK, cleanup, "failed to stat file "
    125 				 "%s in rename()", fname);
    126 
    127 		}
    128 
    129 		/* verify the new file is the same as the original */
    130 		if (buf2.st_dev != olddev || buf2.st_ino != oldino) {
    131 			tst_resm(TFAIL,
    132 				 "rename() failed: new file does "
    133 				 "not point to the same file as old "
    134 				 "file");
    135 			continue;
    136 		}
    137 
    138 		/* verify the old file is unchanged */
    139 		if (buf1.st_dev != olddev || buf1.st_ino != oldino) {
    140 			tst_resm(TFAIL,
    141 				 "rename() failed: old file does "
    142 				 "not point to the original file");
    143 			continue;
    144 		}
    145 
    146 		tst_resm(TPASS, "functionality of rename() is correct");
    147 	}
    148 
    149 	cleanup();
    150 	tst_exit();
    151 }
    152 
    153 /*
    154  * setup() - performs all ONE TIME setup for this test.
    155  */
    156 void setup(void)
    157 {
    158 
    159 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    160 
    161 	TEST_PAUSE;
    162 
    163 	/* Create a temporary directory and make it current. */
    164 	tst_tmpdir();
    165 
    166 	sprintf(fname, "./tfile_%d", getpid());
    167 	sprintf(mname, "./rnfile_%d", getpid());
    168 
    169 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
    170 
    171 	if (stat(fname, &buf1) == -1) {
    172 		tst_brkm(TBROK, cleanup, "failed to stat file %s"
    173 			 "in rename()", fname);
    174 
    175 	}
    176 
    177 	/* save the dev and inode */
    178 	olddev = buf1.st_dev;
    179 	oldino = buf1.st_ino;
    180 
    181 	/* link the "new" file to the "old" file */
    182 	if (link(fname, mname) == -1) {
    183 		tst_brkm(TBROK, cleanup,
    184 			 "link from %s to %s failed!", fname, mname);
    185 	}
    186 }
    187 
    188 /*
    189  * cleanup() - performs all ONE TIME cleanup for this test at
    190  *             completion or premature exit.
    191  */
    192 void cleanup(void)
    193 {
    194 
    195 	/*
    196 	 * Remove the temporary directory.
    197 	 */
    198 	tst_rmdir();
    199 
    200 }
    201