From 10ab81a0de97513d82d05945c12bff87b02ede27 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 2 Jul 2022 21:10:49 +0200 Subject: Cleanup: move mkdir_p from libcommon to libutil Signed-off-by: David Oberhollenzer --- bin/rdsquashfs/Makemodule.am | 2 +- bin/rdsquashfs/rdsquashfs.h | 1 + bin/sqfsdiff/Makemodule.am | 2 +- bin/sqfsdiff/sqfsdiff.h | 1 + include/common.h | 8 -- include/util/util.h | 8 ++ lib/common/Makemodule.am | 3 +- lib/common/mkdir_p.c | 170 ------------------------------------------- lib/util/Makemodule.am | 1 + lib/util/mkdir_p.c | 170 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 184 insertions(+), 182 deletions(-) delete mode 100644 lib/common/mkdir_p.c create mode 100644 lib/util/mkdir_p.c diff --git a/bin/rdsquashfs/Makemodule.am b/bin/rdsquashfs/Makemodule.am index 974e7bc..1ff9c60 100644 --- a/bin/rdsquashfs/Makemodule.am +++ b/bin/rdsquashfs/Makemodule.am @@ -4,7 +4,7 @@ rdsquashfs_SOURCES += bin/rdsquashfs/restore_fstree.c bin/rdsquashfs/describe.c rdsquashfs_SOURCES += bin/rdsquashfs/fill_files.c bin/rdsquashfs/dump_xattrs.c rdsquashfs_SOURCES += bin/rdsquashfs/stat.c rdsquashfs_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -rdsquashfs_LDADD = libcommon.a libio.a libcompat.a libsquashfs.la +rdsquashfs_LDADD = libcommon.a libio.a libcompat.a libutil.a libsquashfs.la rdsquashfs_LDADD += libfstree.a $(LZO_LIBS) $(PTHREAD_LIBS) dist_man1_MANS += bin/rdsquashfs/rdsquashfs.1 diff --git a/bin/rdsquashfs/rdsquashfs.h b/bin/rdsquashfs/rdsquashfs.h index dd50f28..56bb836 100644 --- a/bin/rdsquashfs/rdsquashfs.h +++ b/bin/rdsquashfs/rdsquashfs.h @@ -10,6 +10,7 @@ #include "config.h" #include "common.h" #include "fstree.h" +#include "util/util.h" #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN diff --git a/bin/sqfsdiff/Makemodule.am b/bin/sqfsdiff/Makemodule.am index bd93a74..ff08c7a 100644 --- a/bin/sqfsdiff/Makemodule.am +++ b/bin/sqfsdiff/Makemodule.am @@ -4,7 +4,7 @@ sqfsdiff_SOURCES += bin/sqfsdiff/compare_dir.c bin/sqfsdiff/node_compare.c sqfsdiff_SOURCES += bin/sqfsdiff/compare_files.c bin/sqfsdiff/super.c sqfsdiff_SOURCES += bin/sqfsdiff/extract.c sqfsdiff_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -sqfsdiff_LDADD = libcommon.a libsquashfs.la libio.a libcompat.a +sqfsdiff_LDADD = libcommon.a libsquashfs.la libio.a libcompat.a libutil.a sqfsdiff_LDADD += $(LZO_LIBS) libfstree.a $(PTHREAD_LIBS) dist_man1_MANS += bin/sqfsdiff/sqfsdiff.1 diff --git a/bin/sqfsdiff/sqfsdiff.h b/bin/sqfsdiff/sqfsdiff.h index a8f1b32..65e8120 100644 --- a/bin/sqfsdiff/sqfsdiff.h +++ b/bin/sqfsdiff/sqfsdiff.h @@ -10,6 +10,7 @@ #include "config.h" #include "common.h" #include "fstree.h" +#include "util/util.h" #include #include diff --git a/include/common.h b/include/common.h index 91dccaa..cbd73b2 100644 --- a/include/common.h +++ b/include/common.h @@ -51,14 +51,6 @@ void sqfs_perror(const char *file, const char *action, int error_code); int sqfs_tree_find_hard_links(const sqfs_tree_node_t *root, sqfs_hard_link_t **out); -/* - A wrapper around mkdir() that behaves like 'mkdir -p'. It tries to create - every component of the given path and skips already existing entries. - - Returns 0 on success. -*/ -int mkdir_p(const char *path); - /* A common implementation of the '--version' command line flag. */ void print_version(const char *progname); diff --git a/include/util/util.h b/include/util/util.h index 4b05340..af7d196 100644 --- a/include/util/util.h +++ b/include/util/util.h @@ -35,4 +35,12 @@ SQFS_INTERNAL sqfs_u32 xxh32(const void *input, const size_t len); */ SQFS_INTERNAL bool is_memory_zero(const void *blob, size_t size); +/* + A wrapper around mkdir() that behaves like 'mkdir -p'. It tries to create + every component of the given path and skips already existing entries. + + Returns 0 on success. +*/ +SQFS_INTERNAL int mkdir_p(const char *path); + #endif /* SQFS_UTIL_H */ diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index dd0a0d0..326f831 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -3,8 +3,7 @@ libcommon_a_SOURCES += lib/common/print_version.c lib/common/data_reader_dump.c libcommon_a_SOURCES += lib/common/compress.c lib/common/comp_opt.c libcommon_a_SOURCES += lib/common/data_writer.c include/common.h libcommon_a_SOURCES += lib/common/get_path.c lib/common/data_writer_ostream.c -libcommon_a_SOURCES += lib/common/perror.c -libcommon_a_SOURCES += lib/common/mkdir_p.c lib/common/parse_size.c +libcommon_a_SOURCES += lib/common/perror.c lib/common/parse_size.c libcommon_a_SOURCES += lib/common/print_size.c include/simple_writer.h libcommon_a_SOURCES += include/compress_cli.h libcommon_a_SOURCES += lib/common/writer/init.c lib/common/writer/cleanup.c diff --git a/lib/common/mkdir_p.c b/lib/common/mkdir_p.c deleted file mode 100644 index d250763..0000000 --- a/lib/common/mkdir_p.c +++ /dev/null @@ -1,170 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * mkdir_p.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "common.h" - -#include -#include -#include -#include - -#ifdef _WIN32 -/* - Supported paths: - - :\ - - \\\\ - - \\?\:\ - - \\?\UNC\\\ - - 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 diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 9578fb2..bc3d618 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -9,6 +9,7 @@ libutil_a_SOURCES += include/util/threadpool.h libutil_a_SOURCES += include/util/w32threadwrap.h libutil_a_SOURCES += lib/util/threadpool_serial.c libutil_a_SOURCES += lib/util/is_memory_zero.c +libutil_a_SOURCES += lib/util/mkdir_p.c libutil_a_CFLAGS = $(AM_CFLAGS) libutil_a_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/util/mkdir_p.c b/lib/util/mkdir_p.c new file mode 100644 index 0000000..993d8ec --- /dev/null +++ b/lib/util/mkdir_p.c @@ -0,0 +1,170 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * mkdir_p.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "util/util.h" + +#include +#include +#include +#include + +#ifdef _WIN32 +/* + Supported paths: + - :\ + - \\\\ + - \\?\:\ + - \\?\UNC\\\ + - 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 -- cgit v1.2.3