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