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