-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace remote change op #1166
Replace remote change op #1166
Changes from all commits
64d0ac0
40cf3c5
4d0a43e
d010d5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1735,6 +1735,88 @@ ostree_repo_remote_delete (OstreeRepo *self, | |
return impl_repo_remote_delete (self, NULL, FALSE, name, cancellable, error); | ||
} | ||
|
||
|
||
static gboolean | ||
impl_repo_remote_replace (OstreeRepo *self, | ||
GFile *sysroot, | ||
const char *name, | ||
const char *url, | ||
GVariant *options, | ||
GCancellable *cancellable, | ||
GError **error) | ||
{ | ||
g_return_val_if_fail (name != NULL, FALSE); | ||
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 (!ostree_validate_remote_name (name, error)) | ||
return FALSE; | ||
|
||
g_autoptr(GError) local_error = NULL; | ||
g_autoptr(OstreeRemote) remote = _ostree_repo_get_remote (self, name, &local_error); | ||
if (remote == NULL) | ||
{ | ||
if (!g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) | ||
{ | ||
g_propagate_error (error, g_steal_pointer (&local_error)); | ||
return FALSE; | ||
} | ||
g_clear_error (&local_error); | ||
if (!impl_repo_remote_add (self, sysroot, FALSE, name, url, options, | ||
cancellable, error)) | ||
return FALSE; | ||
} | ||
else | ||
{ | ||
/* Replace the entire option group */ | ||
if (!g_key_file_remove_group (remote->options, remote->group, error)) | ||
return FALSE; | ||
|
||
if (g_str_has_prefix (url, "metalink=")) | ||
g_key_file_set_string (remote->options, remote->group, "metalink", | ||
url + strlen ("metalink=")); | ||
else | ||
g_key_file_set_string (remote->options, remote->group, "url", url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, at this point we should extract this to a helper I'd say. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I didn't spend a lot of time thinking about refactoring things. I mostly wanted to get the feature and tests in place to see what you thought. But I'll look through again and see if there are clear winners. |
||
|
||
if (options != NULL) | ||
keyfile_set_from_vardict (remote->options, remote->group, options); | ||
|
||
/* Write out updated settings */ | ||
if (remote->file != NULL) | ||
{ | ||
gsize length; | ||
g_autofree char *data = g_key_file_to_data (remote->options, &length, | ||
NULL); | ||
|
||
if (!g_file_replace_contents (remote->file, data, length, | ||
NULL, FALSE, 0, NULL, | ||
cancellable, error)) | ||
return FALSE; | ||
} | ||
else | ||
{ | ||
g_autoptr(GKeyFile) config = ostree_repo_copy_config (self); | ||
|
||
/* Remove the existing group if it exists */ | ||
if (!g_key_file_remove_group (config, remote->group, &local_error)) | ||
{ | ||
if (!g_error_matches (local_error, G_KEY_FILE_ERROR, | ||
G_KEY_FILE_ERROR_GROUP_NOT_FOUND)) | ||
{ | ||
g_propagate_error (error, g_steal_pointer (&local_error)); | ||
return FALSE; | ||
} | ||
} | ||
|
||
ot_keyfile_copy_group (remote->options, config, remote->group); | ||
if (!ostree_repo_write_config (self, config, error)) | ||
return FALSE; | ||
} | ||
} | ||
|
||
return TRUE; | ||
} | ||
|
||
/** | ||
* ostree_repo_remote_change: | ||
* @self: Repo | ||
|
@@ -1776,6 +1858,9 @@ ostree_repo_remote_change (OstreeRepo *self, | |
case OSTREE_REPO_REMOTE_CHANGE_DELETE_IF_EXISTS: | ||
return impl_repo_remote_delete (self, sysroot, TRUE, name, | ||
cancellable, error); | ||
case OSTREE_REPO_REMOTE_CHANGE_REPLACE: | ||
return impl_repo_remote_replace (self, sysroot, name, url, options, | ||
cancellable, error); | ||
} | ||
g_assert_not_reached (); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g_clear_error (&local_error)
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I had that, but is it necessary here? It's declared with
g_autoptr
and never reused in this branch. But I can add it to be sure it doesn't leak.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but at this point the error is set - so we must clear it for the invocation of
impl_repo_remote_add()
right below, otherwise get could get the overwriting-error error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to
impl_repo_remote_add
useserror
, notlocal_error
.local_error
is only used to check if the repo exists. The caller'serror
is used when adding andlocal_error
will be cleared regardless of what happens there. I don't mind clearing the error, but I don't think it's really necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, you're totally right. That said, I still think it's best practice to clear checked errors right after checking them. Imagine if someone later comes and wants to add another checked call, sees there's already a
local_error
value and doesn't realize it may have been written earlier.