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 <sys/fcntl.h>
70 #include <sys/stat.h>
71 #include <unistd.h>
72 #include <errno.h>
73
74 #include "test.h"
75
76 void setup();
77 void cleanup();
78
79 char *TCID = "rename01";
80 int TST_TOTAL = 2;
81
82 char fname[255], mname[255];
83 char fdir[255], mdir[255];
84 struct stat buf1;
85 dev_t f_olddev, d_olddev;
86 ino_t f_oldino, d_oldino;
87
88 struct test_case_t {
89 char *name1;
90 char *name2;
91 char *desc;
92 dev_t *olddev;
93 ino_t *oldino;
94 } TC[] = {
95 /* comment goes here */
96 {
97 fname, mname, "file", &f_olddev, &f_oldino},
98 /* comment goes here */
99 {
100 fdir, mdir, "directory", &d_olddev, &d_oldino}
101 };
102
main(int ac,char ** av)103 int main(int ac, char **av)
104 {
105 int lc;
106 int i;
107
108 /*
109 * parse standard options
110 */
111 tst_parse_opts(ac, av, NULL, NULL);
112
113 /*
114 * perform global setup for test
115 */
116 setup();
117
118 /*
119 * check looping state if -i option given
120 */
121 for (lc = 0; TEST_LOOPING(lc); lc++) {
122
123 tst_count = 0;
124
125 /* loop through the test cases */
126 for (i = 0; i < TST_TOTAL; i++) {
127
128 TEST(rename(TC[i].name1, TC[i].name2));
129
130 if (TEST_RETURN == -1) {
131 tst_resm(TFAIL, "call failed unexpectedly");
132 continue;
133 }
134
135 if (stat(TC[i].name2, &buf1) == -1) {
136 tst_brkm(TBROK, cleanup, "stat of %s "
137 "failed", TC[i].desc);
138
139 }
140
141 /*
142 * verify the new file or directory is the
143 * same as the old one
144 */
145 if (buf1.st_dev != *TC[i].olddev ||
146 buf1.st_ino != *TC[i].oldino) {
147 tst_resm(TFAIL, "rename() failed: the "
148 "new %s points to a different "
149 "inode/location", TC[i].desc);
150 continue;
151 }
152 /*
153 * verify that the old file or directory
154 * does not exist
155 */
156 if (stat(fname, &buf1) != -1) {
157 tst_resm(TFAIL, "the old %s still "
158 "exists", TC[i].desc);
159 continue;
160 }
161
162 tst_resm(TPASS, "functionality is correct "
163 "for renaming a %s", TC[i].desc);
164 }
165 /* reset things in case we are looping */
166 if (rename(mname, fname) == -1) {
167 tst_brkm(TBROK, cleanup, "file rename failed");
168 }
169
170 if (rename(mdir, fdir) == -1) {
171 tst_brkm(TBROK, cleanup, "directory rename failed");
172 }
173 }
174
175 cleanup();
176 tst_exit();
177
178 }
179
180 /*
181 * setup() - performs all ONE TIME setup for this test.
182 */
setup(void)183 void setup(void)
184 {
185
186 tst_sig(NOFORK, DEF_HANDLER, cleanup);
187
188 TEST_PAUSE;
189
190 /* Create a temporary directory and make it current. */
191 tst_tmpdir();
192
193 sprintf(fname, "./tfile_%d", getpid());
194 sprintf(mname, "./rnfile_%d", getpid());
195 sprintf(fdir, "./tdir_%d", getpid());
196 sprintf(mdir, "./rndir_%d", getpid());
197
198 SAFE_TOUCH(cleanup, fname, 0700, NULL);
199
200 if (stat(fname, &buf1) == -1) {
201 tst_brkm(TBROK, cleanup, "failed to stat file %s"
202 "in setup()", fname);
203
204 }
205
206 f_olddev = buf1.st_dev;
207 f_oldino = buf1.st_ino;
208
209 /* create "old" directory */
210 if (mkdir(fdir, 00770) == -1) {
211 tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
212 }
213
214 if (stat(fdir, &buf1) == -1) {
215 tst_brkm(TBROK, cleanup, "failed to stat directory %s"
216 "in setup()", fname);
217
218 }
219
220 d_olddev = buf1.st_dev;
221 d_oldino = buf1.st_ino;
222 }
223
224 /*
225 * cleanup() - performs all ONE TIME cleanup for this test at
226 * completion or premature exit.
227 */
cleanup(void)228 void cleanup(void)
229 {
230
231 /*
232 * Remove the temporary directory.
233 */
234 tst_rmdir();
235 }
236