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