Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add a recursive mkdirat function
Baptiste Daroussin committed 10 years ago
commit 20d810026062ea2ed5743990a9da2183e6361198
parent 84ea3ae
2 files changed +31 -1
modified libpkg/private/utils.h
@@ -108,5 +108,6 @@ void *parse_mode(const char *str);
int *text_diff(char *a, char *b);
int merge_3way(char *pivot, char *v1, char *v2, struct sbuf *out);
bool string_end_with(const char *path, const char *str);
+
bool mkdirat_p(int fd, const char *path);

#endif
modified libpkg/utils.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2011-2014 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2016 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2013 Vsevolod Stakhov <vsevolod@FreeBSD.org>
 * All rights reserved.
@@ -802,3 +802,32 @@ pkg_absolutepath(const char *src, char *dest, size_t dest_size, bool fromroot) {

	return (dest);
}
+

+
bool
+
mkdirat_p(int fd, const char *path)
+
{
+
	const char *next;
+
	char *walk, pathdone[MAXPATHLEN];
+

+
	walk = strdup(path);
+
	pathdone[0] = '\0';
+

+
	while ((next = strsep(&walk, "/")) != NULL) {
+
		if (*next == '\0')
+
			continue;
+
		strlcat(pathdone, next, sizeof(pathdone));
+
		if (mkdirat(fd, pathdone, 0755) == -1) {
+
			if (errno == EEXIST) {
+
				strlcat(pathdone, "/", sizeof(pathdone));
+
				continue;
+
			}
+
			pkg_emit_error("Fail to create /%s: %s",
+
			    pathdone, strerror(errno));
+
			free(walk);
+
			return (false);
+
		}
+
		strlcat(pathdone, "/", sizeof(pathdone));
+
	}
+
	free(walk);
+
	return (true);
+
}