xref: /freebsd/tests/sys/cddl/zfs/bin/rename_dir.c (revision 42249ef2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  * $FreeBSD$
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"@(#)rename_dir.c	1.1	07/05/25 SMI"
29 
30 /*
31  * Assertion:
32  * Create two directory trees in zfs filesystem, and rename
33  * directory across the directory structure. ZFS can handle
34  * the race situation.
35  */
36 
37 /*
38  * Need to create the following directory structures before
39  * running this program:
40  *
41  * mkdir -p 1/2/3/4/5 a/b/c/d/e
42  */
43 
44 
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <string.h>
49 
50 int
51 main()
52 {
53 	int i = 1;
54 	char buf[256];
55 	char *msg = "rename() fails to handle race situation\n";
56 
57 	switch (fork()) {
58 	case -1:
59 		perror("fork");
60 		exit(1);
61 		break;
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) strlcat(buf, "c_count: %d,", 256);
71 				(void) strlcat(buf, msg, 256);
72 				(void) fprintf(stderr, buf, c_count);
73 			}
74 		}
75 		break;
76 	default:
77 		while (i > 0) {
78 			int p_count = 0;
79 			if (rename("1", "a/b/c/d/e/1") == 0)
80 				p_count++;
81 			if (rename("a/b/c/d/e/1", "1") == 0)
82 				p_count++;
83 			if (p_count) {
84 				(void) strlcat(buf, "p_count: %d,", 256);
85 				(void) strlcat(buf, msg, 256);
86 				(void) fprintf(stderr, buf, p_count);
87 			}
88 		}
89 		break;
90 	}
91 	return (0);
92 }
93