xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/rename_dir.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * Copyright (c) 2012 by Delphix. All rights reserved.
30  */
31 
32 /*
33  * Assertion:
34  * Create two directory trees in zfs filesystem, and rename
35  * directory across the directory structure. ZFS can handle
36  * the race situation.
37  */
38 
39 /*
40  * Need to create the following directory structures before
41  * running this program:
42  *
43  * mkdir -p 1/2/3/4/5 a/b/c/d/e
44  */
45 
46 
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 
51 int
main(void)52 main(void)
53 {
54 	int i = 1;
55 
56 	switch (fork()) {
57 	case -1:
58 		perror("fork");
59 		exit(1);
60 		break;
61 
62 	case 0:
63 		while (i > 0) {
64 			int c_count = 0;
65 			if (rename("a/b/c", "1/2/3/c") == 0)
66 				c_count++;
67 			if (rename("1/2/3/c", "a/b/c") == 0)
68 				c_count++;
69 			if (c_count)
70 				(void) fprintf(stderr, "c_count: %d", c_count);
71 		}
72 		_exit(0);
73 
74 	default:
75 		while (i > 0) {
76 			int p_count = 0;
77 			if (rename("1", "a/b/c/d/e/1") == 0)
78 				p_count++;
79 			if (rename("a/b/c/d/e/1", "1") == 0)
80 				p_count++;
81 			if (p_count)
82 				(void) fprintf(stderr, "p_count: %d", p_count);
83 		}
84 		return (0);
85 	}
86 }
87