LCOV - code coverage report
Current view: top level - glib/gio - gdocumentportal.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 64 78 82.1 %
Date: 2024-04-23 05:16:05 Functions: 2 2 100.0 %
Branches: 22 32 68.8 %

           Branch data     Line data    Source code
       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                 :          2 : get_document_portal (GXdpDocuments **documents,
      44                 :            :                      char          **documents_mountpoint,
      45                 :            :                      GError        **error)
      46                 :            : {
      47                 :          2 :   GDBusConnection *connection = NULL;
      48                 :            : 
      49                 :          2 :   *documents = NULL;
      50                 :          2 :   *documents_mountpoint = NULL;
      51                 :            : 
      52                 :          2 :   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
      53         [ -  + ]:          2 :   if (connection == NULL)
      54                 :            :     {
      55                 :          0 :       g_prefix_error (error, "Cannot connect to session bus when initializing document portal: ");
      56                 :          0 :       goto out;
      57                 :            :     }
      58                 :            : 
      59                 :          2 :   *documents = gxdp_documents_proxy_new_sync (connection,
      60                 :            :                                               G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
      61                 :            :                                               G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
      62                 :            :                                               "org.freedesktop.portal.Documents",
      63                 :            :                                               "/org/freedesktop/portal/documents",
      64                 :            :                                               NULL, error);
      65         [ -  + ]:          2 :   if (*documents == NULL)
      66                 :            :     {
      67                 :          0 :       g_prefix_error (error, "Cannot create document portal proxy: ");
      68                 :          0 :       goto out;
      69                 :            :     }
      70                 :            : 
      71         [ +  - ]:          2 :   if (!gxdp_documents_call_get_mount_point_sync (*documents,
      72                 :            :                                                  documents_mountpoint,
      73                 :            :                                                  NULL, error))
      74                 :            :     {
      75                 :          0 :       g_clear_object (documents);
      76                 :          0 :       g_prefix_error (error, "Cannot get document portal mount point: ");
      77                 :          0 :       goto out;
      78                 :            :     }
      79                 :            : 
      80                 :          2 : out:
      81                 :          2 :   g_clear_object (&connection);
      82                 :          2 :   return *documents != NULL;
      83                 :            : }
      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                 :          2 : g_document_portal_add_documents (GList       *uris,
      95                 :            :                                  const char  *app_id,
      96                 :            :                                  GError     **error)
      97                 :            : {
      98                 :          2 :   GXdpDocuments *documents = NULL;
      99                 :          2 :   char *documents_mountpoint = NULL;
     100                 :            :   int length;
     101                 :          2 :   GList *ruris = NULL;
     102                 :            :   gboolean *as_is;
     103                 :            :   GVariantBuilder builder;
     104                 :          2 :   GUnixFDList *fd_list = NULL;
     105                 :            :   GList *l;
     106                 :            :   gsize i, j;
     107                 :          2 :   const char *permissions[] = { "read", "write", NULL };
     108                 :          2 :   char **doc_ids = NULL;
     109                 :          2 :   GVariant *extra_out = NULL;
     110                 :            : 
     111         [ -  + ]:          2 :   if (!get_document_portal (&documents, &documents_mountpoint, error))
     112                 :            :     {
     113                 :          0 :       return NULL;
     114                 :            :     }
     115                 :            : 
     116                 :          2 :   length = g_list_length (uris);
     117                 :          2 :   as_is = g_new0 (gboolean, length);
     118                 :            : 
     119                 :          2 :   g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
     120                 :            : 
     121                 :          2 :   fd_list = g_unix_fd_list_new ();
     122         [ +  + ]:          4 :   for (l = uris, i = 0; l; l = l->next, i++)
     123                 :            :     {
     124                 :          2 :       const char *uri = l->data;
     125                 :          2 :       int idx = -1;
     126                 :          2 :       char *path = NULL;
     127                 :            : 
     128                 :          2 :       path = g_filename_from_uri (uri, NULL, NULL);
     129         [ +  - ]:          2 :       if (path != NULL)
     130                 :            :         {
     131                 :            :           int fd;
     132                 :            : 
     133                 :          2 :           fd = g_open (path, O_CLOEXEC | O_RDWR);
     134   [ +  +  +  -  :          2 :           if (fd == -1 && (errno == EACCES || errno == EISDIR))
                   -  + ]
     135                 :            :             {
     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                 :            :             }
     141         [ +  + ]:          2 :           if (fd >= 0)
     142                 :            :             {
     143                 :            : #ifndef HAVE_O_CLOEXEC
     144                 :            :               fcntl (fd, F_SETFD, FD_CLOEXEC);
     145                 :            : #endif
     146                 :          1 :               idx = g_unix_fd_list_append (fd_list, fd, NULL);
     147                 :          1 :               close (fd);
     148                 :            :             }
     149                 :            :         }
     150                 :            : 
     151                 :          2 :       g_free (path);
     152                 :            : 
     153         [ +  + ]:          2 :       if (idx != -1)
     154                 :          1 :         g_variant_builder_add (&builder, "h", idx);
     155                 :            :       else
     156                 :          1 :         as_is[i] = TRUE;
     157                 :            :     }
     158                 :            : 
     159         [ +  + ]:          2 :   if (g_unix_fd_list_get_length (fd_list) > 0)
     160                 :            :     {
     161         [ -  + ]:          1 :       if (!gxdp_documents_call_add_full_sync (documents,
     162                 :            :                                               g_variant_builder_end (&builder),
     163                 :            :                                               XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
     164                 :            :                                               app_id,
     165                 :            :                                               permissions,
     166                 :            :                                               fd_list,
     167                 :            :                                               &doc_ids,
     168                 :            :                                               &extra_out,
     169                 :            :                                               NULL,
     170                 :            :                                               NULL,
     171                 :            :                                               error))
     172                 :          0 :         goto out;
     173                 :            : 
     174         [ +  + ]:          2 :       for (l = uris, i = 0, j = 0; l; l = l->next, i++)
     175                 :            :         {
     176                 :          1 :           const char *uri = l->data;
     177                 :            :           char *ruri;
     178                 :            : 
     179         [ -  + ]:          1 :           if (as_is[i]) /* use as-is, not a file uri */
     180                 :            :             {
     181                 :          0 :               ruri = g_strdup (uri);
     182                 :            :             }
     183         [ -  + ]:          1 :           else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
     184                 :            :             {
     185                 :          0 :               ruri = g_strdup (uri);
     186                 :          0 :               j++;
     187                 :            :             }
     188                 :            :           else
     189                 :            :             {
     190                 :          1 :               char *basename = g_path_get_basename (uri + strlen ("file:"));
     191                 :          1 :               char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
     192                 :          1 :               ruri = g_strconcat ("file:", doc_path, NULL);
     193                 :          1 :               g_free (basename);
     194                 :          1 :               g_free (doc_path);
     195                 :          1 :               j++;
     196                 :            :             }
     197                 :            : 
     198                 :          1 :           ruris = g_list_prepend (ruris, ruri);
     199                 :            :         }
     200                 :            : 
     201                 :          1 :       ruris = g_list_reverse (ruris);
     202                 :            :     }
     203                 :            :   else
     204                 :            :     {
     205                 :          1 :       ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
     206                 :          1 :       g_variant_builder_clear (&builder);
     207                 :            :     }
     208                 :            : 
     209                 :          2 : out:
     210                 :          2 :   g_clear_object (&documents);
     211                 :          2 :   g_clear_pointer (&documents_mountpoint, g_free);
     212                 :          2 :   g_clear_object (&fd_list);
     213                 :          2 :   g_clear_pointer (&extra_out, g_variant_unref);
     214                 :          2 :   g_clear_pointer (&doc_ids, g_strfreev);
     215                 :          2 :   g_free (as_is);
     216                 :            : 
     217                 :          2 :   return ruris;
     218                 :            : }

Generated by: LCOV version 1.14