diff --git a/Makefile.am b/Makefile.am index f99481ab7f..d735627b67 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/systemd/meson.build b/src/systemd/meson.build index 870721b0d7..1bf1ea41d4 100644 --- a/src/systemd/meson.build +++ b/src/systemd/meson.build @@ -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 = [ diff --git a/src/systemd/nm-sd-utils.c b/src/systemd/nm-sd-utils.c new file mode 100644 index 0000000000..914b4b448c --- /dev/null +++ b/src/systemd/nm-sd-utils.c @@ -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); +} diff --git a/src/systemd/nm-sd-utils.h b/src/systemd/nm-sd-utils.h new file mode 100644 index 0000000000..8ca920e273 --- /dev/null +++ b/src/systemd/nm-sd-utils.h @@ -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__ */ + diff --git a/src/systemd/src/basic/path-util.c b/src/systemd/src/basic/path-util.c index df0fe2bcce..b758c34d0c 100644 --- a/src/systemd/src/basic/path-util.c +++ b/src/systemd/src/basic/path-util.c @@ -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; diff --git a/src/tests/test-systemd.c b/src/tests/test-systemd.c index ab5fed22a9..4660bd0d17 100644 --- a/src/tests/test-systemd.c +++ b/src/tests/test-systemd.c @@ -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 (); }