From 9c691524281052026a1b88de3c9a6d8030ae2e95 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 14 Mar 2024 12:28:39 +1000 Subject: [PATCH] Move mkdir_p into a utility header Part-of: --- meson.build | 1 + src/util-files.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ test/litest.c | 28 ++------------------------ 3 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 src/util-files.h diff --git a/meson.build b/meson.build index 26356838..bd38a525 100644 --- a/meson.build +++ b/meson.build @@ -258,6 +258,7 @@ util_headers = [ 'util-bits.h', 'util-input-event.h', 'util-list.h', + 'util-files.h', 'util-macros.h', 'util-matrix.h', 'util-prop-parsers.h', diff --git a/src/util-files.h b/src/util-files.h new file mode 100644 index 00000000..302c69f7 --- /dev/null +++ b/src/util-files.h @@ -0,0 +1,52 @@ +/* + * Copyright © 2024 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include "config.h" + +#include +#include +#include + +#include "util-strings.h" + +static inline int +mkdir_p(const char *dir) +{ + char *path, *parent; + int rc; + + if (streq(dir, "/")) + return 0; + + path = safe_strdup(dir); + parent = dirname(path); + + mkdir_p(parent); + rc = mkdir(dir, 0755); + + free(path); + + return (rc == -1 && errno != EEXIST) ? -errno : 0; +} diff --git a/test/litest.c b/test/litest.c index be855e15..dd262c06 100644 --- a/test/litest.c +++ b/test/litest.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -57,6 +56,7 @@ #include +#include "util-files.h" #include "litest.h" #include "litest-int.h" #include "libinput-util.h" @@ -1558,34 +1558,10 @@ litest_setup_quirks(struct list *created_files_list, setenv("LIBINPUT_QUIRKS_DIR", dirname, 1); } -static inline void -mkdir_p(const char *dir) -{ - char *path, *parent; - int rc; - - if (streq(dir, "/")) - return; - - path = safe_strdup(dir); - parent = dirname(path); - - mkdir_p(parent); - rc = mkdir(dir, 0755); - - if (rc == -1 && errno != EEXIST) { - litest_abort_msg("Failed to create directory %s (%s)\n", - dir, - strerror(errno)); - } - - free(path); -} - static inline void litest_init_udev_rules(struct list *created_files) { - mkdir_p(UDEV_RULES_D); + litest_assert_neg_errno_success(mkdir_p(UDEV_RULES_D)); litest_install_model_quirks(created_files); litest_init_all_device_udev_rules(created_files);