Move mkdir_p into a utility header

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/984>
This commit is contained in:
Peter Hutterer 2024-03-14 12:28:39 +10:00 committed by Marge Bot
parent 3644372696
commit 9c69152428
3 changed files with 55 additions and 26 deletions

View file

@ -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',

52
src/util-files.h Normal file
View file

@ -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;
}

View file

@ -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"
@ -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);