From 9de5c93986bc6a4b33f3f50ed2a94c9aa3a33c7f Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 3 May 2019 21:44:59 +0200 Subject: unsquashfs: add code to unpack the entire file system Signed-off-by: David Oberhollenzer --- lib/util/mkdir_p.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/util/mkdir_p.c (limited to 'lib/util') diff --git a/lib/util/mkdir_p.c b/lib/util/mkdir_p.c new file mode 100644 index 0000000..7d1e052 --- /dev/null +++ b/lib/util/mkdir_p.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include +#include +#include +#include +#include +#include + +#include "util.h" + +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; +} -- cgit v1.2.3