xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/rename/rename03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *  07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify rename(2) functions correctly when the newpath
12*49cdfc7eSAndroid Build Coastguard Worker  * file or directory (empty) exists.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
20*49cdfc7eSAndroid Build Coastguard Worker #define OLD_FILE_NAME MNT_POINT"/oldfile"
21*49cdfc7eSAndroid Build Coastguard Worker #define NEW_FILE_NAME MNT_POINT"/newfile"
22*49cdfc7eSAndroid Build Coastguard Worker #define OLD_DIR_NAME MNT_POINT"/olddir"
23*49cdfc7eSAndroid Build Coastguard Worker #define NEW_DIR_NAME MNT_POINT"/newdir"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct stat old_file_st, old_dir_st, new_file_st, new_dir_st;
26*49cdfc7eSAndroid Build Coastguard Worker 
run(void)27*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(OLD_FILE_NAME, 0700, NULL);
30*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(OLD_DIR_NAME, 00770);
31*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(NEW_FILE_NAME, 0700, NULL);
32*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(NEW_DIR_NAME, 00770);
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(OLD_FILE_NAME, &old_file_st);
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(OLD_DIR_NAME, &old_dir_st);
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(rename(OLD_FILE_NAME, NEW_FILE_NAME),
38*49cdfc7eSAndroid Build Coastguard Worker 						"rename(%s, %s)",
39*49cdfc7eSAndroid Build Coastguard Worker 						OLD_FILE_NAME, NEW_FILE_NAME);
40*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(rename(OLD_DIR_NAME, NEW_DIR_NAME),
41*49cdfc7eSAndroid Build Coastguard Worker 						"rename(%s, %s)",
42*49cdfc7eSAndroid Build Coastguard Worker 						OLD_DIR_NAME, NEW_DIR_NAME);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(NEW_FILE_NAME, &new_file_st);
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(NEW_DIR_NAME, &new_dir_st);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_file_st.st_dev, new_file_st.st_dev);
48*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_file_st.st_ino, new_file_st.st_ino);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_dir_st.st_dev, new_dir_st.st_dev);
51*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_dir_st.st_ino, new_dir_st.st_ino);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(stat(OLD_FILE_NAME, &old_file_st),
54*49cdfc7eSAndroid Build Coastguard Worker 				ENOENT,
55*49cdfc7eSAndroid Build Coastguard Worker 				"stat(%s, &old_file_st)",
56*49cdfc7eSAndroid Build Coastguard Worker 				OLD_FILE_NAME);
57*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(stat(OLD_DIR_NAME, &old_dir_st),
58*49cdfc7eSAndroid Build Coastguard Worker 				ENOENT,
59*49cdfc7eSAndroid Build Coastguard Worker 				"stat(%s, &old_dir_st)",
60*49cdfc7eSAndroid Build Coastguard Worker 				OLD_DIR_NAME);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	/* cleanup between loops */
63*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(NEW_FILE_NAME);
64*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_RMDIR(NEW_DIR_NAME);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
68*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
69*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
70*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
71*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
72*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1
73*49cdfc7eSAndroid Build Coastguard Worker };
74