diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2024-03-14 12:28:39 +1000 |
---|---|---|
committer | Marge Bot <emma+marge@anholt.net> | 2024-03-18 16:25:47 +0000 |
commit | 9c691524281052026a1b88de3c9a6d8030ae2e95 (patch) | |
tree | ca210f72a6f0fa38ccc6c60db1b18ffb715cecbe | |
parent | 3644372696f7e509c13715b81bdf8554e8a25efe (diff) |
Move mkdir_p into a utility header
Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/984>
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/util-files.h | 52 | ||||
-rw-r--r-- | test/litest.c | 28 |
3 files changed, 55 insertions, 26 deletions
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 <errno.h> +#include <libgen.h> +#include <sys/stat.h> + +#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 <check.h> #include <dirent.h> #include <errno.h> -#include <libgen.h> #include <fcntl.h> #include <fnmatch.h> #include <getopt.h> @@ -57,6 +56,7 @@ #include <valgrind/valgrind.h> +#include "util-files.h" #include "litest.h" #include "litest-int.h" #include "libinput-util.h" @@ -1559,33 +1559,9 @@ litest_setup_quirks(struct list *created_files_list, } 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); |