LCOV - code coverage report
Current view: top level - glib/gio - gunixmount.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 3 151 2.0 %
Date: 2024-04-16 05:15:53 Functions: 1 27 3.7 %
Branches: 2 42 4.8 %

           Branch data     Line data    Source code
       1                 :            : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
       2                 :            : 
       3                 :            : /* GIO - GLib Input, Output and Streaming Library
       4                 :            :  * 
       5                 :            :  * Copyright (C) 2006-2007 Red Hat, Inc.
       6                 :            :  *
       7                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       8                 :            :  *
       9                 :            :  * This library is free software; you can redistribute it and/or
      10                 :            :  * modify it under the terms of the GNU Lesser General Public
      11                 :            :  * License as published by the Free Software Foundation; either
      12                 :            :  * version 2.1 of the License, or (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This library is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :  * Lesser General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU Lesser General
      20                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      21                 :            :  *
      22                 :            :  * Author: Alexander Larsson <alexl@redhat.com>
      23                 :            :  *         David Zeuthen <davidz@redhat.com>
      24                 :            :  */
      25                 :            : 
      26                 :            : #include "config.h"
      27                 :            : 
      28                 :            : #include <string.h>
      29                 :            : #include <sys/wait.h>
      30                 :            : #include <unistd.h>
      31                 :            : 
      32                 :            : #include <glib.h>
      33                 :            : #include "gsubprocess.h"
      34                 :            : #include "gioenums.h"
      35                 :            : #include "gunixvolumemonitor.h"
      36                 :            : #include "gunixmount.h"
      37                 :            : #include "gunixmounts.h"
      38                 :            : #include "gunixvolume.h"
      39                 :            : #include "gmountprivate.h"
      40                 :            : #include "gmount.h"
      41                 :            : #include "gfile.h"
      42                 :            : #include "gvolumemonitor.h"
      43                 :            : #include "gthemedicon.h"
      44                 :            : #include "gioerror.h"
      45                 :            : #include "glibintl.h"
      46                 :            : /* for BUFSIZ */
      47                 :            : #include <stdio.h>
      48                 :            : 
      49                 :            : 
      50                 :            : struct _GUnixMount {
      51                 :            :   GObject parent;
      52                 :            : 
      53                 :            :   GVolumeMonitor   *volume_monitor;
      54                 :            : 
      55                 :            :   GUnixVolume      *volume; /* owned by volume monitor */
      56                 :            : 
      57                 :            :   char *name;
      58                 :            :   GIcon *icon;
      59                 :            :   GIcon *symbolic_icon;
      60                 :            :   char *device_path;
      61                 :            :   char *mount_path;
      62                 :            : 
      63                 :            :   gboolean can_eject;
      64                 :            : };
      65                 :            : 
      66                 :            : static void g_unix_mount_mount_iface_init (GMountIface *iface);
      67                 :            : 
      68                 :            : #define g_unix_mount_get_type _g_unix_mount_get_type
      69   [ #  #  #  #  :          0 : G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
                   #  # ]
      70                 :            :                          G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
      71                 :            :                                                 g_unix_mount_mount_iface_init))
      72                 :            : 
      73                 :            : 
      74                 :            : static void
      75                 :          0 : g_unix_mount_finalize (GObject *object)
      76                 :            : {
      77                 :            :   GUnixMount *mount;
      78                 :            :   
      79                 :          0 :   mount = G_UNIX_MOUNT (object);
      80                 :            : 
      81         [ #  # ]:          0 :   if (mount->volume_monitor != NULL)
      82                 :          0 :     g_object_unref (mount->volume_monitor);
      83                 :            : 
      84         [ #  # ]:          0 :   if (mount->volume)
      85                 :          0 :     _g_unix_volume_unset_mount (mount->volume, mount);
      86                 :            :     
      87                 :            :   /* TODO: g_warn_if_fail (volume->volume == NULL); */
      88                 :          0 :   g_object_unref (mount->icon);
      89                 :          0 :   g_object_unref (mount->symbolic_icon);
      90                 :          0 :   g_free (mount->name);
      91                 :          0 :   g_free (mount->device_path);
      92                 :          0 :   g_free (mount->mount_path);
      93                 :            : 
      94                 :          0 :   G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
      95                 :          0 : }
      96                 :            : 
      97                 :            : static void
      98                 :          0 : g_unix_mount_class_init (GUnixMountClass *klass)
      99                 :            : {
     100                 :          0 :   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     101                 :            : 
     102                 :          0 :   gobject_class->finalize = g_unix_mount_finalize;
     103                 :          0 : }
     104                 :            : 
     105                 :            : static void
     106                 :          0 : g_unix_mount_init (GUnixMount *unix_mount)
     107                 :            : {
     108                 :          0 : }
     109                 :            : 
     110                 :            : GUnixMount *
     111                 :         36 : _g_unix_mount_new (GVolumeMonitor  *volume_monitor,
     112                 :            :                    GUnixMountEntry *mount_entry,
     113                 :            :                    GUnixVolume     *volume)
     114                 :            : {
     115                 :            :   GUnixMount *mount;
     116                 :            :   
     117                 :            :   /* No volume for mount: Ignore internal things */
     118   [ +  -  +  - ]:         36 :   if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
     119                 :         36 :     return NULL;
     120                 :            : 
     121                 :          0 :   mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
     122         [ #  # ]:          0 :   mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
     123                 :          0 :   mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
     124                 :          0 :   mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
     125                 :          0 :   mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
     126                 :            : 
     127                 :          0 :   mount->name = g_unix_mount_guess_name (mount_entry);
     128                 :          0 :   mount->icon = g_unix_mount_guess_icon (mount_entry);
     129                 :          0 :   mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
     130                 :            : 
     131                 :            :   /* need to do this last */
     132                 :          0 :   mount->volume = volume;
     133         [ #  # ]:          0 :   if (volume != NULL)
     134                 :          0 :     _g_unix_volume_set_mount (volume, mount);
     135                 :            : 
     136                 :          0 :   return mount;
     137                 :            : }
     138                 :            : 
     139                 :            : void
     140                 :          0 : _g_unix_mount_unmounted (GUnixMount *mount)
     141                 :            : {
     142         [ #  # ]:          0 :   if (mount->volume != NULL)
     143                 :            :     {
     144                 :          0 :       _g_unix_volume_unset_mount (mount->volume, mount);
     145                 :          0 :       mount->volume = NULL;
     146                 :          0 :       g_signal_emit_by_name (mount, "changed");
     147                 :            :       /* there's really no need to emit mount_changed on the volume monitor 
     148                 :            :        * as we're going to be deleted.. */
     149                 :            :     }
     150                 :          0 : }
     151                 :            : 
     152                 :            : void
     153                 :          0 : _g_unix_mount_unset_volume (GUnixMount *mount,
     154                 :            :                             GUnixVolume  *volume)
     155                 :            : {
     156         [ #  # ]:          0 :   if (mount->volume == volume)
     157                 :            :     {
     158                 :          0 :       mount->volume = NULL;
     159                 :            :       /* TODO: Emit changed in idle to avoid locking issues */
     160                 :          0 :       g_signal_emit_by_name (mount, "changed");
     161         [ #  # ]:          0 :       if (mount->volume_monitor != NULL)
     162                 :          0 :         g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
     163                 :            :     }
     164                 :          0 : }
     165                 :            : 
     166                 :            : static GFile *
     167                 :          0 : g_unix_mount_get_root (GMount *mount)
     168                 :            : {
     169                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     170                 :            : 
     171                 :          0 :   return g_file_new_for_path (unix_mount->mount_path);
     172                 :            : }
     173                 :            : 
     174                 :            : static GIcon *
     175                 :          0 : g_unix_mount_get_icon (GMount *mount)
     176                 :            : {
     177                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     178                 :            : 
     179                 :          0 :   return g_object_ref (unix_mount->icon);
     180                 :            : }
     181                 :            : 
     182                 :            : static GIcon *
     183                 :          0 : g_unix_mount_get_symbolic_icon (GMount *mount)
     184                 :            : {
     185                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     186                 :            : 
     187                 :          0 :   return g_object_ref (unix_mount->symbolic_icon);
     188                 :            : }
     189                 :            : 
     190                 :            : static char *
     191                 :          0 : g_unix_mount_get_uuid (GMount *mount)
     192                 :            : {
     193                 :          0 :   return NULL;
     194                 :            : }
     195                 :            : 
     196                 :            : static char *
     197                 :          0 : g_unix_mount_get_name (GMount *mount)
     198                 :            : {
     199                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     200                 :            :   
     201                 :          0 :   return g_strdup (unix_mount->name);
     202                 :            : }
     203                 :            : 
     204                 :            : gboolean
     205                 :          0 : _g_unix_mount_has_mount_path (GUnixMount *mount,
     206                 :            :                               const char  *mount_path)
     207                 :            : {
     208                 :          0 :   return strcmp (mount->mount_path, mount_path) == 0;
     209                 :            : }
     210                 :            : 
     211                 :            : static GDrive *
     212                 :          0 : g_unix_mount_get_drive (GMount *mount)
     213                 :            : {
     214                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     215                 :            : 
     216         [ #  # ]:          0 :   if (unix_mount->volume != NULL)
     217                 :          0 :     return g_volume_get_drive (G_VOLUME (unix_mount->volume));
     218                 :            : 
     219                 :          0 :   return NULL;
     220                 :            : }
     221                 :            : 
     222                 :            : static GVolume *
     223                 :          0 : g_unix_mount_get_volume (GMount *mount)
     224                 :            : {
     225                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     226                 :            : 
     227         [ #  # ]:          0 :   if (unix_mount->volume)
     228                 :          0 :     return G_VOLUME (g_object_ref (unix_mount->volume));
     229                 :            :   
     230                 :          0 :   return NULL;
     231                 :            : }
     232                 :            : 
     233                 :            : static gboolean
     234                 :          0 : g_unix_mount_can_unmount (GMount *mount)
     235                 :            : {
     236                 :          0 :   return TRUE;
     237                 :            : }
     238                 :            : 
     239                 :            : static gboolean
     240                 :          0 : g_unix_mount_can_eject (GMount *mount)
     241                 :            : {
     242                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     243                 :          0 :   return unix_mount->can_eject;
     244                 :            : }
     245                 :            : 
     246                 :            : static void
     247                 :          0 : eject_unmount_done (GObject      *source,
     248                 :            :                     GAsyncResult *result,
     249                 :            :                     gpointer      user_data)
     250                 :            : {
     251                 :          0 :   GSubprocess *subprocess = G_SUBPROCESS (source);
     252                 :          0 :   GTask *task = user_data;
     253                 :          0 :   GError *error = NULL;
     254                 :            :   gchar *stderr_str;
     255                 :            : 
     256         [ #  # ]:          0 :   if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
     257                 :            :     {
     258                 :          0 :       g_task_return_error (task, error);
     259                 :          0 :       g_error_free (error);
     260                 :            :     }
     261                 :            :   else /* successful communication */
     262                 :            :     {
     263         [ #  # ]:          0 :       if (!g_subprocess_get_successful (subprocess))
     264                 :            :         /* ...but bad exit code */
     265                 :          0 :         g_task_return_new_error_literal (task, G_IO_ERROR, G_IO_ERROR_FAILED, stderr_str);
     266                 :            :       else
     267                 :            :         /* ...and successful exit code */
     268                 :          0 :         g_task_return_boolean (task, TRUE);
     269                 :            : 
     270                 :          0 :       g_free (stderr_str);
     271                 :            :     }
     272                 :            : 
     273                 :          0 :   g_object_unref (task);
     274                 :          0 : }
     275                 :            : 
     276                 :            : static gboolean
     277                 :          0 : eject_unmount_do_cb (gpointer user_data)
     278                 :            : {
     279                 :          0 :   GTask *task = user_data;
     280                 :          0 :   GError *error = NULL;
     281                 :            :   GSubprocess *subprocess;
     282                 :            :   const gchar **argv;
     283                 :            : 
     284                 :          0 :   argv = g_task_get_task_data (task);
     285                 :            : 
     286         [ #  # ]:          0 :   if (g_task_return_error_if_cancelled (task))
     287                 :            :     {
     288                 :          0 :       g_object_unref (task);
     289                 :          0 :       return G_SOURCE_REMOVE;
     290                 :            :     }
     291                 :            : 
     292                 :          0 :   subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
     293                 :          0 :   g_assert_no_error (error);
     294                 :            : 
     295                 :          0 :   g_subprocess_communicate_utf8_async (subprocess, NULL,
     296                 :            :                                        g_task_get_cancellable (task),
     297                 :            :                                        eject_unmount_done, task);
     298                 :            : 
     299                 :          0 :   return G_SOURCE_REMOVE;
     300                 :            : }
     301                 :            : 
     302                 :            : static void
     303                 :          0 : eject_unmount_do (GMount              *mount,
     304                 :            :                   GCancellable        *cancellable,
     305                 :            :                   GAsyncReadyCallback  callback,
     306                 :            :                   gpointer             user_data,
     307                 :            :                   char               **argv,
     308                 :            :                   const gchar         *task_name)
     309                 :            : {
     310                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     311                 :            :   GTask *task;
     312                 :            :   GSource *timeout;
     313                 :            : 
     314                 :          0 :   task = g_task_new (mount, cancellable, callback, user_data);
     315         [ #  # ]:          0 :   g_task_set_source_tag (task, eject_unmount_do);
     316                 :          0 :   g_task_set_name (task, task_name);
     317                 :          0 :   g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev);
     318                 :            : 
     319         [ #  # ]:          0 :   if (unix_mount->volume_monitor != NULL)
     320                 :          0 :     g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
     321                 :            : 
     322                 :          0 :   g_signal_emit_by_name (mount, "pre-unmount", 0);
     323                 :            : 
     324                 :          0 :   timeout = g_timeout_source_new (500);
     325                 :          0 :   g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
     326                 :          0 :   g_source_unref (timeout);
     327                 :          0 : }
     328                 :            : 
     329                 :            : static void
     330                 :          0 : g_unix_mount_unmount (GMount             *mount,
     331                 :            :                       GMountUnmountFlags flags,
     332                 :            :                       GCancellable        *cancellable,
     333                 :            :                       GAsyncReadyCallback  callback,
     334                 :            :                       gpointer             user_data)
     335                 :            : {
     336                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     337                 :          0 :   char *argv[] = {"umount", NULL, NULL};
     338                 :            : 
     339         [ #  # ]:          0 :   if (unix_mount->mount_path != NULL)
     340                 :          0 :     argv[1] = unix_mount->mount_path;
     341                 :            :   else
     342                 :          0 :     argv[1] = unix_mount->device_path;
     343                 :            : 
     344                 :          0 :   eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] unmount mount");
     345                 :          0 : }
     346                 :            : 
     347                 :            : static gboolean
     348                 :          0 : g_unix_mount_unmount_finish (GMount       *mount,
     349                 :            :                              GAsyncResult  *result,
     350                 :            :                              GError       **error)
     351                 :            : {
     352                 :          0 :   return g_task_propagate_boolean (G_TASK (result), error);
     353                 :            : }
     354                 :            : 
     355                 :            : static void
     356                 :          0 : g_unix_mount_eject (GMount             *mount,
     357                 :            :                     GMountUnmountFlags flags,
     358                 :            :                     GCancellable        *cancellable,
     359                 :            :                     GAsyncReadyCallback  callback,
     360                 :            :                     gpointer             user_data)
     361                 :            : {
     362                 :          0 :   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
     363                 :          0 :   char *argv[] = {"eject", NULL, NULL};
     364                 :            : 
     365         [ #  # ]:          0 :   if (unix_mount->mount_path != NULL)
     366                 :          0 :     argv[1] = unix_mount->mount_path;
     367                 :            :   else
     368                 :          0 :     argv[1] = unix_mount->device_path;
     369                 :            : 
     370                 :          0 :   eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] eject mount");
     371                 :          0 : }
     372                 :            : 
     373                 :            : static gboolean
     374                 :          0 : g_unix_mount_eject_finish (GMount       *mount,
     375                 :            :                            GAsyncResult  *result,
     376                 :            :                            GError       **error)
     377                 :            : {
     378                 :          0 :   return g_task_propagate_boolean (G_TASK (result), error);
     379                 :            : }
     380                 :            : 
     381                 :            : static void
     382                 :          0 : g_unix_mount_mount_iface_init (GMountIface *iface)
     383                 :            : {
     384                 :          0 :   iface->get_root = g_unix_mount_get_root;
     385                 :          0 :   iface->get_name = g_unix_mount_get_name;
     386                 :          0 :   iface->get_icon = g_unix_mount_get_icon;
     387                 :          0 :   iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
     388                 :          0 :   iface->get_uuid = g_unix_mount_get_uuid;
     389                 :          0 :   iface->get_drive = g_unix_mount_get_drive;
     390                 :          0 :   iface->get_volume = g_unix_mount_get_volume;
     391                 :          0 :   iface->can_unmount = g_unix_mount_can_unmount;
     392                 :          0 :   iface->can_eject = g_unix_mount_can_eject;
     393                 :          0 :   iface->unmount = g_unix_mount_unmount;
     394                 :          0 :   iface->unmount_finish = g_unix_mount_unmount_finish;
     395                 :          0 :   iface->eject = g_unix_mount_eject;
     396                 :          0 :   iface->eject_finish = g_unix_mount_eject_finish;
     397                 :          0 : }

Generated by: LCOV version 1.14