LCOV - code coverage report
Current view: top level - gio - gio-tool-trash.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 0.0 % 156 0
Test Date: 2026-02-03 14:41:24 Functions: 0.0 % 4 0
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright 2015 Red Hat, Inc.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :             :  *
       6                 :             :  * This library is free software; you can redistribute it and/or
       7                 :             :  * modify it under the terms of the GNU Lesser General Public
       8                 :             :  * License as published by the Free Software Foundation; either
       9                 :             :  * version 2.1 of the License, or (at your option) any later version.
      10                 :             :  *
      11                 :             :  * This library is distributed in the hope that it will be useful,
      12                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :             :  * Lesser General Public License for more details.
      15                 :             :  *
      16                 :             :  * You should have received a copy of the GNU Lesser General Public
      17                 :             :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :             :  *
      19                 :             :  * Author: Matthias Clasen <mclasen@redhat.com>
      20                 :             :  */
      21                 :             : 
      22                 :             : #include "config.h"
      23                 :             : 
      24                 :             : #include <gio/gio.h>
      25                 :             : #include <gi18n.h>
      26                 :             : 
      27                 :             : #include "gio-tool.h"
      28                 :             : 
      29                 :             : 
      30                 :             : static gboolean global_force = FALSE;
      31                 :             : static gboolean empty = FALSE;
      32                 :             : static gboolean restore = FALSE;
      33                 :             : static gboolean list = FALSE;
      34                 :             : static const GOptionEntry entries[] = {
      35                 :             :   { "force", 'f', 0, G_OPTION_ARG_NONE, &global_force, N_("Ignore nonexistent files, never prompt"), NULL },
      36                 :             :   { "empty", 0, 0, G_OPTION_ARG_NONE, &empty, N_("Empty the trash"), NULL },
      37                 :             :   { "list", 0, 0, G_OPTION_ARG_NONE, &list, N_("List files in the trash with their original locations"), NULL },
      38                 :             :   { "restore", 0, 0, G_OPTION_ARG_NONE, &restore, N_("Restore a file from trash to its original location (possibly "
      39                 :             :                                                      "recreating the directory)"), NULL },
      40                 :             :   G_OPTION_ENTRY_NULL
      41                 :             : };
      42                 :             : 
      43                 :             : static gboolean
      44                 :           0 : delete_trash_file (GFile *file, gboolean del_file, gboolean del_children, GError **error)
      45                 :             : {
      46                 :             :   GFileInfo *info;
      47                 :             :   GFile *child;
      48                 :             :   GFileEnumerator *enumerator;
      49                 :           0 :   GError *local_error = NULL;
      50                 :           0 :   gboolean success = TRUE;
      51                 :             : 
      52                 :           0 :   g_return_val_if_fail (g_file_has_uri_scheme (file, "trash"), FALSE);
      53                 :             : 
      54                 :           0 :   if (del_children)
      55                 :             :     {
      56                 :           0 :       enumerator = g_file_enumerate_children (file,
      57                 :             :                                               G_FILE_ATTRIBUTE_STANDARD_NAME ","
      58                 :             :                                               G_FILE_ATTRIBUTE_STANDARD_TYPE,
      59                 :             :                                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
      60                 :             :                                               NULL,
      61                 :             :                                               &local_error);
      62                 :           0 :       if (!enumerator)
      63                 :             :         {
      64                 :           0 :           g_propagate_error (error, g_steal_pointer (&local_error));
      65                 :           0 :           return FALSE;
      66                 :             :         }
      67                 :             : 
      68                 :           0 :       while ((info = g_file_enumerator_next_file (enumerator, NULL, &local_error)) != NULL)
      69                 :             :         {
      70                 :           0 :           child = g_file_get_child (file, g_file_info_get_name (info));
      71                 :             : 
      72                 :             :           /* The g_file_delete operation works differently for locations
      73                 :             :            * provided by the trash backend as it prevents modifications of
      74                 :             :            * trashed items. For that reason, it is enough to call
      75                 :             :            * g_file_delete on top-level items only.
      76                 :             :            */
      77                 :           0 :           if (!delete_trash_file (child, TRUE, FALSE, &local_error))
      78                 :             :             {
      79                 :           0 :               g_object_unref (child);
      80                 :           0 :               g_object_unref (info);
      81                 :           0 :               success = FALSE;
      82                 :           0 :               break;
      83                 :             :             }
      84                 :             : 
      85                 :           0 :           g_object_unref (child);
      86                 :           0 :           g_object_unref (info);
      87                 :             :         }
      88                 :             : 
      89                 :           0 :       if (local_error)
      90                 :             :         {
      91                 :           0 :           g_propagate_error (error, g_steal_pointer (&local_error));
      92                 :           0 :           success = FALSE;
      93                 :             :         }
      94                 :             : 
      95                 :           0 :       if (!g_file_enumerator_close (enumerator, NULL, &local_error))
      96                 :             :         {
      97                 :           0 :           if (success)
      98                 :           0 :             g_propagate_error (error, g_steal_pointer (&local_error));
      99                 :             :           else
     100                 :           0 :             g_clear_error (&local_error);
     101                 :           0 :           success = FALSE;
     102                 :             :         }
     103                 :             : 
     104                 :           0 :       g_object_unref (enumerator);
     105                 :             : 
     106                 :           0 :       if (!success)
     107                 :           0 :         return FALSE;
     108                 :             :     }
     109                 :             : 
     110                 :           0 :   if (del_file)
     111                 :             :     {
     112                 :           0 :       if (!g_file_delete (file, NULL, &local_error))
     113                 :             :         {
     114                 :           0 :           g_propagate_error (error, g_steal_pointer (&local_error));
     115                 :           0 :           return FALSE;
     116                 :             :         }
     117                 :             :     }
     118                 :             : 
     119                 :           0 :   return TRUE;
     120                 :             : }
     121                 :             : 
     122                 :             : static gboolean
     123                 :           0 : restore_trash (GFile         *file,
     124                 :             :                gboolean       force,
     125                 :             :                GCancellable  *cancellable,
     126                 :             :                GError       **error)
     127                 :             : {
     128                 :           0 :   GFileInfo *info = NULL;
     129                 :           0 :   GFile *target = NULL;
     130                 :           0 :   GFile *dir_target = NULL;
     131                 :           0 :   gboolean ret = FALSE;
     132                 :           0 :   const gchar *orig_path = NULL;
     133                 :           0 :   GError *local_error = NULL;
     134                 :             : 
     135                 :           0 :   info = g_file_query_info (file, G_FILE_ATTRIBUTE_TRASH_ORIG_PATH, G_FILE_QUERY_INFO_NONE, cancellable, &local_error);
     136                 :           0 :   if (local_error)
     137                 :             :     {
     138                 :           0 :       g_propagate_error (error, local_error);
     139                 :           0 :       goto exit_func;
     140                 :             :     }
     141                 :             : 
     142                 :           0 :   orig_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_TRASH_ORIG_PATH);
     143                 :           0 :   if (!orig_path)
     144                 :             :     {
     145                 :           0 :       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("Unable to find original path"));
     146                 :           0 :       goto exit_func;
     147                 :             :     }
     148                 :             : 
     149                 :           0 :   target = g_file_new_for_commandline_arg (orig_path);
     150                 :             : 
     151                 :           0 :   dir_target = g_file_get_parent (target);
     152                 :           0 :   if (dir_target)
     153                 :             :     {
     154                 :           0 :       g_file_make_directory_with_parents (dir_target, cancellable, &local_error);
     155                 :           0 :       if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
     156                 :             :         {
     157                 :           0 :           g_clear_error (&local_error);
     158                 :             :         }
     159                 :           0 :       else if (local_error != NULL)
     160                 :             :         {
     161                 :           0 :           g_propagate_prefixed_error (error, local_error, _("Unable to recreate original location: "));
     162                 :           0 :           goto exit_func;
     163                 :             :         }
     164                 :             :     }
     165                 :             : 
     166                 :           0 :   if (!g_file_move (file,
     167                 :             :                     target,
     168                 :             :                     force ? G_FILE_COPY_OVERWRITE : G_FILE_COPY_NONE,
     169                 :             :                     cancellable,
     170                 :             :                     NULL,
     171                 :             :                     NULL,
     172                 :             :                     &local_error))
     173                 :             :     {
     174                 :           0 :       g_propagate_prefixed_error (error, local_error, _("Unable to move file to its original location: "));
     175                 :           0 :       goto exit_func;
     176                 :             :     }
     177                 :           0 :   ret = TRUE;
     178                 :             : 
     179                 :           0 : exit_func:
     180                 :           0 :   g_clear_object (&target);
     181                 :           0 :   g_clear_object (&dir_target);
     182                 :           0 :   g_clear_object (&info);
     183                 :           0 :   return ret;
     184                 :             : }
     185                 :             : 
     186                 :             : static gboolean
     187                 :           0 : trash_list (GFile         *file,
     188                 :             :             GCancellable  *cancellable,
     189                 :             :             GError       **error)
     190                 :             : {
     191                 :             :   GFileEnumerator *enumerator;
     192                 :             :   GFileInfo *info;
     193                 :           0 :   GError *local_error = NULL;
     194                 :             :   gboolean res;
     195                 :             : 
     196                 :           0 :   enumerator = g_file_enumerate_children (file,
     197                 :             :                                           G_FILE_ATTRIBUTE_STANDARD_NAME ","
     198                 :             :                                           G_FILE_ATTRIBUTE_TRASH_ORIG_PATH,
     199                 :             :                                           G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
     200                 :             :                                           cancellable,
     201                 :             :                                           &local_error);
     202                 :           0 :   if (!enumerator)
     203                 :             :     {
     204                 :           0 :       g_propagate_error (error, local_error);
     205                 :           0 :       return FALSE;
     206                 :             :     }
     207                 :             : 
     208                 :           0 :   res = TRUE;
     209                 :           0 :   while ((info = g_file_enumerator_next_file (enumerator, cancellable, &local_error)) != NULL)
     210                 :             :     {
     211                 :             :       const char *name;
     212                 :             :       const char *orig_path;
     213                 :             :       char *uri;
     214                 :             :       gchar *utf8_path;
     215                 :             :       GFile* child;
     216                 :             : 
     217                 :           0 :       name = g_file_info_get_name (info);
     218                 :           0 :       child = g_file_get_child (file, name);
     219                 :           0 :       uri = g_file_get_uri (child);
     220                 :           0 :       g_object_unref (child);
     221                 :           0 :       orig_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_TRASH_ORIG_PATH);
     222                 :           0 :       utf8_path = g_filename_to_utf8 (orig_path, -1, NULL, NULL, NULL);
     223                 :             : 
     224                 :           0 :       g_print ("%s\t%s\n", uri, utf8_path);
     225                 :             : 
     226                 :           0 :       g_object_unref (info);
     227                 :           0 :       g_free (uri);
     228                 :           0 :       g_free (utf8_path);
     229                 :             :     }
     230                 :             : 
     231                 :           0 :   if (local_error)
     232                 :             :     {
     233                 :           0 :       g_propagate_error (error, local_error);
     234                 :           0 :       local_error = NULL;
     235                 :           0 :       res = FALSE;
     236                 :             :     }
     237                 :             : 
     238                 :           0 :   if (!g_file_enumerator_close (enumerator, cancellable, &local_error))
     239                 :             :     {
     240                 :           0 :       print_file_error (file, local_error->message);
     241                 :           0 :       g_clear_error (&local_error);
     242                 :           0 :       res = FALSE;
     243                 :             :     }
     244                 :             : 
     245                 :           0 :   g_object_unref (enumerator);
     246                 :             : 
     247                 :           0 :   return res;
     248                 :             : }
     249                 :             : 
     250                 :             : int
     251                 :           0 : handle_trash (int argc, char *argv[], gboolean do_help)
     252                 :             : {
     253                 :             :   GOptionContext *context;
     254                 :             :   gchar *param;
     255                 :           0 :   GError *error = NULL;
     256                 :           0 :   int retval = 0;
     257                 :             :   GFile *file;
     258                 :             : 
     259                 :           0 :   g_set_prgname ("gio trash");
     260                 :             : 
     261                 :             :   /* Translators: commandline placeholder */
     262                 :           0 :   param = g_strdup_printf ("[%s…]", _("LOCATION"));
     263                 :           0 :   context = g_option_context_new (param);
     264                 :           0 :   g_free (param);
     265                 :           0 :   g_option_context_set_help_enabled (context, FALSE);
     266                 :           0 :   g_option_context_set_summary (context,
     267                 :           0 :       _("Move/Restore files or directories to the trash."));
     268                 :           0 :   g_option_context_set_description (context,
     269                 :           0 :       _("Note: for --restore switch, if the original location of the trashed file \n"
     270                 :             :         "already exists, it will not be overwritten unless --force is set."));
     271                 :           0 :   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
     272                 :             : 
     273                 :           0 :   if (do_help)
     274                 :             :     {
     275                 :           0 :       show_help (context, NULL);
     276                 :           0 :       g_option_context_free (context);
     277                 :           0 :       return 0;
     278                 :             :     }
     279                 :             : 
     280                 :           0 :   if (!g_option_context_parse (context, &argc, &argv, &error))
     281                 :             :     {
     282                 :           0 :       show_help (context, error->message);
     283                 :           0 :       g_error_free (error);
     284                 :           0 :       g_option_context_free (context);
     285                 :           0 :       return 1;
     286                 :             :     }
     287                 :             : 
     288                 :           0 :   if (argc > 1)
     289                 :             :     {
     290                 :             :       int i;
     291                 :             : 
     292                 :           0 :       for (i = 1; i < argc; i++)
     293                 :             :         {
     294                 :           0 :           file = g_file_new_for_commandline_arg (argv[i]);
     295                 :           0 :           error = NULL;
     296                 :           0 :           if (restore)
     297                 :             :             {
     298                 :           0 :               if (!g_file_has_uri_scheme (file, "trash"))
     299                 :             :                 {
     300                 :           0 :                   print_file_error (file, _("Location given doesn't start with trash:///"));
     301                 :           0 :                   retval = 1;
     302                 :             :                 }
     303                 :           0 :               else if (!restore_trash (file, global_force, NULL, &error))
     304                 :             :                 {
     305                 :           0 :                   print_file_error (file, error->message);
     306                 :           0 :                   retval = 1;
     307                 :             :                 }
     308                 :             :             }
     309                 :           0 :           else if (!g_file_trash (file, NULL, &error))
     310                 :             :             {
     311                 :           0 :               if (!global_force ||
     312                 :           0 :                   !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
     313                 :             :                 {
     314                 :           0 :                   print_file_error (file, error->message);
     315                 :           0 :                   retval = 1;
     316                 :             :                 }
     317                 :             :             }
     318                 :           0 :           g_clear_error (&error);
     319                 :           0 :           g_object_unref (file);
     320                 :             :         }
     321                 :             :     }
     322                 :           0 :   else if (list)
     323                 :             :     {
     324                 :           0 :       file = g_file_new_for_uri ("trash:");
     325                 :           0 :       trash_list (file, NULL, &error);
     326                 :           0 :       if (error)
     327                 :             :         {
     328                 :           0 :           print_file_error (file, error->message);
     329                 :           0 :           g_clear_error (&error);
     330                 :           0 :           retval = 1;
     331                 :             :         }
     332                 :           0 :       g_object_unref (file);
     333                 :             :     }
     334                 :           0 :   else if (empty)
     335                 :             :     {
     336                 :           0 :       file = g_file_new_for_uri ("trash:");
     337                 :           0 :       if (!delete_trash_file (file, FALSE, TRUE, &error))
     338                 :             :         {
     339                 :           0 :           print_file_error (file, error->message);
     340                 :           0 :           g_clear_error (&error);
     341                 :           0 :           retval = 1;
     342                 :             :         }
     343                 :           0 :       g_object_unref (file);
     344                 :             :     }
     345                 :             : 
     346                 :           0 :   if (argc == 1 && !empty && !list)
     347                 :             :     {
     348                 :           0 :       show_help (context, _("No locations given"));
     349                 :           0 :       g_option_context_free (context);
     350                 :           0 :       return 1;
     351                 :             :     }
     352                 :             : 
     353                 :           0 :   g_option_context_free (context);
     354                 :             : 
     355                 :           0 :   return retval;
     356                 :             : }
        

Generated by: LCOV version 2.0-1