Coverage Report

Created: 2025-06-13 06:55

/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
GList *
94
g_document_portal_add_documents (GList       *uris,
95
                                 const char  *app_id,
96
                                 GError     **error)
97
0
{
98
0
  GXdpDocuments *documents = NULL;
99
0
  char *documents_mountpoint = NULL;
100
0
  int length;
101
0
  GList *ruris = NULL;
102
0
  gboolean *as_is;
103
0
  GVariantBuilder builder;
104
0
  GUnixFDList *fd_list = NULL;
105
0
  GList *l;
106
0
  gsize i, j;
107
0
  const char *permissions[] = { "read", "write", NULL };
108
0
  char **doc_ids = NULL;
109
0
  GVariant *extra_out = NULL;
110
111
0
  if (!get_document_portal (&documents, &documents_mountpoint, error))
112
0
    {
113
0
      return NULL;
114
0
    }
115
116
0
  length = g_list_length (uris);
117
0
  as_is = g_new0 (gboolean, length);
118
119
0
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
120
121
0
  fd_list = g_unix_fd_list_new ();
122
0
  for (l = uris, i = 0; l; l = l->next, i++)
123
0
    {
124
0
      const char *uri = l->data;
125
0
      int idx = -1;
126
0
      char *path = NULL;
127
128
0
      path = g_filename_from_uri (uri, NULL, NULL);
129
0
      if (path != NULL)
130
0
        {
131
0
          int fd;
132
133
0
          fd = g_open (path, O_CLOEXEC | O_RDWR);
134
0
          if (fd == -1 && (errno == EACCES || errno == EISDIR))
135
0
            {
136
              /* If we don't have write access, fall back to read-only,
137
               * and stop requesting the write permission */
138
0
              fd = g_open (path, O_CLOEXEC | O_RDONLY);
139
0
              permissions[1] = NULL;
140
0
            }
141
0
          if (fd >= 0)
142
0
            {
143
#ifndef HAVE_O_CLOEXEC
144
              fcntl (fd, F_SETFD, FD_CLOEXEC);
145
#endif
146
0
              idx = g_unix_fd_list_append (fd_list, fd, NULL);
147
0
              close (fd);
148
0
            }
149
0
        }
150
151
0
      g_free (path);
152
153
0
      if (idx != -1)
154
0
        g_variant_builder_add (&builder, "h", idx);
155
0
      else
156
0
        as_is[i] = TRUE;
157
0
    }
158
159
0
  if (g_unix_fd_list_get_length (fd_list) > 0)
160
0
    {
161
0
      if (!gxdp_documents_call_add_full_sync (documents,
162
0
                                              g_variant_builder_end (&builder),
163
0
                                              XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
164
0
                                              app_id,
165
0
                                              permissions,
166
0
                                              fd_list,
167
0
                                              &doc_ids,
168
0
                                              &extra_out,
169
0
                                              NULL,
170
0
                                              NULL,
171
0
                                              error))
172
0
        goto out;
173
174
0
      for (l = uris, i = 0, j = 0; l; l = l->next, i++)
175
0
        {
176
0
          const char *uri = l->data;
177
0
          char *ruri;
178
179
0
          if (as_is[i]) /* use as-is, not a file uri */
180
0
            {
181
0
              ruri = g_strdup (uri);
182
0
            }
183
0
          else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
184
0
            {
185
0
              ruri = g_strdup (uri);
186
0
              j++;
187
0
            }
188
0
          else
189
0
            {
190
0
              char *basename = g_path_get_basename (uri + strlen ("file:"));
191
0
              char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
192
0
              ruri = g_strconcat ("file:", doc_path, NULL);
193
0
              g_free (basename);
194
0
              g_free (doc_path);
195
0
              j++;
196
0
            }
197
198
0
          ruris = g_list_prepend (ruris, ruri);
199
0
        }
200
201
0
      ruris = g_list_reverse (ruris);
202
0
    }
203
0
  else
204
0
    {
205
0
      ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
206
0
      g_variant_builder_clear (&builder);
207
0
    }
208
209
0
out:
210
0
  g_clear_object (&documents);
211
0
  g_clear_pointer (&documents_mountpoint, g_free);
212
0
  g_clear_object (&fd_list);
213
0
  g_clear_pointer (&extra_out, g_variant_unref);
214
0
  g_clear_pointer (&doc_ids, g_strfreev);
215
0
  g_free (as_is);
216
217
0
  return ruris;
218
0
}