From 145dfce5f04a3a74a49cc016f15b0aa6d9996662 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 7 Oct 2019 14:56:09 +0200 Subject: Cleanup: move directory handling code to libcommon Signed-off-by: David Oberhollenzer --- include/common.h | 18 +++++++++++ include/util.h | 21 ------------- lib/common/Makemodule.am | 1 + lib/common/dirstack.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ lib/common/mkdir_p.c | 45 +++++++++++++++++++++++++++ lib/util/Makemodule.am | 6 ++-- lib/util/dirstack.c | 79 ------------------------------------------------ lib/util/mkdir_p.c | 47 ---------------------------- 8 files changed, 143 insertions(+), 151 deletions(-) create mode 100644 lib/common/dirstack.c create mode 100644 lib/common/mkdir_p.c delete mode 100644 lib/util/dirstack.c delete mode 100644 lib/util/mkdir_p.c diff --git a/include/common.h b/include/common.h index 19c95a3..0e9da0c 100644 --- a/include/common.h +++ b/include/common.h @@ -138,4 +138,22 @@ void sqfs_writer_cleanup(sqfs_writer_t *sqfs); void sqfs_perror(const char *file, const char *action, int error_code); + +/* + 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); + +/* Returns 0 on success. On failure, prints error message to stderr. */ +int pushd(const char *path); + +/* Same as pushd, but the string doesn't have to be null-terminated. */ +int pushdn(const char *path, size_t len); + +/* Returns 0 on success. On failure, prints error message to stderr. */ +int popd(void); + #endif /* COMMON_H */ diff --git a/include/util.h b/include/util.h index 9ed2189..a9cfffd 100644 --- a/include/util.h +++ b/include/util.h @@ -58,27 +58,6 @@ int write_data(const char *errstr, int fd, const void *data, size_t size); SQFS_INTERNAL void print_version(void); -/* - 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); - -/* Returns 0 on success. On failure, prints error message to stderr. */ -SQFS_INTERNAL -int pushd(const char *path); - -/* Same as pushd, but the string doesn't have to be null-terminated. */ -SQFS_INTERNAL -int pushdn(const char *path, size_t len); - -/* Returns 0 on success. On failure, prints error message to stderr. */ -SQFS_INTERNAL -int popd(void); - /* Helper for allocating data structures with flexible array members. diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index 75f771d..db366af 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -5,5 +5,6 @@ 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/io_stdin.c libcommon_a_SOURCES += lib/common/writer.c lib/common/perror.c +libcommon_a_SOURCES += lib/common/dirstack.c lib/common/mkdir_p.c noinst_LIBRARIES += libcommon.a diff --git a/lib/common/dirstack.c b/lib/common/dirstack.c new file mode 100644 index 0000000..8f73898 --- /dev/null +++ b/lib/common/dirstack.c @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dirstack.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "common.h" + +#include +#include +#include +#include +#include +#include + +#define STACK_DEPTH 128 + +static int dirstack[STACK_DEPTH]; +static int stacktop = 0; + +int pushd(const char *path) +{ + int fd; + + assert(stacktop < STACK_DEPTH); + + fd = open(".", O_DIRECTORY | O_PATH | O_RDONLY | O_CLOEXEC); + + if (fd < 0) { + perror("open ./"); + return -1; + } + + if (chdir(path)) { + perror(path); + close(fd); + return -1; + } + + dirstack[stacktop++] = fd; + return 0; +} + +int pushdn(const char *path, size_t len) +{ + char *temp; + int ret; + + temp = strndup(path, len); + if (temp == NULL) { + perror("pushd"); + return -1; + } + + ret = pushd(temp); + + free(temp); + return ret; +} + +int popd(void) +{ + int fd; + + assert(stacktop > 0); + + fd = dirstack[stacktop - 1]; + + if (fchdir(fd)) { + perror("popd"); + return -1; + } + + --stacktop; + close(fd); + return 0; +} diff --git a/lib/common/mkdir_p.c b/lib/common/mkdir_p.c new file mode 100644 index 0000000..cb433b3 --- /dev/null +++ b/lib/common/mkdir_p.c @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * mkdir_p.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "common.h" + +#include +#include +#include +#include + +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; +} diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 5ba82bc..8dce70c 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -1,8 +1,6 @@ -libutil_la_SOURCES = lib/util/write_data.c include/util.h -libutil_la_SOURCES += lib/util/mkdir_p.c include/compat.h +libutil_la_SOURCES = lib/util/write_data.c include/util.h include/compat.h libutil_la_SOURCES += lib/util/str_table.c include/str_table.h -libutil_la_SOURCES += lib/util/dirstack.c lib/util/alloc.c -libutil_la_SOURCES += lib/util/canonicalize_name.c +libutil_la_SOURCES += lib/util/alloc.c lib/util/canonicalize_name.c libutil_la_CFLAGS = $(AM_CFLAGS) libutil_la_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/util/dirstack.c b/lib/util/dirstack.c deleted file mode 100644 index 20a89cc..0000000 --- a/lib/util/dirstack.c +++ /dev/null @@ -1,79 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * dirstack.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" - -#include -#include -#include -#include -#include -#include - -#include "util.h" - -#define STACK_DEPTH 128 - -static int dirstack[STACK_DEPTH]; -static int stacktop = 0; - -int pushd(const char *path) -{ - int fd; - - assert(stacktop < STACK_DEPTH); - - fd = open(".", O_DIRECTORY | O_PATH | O_RDONLY | O_CLOEXEC); - - if (fd < 0) { - perror("open ./"); - return -1; - } - - if (chdir(path)) { - perror(path); - close(fd); - return -1; - } - - dirstack[stacktop++] = fd; - return 0; -} - -int pushdn(const char *path, size_t len) -{ - char *temp; - int ret; - - temp = strndup(path, len); - if (temp == NULL) { - perror("pushd"); - return -1; - } - - ret = pushd(temp); - - free(temp); - return ret; -} - -int popd(void) -{ - int fd; - - assert(stacktop > 0); - - fd = dirstack[stacktop - 1]; - - if (fchdir(fd)) { - perror("popd"); - return -1; - } - - --stacktop; - close(fd); - return 0; -} diff --git a/lib/util/mkdir_p.c b/lib/util/mkdir_p.c deleted file mode 100644 index 93d0f59..0000000 --- a/lib/util/mkdir_p.c +++ /dev/null @@ -1,47 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * mkdir_p.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" - -#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