core: add "nm-sd-utils.h" to access system internal helper

We have a fork of a lot of useful systemd helper code.
However, until now we shyed away from using it aside from
the bits that we really need.

That means, although we have some really nice implementations
in our source-tree, we didn't use them. Either we were missing
them, or we had to re-implement them.

Add "nm-sd-utils.h" header to very carefully make internal
systemd API accessible to the rest of core.

This is not intended as a vehicle to access all of internal
API. Instead, this must be used with care, and only a hand picked
selection of functions must be exposed. Use with caution, but where it
makes sense.
This commit is contained in:
Thomas Haller 2018-10-03 21:50:27 +02:00
parent d0a99176a7
commit eece5aff09
6 changed files with 128 additions and 3 deletions

View file

@ -1453,6 +1453,8 @@ src_libsystemd_nm_la_libadd = \
src_libsystemd_nm_la_SOURCES = \
src/systemd/nm-sd.c \
src/systemd/nm-sd.h \
src/systemd/nm-sd-utils.c \
src/systemd/nm-sd-utils.h \
src/systemd/sd-adapt/nm-sd-adapt.c \
src/systemd/sd-adapt/nm-sd-adapt.h \
src/systemd/sd-adapt/architecture.h \

View file

@ -49,7 +49,8 @@ sources = files(
'src/libsystemd/sd-id128/id128-util.c',
'src/libsystemd/sd-id128/sd-id128.c',
'src/shared/dns-domain.c',
'nm-sd.c'
'nm-sd.c',
'nm-sd-utils.c',
)
incs = [

45
src/systemd/nm-sd-utils.c Normal file
View file

@ -0,0 +1,45 @@
/* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright (C) 2018 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-sd-utils.h"
#include "nm-sd-adapt.h"
#include "path-util.h"
/*****************************************************************************/
gboolean
nm_sd_utils_path_equal (const char *a, const char *b)
{
return path_equal (a, b);
}
char *
nm_sd_utils_path_simplify (char *path, gboolean kill_dots)
{
return path_simplify (path, kill_dots);
}
const char *
nm_sd_utils_path_startswith (const char *path, const char *prefix)
{
return path_startswith (path, prefix);
}

33
src/systemd/nm-sd-utils.h Normal file
View file

@ -0,0 +1,33 @@
/* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright (C) 2018 Red Hat, Inc.
*/
#ifndef __NM_SD_UTILS_H__
#define __NM_SD_UTILS_H__
/*****************************************************************************/
gboolean nm_sd_utils_path_equal (const char *a, const char *b);
char *nm_sd_utils_path_simplify (char *path, gboolean kill_dots);
const char *nm_sd_utils_path_startswith (const char *path, const char *prefix);
/*****************************************************************************/
#endif /* __NM_SD_UTILS_H__ */

View file

@ -380,7 +380,6 @@ char *path_simplify(char *path, bool kill_dots) {
return path;
}
#if 0 /* NM_IGNORED */
char* path_startswith(const char *path, const char *prefix) {
assert(path);
assert(prefix);
@ -423,7 +422,6 @@ char* path_startswith(const char *path, const char *prefix) {
prefix += b;
}
}
#endif /* NM_IGNORED */
int path_compare(const char *a, const char *b) {
int d;

View file

@ -20,6 +20,7 @@
#include "nm-default.h"
#include "systemd/nm-sd.h"
#include "systemd/nm-sd-utils.h"
#include "nm-test-utils-core.h"
@ -173,6 +174,50 @@ test_sd_event (void)
/*****************************************************************************/
static void
test_path_equal (void)
{
#define _path_equal_check1(path, kill_dots, expected) \
G_STMT_START { \
const gboolean _kill_dots = (kill_dots); \
const char *_path0 = (path); \
const char *_expected = (expected); \
gs_free char *_path = g_strdup (_path0); \
const char *_path_result; \
\
if ( !_kill_dots \
&& !nm_sd_utils_path_equal (_path0, _expected)) \
g_error ("Paths \"%s\" and \"%s\" don't compare equal", _path0, _expected); \
\
_path_result = nm_sd_utils_path_simplify (_path, _kill_dots); \
g_assert (_path_result == _path); \
g_assert_cmpstr (_path, ==, _expected); \
} G_STMT_END
#define _path_equal_check(path, expected_no_kill_dots, expected_kill_dots) \
G_STMT_START { \
_path_equal_check1 (path, FALSE, expected_no_kill_dots); \
_path_equal_check1 (path, TRUE, expected_kill_dots ?: expected_no_kill_dots); \
} G_STMT_END
_path_equal_check ("", "", NULL);
_path_equal_check (".", ".", "");
_path_equal_check ("..", "..", NULL);
_path_equal_check ("/..", "/..", NULL);
_path_equal_check ("//..", "/..", NULL);
_path_equal_check ("/.", "/.", "/");
_path_equal_check ("./", ".", "");
_path_equal_check ("./.", "./.", "");
_path_equal_check (".///.", "./.", "");
_path_equal_check (".///./", "./.", "");
_path_equal_check (".////", ".", "");
_path_equal_check ("//..//foo/", "/../foo", NULL);
_path_equal_check ("///foo//./bar/.", "/foo/./bar/.", "/foo/bar");
_path_equal_check (".//./foo//./bar/.", "././foo/./bar/.", "foo/bar");
}
/*****************************************************************************/
NMTST_DEFINE ();
int
@ -183,6 +228,7 @@ main (int argc, char **argv)
g_test_add_func ("/systemd/dhcp/create", test_dhcp_create);
g_test_add_func ("/systemd/lldp/create", test_lldp_create);
g_test_add_func ("/systemd/sd-event", test_sd_event);
g_test_add_func ("/systemd/test_path_equal", test_path_equal);
return g_test_run ();
}