compare: add two new operators "==" and "<>"

These two operators always indicate ordering comparisons, as opposed to
"=" and "!=" which depending on context mean literal string compares.

This is useful for ConditionOSRelease= for example, as this means
there's now always a way to do version compares.
This commit is contained in:
Lennart Poettering 2022-08-26 17:20:56 +02:00
parent 38c09fa008
commit 6061c86693
2 changed files with 35 additions and 29 deletions

View file

@ -1244,15 +1244,17 @@
<para><literal>device-tree-compatible(<replaceable>value</replaceable>)</literal> for systems with a device tree that is compatible to
<literal>value</literal>.</para>
<para><literal>smbios-field(<replaceable>field</replaceable> <replaceable>operator</replaceable> <replaceable>value</replaceable>)</literal>
for systems with a SMBIOS field containing a certain value.
<literal>field</literal> is the name of the SMBIOS field exposed as <literal>sysfs</literal> attribute file
below <filename>/sys/class/dmi/id/</filename>.
<para><literal>smbios-field(<replaceable>field</replaceable> <replaceable>operator</replaceable>
<replaceable>value</replaceable>)</literal> for systems with a SMBIOS field containing a certain
value. <literal>field</literal> is the name of the SMBIOS field exposed as
<literal>sysfs</literal> attribute file below <filename>/sys/class/dmi/id/</filename>.
<literal>operator</literal> is one of <literal>&lt;</literal>, <literal>&lt;=</literal>,
<literal>&gt;=</literal>, <literal>&gt;</literal>, <literal>=</literal>, <literal>!=</literal> for version
comparison, or <literal>=$</literal>, <literal>!=$</literal> for string comparison.
<literal>value</literal> is the expected value of the SMBIOS field (shell-style globs are possible if
<literal>=$</literal> or<literal>!=$</literal> is used).</para>
<literal>&gt;=</literal>, <literal>&gt;</literal>, <literal>==</literal>,
<literal>&lt;&gt;</literal> for version comparison, <literal>=</literal> and <literal>!=</literal>
for literal string comparison, or <literal>=$</literal>, <literal>!=$</literal> for shell-style
glob comparison. <literal>value</literal> is the expected value of the SMBIOS field value
(possibly containing shell style globs in case <literal>=$</literal>/<literal>!=$</literal> is
used).</para>
</listitem>
</varlistentry>
@ -1333,9 +1335,10 @@
version (as reported by <command>uname -r</command>) matches a certain expression (or if prefixed
with the exclamation mark does not match it). The argument must be a list of (potentially quoted)
expressions. For each of the expressions, if it starts with one of <literal>&lt;</literal>,
<literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>, <literal>&gt;=</literal>,
<literal>&gt;</literal> a relative version comparison is done, otherwise the specified string is
matched with shell-style globs.</para>
<literal>&lt;=</literal>, <literal>=</literal> (or <literal>==</literal>), <literal>!=</literal>
(or <literal>&lt;&gt;</literal>), <literal>&gt;=</literal>, <literal>&gt;</literal> a relative
version comparison is done, otherwise the specified string is matched with shell-style
globs.</para>
<para>Note that using the kernel version string is an unreliable way to determine which features
are supported by a kernel, because of the widespread practice of backporting drivers, features, and
@ -1605,10 +1608,11 @@
<listitem><para>Verify that the specified amount of system memory is available to the current
system. Takes a memory size in bytes as argument, optionally prefixed with a comparison operator
<literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>,
<literal>&gt;=</literal>, <literal>&gt;</literal>. On bare-metal systems compares the amount of
physical memory in the system with the specified size, adhering to the specified comparison
operator. In containers compares the amount of memory assigned to the container instead.</para>
<literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal> (or <literal>==</literal>),
<literal>!=</literal> (or <literal>&lt;&gt;</literal>), <literal>&gt;=</literal>,
<literal>&gt;</literal>. On bare-metal systems compares the amount of physical memory in the system
with the specified size, adhering to the specified comparison operator. In containers compares the
amount of memory assigned to the container instead.</para>
</listitem>
</varlistentry>
@ -1617,13 +1621,14 @@
<listitem><para>Verify that the specified number of CPUs is available to the current system. Takes
a number of CPUs as argument, optionally prefixed with a comparison operator
<literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>,
<literal>&gt;=</literal>, <literal>&gt;</literal>. Compares the number of CPUs in the CPU affinity
mask configured of the service manager itself with the specified number, adhering to the specified
comparison operator. On physical systems the number of CPUs in the affinity mask of the service
manager usually matches the number of physical CPUs, but in special and virtual environments might
differ. In particular, in containers the affinity mask usually matches the number of CPUs assigned
to the container and not the physically available ones.</para></listitem>
<literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal> (or <literal>==</literal>),
<literal>!=</literal> (or <literal>&lt;&gt;</literal>), <literal>&gt;=</literal>,
<literal>&gt;</literal>. Compares the number of CPUs in the CPU affinity mask configured of the
service manager itself with the specified number, adhering to the specified comparison operator. On
physical systems the number of CPUs in the affinity mask of the service manager usually matches the
number of physical CPUs, but in special and virtual environments might differ. In particular, in
containers the affinity mask usually matches the number of CPUs assigned to the container and not
the physically available ones.</para></listitem>
</varlistentry>
<varlistentry>
@ -1694,13 +1699,12 @@
<listitem><para>Verify that a specific <literal>key=value</literal> pair is set in the host's
<citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
<para>Other than exact string matching with <literal>=</literal>, and <literal>!=</literal>,
relative comparisons are supported for versioned parameters (e.g. <literal>VERSION_ID</literal>),
and shell-style wildcard comparisons (<literal>*</literal>, <literal>?</literal>,
<literal>[]</literal>) are supported with the <literal>=$</literal> (match) and
<literal>!=$</literal> (non-match). The comparator can be one of <literal>&lt;</literal>,
<literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>, <literal>&gt;=</literal>,
<literal>&gt;</literal>, <literal>=$</literal> and <literal>!=$</literal>.</para>
<para>Other than exact string matching (with <literal>=</literal> and <literal>!=</literal>),
relative comparisons are supported for versioned parameters (e.g. <literal>VERSION_ID</literal>;
with <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>==</literal>,
<literal>&lt;&gt;</literal>, <literal>&gt;=</literal>, <literal>&gt;</literal>), and shell-style
wildcard comparisons (<literal>*</literal>, <literal>?</literal>, <literal>[]</literal>) are
supported with the <literal>=$</literal> (match) and <literal>!=$</literal> (non-match).</para>
</listitem>
</varlistentry>

View file

@ -15,10 +15,12 @@ CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags
{ COMPARE_FNMATCH_EQUAL, "=$", .valid_mask = COMPARE_ALLOW_FNMATCH },
{ COMPARE_FNMATCH_UNEQUAL, "!=$", .valid_mask = COMPARE_ALLOW_FNMATCH },
{ COMPARE_UNEQUAL, "<>" },
{ COMPARE_LOWER_OR_EQUAL, "<=" },
{ COMPARE_GREATER_OR_EQUAL, ">=" },
{ COMPARE_LOWER, "<" },
{ COMPARE_GREATER, ">" },
{ COMPARE_EQUAL, "==" },
{ COMPARE_STRING_EQUAL, "=", .need_mask = COMPARE_EQUAL_BY_STRING },
{ COMPARE_EQUAL, "=" },
{ COMPARE_STRING_UNEQUAL, "!=", .need_mask = COMPARE_EQUAL_BY_STRING },