Coverage Report

Created: 2025-06-13 06:20

/src/glib/gio/gdocumentportal.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright 2016 Endless Mobile, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
23
#include <sys/stat.h>
24
#include <fcntl.h>
25
#include <errno.h>
26
#include <string.h>
27
28
#include "gdocumentportal.h"
29
#include "xdp-dbus.h"
30
#include "gstdio.h"
31
32
#ifdef G_OS_UNIX
33
#include "gunixfdlist.h"
34
#endif
35
36
#ifndef O_CLOEXEC
37
#define O_CLOEXEC 0
38
#else
39
#define HAVE_O_CLOEXEC 1
40
#endif
41
42
static gboolean
43
get_document_portal (GXdpDocuments **documents,
44
                     char          **documents_mountpoint,
45
                     GError        **error)
46
0
{
47
0
  GDBusConnection *connection = NULL;
48
49
0
  *documents = NULL;
50
0
  *documents_mountpoint = NULL;
51
52
0
  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
53
0
  if (connection == NULL)
54
0
    {
55
0
      g_prefix_error (error, "Cannot connect to session bus when initializing document portal: ");
56
0
      goto out;
57
0
    }
58
59
0
  *documents = gxdp_documents_proxy_new_sync (connection,
60
0
                                              G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
61
0
                                              G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
62
0
                                              "org.freedesktop.portal.Documents",
63
0
                                              "/org/freedesktop/portal/documents",
64
0
                                              NULL, error);
65
0
  if (*documents == NULL)
66
0
    {
67
0
      g_prefix_error (error, "Cannot create document portal proxy: ");
68
0
      goto out;
69
0
    }
70
71
0
  if (!gxdp_documents_call_get_mount_point_sync (*documents,
72
0
                                                 documents_mountpoint,
73
0
                                                 NULL, error))
74
0
    {
75
0
      g_clear_object (documents);
76
0
      g_prefix_error (error, "Cannot get document portal mount point: ");
77
0
      goto out;
78
0
    }
79
80
0
out:
81
0
  g_clear_object (&connection);
82
0
  return *documents != NULL;
83
0
}
84
85
/* Flags accepted by org.freedesktop.portal.Documents.AddFull */
86
enum {
87
  XDP_ADD_FLAGS_REUSE_EXISTING             =  (1 << 0),
88
  XDP_ADD_FLAGS_PERSISTENT                 =  (1 << 1),
89
  XDP_ADD_FLAGS_AS_NEEDED_BY_APP           =  (1 << 2),
90
  XDP_ADD_FLAGS_FLAGS_ALL                  = ((1 << 3) - 1)
91
};
92
93
/*
94
 * Assume that opening a file read/write failed with @saved_errno,
95
 * and return TRUE if opening the same file read-only might succeed.
96
 */
97
static gboolean
98
opening_ro_might_succeed (int saved_errno)
99
0
{
100
0
  switch (saved_errno)
101
0
    {
102
0
    case EACCES:
103
0
    case EISDIR:
104
0
#ifdef EPERM
105
0
    case EPERM:
106
0
#endif
107
0
#ifdef EROFS
108
0
    case EROFS:
109
0
#endif
110
0
#ifdef ETXTBSY
111
0
    case ETXTBSY:
112
0
#endif
113
0
      return TRUE;
114
115
0
    default:
116
0
      return FALSE;
117
0
    }
118
0
}
119
120
GList *
121
g_document_portal_add_documents (GList       *uris,
122
                                 const char  *app_id,
123
                                 GError     **error)
124
0
{
125
0
  GXdpDocuments *documents = NULL;
126
0
  char *documents_mountpoint = NULL;
127
0
  int length;
128
0
  GList *ruris = NULL;
129
0
  gboolean *as_is;
130
0
  GVariantBuilder builder;
131
0
  GUnixFDList *fd_list = NULL;
132
0
  GList *l;
133
0
  gsize i, j;
134
0
  const char *permissions[] = { "read", "write", NULL };
135
0
  char **doc_ids = NULL;
136
0
  GVariant *extra_out = NULL;
137
138
0
  if (!get_document_portal (&documents, &documents_mountpoint, error))
139
0
    {
140
0
      return NULL;
141
0
    }
142
143
0
  length = g_list_length (uris);
144
0
  as_is = g_new0 (gboolean, length);
145
146
0
  g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("ah"));
147
148
0
  fd_list = g_unix_fd_list_new ();
149
0
  for (l = uris, i = 0; l; l = l->next, i++)
150
0
    {
151
0
      const char *uri = l->data;
152
0
      int idx = -1;
153
0
      char *path = NULL;
154
155
0
      path = g_filename_from_uri (uri, NULL, NULL);
156
0
      if (path != NULL)
157
0
        {
158
0
          int fd;
159
160
0
          fd = g_open (path, O_CLOEXEC | O_RDWR);
161
0
          if (fd == -1 && opening_ro_might_succeed (errno))
162
0
            {
163
              /* If we don't have write access, fall back to read-only,
164
               * and stop requesting the write permission */
165
0
              fd = g_open (path, O_CLOEXEC | O_RDONLY);
166
0
              permissions[1] = NULL;
167
0
            }
168
0
          if (fd >= 0)
169
0
            {
170
#ifndef HAVE_O_CLOEXEC
171
              fcntl (fd, F_SETFD, FD_CLOEXEC);
172
#endif
173
0
              idx = g_unix_fd_list_append (fd_list, fd, NULL);
174
0
              close (fd);
175
0
            }
176
0
        }
177
178
0
      g_free (path);
179
180
0
      if (idx != -1)
181
0
        g_variant_builder_add (&builder, "h", idx);
182
0
      else
183
0
        as_is[i] = TRUE;
184
0
    }
185
186
0
  if (g_unix_fd_list_get_length (fd_list) > 0)
187
0
    {
188
0
      if (!gxdp_documents_call_add_full_sync (documents,
189
0
                                              g_variant_builder_end (&builder),
190
0
                                              XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
191
0
                                              app_id,
192
0
                                              permissions,
193
0
                                              fd_list,
194
0
                                              &doc_ids,
195
0
                                              &extra_out,
196
0
                                              NULL,
197
0
                                              NULL,
198
0
                                              error))
199
0
        goto out;
200
201
0
      for (l = uris, i = 0, j = 0; l; l = l->next, i++)
202
0
        {
203
0
          const char *uri = l->data;
204
0
          char *ruri;
205
206
0
          if (as_is[i]) /* use as-is, not a file uri */
207
0
            {
208
0
              ruri = g_strdup (uri);
209
0
            }
210
0
          else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
211
0
            {
212
0
              ruri = g_strdup (uri);
213
0
              j++;
214
0
            }
215
0
          else
216
0
            {
217
0
              char *basename = g_path_get_basename (uri + strlen ("file:"));
218
0
              char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
219
0
              ruri = g_strconcat ("file:", doc_path, NULL);
220
0
              g_free (basename);
221
0
              g_free (doc_path);
222
0
              j++;
223
0
            }
224
225
0
          ruris = g_list_prepend (ruris, ruri);
226
0
        }
227
228
0
      ruris = g_list_reverse (ruris);
229
0
    }
230
0
  else
231
0
    {
232
0
      ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
233
0
      g_variant_builder_clear (&builder);
234
0
    }
235
236
0
out:
237
0
  g_clear_object (&documents);
238
0
  g_clear_pointer (&documents_mountpoint, g_free);
239
0
  g_clear_object (&fd_list);
240
0
  g_clear_pointer (&extra_out, g_variant_unref);
241
0
  g_clear_pointer (&doc_ids, g_strfreev);
242
0
  g_free (as_is);
243
244
0
  return ruris;
245
0
}