Skip to content

Commit d22537d

Browse files
committed
builder-manifest: Prevent writing duplicate groups in platform metadata
Previously, builder_manifest_create_platform_base() copied the SDK metadata to metadata.platform and builder_manifest_finish_platform() later appended the platform's built-extensions to it. If the SDK metadata already contained a Build group with built-extensions, this would resulted in duplicate groups and keys in the final platform metadata. Fix this by loading both metadata with GKeyFile and merging the build-extensions entries and deduplicated string values. Fixes the second issue in: #618 (comment)
1 parent 5fff033 commit d22537d

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

src/builder-manifest.c

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,26 +3523,42 @@ builder_manifest_finish (BuilderManifest *self,
35233523

35243524
if (sub_ids->len > 0)
35253525
{
3526-
g_autoptr(GFile) metadata_file = NULL;
3527-
g_autoptr(GFileOutputStream) output = NULL;
3528-
g_autoptr(GString) extension_contents = g_string_new ("\n"
3529-
"[Build]\n");
3526+
g_autoptr(GFile) metadata_file = g_file_get_child (app_dir, "metadata");
3527+
g_autoptr(GKeyFile) keyfile = g_key_file_new ();
3528+
g_auto(GStrv) old_subs = NULL;
3529+
g_autoptr(GPtrArray) all_subs = g_ptr_array_new ();
3530+
gsize j;
3531+
3532+
if (!g_key_file_load_from_file (keyfile,
3533+
flatpak_file_get_path_cached (metadata_file),
3534+
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
3535+
error))
3536+
return FALSE;
3537+
3538+
old_subs = g_key_file_get_string_list (keyfile,
3539+
FLATPAK_METADATA_GROUP_BUILD,
3540+
FLATPAK_METADATA_KEY_BUILD_EXTENSIONS,
3541+
NULL, NULL);
3542+
3543+
for (j = 0; old_subs != NULL && old_subs[j] != NULL; j++)
3544+
g_ptr_array_add (all_subs, old_subs[j]);
35303545

3531-
g_string_append (extension_contents, FLATPAK_METADATA_KEY_BUILD_EXTENSIONS"=");
3532-
for (i = 0; i < sub_ids->len; i++)
3546+
for (j = 0; j < sub_ids->len; j++)
35333547
{
3534-
g_string_append (extension_contents, (const char *)sub_ids->pdata[i]);
3535-
g_string_append (extension_contents, ";");
3548+
if (old_subs == NULL ||
3549+
!g_strv_contains ((const char * const *) old_subs, sub_ids->pdata[j]))
3550+
g_ptr_array_add (all_subs, sub_ids->pdata[j]);
35363551
}
35373552

3538-
metadata_file = g_file_get_child (app_dir, "metadata");
3539-
output = g_file_append_to (metadata_file, G_FILE_CREATE_NONE, NULL, error);
3540-
if (output == NULL)
3541-
return FALSE;
3553+
g_key_file_set_string_list (keyfile,
3554+
FLATPAK_METADATA_GROUP_BUILD,
3555+
FLATPAK_METADATA_KEY_BUILD_EXTENSIONS,
3556+
(const char * const *) all_subs->pdata,
3557+
all_subs->len);
35423558

3543-
if (!g_output_stream_write_all (G_OUTPUT_STREAM (output),
3544-
extension_contents->str, extension_contents->len,
3545-
NULL, NULL, error))
3559+
if (!g_key_file_save_to_file (keyfile,
3560+
flatpak_file_get_path_cached (metadata_file),
3561+
error))
35463562
return FALSE;
35473563
}
35483564

@@ -3965,27 +3981,45 @@ builder_manifest_finish_platform (BuilderManifest *self,
39653981

39663982
if (sub_ids->len > 0)
39673983
{
3968-
g_autoptr(GFile) metadata_file = NULL;
3969-
g_autoptr(GFileOutputStream) output = NULL;
3970-
g_autoptr(GString) extension_contents = g_string_new ("\n"
3971-
"[Build]\n");
3984+
g_autoptr(GFile) metadata_file = g_file_get_child (app_dir, "metadata.platform");
3985+
g_autoptr(GKeyFile) keyfile = g_key_file_new ();
3986+
g_auto(GStrv) old_subs = NULL;
3987+
g_autoptr(GPtrArray) all_subs = g_ptr_array_new ();
3988+
gsize j;
39723989

3973-
g_string_append (extension_contents, FLATPAK_METADATA_KEY_BUILD_EXTENSIONS"=");
3974-
for (i = 0; i < sub_ids->len; i++)
3975-
{
3976-
g_string_append (extension_contents, (const char *)sub_ids->pdata[i]);
3977-
g_string_append (extension_contents, ";");
3978-
}
3979-
metadata_file = g_file_get_child (app_dir, "metadata.platform");
39803990
if (!flatpak_break_hardlink (metadata_file, error))
39813991
return FALSE;
3982-
output = g_file_append_to (metadata_file, G_FILE_CREATE_NONE, NULL, error);
3983-
if (output == NULL)
3992+
3993+
if (!g_key_file_load_from_file (keyfile,
3994+
flatpak_file_get_path_cached (metadata_file),
3995+
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
3996+
error))
39843997
return FALSE;
39853998

3986-
if (!g_output_stream_write_all (G_OUTPUT_STREAM (output),
3987-
extension_contents->str, extension_contents->len,
3988-
NULL, NULL, error))
3999+
old_subs = g_key_file_get_string_list (keyfile,
4000+
FLATPAK_METADATA_GROUP_BUILD,
4001+
FLATPAK_METADATA_KEY_BUILD_EXTENSIONS,
4002+
NULL, NULL);
4003+
4004+
for (j = 0; old_subs != NULL && old_subs[j] != NULL; j++)
4005+
g_ptr_array_add (all_subs, old_subs[j]);
4006+
4007+
for (j = 0; j < sub_ids->len; j++)
4008+
{
4009+
if (old_subs == NULL ||
4010+
!g_strv_contains ((const char * const *) old_subs, sub_ids->pdata[j]))
4011+
g_ptr_array_add (all_subs, sub_ids->pdata[j]);
4012+
}
4013+
4014+
g_key_file_set_string_list (keyfile,
4015+
FLATPAK_METADATA_GROUP_BUILD,
4016+
FLATPAK_METADATA_KEY_BUILD_EXTENSIONS,
4017+
(const char * const *) all_subs->pdata,
4018+
all_subs->len);
4019+
4020+
if (!g_key_file_save_to_file (keyfile,
4021+
flatpak_file_get_path_cached (metadata_file),
4022+
error))
39894023
return FALSE;
39904024
}
39914025

0 commit comments

Comments
 (0)