• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *	rename01
23  *
24  * DESCRIPTION
25  *	This test will verify the rename(2) syscall basic functionality.
26  *	Verify rename() works when the "new" file or directory does not exist.
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  *              1.  "old" is plain file, new does not exists
37  *                  create the "old" file, make sure the "new" file
38  *                  dose not exist
39  *                  rename the "old" to the "new" file
40  *                  verify the "new" file points to the "old" file
41  *                  verify the "old" file does not exist
42  *
43  *              2.  "old" is a directory,"new" does not exists
44  *                  create the "old" directory, make sure "new"
45  *                  dose not exist
46  *                  rename the "old" to the "new"
47  *                  verify the "new" points to the "old"
48  *                  verify the "old" does not exist
49  *	Cleanup:
50  *		Print errno log and/or timing stats if options given
51  *		Delete the temporary directory created.
52  *
53  * USAGE
54  *	rename01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55  *	where,  -c n : Run n copies concurrently.
56  *		-f   : Turn off functionality Testing.
57  *		-i n : Execute test n times.
58  *		-I x : Execute test for x seconds.
59  *		-P x : Pause for x seconds between iterations.
60  *		-t   : Turn on syscall timing.
61  *
62  * HISTORY
63  *	07/2001 Ported by Wayne Boyer
64  *
65  * RESTRICTIONS
66  *	None.
67  */
68 #include <sys/types.h>
69 #include <fcntl.h>
70 #include <sys/stat.h>
71 #include <unistd.h>
72 #include <errno.h>
73 
74 #include "test.h"
75 #include "safe_macros.h"
76 
77 void setup();
78 void cleanup();
79 
80 char *TCID = "rename01";
81 int TST_TOTAL = 2;
82 
83 char fname[255], mname[255];
84 char fdir[255], mdir[255];
85 struct stat buf1;
86 dev_t f_olddev, d_olddev;
87 ino_t f_oldino, d_oldino;
88 
89 struct test_case_t {
90 	char *name1;
91 	char *name2;
92 	char *desc;
93 	dev_t *olddev;
94 	ino_t *oldino;
95 } TC[] = {
96 	/* comment goes here */
97 	{
98 	fname, mname, "file", &f_olddev, &f_oldino},
99 	    /* comment goes here */
100 	{
101 	fdir, mdir, "directory", &d_olddev, &d_oldino}
102 };
103 
main(int ac,char ** av)104 int main(int ac, char **av)
105 {
106 	int lc;
107 	int i;
108 
109 	/*
110 	 * parse standard options
111 	 */
112 	tst_parse_opts(ac, av, NULL, NULL);
113 
114 	/*
115 	 * perform global setup for test
116 	 */
117 	setup();
118 
119 	/*
120 	 * check looping state if -i option given
121 	 */
122 	for (lc = 0; TEST_LOOPING(lc); lc++) {
123 
124 		tst_count = 0;
125 
126 		/* loop through the test cases */
127 		for (i = 0; i < TST_TOTAL; i++) {
128 
129 			TEST(rename(TC[i].name1, TC[i].name2));
130 
131 			if (TEST_RETURN == -1) {
132 				tst_resm(TFAIL, "call failed unexpectedly");
133 				continue;
134 			}
135 
136 			SAFE_STAT(cleanup, TC[i].name2, &buf1);
137 
138 			/*
139 			 * verify the new file or directory is the
140 			 * same as the old one
141 			 */
142 			if (buf1.st_dev != *TC[i].olddev ||
143 			    buf1.st_ino != *TC[i].oldino) {
144 				tst_resm(TFAIL, "rename() failed: the "
145 					 "new %s points to a different "
146 					 "inode/location", TC[i].desc);
147 				continue;
148 			}
149 			/*
150 			 * verify that the old file or directory
151 			 * does not exist
152 			 */
153 			if (stat(fname, &buf1) != -1) {
154 				tst_resm(TFAIL, "the old %s still "
155 					 "exists", TC[i].desc);
156 				continue;
157 			}
158 
159 			tst_resm(TPASS, "functionality is correct "
160 				 "for renaming a %s", TC[i].desc);
161 		}
162 		/* reset things in case we are looping */
163 		SAFE_RENAME(cleanup, mname, fname);
164 
165 		SAFE_RENAME(cleanup, mdir, fdir);
166 	}
167 
168 	cleanup();
169 	tst_exit();
170 
171 }
172 
173 /*
174  * setup() - performs all ONE TIME setup for this test.
175  */
setup(void)176 void setup(void)
177 {
178 
179 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
180 
181 	TEST_PAUSE;
182 
183 	/* Create a temporary directory and make it current. */
184 	tst_tmpdir();
185 
186 	sprintf(fname, "./tfile_%d", getpid());
187 	sprintf(mname, "./rnfile_%d", getpid());
188 	sprintf(fdir, "./tdir_%d", getpid());
189 	sprintf(mdir, "./rndir_%d", getpid());
190 
191 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
192 
193 	SAFE_STAT(cleanup, fname, &buf1);
194 
195 	f_olddev = buf1.st_dev;
196 	f_oldino = buf1.st_ino;
197 
198 	/* create "old" directory */
199 	SAFE_MKDIR(cleanup, fdir, 00770);
200 
201 	SAFE_STAT(cleanup, fdir, &buf1);
202 
203 	d_olddev = buf1.st_dev;
204 	d_oldino = buf1.st_ino;
205 }
206 
207 /*
208  * cleanup() - performs all ONE TIME cleanup for this test at
209  *             completion or premature exit.
210  */
cleanup(void)211 void cleanup(void)
212 {
213 
214 	/*
215 	 * Remove the temporary directory.
216 	 */
217 	tst_rmdir();
218 }
219