lib/core: Add ostree_validate_remote_name() for remote names

There are a few places in the code where ad-hoc validation was being
performed. Might as well formalise it a bit more.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Closes: #948
Approved by: cgwalters
This commit is contained in:
Philip Withnall authored and Atomic Bot committed 2017-06-20 21:52:22 +00:00
1 parent fd50c97322
commit 20dc9454b3
6 files changed
+62 -6

No files matched your search

+3 -1
View File
@@ -19,7 +19,9 @@
/* Add new symbols here. Release commits should copy this section into -released.sym. */
LIBOSTREE_2017.8 {
};
global:
ostree_validate_remote_name;
} LIBOSTREE_2017.7;
/* Stub section for the stable release *after* this development one; don't
* edit this other than to update the last number. This is just a copy/paste
+31 -1
View File
@@ -101,6 +101,7 @@ ostree_validate_checksum_string (const char *sha256,
#define OSTREE_REF_FRAGMENT_REGEXP "[-._\\w\\d]+"
#define OSTREE_REF_REGEXP "(?:" OSTREE_REF_FRAGMENT_REGEXP "/)*" OSTREE_REF_FRAGMENT_REGEXP
#define OSTREE_REMOTE_NAME_REGEXP OSTREE_REF_FRAGMENT_REGEXP
/**
* ostree_parse_refspec:
@@ -125,7 +126,7 @@ ostree_parse_refspec (const char *refspec,
static gsize regex_initialized;
if (g_once_init_enter (&regex_initialized))
{
regex = g_regex_new ("^(" OSTREE_REF_FRAGMENT_REGEXP ":)?(" OSTREE_REF_REGEXP ")$", 0, 0, NULL);
regex = g_regex_new ("^(" OSTREE_REMOTE_NAME_REGEXP ":)?(" OSTREE_REF_REGEXP ")$", 0, 0, NULL);
g_assert (regex);
g_once_init_leave (&regex_initialized, 1);
}
@@ -180,6 +181,35 @@ ostree_validate_rev (const char *rev,
return TRUE;
}
/**
* ostree_validate_remote_name:
* @remote_name: A remote name
* @error: Error
*
* Returns: %TRUE if @remote_name is a valid remote name
* Since: 2017.7
*/
gboolean
ostree_validate_remote_name (const char *remote_name,
GError **error)
{
g_autoptr(GMatchInfo) match = NULL;
static gsize regex_initialized;
static GRegex *regex;
if (g_once_init_enter (&regex_initialized))
{
regex = g_regex_new ("^" OSTREE_REMOTE_NAME_REGEXP "$", 0, 0, NULL);
g_assert (regex);
g_once_init_leave (&regex_initialized, 1);
}
if (!g_regex_match (regex, remote_name, 0, &match))
return glnx_throw (error, "Invalid remote name %s", remote_name);
return TRUE;
}
GVariant *
_ostree_file_header_new (GFileInfo *file_info,
GVariant *xattrs)
+3
View File
@@ -242,6 +242,9 @@ int ostree_cmp_checksum_bytes (const guchar *a, const guchar *b);
_OSTREE_PUBLIC
gboolean ostree_validate_rev (const char *rev, GError **error);
_OSTREE_PUBLIC
gboolean ostree_validate_remote_name (const char *remote_name, GError **error);
_OSTREE_PUBLIC
gboolean ostree_parse_refspec (const char *refspec,
char **out_remote,
+4 -4
View File
@@ -876,8 +876,8 @@ impl_repo_remote_add (OstreeRepo *self,
g_return_val_if_fail (url != NULL, FALSE);
g_return_val_if_fail (options == NULL || g_variant_is_of_type (options, G_VARIANT_TYPE ("a{sv}")), FALSE);
if (strchr (name, '/') != NULL)
return glnx_throw (error, "Invalid character '/' in remote name: %s", name);
if (!ostree_validate_remote_name (name, error))
return FALSE;
g_autoptr(OstreeRemote) remote = _ostree_repo_get_remote (self, name, NULL);
if (remote != NULL && if_not_exists)
@@ -1005,8 +1005,8 @@ impl_repo_remote_delete (OstreeRepo *self,
{
g_return_val_if_fail (name != NULL, FALSE);
if (strchr (name, '/') != NULL)
return glnx_throw (error, "Invalid character '/' in remote name: %s", name);
if (!ostree_validate_remote_name (name, error))
return FALSE;
g_autoptr(OstreeRemote) remote = NULL;
if (if_exists)