LCOV - code coverage report
Current view: top level - gio - gdocumentportal.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 85.0 % 100 85
Test Date: 2026-02-10 05:15:13 Functions: 100.0 % 3 3
Branches: - 0 0

             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                 :          11 : get_document_portal (GXdpDocuments **documents,
      44                 :             :                      char          **documents_mountpoint,
      45                 :             :                      GError        **error)
      46                 :             : {
      47                 :          11 :   GDBusConnection *connection = NULL;
      48                 :             : 
      49                 :          11 :   *documents = NULL;
      50                 :          11 :   *documents_mountpoint = NULL;
      51                 :             : 
      52                 :          11 :   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
      53                 :          11 :   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                 :          11 :   *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                 :          11 :   if (*documents == NULL)
      66                 :             :     {
      67                 :           0 :       g_prefix_error (error, "Cannot create document portal proxy: ");
      68                 :           0 :       goto out;
      69                 :             :     }
      70                 :             : 
      71                 :          11 :   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                 :          11 : out:
      81                 :          11 :   g_clear_object (&connection);
      82                 :          11 :   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                 :             : } G_GNUC_FLAG_ENUM;
      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                 :           5 : opening_ro_might_succeed (int saved_errno)
      99                 :             : {
     100                 :           5 :   switch (saved_errno)
     101                 :             :     {
     102                 :           0 :     case EACCES:
     103                 :             :     case EISDIR:
     104                 :             : #ifdef EPERM
     105                 :             :     case EPERM:
     106                 :             : #endif
     107                 :             : #ifdef EROFS
     108                 :             :     case EROFS:
     109                 :             : #endif
     110                 :             : #ifdef ETXTBSY
     111                 :             :     case ETXTBSY:
     112                 :             : #endif
     113                 :           0 :       return TRUE;
     114                 :             : 
     115                 :           5 :     default:
     116                 :           5 :       return FALSE;
     117                 :             :     }
     118                 :             : }
     119                 :             : 
     120                 :             : GList *
     121                 :          11 : g_document_portal_add_documents (GList       *uris,
     122                 :             :                                  const char  *app_id,
     123                 :             :                                  GError     **error)
     124                 :             : {
     125                 :          11 :   GXdpDocuments *documents = NULL;
     126                 :          11 :   char *documents_mountpoint = NULL;
     127                 :             :   int length;
     128                 :          11 :   GList *ruris = NULL;
     129                 :             :   gboolean *as_is;
     130                 :             :   GVariantBuilder builder;
     131                 :          11 :   GUnixFDList *fd_list = NULL;
     132                 :             :   GList *l;
     133                 :             :   gsize i, j;
     134                 :          11 :   const char *permissions[] = { "read", "write", NULL };
     135                 :          11 :   char **doc_ids = NULL;
     136                 :          11 :   GVariant *extra_out = NULL;
     137                 :             : 
     138                 :          11 :   if (!get_document_portal (&documents, &documents_mountpoint, error))
     139                 :             :     {
     140                 :           0 :       return NULL;
     141                 :             :     }
     142                 :             : 
     143                 :          11 :   length = g_list_length (uris);
     144                 :          11 :   as_is = g_new0 (gboolean, length);
     145                 :             : 
     146                 :          11 :   g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("ah"));
     147                 :             : 
     148                 :          11 :   fd_list = g_unix_fd_list_new ();
     149                 :          27 :   for (l = uris, i = 0; l; l = l->next, i++)
     150                 :             :     {
     151                 :          16 :       const char *uri = l->data;
     152                 :          16 :       int idx = -1;
     153                 :          16 :       char *path = NULL;
     154                 :             : 
     155                 :          16 :       path = g_filename_from_uri (uri, NULL, NULL);
     156                 :          16 :       if (path != NULL)
     157                 :             :         {
     158                 :             :           int fd;
     159                 :             : 
     160                 :          16 :           fd = g_open (path, O_CLOEXEC | O_RDWR);
     161                 :          16 :           if (fd == -1 && opening_ro_might_succeed (errno))
     162                 :             :             {
     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                 :             :             }
     168                 :          16 :           if (fd >= 0)
     169                 :             :             {
     170                 :             : #ifndef HAVE_O_CLOEXEC
     171                 :             :               fcntl (fd, F_SETFD, FD_CLOEXEC);
     172                 :             : #endif
     173                 :          11 :               idx = g_unix_fd_list_append (fd_list, fd, NULL);
     174                 :          11 :               close (fd);
     175                 :             :             }
     176                 :             :         }
     177                 :             : 
     178                 :          16 :       g_free (path);
     179                 :             : 
     180                 :          16 :       if (idx != -1)
     181                 :          11 :         g_variant_builder_add (&builder, "h", idx);
     182                 :             :       else
     183                 :           5 :         as_is[i] = TRUE;
     184                 :             :     }
     185                 :             : 
     186                 :          11 :   if (g_unix_fd_list_get_length (fd_list) > 0)
     187                 :             :     {
     188                 :           8 :       if (!gxdp_documents_call_add_full_sync (documents,
     189                 :             :                                               g_variant_builder_end (&builder),
     190                 :             :                                               XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
     191                 :             :                                               app_id,
     192                 :             :                                               permissions,
     193                 :             :                                               fd_list,
     194                 :             :                                               &doc_ids,
     195                 :             :                                               &extra_out,
     196                 :             :                                               NULL,
     197                 :             :                                               NULL,
     198                 :             :                                               error))
     199                 :           0 :         goto out;
     200                 :             : 
     201                 :          19 :       for (l = uris, i = 0, j = 0; l; l = l->next, i++)
     202                 :             :         {
     203                 :          13 :           const char *uri = l->data;
     204                 :             :           char *ruri;
     205                 :             : 
     206                 :          13 :           if (as_is[i]) /* use as-is, not a file uri */
     207                 :             :             {
     208                 :           2 :               ruri = g_strdup (uri);
     209                 :             :             }
     210                 :          11 :           else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
     211                 :             :             {
     212                 :           0 :               ruri = g_strdup (uri);
     213                 :           0 :               j++;
     214                 :             :             }
     215                 :             :           else
     216                 :             :             {
     217                 :             :               const char *doc_name;
     218                 :             :               char *doc_path;
     219                 :             :               GDir *doc_dir;
     220                 :          11 :               GError *local_error = NULL;
     221                 :             : 
     222                 :          11 :               doc_path = g_build_filename (documents_mountpoint, doc_ids[j], NULL);
     223                 :          11 :               doc_dir = g_dir_open (doc_path, 0, &local_error);
     224                 :          11 :               g_clear_pointer (&doc_path, g_free);
     225                 :             : 
     226                 :          11 :               if (!doc_dir)
     227                 :             :                 {
     228                 :           1 :                   g_set_error_literal (error, G_IO_ERROR,
     229                 :           1 :                                        g_io_error_from_file_error (local_error->code),
     230                 :           1 :                                        local_error->message);
     231                 :             : 
     232                 :           1 :                   g_clear_error (&local_error);
     233                 :           1 :                   g_clear_list (&ruris, g_free);
     234                 :           2 :                   goto out;
     235                 :             :                 }
     236                 :             : 
     237                 :          10 :               doc_name = g_dir_read_name (doc_dir);
     238                 :             : 
     239                 :          10 :               if (!doc_name)
     240                 :             :                 {
     241                 :           1 :                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
     242                 :             :                                "Failed to read %s" G_DIR_SEPARATOR_S "%s",
     243                 :           1 :                                documents_mountpoint, doc_ids[j]);
     244                 :             : 
     245                 :           1 :                   g_clear_pointer (&doc_dir, g_dir_close);
     246                 :           1 :                   g_clear_list (&ruris, g_free);
     247                 :           1 :                   goto out;
     248                 :             :                 }
     249                 :             : 
     250                 :           9 :               ruri = g_strconcat ("file:",
     251                 :             :                                   documents_mountpoint, G_DIR_SEPARATOR_S,
     252                 :           9 :                                   doc_ids[j], G_DIR_SEPARATOR_S,
     253                 :             :                                   doc_name, NULL);
     254                 :             : 
     255                 :           9 :               g_clear_pointer (&doc_dir, g_dir_close);
     256                 :           9 :               j++;
     257                 :             :             }
     258                 :             : 
     259                 :          11 :           ruris = g_list_prepend (ruris, ruri);
     260                 :             :         }
     261                 :             : 
     262                 :           6 :       ruris = g_list_reverse (ruris);
     263                 :             :     }
     264                 :             :   else
     265                 :             :     {
     266                 :           3 :       ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
     267                 :           3 :       g_variant_builder_clear (&builder);
     268                 :             :     }
     269                 :             : 
     270                 :          11 : out:
     271                 :          11 :   g_clear_object (&documents);
     272                 :          11 :   g_clear_pointer (&documents_mountpoint, g_free);
     273                 :          11 :   g_clear_object (&fd_list);
     274                 :          11 :   g_clear_pointer (&extra_out, g_variant_unref);
     275                 :          11 :   g_clear_pointer (&doc_ids, g_strfreev);
     276                 :          11 :   g_free (as_is);
     277                 :             : 
     278                 :          11 :   return ruris;
     279                 :             : }
        

Generated by: LCOV version 2.0-1