aboutsummaryrefslogtreecommitdiff
path: root/lib/common/mkdir_p.c
blob: d25076377b517fe18f73315e371d278f538bea30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * mkdir_p.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "common.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#ifdef _WIN32
/*
  Supported paths:
   - <letter>:\ <absolute path>
   - \\<server>\<share>\ <absolute path>
   - \\?\<letter>:\ <absolute path>
   - \\?\UNC\<server>\<share>\ <absolute path>
   - Relative path not starting with '\'
 */

static WCHAR *skip_unc_path(WCHAR *ptr)
{
	/* server */
	if (*ptr == '\0' || *ptr == '\\')
		return NULL;

	while (*ptr != '\0' && *ptr != '\\')
		++ptr;

	if (*(ptr++) != '\\')
		return NULL;

	/* share */
	if (*ptr == '\0' || *ptr == '\\')
		return NULL;

	while (*ptr != '\0' && *ptr != '\\')
		++ptr;

	return (*ptr == '\\') ? (ptr + 1) : ptr;
}

static WCHAR *skip_prefix(WCHAR *ptr)
{
	if (isalpha(ptr[0]) && ptr[1] == ':' && ptr[2] == '\\')
		return ptr + 3;

	if (ptr[0] == '\\' && ptr[1] == '\\') {
		if (ptr[2] == '?') {
			if (ptr[3] != '\\')
				return NULL;

			ptr += 4;

			if ((ptr[0] == 'u' || ptr[0] == 'U') &&
			    (ptr[1] == 'n' || ptr[1] == 'N') &&
			    (ptr[2] == 'c' || ptr[2] == 'C') &&
			    ptr[3] == '\\') {
				ptr += 4;

				return skip_unc_path(ptr);
			}

			if (isalpha(ptr[0]) && ptr[1] == ':' && ptr[2] == '\\')
				return ptr + 3;

			return NULL;
		}

		return skip_unc_path(ptr);
	}

	if (ptr[0] == '\\')
		return NULL;

	return ptr;
}

int mkdir_p(const char *path)
{
	WCHAR *wpath, *ptr, *end;
	DWORD error;
	bool done;


	wpath = path_to_windows(path);
	if (wpath == NULL)
		return -1;

	ptr = skip_prefix(wpath);
	if (ptr == NULL) {
		fprintf(stderr, "Illegal or unsupported path: %s\n", path);
		goto fail;
	}

	while (*ptr != '\0') {
		if (*ptr == '\\') {
			++ptr;
			continue;
		}

		for (end = ptr; *end != '\0' && *end != '\\'; ++end)
			++end;

		if (*end == '\\') {
			done = false;
			*end = '\0';
		} else {
			done = true;
		}

		if (!CreateDirectoryW(wpath, NULL)) {
			error = GetLastError();

			if (error != ERROR_ALREADY_EXISTS) {
				fprintf(stderr, "Creating %s: %ld\n",
					path, error);
				goto fail;
			}
		}

		if (!done)
			*end = '\\';

		ptr = done ? end : (end + 1);
	}

	free(wpath);
	return 0;
fail:
	free(wpath);
	return -1;
}
#else
int mkdir_p(const char *path)
{
	size_t i, len;
	char *buffer;

	while (path[0] == '/' && path[1] == '/')
		++path;

	if (*path == '\0' || (path[0] == '/' && path[1] == '\0'))
		return 0;

	len = strlen(path) + 1;
	buffer = alloca(len);

	for (i = 0; i < len; ++i) {
		if (i > 0 && (path[i] == '/' || path[i] == '\0')) {
			buffer[i] = '\0';

			if (mkdir(buffer, 0755) != 0) {
				if (errno != EEXIST) {
					fprintf(stderr, "mkdir %s: %s\n",
						buffer, strerror(errno));
					return -1;
				}
			}
		}

		buffer[i] = path[i];
	}

	return 0;
}
#endif