dir: Add basic OS info to Flatpak-Os-Info header when pulling objects

Fixes https://github.com/flatpak/flatpak/issues/5549
This commit is contained in:
bbhtt 2025-10-09 18:27:13 +05:30 committed by Sebastian Wick
parent 8a21f2dfe1
commit 0bf531f44a
5 changed files with 122 additions and 3 deletions

View File

@ -177,6 +177,21 @@ parse_lang (const char *value, GError **error)
return g_strdup (value);
}
static char *
parse_boolean (const char *value,
GError **error)
{
if (g_strcmp0 (value, "true") == 0)
return g_strdup ("true");
else if (g_strcmp0 (value, "false") == 0)
return g_strdup ("false");
else
{
flatpak_fail (error, _("'%s' is not a valid value (use 'true' or 'false')"), value);
return NULL;
}
}
static char *
print_locale (const char *value)
{
@ -191,6 +206,20 @@ print_lang (const char *value)
return g_strdup (value);
}
static char *
print_boolean (const char *value)
{
if (!value)
return g_strdup ("*unset*");
if (g_strcmp0 (value, "true") == 0)
return g_strdup ("true");
else if (g_strcmp0 (value, "false") == 0)
return g_strdup ("false");
else
return g_strdup ("*invalid*");
}
static char *
get_lang_default (FlatpakDir *dir)
{
@ -199,6 +228,12 @@ get_lang_default (FlatpakDir *dir)
return g_strjoinv (";", langs);
}
static char *
get_report_os_info_default (FlatpakDir *dir)
{
return g_strdup ("true");
}
typedef struct
{
const char *name;
@ -210,6 +245,7 @@ typedef struct
ConfigKey keys[] = {
{ "languages", parse_lang, print_lang, get_lang_default },
{ "extra-languages", parse_locale, print_locale, NULL },
{ "report-os-info", parse_boolean, print_boolean, get_report_os_info_default },
};
static ConfigKey *

View File

@ -2444,6 +2444,24 @@ flatpak_ensure_user_cache_dir_location (GError **error)
return g_steal_pointer (&cache_dir);
}
static char *
flatpak_dir_get_os_info (FlatpakDir *self)
{
g_autofree char *os_report_config = NULL;
g_autofree char *os_id = NULL;
g_autofree char *os_version = NULL;
os_report_config = flatpak_dir_get_config (self, "report-os-info", NULL);
if (g_strcmp0 (os_report_config, "false") == 0)
return NULL;
os_id = flatpak_get_os_release_id ();
os_version = flatpak_get_os_release_version_id ();
return g_strdup_printf ("%s;%s;%s", os_id, os_version, flatpak_get_arch ());
}
static GFile *
flatpak_dir_get_oci_cache_file (FlatpakDir *self,
const char *remote,
@ -5815,7 +5833,8 @@ get_common_pull_options (GVariantBuilder *builder,
const char *current_local_checksum,
gboolean force_disable_deltas,
OstreeRepoPullFlags flags,
FlatpakProgress *progress)
FlatpakProgress *progress,
FlatpakDir *dir)
{
guint32 update_interval = 0;
GVariantBuilder hdr_builder;
@ -5857,6 +5876,13 @@ get_common_pull_options (GVariantBuilder *builder,
}
if (current_local_checksum)
g_variant_builder_add (&hdr_builder, "(ss)", "Flatpak-Upgrade-From", current_local_checksum);
{
g_autofree char *os_info = flatpak_dir_get_os_info (dir);
if (os_info)
g_variant_builder_add (&hdr_builder, "(ss)", "Flatpak-Os-Info", os_info);
}
g_variant_builder_add (builder, "{s@v}", "http-headers",
g_variant_new_variant (g_variant_builder_end (&hdr_builder)));
g_variant_builder_add (builder, "{s@v}", "append-user-agent",
@ -5895,6 +5921,7 @@ repo_pull (OstreeRepo *self,
FlatpakPullFlags flatpak_flags,
OstreeRepoPullFlags flags,
FlatpakProgress *progress,
FlatpakDir *dir,
GCancellable *cancellable,
GError **error)
{
@ -5930,7 +5957,7 @@ repo_pull (OstreeRepo *self,
/* Pull options */
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
get_common_pull_options (&builder, state, ref_to_fetch, token, dirs_to_pull, current_checksum,
force_disable_deltas, flags, progress);
force_disable_deltas, flags, progress, dir);
if (sideload_repo)
{
@ -6544,7 +6571,7 @@ flatpak_dir_pull (FlatpakDir *self,
if (!repo_pull (repo, state,
subdirs_arg ? (const char **) subdirs_arg->pdata : NULL,
ref, rev, sideload_repo, token, flatpak_flags, flags,
progress,
progress, self,
cancellable, error))
{
g_prefix_error (error, _("While pulling %s from remote %s: "), ref, state->remote_name);

View File

@ -77,6 +77,8 @@ gint flatpak_strcmp0_ptr (gconstpointer a,
/* Sometimes this is /var/run which is a symlink, causing weird issues when we pass
* it as a path into the sandbox */
char * flatpak_get_real_xdg_runtime_dir (void);
char * flatpak_get_os_release_id (void);
char * flatpak_get_os_release_version_id (void);
gboolean flatpak_has_path_prefix (const char *str,
const char *prefix);

View File

@ -469,6 +469,50 @@ flatpak_get_arches (void)
return (const char **) arches;
}
static char *
get_os_release_value (const char *key,
const char *default_value)
{
const char *file = "/etc/os-release";
g_autofree char *contents = NULL;
g_autoptr(GKeyFile) keyfile = g_key_file_new ();
g_autoptr(GString) str = NULL;
g_autofree char *value = NULL;
g_autofree char *unquoted = NULL;
if (!g_file_test (file, G_FILE_TEST_EXISTS))
file = "/usr/lib/os-release";
if (!g_file_get_contents (file, &contents, NULL, NULL))
return g_strdup (default_value);
str = g_string_new (contents);
g_string_prepend (str, "[os-release]\n");
if (!g_key_file_load_from_data (keyfile, str->str, -1, G_KEY_FILE_NONE, NULL))
return g_strdup (default_value);
value = flatpak_keyfile_get_string_non_empty (keyfile, "os-release", key);
unquoted = value ? g_shell_unquote (value, NULL) : NULL;
if (!unquoted)
return g_strdup (default_value);
return g_steal_pointer (&unquoted);
}
char *
flatpak_get_os_release_id (void)
{
return get_os_release_value ("ID", "linux");
}
char *
flatpak_get_os_release_version_id (void)
{
return get_os_release_value ("VERSION_ID", "unknown");
}
const char **
flatpak_get_gl_drivers (void)
{

View File

@ -76,6 +76,16 @@
(for example, <literal>en;en_DK;zh_HK.big5hkscs;uz_UZ.utf8@cyrillic</literal>).
</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>report-os-info</varname></term>
<listitem><para>
If this key is set to <literal>false</literal> or the <filename>no-report-os-info</filename>
file exists at <envar>FLATPAK_CONFIG_DIR</envar> Flatpak will not report the OS name,
version, and architecture from <filename>/etc/os-release</filename> to the Flatpak
remote via the <literal>Flatpak-Os-Info</literal> HTTP header when pulling objects.
The default value of the key if unset is <literal>true</literal>.
</para></listitem>
</varlistentry>
</variablelist>
<para>