Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gportalsupport.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright 2016 Red Hat, Inc.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#include "config.h"
20
21
#include "gportalsupport.h"
22
23
static gboolean use_portal;
24
static gboolean network_available;
25
static gboolean dconf_access;
26
27
static void
28
read_flatpak_info (void)
29
0
{
30
0
  static gsize flatpak_info_read = 0;
31
0
  const gchar *path = "/.flatpak-info";
32
33
0
  if (!g_once_init_enter (&flatpak_info_read))
34
0
    return;
35
36
0
  if (g_file_test (path, G_FILE_TEST_EXISTS))
37
0
    {
38
0
      GKeyFile *keyfile;
39
40
0
      use_portal = TRUE;
41
0
      network_available = FALSE;
42
0
      dconf_access = FALSE;
43
44
0
      keyfile = g_key_file_new ();
45
0
      if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL))
46
0
        {
47
0
          char **shared = NULL;
48
0
          char *dconf_policy = NULL;
49
50
0
          shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL);
51
0
          if (shared)
52
0
            {
53
0
              network_available = g_strv_contains ((const char * const *)shared, "network");
54
0
              g_strfreev (shared);
55
0
            }
56
57
0
          dconf_policy = g_key_file_get_string (keyfile, "Session Bus Policy", "ca.desrt.dconf", NULL);
58
0
          if (dconf_policy)
59
0
            {
60
0
              if (strcmp (dconf_policy, "talk") == 0)
61
0
                dconf_access = TRUE;
62
0
              g_free (dconf_policy);
63
0
            }
64
0
        }
65
66
0
      g_key_file_unref (keyfile);
67
0
    }
68
0
  else
69
0
    {
70
0
      const char *var;
71
72
0
      var = g_getenv ("GTK_USE_PORTAL");
73
0
      if (var && var[0] == '1')
74
0
        use_portal = TRUE;
75
0
      network_available = TRUE;
76
0
      dconf_access = TRUE;
77
0
    }
78
79
0
  g_once_init_leave (&flatpak_info_read, 1);
80
0
}
81
82
gboolean
83
glib_should_use_portal (void)
84
0
{
85
0
  read_flatpak_info ();
86
0
  return use_portal;
87
0
}
88
89
gboolean
90
glib_network_available_in_sandbox (void)
91
0
{
92
0
  read_flatpak_info ();
93
0
  return network_available;
94
0
}
95
96
gboolean
97
glib_has_dconf_access_in_sandbox (void)
98
0
{
99
0
  read_flatpak_info ();
100
0
  return dconf_access;
101
0
}