Coverage Report

Created: 2025-07-01 07:09

/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
 * 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 <sys/stat.h>
22
#include <fcntl.h>
23
#include <errno.h>
24
#include <string.h>
25
26
#include "gdocumentportal.h"
27
#include "xdp-dbus.h"
28
#include "gstdio.h"
29
30
#ifdef G_OS_UNIX
31
#include "gunixfdlist.h"
32
#endif
33
34
#ifndef O_CLOEXEC
35
#define O_CLOEXEC 0
36
#else
37
#define HAVE_O_CLOEXEC 1
38
#endif
39
40
static gboolean
41
get_document_portal (GXdpDocuments **documents,
42
                     char          **documents_mountpoint,
43
                     GError        **error)
44
0
{
45
0
  GDBusConnection *connection = NULL;
46
47
0
  *documents = NULL;
48
0
  *documents_mountpoint = NULL;
49
50
0
  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
51
0
  if (connection == NULL)
52
0
    {
53
0
      g_prefix_error (error, "Cannot connect to session bus when initializing document portal: ");
54
0
      goto out;
55
0
    }
56
57
0
  *documents = gxdp_documents_proxy_new_sync (connection,
58
0
                                              G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
59
0
                                              G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
60
0
                                              "org.freedesktop.portal.Documents",
61
0
                                              "/org/freedesktop/portal/documents",
62
0
                                              NULL, error);
63
0
  if (*documents == NULL)
64
0
    {
65
0
      g_prefix_error (error, "Cannot create document portal proxy: ");
66
0
      goto out;
67
0
    }
68
69
0
  if (!gxdp_documents_call_get_mount_point_sync (*documents,
70
0
                                                 documents_mountpoint,
71
0
                                                 NULL, error))
72
0
    {
73
0
      g_clear_object (documents);
74
0
      g_prefix_error (error, "Cannot get document portal mount point: ");
75
0
      goto out;
76
0
    }
77
78
0
out:
79
0
  g_clear_object (&connection);
80
0
  return *documents != NULL;
81
0
}
82
83
/* Flags accepted by org.freedesktop.portal.Documents.AddFull */
84
enum {
85
  XDP_ADD_FLAGS_REUSE_EXISTING             =  (1 << 0),
86
  XDP_ADD_FLAGS_PERSISTENT                 =  (1 << 1),
87
  XDP_ADD_FLAGS_AS_NEEDED_BY_APP           =  (1 << 2),
88
  XDP_ADD_FLAGS_FLAGS_ALL                  = ((1 << 3) - 1)
89
};
90
91
GList *
92
g_document_portal_add_documents (GList       *uris,
93
                                 const char  *app_id,
94
                                 GError     **error)
95
0
{
96
0
  GXdpDocuments *documents = NULL;
97
0
  char *documents_mountpoint = NULL;
98
0
  int length;
99
0
  GList *ruris = NULL;
100
0
  gboolean *as_is;
101
0
  GVariantBuilder builder;
102
0
  GUnixFDList *fd_list = NULL;
103
0
  GList *l;
104
0
  gsize i, j;
105
0
  const char *permissions[] = { "read", "write", NULL };
106
0
  char **doc_ids = NULL;
107
0
  GVariant *extra_out = NULL;
108
109
0
  if (!get_document_portal (&documents, &documents_mountpoint, error))
110
0
    {
111
0
      return NULL;
112
0
    }
113
114
0
  length = g_list_length (uris);
115
0
  as_is = g_new0 (gboolean, length);
116
117
0
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
118
119
0
  fd_list = g_unix_fd_list_new ();
120
0
  for (l = uris, i = 0; l; l = l->next, i++)
121
0
    {
122
0
      const char *uri = l->data;
123
0
      int idx = -1;
124
0
      char *path = NULL;
125
126
0
      path = g_filename_from_uri (uri, NULL, NULL);
127
0
      if (path != NULL)
128
0
        {
129
0
          int fd;
130
131
0
          fd = g_open (path, O_CLOEXEC | O_RDWR);
132
0
          if (fd == -1 && (errno == EACCES || errno == EISDIR))
133
0
            {
134
              /* If we don't have write access, fall back to read-only,
135
               * and stop requesting the write permission */
136
0
              fd = g_open (path, O_CLOEXEC | O_RDONLY);
137
0
              permissions[1] = NULL;
138
0
            }
139
0
          if (fd >= 0)
140
0
            {
141
#ifndef HAVE_O_CLOEXEC
142
              fcntl (fd, F_SETFD, FD_CLOEXEC);
143
#endif
144
0
              idx = g_unix_fd_list_append (fd_list, fd, NULL);
145
0
              close (fd);
146
0
            }
147
0
        }
148
149
0
      g_free (path);
150
151
0
      if (idx != -1)
152
0
        g_variant_builder_add (&builder, "h", idx);
153
0
      else
154
0
        as_is[i] = TRUE;
155
0
    }
156
157
0
  if (g_unix_fd_list_get_length (fd_list) > 0)
158
0
    {
159
0
      if (!gxdp_documents_call_add_full_sync (documents,
160
0
                                              g_variant_builder_end (&builder),
161
0
                                              XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
162
0
                                              app_id,
163
0
                                              permissions,
164
0
                                              fd_list,
165
0
                                              &doc_ids,
166
0
                                              &extra_out,
167
0
                                              NULL,
168
0
                                              NULL,
169
0
                                              error))
170
0
        goto out;
171
172
0
      for (l = uris, i = 0, j = 0; l; l = l->next, i++)
173
0
        {
174
0
          const char *uri = l->data;
175
0
          char *ruri;
176
177
0
          if (as_is[i]) /* use as-is, not a file uri */
178
0
            {
179
0
              ruri = g_strdup (uri);
180
0
            }
181
0
          else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
182
0
            {
183
0
              ruri = g_strdup (uri);
184
0
              j++;
185
0
            }
186
0
          else
187
0
            {
188
0
              char *basename = g_path_get_basename (uri + strlen ("file:"));
189
0
              char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
190
0
              ruri = g_strconcat ("file:", doc_path, NULL);
191
0
              g_free (basename);
192
0
              g_free (doc_path);
193
0
              j++;
194
0
            }
195
196
0
          ruris = g_list_prepend (ruris, ruri);
197
0
        }
198
199
0
      ruris = g_list_reverse (ruris);
200
0
    }
201
0
  else
202
0
    {
203
0
      ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
204
0
    }
205
206
0
out:
207
0
  g_clear_object (&documents);
208
0
  g_clear_pointer (&documents_mountpoint, g_free);
209
0
  g_clear_object (&fd_list);
210
0
  g_clear_pointer (&extra_out, g_variant_unref);
211
0
  g_clear_pointer (&doc_ids, g_strfreev);
212
0
  g_free (as_is);
213
214
0
  return ruris;
215
0
}