Commit Graph

461 Commits

Author SHA1 Message Date
Thomas Haller
3b69f02164 all: unify format of our Copyright source code comments
```bash

readarray -d '' FILES < <(
  git ls-files -z \
    ':(exclude)po' \
    ':(exclude)shared/c-rbtree' \
    ':(exclude)shared/c-list' \
    ':(exclude)shared/c-siphash' \
    ':(exclude)shared/c-stdaux' \
    ':(exclude)shared/n-acd' \
    ':(exclude)shared/n-dhcp4' \
    ':(exclude)src/systemd/src' \
    ':(exclude)shared/systemd/src' \
    ':(exclude)m4' \
    ':(exclude)COPYING*'
  )

sed \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[-–] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C1pyright#\5 - \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[,] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C2pyright#\5, \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C3pyright#\5#\7/' \
  -e 's/^Copyright \(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/C4pyright#\1#\3/' \
  -i \
  "${FILES[@]}"

echo ">>> untouched Copyright lines"
git grep Copyright "${FILES[@]}"

echo ">>> Copyright lines with unusual extra"
git grep '\<C[0-9]pyright#' "${FILES[@]}" | grep -i reserved

sed \
  -e 's/\<C[0-9]pyright#\([^#]*\)#\(.*\)$/Copyright (C) \1 \2/' \
  -i \
  "${FILES[@]}"

```

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/298
2019-10-02 17:03:52 +02:00
Iñigo Martínez
de90fc0810 meson: Improve the shared utils test build file
A new variable has been created for the C compiler flags to avoid
polluting the target creation call so the flags are encapsulated.
2019-10-01 09:49:33 +02:00
Iñigo Martínez
c74e428342 meson: Improve the shared build file
The `shared` build file has been improved by applying a set of
changes:

- Indentation has been fixed to be consistent.
- Unused libraries and dependencies have been removed.
- Dependencies have been reviewed to store the necessary data.
- Set of objects used in targets have been grouped together.
- Header files have been removed from sources lists as it's
  unnecessary.
- Library variable names have been changed to `lib{name}` pattern
  following their filename pattern.
- `shared` prefix has been removed from all variables using it.
- `version_header` its related configuration `version_conf`
  variables have been renamed to `nm_version_macro*` following
  its input and final file names.
2019-10-01 09:49:33 +02:00
Thomas Haller
abff46cacf all: manually drop code comments with file description 2019-10-01 07:50:52 +02:00
Thomas Haller
c1559dae3f shared: add nmtst_g_source_assert_not_called() helper 2019-09-30 16:00:45 +02:00
Thomas Haller
b6acec0fbc shared: add compat macro for G_PID_FORMAT
Having G_PID_FORMAT macro is useful, but it's only available in
recent glib versions. Add a compat implementation and a test that
our assumptions hold.
2019-09-26 08:18:58 +02:00
Lubomir Rintel
24028a2246 all: SPDX header conversion
$ find * -type f |xargs perl contrib/scripts/spdx.pl
  $ git rm contrib/scripts/spdx.pl
2019-09-10 11:19:56 +02:00
Thomas Haller
a265892385 shared/tests: add nmtst_keyfile_get_num_keys() helper 2019-08-27 10:44:23 +02:00
Beniamino Galvani
00bb6cdb4f build: fix meson warning about path separator in target
Fix the following:

 WARNING: Target "nm-utils/tests/test-shared-general" has a path
 separator in its name.  This is not supported, it can cause
 unexpected failures and will become a hard error in the future.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/issues/217
2019-08-05 16:05:30 +02:00
Thomas Haller
441dd1f3c8 libnm: add nm_connection_to_dbus_full() with options argument
No options are implemented yet.
2019-06-28 16:48:17 +02:00
Thomas Haller
f182d4fa20 shared/tests: add test for nm_utils_bin2hexstr_full() 2019-06-19 15:49:57 +02:00
Thomas Haller
87a73df959 all: drop empty first line from sources
git ls-files -z -- ':(exclude)src/settings/plugins/keyfile/tests/keyfiles' | xargs -0 -n1 sed -i '1 { /^$/d }'
2019-06-11 10:15:06 +02:00
Thomas Haller
c0e075c902 all: drop emacs file variables from source files
We no longer add these. If you use Emacs, configure it yourself.

Also, due to our "smart-tab" usage the editor anyway does a subpar
job handling our tabs. However, on the upside every user can choose
whatever tab-width he/she prefers. If "smart-tabs" are used properly
(like we do), every tab-width will work.

No manual changes, just ran commands:

    F=($(git grep -l -e '-\*-'))
    sed '1 { /\/\* *-\*-  *[mM]ode.*\*\/$/d }'     -i "${F[@]}"
    sed '1,4 { /^\(#\|--\|dnl\) *-\*- [mM]ode/d }' -i "${F[@]}"

Check remaining lines with:

    git grep -e '-\*-'

The ultimate purpose of this is to cleanup our files and eventually use
SPDX license identifiers. For that, first get rid of the boilerplate lines.
2019-06-11 10:04:00 +02:00
Thomas Haller
98ec22d4be shared: add nmtst_get_rand_uint() helper 2019-06-11 08:25:10 +02:00
Thomas Haller
d7932ee5f1 tests/trivial: rename nmtst_get_rand_int() to nmtst_get_rand_uint32()
nmtst_get_rand_int() was originally named that way, because it
calls g_rand_int(). But I think if a function returns an uint32, it
should also be named that way.

Rename.
2019-06-11 08:25:10 +02:00
Thomas Haller
b927287c7d shared/utils: add nmtst_assert_setting_dbus_roundtrip() util 2019-05-23 18:09:49 +02:00
Thomas Haller
892440c4ab shared/tests: add nmtst_assert_setting_is_equal() util 2019-05-23 18:09:49 +02:00
Thomas Haller
ae7f32e03b shared/tests: add nmtst_variant_from_string() util 2019-05-23 18:09:49 +02:00
Thomas Haller
2946d07085 shared: implement _nm_utils_monotonic_timestamp_initialized() in "nm-logging-stub.c" 2019-05-22 20:04:08 +02:00
Thomas Haller
7a25f67af7 shared/tests: add tests for libnm-core-aux
These tests cannot (easily) be under "shared/nm-libnm-core-aux/tests"
because libnm/libnm-core requires code under shared while
"nm-libnm-core-aux" requires libnm/libnm-core. With autotools that is
not problem, but with meson we include sub directories in a particular
order and there is no way to foward declare stuff (AFAIK). To avoid
the circular dependency, add the tests to "clients/common/tests", which
is always built last.
2019-04-25 07:47:37 +02:00
Thomas Haller
80db06f768 shared: move most of "shared/nm-utils" to "shared/nm-glib-aux"
From the files under "shared/nm-utils" we build an internal library
that provides glib-based helper utilities.

Move the files of that basic library to a new subdirectory
"shared/nm-glib-aux" and rename the helper library "libnm-core-base.la"
to "libnm-glib-aux.la".

Reasons:

 - the name "utils" is overused in our code-base. Everything's an
   "utils". Give this thing a more distinct name.

 - there were additional files under "shared/nm-utils", which are not
   part of this internal library "libnm-utils-base.la". All the files
   that are part of this library should be together in the same
   directory, but files that are not, should not be there.

 - the new name should better convey what this library is and what is isn't:
   it's a set of utilities and helper functions that extend glib with
   funcitonality that we commonly need.

There are still some files left under "shared/nm-utils". They have less
a unifying propose to be in their own directory, so I leave them there
for now. But at least they are separate from "shared/nm-glib-aux",
which has a very clear purpose.
2019-04-18 18:59:09 +02:00
Thomas Haller
2973d68253 shared: move udev helper to separate directory "shared/nm-udev-aux"
We built (among others) two libraries from the sources in "shared/nm-utils":
"libnm-utils-base.la" and "libnm-utils-udev.la".

It's confusing. Instead use directories so there is a direct
correspondence between these internal libraries and the source files.
2019-04-18 18:58:28 +02:00
Thomas Haller
b434b9ec07 shared: split C-only helper "shared/nm-std-aux" utils out of "shared/nm-utils"
"shared/nm-utils" contains general purpose utility functions that only
depend on glib (and extend glib with some helper functions).

We will also add code that does not use glib, hence it would be good
if the part of "shared/nm-utils" that does not depend on glib, could be
used by these future projects.

Also, we use the term "utils" everywhere. While that covers the purpose
and content well, having everything called "nm-something-utils" is not
great. Instead, call this "nm-std-aux", inspired by "c-util/c-stdaux".
2019-04-18 18:57:24 +02:00
Thomas Haller
304eab8703 shared: remove unused _nm_utils_escape_plain()/_nm_utils_escape_spaces() API
... and the "unescape" variants.

This is replaced by nm_utils_escaped_tokens_split()
and nm_utils_escaped_tokens_escape*() API.
2019-04-18 17:53:09 +02:00
Thomas Haller
832adf323e shared: add nm_utils_escaped_tokens_escape_gstr_assert() 2019-04-17 11:26:30 +02:00
Thomas Haller
ced7dbe8bf shared: remove unused nm_utils_str_simpletokens_extract_next()
This can be replaced by nm_utils_escaped_tokens_split().

Note that nm_utils_escaped_tokens_split() does not behave exactly
the same. For example, nm_utils_str_simpletokens_extract_next() would
remove all backslashes and leave only the following character.
nm_utils_escaped_tokens_split() instead only strips backslashes
that preceed a delimiter, whitespace or another backslash.

But we should have one preferred way of tokenizing, and I find this
preferable, because it allows for most backslashes to appear verbatim.
2019-04-17 11:11:21 +02:00
Thomas Haller
e206e3d4d8 shared: add nm_utils_escaped_tokens_escape()
This escapes strings so that they can be concatenated with a delimiter
and without loss tokenized with nm_utils_escaped_tokens_split().

Note that this is similar to _nm_utils_escape_plain() and
_nm_utils_escape_spaces(). The difference is that
nm_utils_escaped_tokens_escape() also escapes the last trailing
whitespace. That means, if delimiters contains all NM_ASCII_SPACES, then
it is identical to _nm_utils_escape_spaces(). Otherwise, the trailing
space is treated specially. That is, because
nm_utils_escaped_tokens_split() uses NM_UTILS_STRSPLIT_SET_FLAGS_STRSTRIP,
to strip leading and trailing whitespace. To still express a trailing
whitespace, the last whitespace must be escaped. Note that
NM_UTILS_STRSPLIT_SET_FLAGS_STRSTRIP also honors escaping any whitespace
(not only at the last position), but when escaping we don't need to
escape them, because unescaped (non-trailing) whitespace are taken just
fine.

The pair nm_utils_escaped_tokens_split() and
nm_utils_escaped_tokens_escape() are proposed as default way of
tokenizing a list of items. For example, with

  $ nmcli connection modify "$PROFILE" +ipv4.routing-rules 'priority 5 from 192.168.7.5/32 table 5, priority 6 iif a\, from 192.168.7.5/32 table 6'

Here we implement a to/from string function to handle one item
(nm_ip_routing_rule_{from,to}_string()). When such elements are combined with ',',
then we need to support an additional layer of escaping on top of that.
The advantage is that the indvidual to/from string functions are agnostic
to this second layer of escaping/tokenizing that nmcli employs to handle
a list of these items.
The disadvantage is that we possibly get multiple layers of backslash
escapings. That is only mitigated by the fact that nm_utils_escaped_tokens_*()
supports a syntax for which *most* characters don't need any special escaping.
Only delimiters, backslash, and the trailing space needs escaping, and
these are cases are expected to be few.
2019-04-17 11:11:21 +02:00
Thomas Haller
9522aaf226 shared: add NM_UTILS_STRSPLIT_SET_FLAGS_ESCAPED to nm_utils_strsplit_set_full()
Add a new flag that will remove escape characters after splitting
the string.

This implements a special kind of backslash escaping. It's not C escape
sequences (like '\n' or '\020'), but simply to take the special character
following the backslash verbatim. Note that the backslash is only
considered special, if it's followed by a delimiter, another backslash,
or a whitespace (in combination with %NM_UTILS_STRSPLIT_SET_FLAGS_STRSTRIP).

The main purpose of this form of escaping is nmcli's list options, like

  $ nmcli connection modify "$PROFILE" +ipv4.routing-rules 'priority 5 from 192.168.7.5/32 table 5, priority 6 iif a\, from 192.168.7.5/32 table 6'

It's a contrieved example, but the list options are a list of IP
addresses, rules, etc. They implement their own syntax for one element,
and are concatenated by ','. To support that one element may have
arbitrary characters (including the delimiter and whitespaces), nmcli
employs a tokenization with this special kind of escaping.
2019-04-17 11:11:21 +02:00
Thomas Haller
5b2b0dcadf shared: add NM_UTILS_STRSPLIT_SET_FLAGS_STRSTRIP to nm_utils_strsplit_set_full()
This will essentially call g_strstrip() on each token.

There are some specialties:

 - if the resulting word is empty after stripping, then according to
   %NM_UTILS_STRSPLIT_SET_FLAGS_PRESERVE_EMPTY, the empty token will be
   removed. If that results in an empty string array, %NULL will be
   returned.

 - if %NM_UTILS_STRSPLIT_SET_FLAGS_ALLOW_ESCAPING is set, then
   whitespace that is backslash escaped is not removed.

Since this is a post-operation that happens after tokeninzing, it
could be done as a separate function. And we already have this function:
_nm_utils_unescape_plain() and _nm_utils_unescape_spaces().
However, that is ugly for several reasons:

 - the stripping should be part of the tokenizing, you shouldn't need
   several steps.

 - nm_utils_strsplit_set_full() returns a "const char **" which
   indicates the strings must not be freed. However, it is perfectly
   valid to modify the string inplace. Hence, the post-op function
   would need to cast the strings to "char *", which seems ugly
   (although we do that on many places, and it's guaranteed to work).

 - _nm_utils_unescape_plain()/_nm_utils_unescape_spaces() is indeed
   already used together with nm_utils_strsplit_set_full(). However,
   it requires to initialize the cb_lookup buffer twice. I would expect
   that initializing the cb_lookup buffer is a large portion of what
   the function does already (for short strings).
   This issue will be solved in the next commit by adding yet another flag
   which allows to unescape.
2019-04-17 11:10:11 +02:00
Lubomir Rintel
5801f89f4d all: goodbye libnm-glib
This removes libnm-glib, libnm-glib-vpn, and libnm-util for good.
The it has been replaced with libnm since NetworkManager 1.0, disabled
by default since 1.12 and no up-to-date distributions ship it for years
now.

Removing the libraries allows us to:

* Remove the horrible hacks that were in place to deal with accidental use
  of both the new and old library in a single process.
* Relief the translators of maintenance burden of similar yet different
  strings.
* Get rid of known bad code without chances of ever getting fixed
  (libnm-glib/nm-object.c and libnm-glib/nm-object-cache.c)
* Generally lower the footprint of the releases and our workspace

If there are some really really legacy users; they can just build
libnm-glib and friends from the NetworkManager-1.16 distribution. The
D-Bus API is stable and old libnm-glib will keep working forever.

https://github.com/NetworkManager/NetworkManager/pull/308
2019-04-16 15:52:27 +02:00
Thomas Haller
f28a618889 shared: add nm_auto_clear_variant_builder 2019-04-12 11:07:25 +02:00
Thomas Haller
a1425a4c91 shared: pre-calculate number of tokens in nm_utils_strsplit_set_full()
Instead of growing the buffer for the tokens (and reallocating),
do one pre-run over the string and count the delimiters. This
way we know how much space we need and we don't need to
reallocate.

Interestingly, this is notably slower than the previous implementation,
because previously if would not bother determining the right number of
tokens but just over-allocate with a reasonable guess of 8 and grow the
buffer exponentially. Still, I like this better because while it may
be slower in common scenarios, it allocates the exact number of buffer
space.
2019-04-10 15:05:57 +02:00
Thomas Haller
5c1f93943e shared: add NM_UTILS_STRSPLIT_SET_FLAGS_PRESERVE_EMPTY flag for nm_utils_strsplit_set_full()
Previously, nm_utils_strsplit_set_full() would always remove empty
tokens. Add a flag NM_UTILS_STRSPLIT_SET_FLAGS_PRESERVE_EMPTY to avoid
that.

This makes nm_utils_strsplit_set_full() return the same result as
g_strsplit_set() and a direct replacement for it -- except for "",
where we return %NULL.
2019-04-10 15:05:57 +02:00
Thomas Haller
453b3ea362 shared: refactor allowed-escaped handling in nm_utils_strsplit_set_full()
Drop the next_char() and is_delimiter() macros. They are difficult to
understand, because they both have a state-variable (escaped).
Instead, the state of whether we handle an escape or not, shall only
depend on the current line of code.
2019-04-10 15:05:57 +02:00
Thomas Haller
e2217a26e7 shared: refactor lookup of delimiter tables in nm_utils_strsplit_set_full() 2019-04-10 15:05:57 +02:00
Thomas Haller
ce456f5b77 all: don't accept %NULL as delimiters for nm_utils_strsplit_set()
The caller should make a conscious decision which delimiters to use.
Unfortunately, there is a variety of different demiters in use. This
should be unitfied and the callers should use one of a few specific
set of delimiters.

This could be unified by (re)using a define as delimiters, like

   strv = nm_utils_strsplit_set_full (value, MULTILIST_WITH_ESCAPE_CHARS, NM_UTILS_STRSPLIT_SET_FLAGS_ALLOW_ESCAPING);

where MULTILIST_WITH_ESCAPE_CHARS has a particular meaning that should
be reused for similar uses.

However, leaving the delimiter at NULL is not good because it's unclear who
wants that default behavior (and what the default should be). Don't allow that.

There are almost no callers that relied on this default anyway.
2019-04-10 15:05:57 +02:00
Thomas Haller
84f2037648 shared: add flags argument to nm_utils_strsplit_set()
It will be useful to extend nm_utils_strsplit_set() with various
flavors and subtly different behaviors. Add a flags argument to
support these.
2019-04-10 15:05:57 +02:00
Thomas Haller
df89f57c1d shared: cleanup _nm_utils_ascii_str_to_bool()
Previously, this would re-implement what nm_strstrip_avoid_copy()
was doing.

Use nm_strstrip_avoid_copy_a() instead, which avoids the code
duplication and the heap allocation in common cases.
2019-04-04 21:01:15 +02:00
Thomas Haller
2861a7ae0a shared: add nm_strstrip_avoid_copy_a() helper 2019-04-04 21:01:15 +02:00
Thomas Haller
26d144d5a0 shared: move nm_memdup(), nm_strndup_a(), and nm_strdup_int() to "nm-macros-internal.h"
The main difference between "shared/nm-utils/nm-macros-internal.h" and
"shared/nm-utils/nm-shared-utils.h" is that the former is header-only
while the latter has a source file as well.

Apart from that, both headers are included everywhere.

The next commit will add nm_strstrip_avoid_copy_a() to
"nm-macros-internal.h" header, which will use nm_strndup_a().
Hence, also nm_strndup_a() should be in "nm-macros-internal.h".
2019-04-04 21:01:15 +02:00
Thomas Haller
08c327f8a6 shared/trivial: rename variables in nm_strndup_a() and nm_str_skip_leading_spaces() macros
"_str" is a very tempting name for a temporary variable.

Rename the variable in nm_strndup_a() macro, so that other macros can
call these (more general) macros (and still use the name "_str").
2019-04-04 21:01:15 +02:00
Thomas Haller
f63594c26b shared: add nm_auto_unref_gmainloop cleanup macro 2019-04-04 09:56:19 +02:00
Lubomir Rintel
b027723e00 Revert "all: goodbye libnm-glib"
We need this for a little little longer :(

This reverts commit 1de8383ad9.
2019-04-03 08:52:38 +02:00
Thomas Haller
61aad8cda4 shared: better implement compat version of explicit_bzero()
If we don't have explicit_bzero(), try a bit harder and use
a volatile pointer.

This is also what libsecret's egg_secure_clear() does [1]. However, for
us this is less important, because commonly we expect glibc to provide
a useable explicit_bzero().

[1] b5442654d4/egg/egg-secure-memory.c (L1352)
2019-04-02 19:30:33 +02:00
Thomas Haller
e96643a0b6 shared: add nm_utils_str_simpletokens_extract_next() 2019-03-27 16:23:30 +01:00
Thomas Haller
0ccb25c5fa shared: add nm_ip_addr_is_null() helper 2019-03-27 16:23:30 +01:00
Thomas Haller
da5895951a shared: add _nm_utils_escape_plain() API 2019-03-25 09:12:33 +01:00
Thomas Haller
e26e0fdffd all: consider all ASCII spaces for _nm_utils_escape_spaces() and unescape 2019-03-25 09:12:33 +01:00
Thomas Haller
5b8305c27c shared: add NM_ASCII_SPACES macro 2019-03-25 09:12:33 +01:00
Thomas Haller
e3fa570c1b shared: add "strip" argument to _nm_utils_unescape_spaces()
It's usually not necessary, because _nm_utils_unescape_spaces()
gets called after nm_utils_strsplit_set(), which already removes
the non-escaped spaces.

Still, for completeness, this should be here. Also, because with
this the function is useful for individual options (not delimiter
separate list values), to support automatically dropping leading or
trailing whitespace, but also support escaping them.
2019-03-25 09:12:33 +01:00