xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/rename/rename01.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() when the newpath file or directory does not exist.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker static const char *old_file_name = "oldfile";
20*49cdfc7eSAndroid Build Coastguard Worker static const char *old_dir_name = "olddir";
21*49cdfc7eSAndroid Build Coastguard Worker static const char *new_file_name = "newfile";
22*49cdfc7eSAndroid Build Coastguard Worker static const char *new_dir_name = "newdir";
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct stat old_file_st, old_dir_st, new_file_st, new_dir_st;
25*49cdfc7eSAndroid Build Coastguard Worker 
swap(const char ** a,const char ** b)26*49cdfc7eSAndroid Build Coastguard Worker static inline void swap(const char **a, const char **b)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker 	const char *tmp__ = *a;
29*49cdfc7eSAndroid Build Coastguard Worker 	*a = *b;
30*49cdfc7eSAndroid Build Coastguard Worker 	*b = tmp__;
31*49cdfc7eSAndroid Build Coastguard Worker }
32*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)33*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHDIR(MNT_POINT);
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(old_file_name, 0700, NULL);
38*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(old_dir_name, 00770);
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(old_file_name, &old_file_st);
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(old_dir_name, &old_dir_st);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
run(void)44*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(rename(old_file_name, new_file_name),
47*49cdfc7eSAndroid Build Coastguard Worker 						"rename(%s, %s)",
48*49cdfc7eSAndroid Build Coastguard Worker 						old_file_name, new_file_name);
49*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(rename(old_dir_name, new_dir_name),
50*49cdfc7eSAndroid Build Coastguard Worker 						"rename(%s, %s)",
51*49cdfc7eSAndroid Build Coastguard Worker 						old_dir_name, new_dir_name);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(new_file_name, &new_file_st);
54*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(new_dir_name, &new_dir_st);
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_file_st.st_dev, new_file_st.st_dev);
57*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_file_st.st_ino, new_file_st.st_ino);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_dir_st.st_dev, new_dir_st.st_dev);
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(old_dir_st.st_ino, new_dir_st.st_ino);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(stat(old_file_name, &old_file_st),
63*49cdfc7eSAndroid Build Coastguard Worker 				ENOENT,
64*49cdfc7eSAndroid Build Coastguard Worker 				"stat(%s, &old_file_st)",
65*49cdfc7eSAndroid Build Coastguard Worker 				old_file_name);
66*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(stat(old_dir_name, &old_dir_st),
67*49cdfc7eSAndroid Build Coastguard Worker 				ENOENT,
68*49cdfc7eSAndroid Build Coastguard Worker 				"stat(%s, &old_dir_st)",
69*49cdfc7eSAndroid Build Coastguard Worker 				old_dir_name);
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	/* reset between loops */
72*49cdfc7eSAndroid Build Coastguard Worker 	swap(&old_file_name, &new_file_name);
73*49cdfc7eSAndroid Build Coastguard Worker 	swap(&old_dir_name, &new_dir_name);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
77*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
78*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
79*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
80*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
81*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
82*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1
83*49cdfc7eSAndroid Build Coastguard Worker };
84