xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/rename/rename04.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 that rename() fails with EEXIST or ENOTEMPTY when
12  * newpath is a non-empty directory.
13  */
14 
15 #include <stdio.h>
16 #include "tst_test.h"
17 
18 #define MNT_POINT "mntpoint"
19 #define DIR1 "dir1"
20 #define DIR2 "dir2"
21 #define TEMP_FILE DIR2"/tmpfile"
22 
setup(void)23 static void setup(void)
24 {
25 	SAFE_CHDIR(MNT_POINT);
26 	SAFE_MKDIR(DIR1, 00770);
27 	SAFE_MKDIR(DIR2, 00770);
28 	SAFE_TOUCH(TEMP_FILE, 0700, NULL);
29 }
30 
run(void)31 static void run(void)
32 {
33 	TEST(rename(DIR1, DIR2));
34 
35 	if (TST_RET == -1 && (TST_ERR == ENOTEMPTY || TST_ERR == EEXIST))
36 		tst_res(TPASS | TTERRNO, "rename() failed as expected");
37 	else if (TST_RET == 0)
38 		tst_res(TFAIL, "rename() succeeded unexpectedly");
39 	else
40 		tst_res(TFAIL | TTERRNO, "rename() failed, but not with expected errno");
41 }
42 
43 static struct tst_test test = {
44 	.setup = setup,
45 	.test_all = run,
46 	.needs_root = 1,
47 	.mntpoint = MNT_POINT,
48 	.mount_device = 1,
49 	.all_filesystems = 1
50 };
51