xref: /freebsd-src/sys/contrib/openzfs/lib/libspl/mkdirp.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Use is subject to license terms.
25eda14cbcSMatt Macy  */
26eda14cbcSMatt Macy 
27eda14cbcSMatt Macy /*	Copyright (c) 1988 AT&T	*/
28eda14cbcSMatt Macy /*	  All Rights Reserved  	*/
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy /*
31eda14cbcSMatt Macy  * Creates directory and it's parents if the parents do not
32eda14cbcSMatt Macy  * exist yet.
33eda14cbcSMatt Macy  *
34eda14cbcSMatt Macy  * Returns -1 if fails for reasons other than non-existing
35eda14cbcSMatt Macy  * parents.
36eda14cbcSMatt Macy  * Does NOT simplify pathnames with . or .. in them.
37eda14cbcSMatt Macy  */
38eda14cbcSMatt Macy 
39eda14cbcSMatt Macy #include <sys/types.h>
40eda14cbcSMatt Macy #include <libgen.h>
41eda14cbcSMatt Macy #include <stdlib.h>
42eda14cbcSMatt Macy #include <unistd.h>
43eda14cbcSMatt Macy #include <errno.h>
44eda14cbcSMatt Macy #include <string.h>
45eda14cbcSMatt Macy #include <sys/stat.h>
46eda14cbcSMatt Macy 
47eda14cbcSMatt Macy static char *simplify(const char *str);
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy int
mkdirp(const char * d,mode_t mode)50eda14cbcSMatt Macy mkdirp(const char *d, mode_t mode)
51eda14cbcSMatt Macy {
52eda14cbcSMatt Macy 	char  *endptr, *ptr, *slash, *str;
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy 	str = simplify(d);
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy 	/* If space couldn't be allocated for the simplified names, return. */
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy 	if (str == NULL)
59eda14cbcSMatt Macy 		return (-1);
60eda14cbcSMatt Macy 
61eda14cbcSMatt Macy 		/* Try to make the directory */
62eda14cbcSMatt Macy 
63eda14cbcSMatt Macy 	if (mkdir(str, mode) == 0) {
64eda14cbcSMatt Macy 		free(str);
65eda14cbcSMatt Macy 		return (0);
66eda14cbcSMatt Macy 	}
67eda14cbcSMatt Macy 	if (errno != ENOENT) {
68eda14cbcSMatt Macy 		free(str);
69eda14cbcSMatt Macy 		return (-1);
70eda14cbcSMatt Macy 	}
71eda14cbcSMatt Macy 	endptr = strrchr(str, '\0');
72eda14cbcSMatt Macy 	slash = strrchr(str, '/');
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy 		/* Search upward for the non-existing parent */
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy 	while (slash != NULL) {
77eda14cbcSMatt Macy 
78eda14cbcSMatt Macy 		ptr = slash;
79eda14cbcSMatt Macy 		*ptr = '\0';
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 			/* If reached an existing parent, break */
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 		if (access(str, F_OK) == 0)
84eda14cbcSMatt Macy 			break;
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 			/* If non-existing parent */
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy 		else {
89eda14cbcSMatt Macy 			slash = strrchr(str, '/');
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 				/* If under / or current directory, make it. */
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 			if (slash == NULL || slash == str) {
94eda14cbcSMatt Macy 				if (mkdir(str, mode) != 0 && errno != EEXIST) {
95eda14cbcSMatt Macy 					free(str);
96eda14cbcSMatt Macy 					return (-1);
97eda14cbcSMatt Macy 				}
98eda14cbcSMatt Macy 				break;
99eda14cbcSMatt Macy 			}
100eda14cbcSMatt Macy 		}
101eda14cbcSMatt Macy 	}
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy 	/* Create directories starting from upmost non-existing parent */
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy 	while ((ptr = strchr(str, '\0')) != endptr) {
106eda14cbcSMatt Macy 		*ptr = '/';
107eda14cbcSMatt Macy 		if (mkdir(str, mode) != 0 && errno != EEXIST) {
108eda14cbcSMatt Macy 			/*
109eda14cbcSMatt Macy 			 *  If the mkdir fails because str already
110eda14cbcSMatt Macy 			 *  exists (EEXIST), then str has the form
111eda14cbcSMatt Macy 			 *  "existing-dir/..", and this is really
112eda14cbcSMatt Macy 			 *  ok. (Remember, this loop is creating the
113eda14cbcSMatt Macy 			 *  portion of the path that didn't exist)
114eda14cbcSMatt Macy 			 */
115eda14cbcSMatt Macy 			free(str);
116eda14cbcSMatt Macy 			return (-1);
117eda14cbcSMatt Macy 		}
118eda14cbcSMatt Macy 	}
119eda14cbcSMatt Macy 	free(str);
120eda14cbcSMatt Macy 	return (0);
121eda14cbcSMatt Macy }
122eda14cbcSMatt Macy 
123eda14cbcSMatt Macy /*
124eda14cbcSMatt Macy  *	simplify - given a pathname, simplify that path by removing
125eda14cbcSMatt Macy  *		   duplicate contiguous slashes.
126eda14cbcSMatt Macy  *
127eda14cbcSMatt Macy  *		   A simplified copy of the argument is returned to the
128eda14cbcSMatt Macy  *		   caller, or NULL is returned on error.
129eda14cbcSMatt Macy  *
130eda14cbcSMatt Macy  *		   The caller should handle error reporting based upon the
131eda14cbcSMatt Macy  *		   returned value, and should free the returned value,
132eda14cbcSMatt Macy  *		   when appropriate.
133eda14cbcSMatt Macy  */
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy static char *
simplify(const char * str)136eda14cbcSMatt Macy simplify(const char *str)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy 	int i;
139eda14cbcSMatt Macy 	size_t mbPathlen;	/* length of multi-byte path */
140eda14cbcSMatt Macy 	size_t wcPathlen;	/* length of wide-character path */
141eda14cbcSMatt Macy 	wchar_t *wptr;		/* scratch pointer */
142eda14cbcSMatt Macy 	wchar_t *wcPath;	/* wide-character version of the path */
143eda14cbcSMatt Macy 	char *mbPath;		/* The copy fo the path to be returned */
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 	/*
146eda14cbcSMatt Macy 	 *  bail out if there is nothing there.
147eda14cbcSMatt Macy 	 */
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 	if (!str) {
150eda14cbcSMatt Macy 		errno = ENOENT;
151eda14cbcSMatt Macy 		return (NULL);
152eda14cbcSMatt Macy 	}
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 	/*
155eda14cbcSMatt Macy 	 *  Get a copy of the argument.
156eda14cbcSMatt Macy 	 */
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 	if ((mbPath = strdup(str)) == NULL) {
159eda14cbcSMatt Macy 		return (NULL);
160eda14cbcSMatt Macy 	}
161eda14cbcSMatt Macy 
162eda14cbcSMatt Macy 	/*
163eda14cbcSMatt Macy 	 *  convert the multi-byte version of the path to a
164eda14cbcSMatt Macy 	 *  wide-character rendering, for doing our figuring.
165eda14cbcSMatt Macy 	 */
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	mbPathlen = strlen(mbPath);
168eda14cbcSMatt Macy 
169eda14cbcSMatt Macy 	if ((wcPath = calloc(mbPathlen+1, sizeof (wchar_t))) == NULL) {
170eda14cbcSMatt Macy 		free(mbPath);
171eda14cbcSMatt Macy 		return (NULL);
172eda14cbcSMatt Macy 	}
173eda14cbcSMatt Macy 
174eda14cbcSMatt Macy 	if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
175eda14cbcSMatt Macy 		free(mbPath);
176eda14cbcSMatt Macy 		free(wcPath);
177eda14cbcSMatt Macy 		return (NULL);
178eda14cbcSMatt Macy 	}
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy 	/*
181eda14cbcSMatt Macy 	 *  remove duplicate slashes first ("//../" -> "/")
182eda14cbcSMatt Macy 	 */
183eda14cbcSMatt Macy 
184eda14cbcSMatt Macy 	for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
185eda14cbcSMatt Macy 		*wptr++ = wcPath[i];
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 		if (wcPath[i] == '/') {
188eda14cbcSMatt Macy 			i++;
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 			while (wcPath[i] == '/') {
191eda14cbcSMatt Macy 				i++;
192eda14cbcSMatt Macy 			}
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 			i--;
195eda14cbcSMatt Macy 		}
196eda14cbcSMatt Macy 	}
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 	*wptr = '\0';
199eda14cbcSMatt Macy 
200eda14cbcSMatt Macy 	/*
201eda14cbcSMatt Macy 	 *  now convert back to the multi-byte format.
202eda14cbcSMatt Macy 	 */
203eda14cbcSMatt Macy 
204eda14cbcSMatt Macy 	if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {
205eda14cbcSMatt Macy 		free(mbPath);
206eda14cbcSMatt Macy 		free(wcPath);
207eda14cbcSMatt Macy 		return (NULL);
208eda14cbcSMatt Macy 	}
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy 	free(wcPath);
211eda14cbcSMatt Macy 	return (mbPath);
212eda14cbcSMatt Macy }
213