string-util: move version_is_valid() into generic code
While we are at it, replace the sloppy use of filename_is_valid() by the less sloppy filename_part_is_valid() (as added by the preceeding commit), since we don#t want to be too restrictive here. (After all, version strings invalid as standalone filenames might be valid as part of filenames, and hence we should allow them).
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "memstream-util.h"
|
||||
#include "path-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
@@ -1296,3 +1297,17 @@ char *startswith_strv(const char *string, char **strv) {
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
bool version_is_valid(const char *s) {
|
||||
if (isempty(s))
|
||||
return false;
|
||||
|
||||
if (!filename_part_is_valid(s))
|
||||
return false;
|
||||
|
||||
/* This is a superset of the characters used by semver. We additionally allow "," and "_". */
|
||||
if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -275,3 +275,5 @@ char *startswith_strv(const char *string, char **strv);
|
||||
|
||||
#define STARTSWITH_SET(p, ...) \
|
||||
startswith_strv(p, STRV_MAKE(__VA_ARGS__))
|
||||
|
||||
bool version_is_valid(const char *s);
|
||||
|
||||
@@ -15,8 +15,6 @@ systemd_sysupdate_sources = files(
|
||||
'sysupdate-transfer.h',
|
||||
'sysupdate-update-set.c',
|
||||
'sysupdate-update-set.h',
|
||||
'sysupdate-util.c',
|
||||
'sysupdate-util.h',
|
||||
'sysupdate.c',
|
||||
'sysupdate.h',
|
||||
)
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include "stdio-util.h"
|
||||
#include "string-util.h"
|
||||
#include "sysupdate-pattern.h"
|
||||
#include "sysupdate-util.h"
|
||||
|
||||
typedef enum PatternElementType {
|
||||
PATTERN_LITERAL,
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include "sysupdate-pattern.h"
|
||||
#include "sysupdate-resource.h"
|
||||
#include "sysupdate-transfer.h"
|
||||
#include "sysupdate-util.h"
|
||||
#include "sysupdate.h"
|
||||
#include "tmpfile-util.h"
|
||||
#include "web-util.h"
|
||||
|
||||
@@ -2,16 +2,3 @@
|
||||
|
||||
#include "path-util.h"
|
||||
#include "sysupdate-util.h"
|
||||
|
||||
bool version_is_valid(const char *s) {
|
||||
if (isempty(s))
|
||||
return false;
|
||||
|
||||
if (!filename_is_valid(s))
|
||||
return false;
|
||||
|
||||
if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool version_is_valid(const char *s);
|
||||
@@ -1264,4 +1264,14 @@ TEST(strstrafter) {
|
||||
assert_se(!strstrafter(buffer, "-"));
|
||||
}
|
||||
|
||||
TEST(version_is_valid) {
|
||||
assert_se(!version_is_valid(NULL));
|
||||
assert_se(!version_is_valid(""));
|
||||
assert_se(version_is_valid("0"));
|
||||
assert_se(version_is_valid("5"));
|
||||
assert_se(version_is_valid("999999"));
|
||||
assert_se(version_is_valid("999999.5"));
|
||||
assert_se(version_is_valid("6.2.12-300.fc38.x86_64"));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
||||
|
||||
Reference in New Issue
Block a user