LCOV - code coverage report
Current view: top level - glib/gio - gio-tool-move.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 95 0.0 %
Date: 2024-04-23 05:16:05 Functions: 0 2 0.0 %
Branches: 0 54 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                 :            : #include <stdio.h>
      27                 :            : 
      28                 :            : #include "gio-tool.h"
      29                 :            : 
      30                 :            : 
      31                 :            : /* statics {{{1 */
      32                 :            : 
      33                 :            : static gboolean no_target_directory = FALSE;
      34                 :            : static gboolean progress = FALSE;
      35                 :            : static gboolean interactive = FALSE;
      36                 :            : static gboolean backup = FALSE;
      37                 :            : static gboolean no_copy_fallback = FALSE;
      38                 :            : 
      39                 :            : static const GOptionEntry entries[] = {
      40                 :            :   { "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
      41                 :            :   { "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
      42                 :            :   { "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
      43                 :            :   { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
      44                 :            :   { "no-copy-fallback", 'C', 0, G_OPTION_ARG_NONE, &no_copy_fallback, N_("Don’t use copy and delete fallback"), NULL },
      45                 :            :   G_OPTION_ENTRY_NULL
      46                 :            : };
      47                 :            : 
      48                 :            : static gint64 start_time;
      49                 :            : static gint64 previous_time;
      50                 :            : 
      51                 :            : static void
      52                 :          0 : show_progress (goffset current_num_bytes,
      53                 :            :                goffset total_num_bytes,
      54                 :            :                gpointer user_data)
      55                 :            : {
      56                 :            :   gint64 tv;
      57                 :            :   char *current_size, *total_size, *rate;
      58                 :            : 
      59                 :          0 :   tv = g_get_monotonic_time ();
      60   [ #  #  #  # ]:          0 :   if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
      61                 :            :       current_num_bytes != total_num_bytes)
      62                 :          0 :     return;
      63                 :            : 
      64                 :          0 :   current_size = g_format_size (current_num_bytes);
      65                 :          0 :   total_size = g_format_size (total_num_bytes);
      66                 :          0 :   rate = g_format_size (current_num_bytes /
      67         [ #  # ]:          0 :                         MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
      68                 :          0 :   g_print ("\r\033[K");
      69                 :          0 :   g_print (_("Transferred %s out of %s (%s/s)"),
      70                 :            :            current_size, total_size, rate);
      71                 :            : 
      72                 :          0 :   previous_time = tv;
      73                 :            : 
      74                 :          0 :   g_free (current_size);
      75                 :          0 :   g_free (total_size);
      76                 :          0 :   g_free (rate);
      77                 :            : }
      78                 :            : 
      79                 :            : int
      80                 :          0 : handle_move (int argc, char *argv[], gboolean do_help)
      81                 :            : {
      82                 :            :   GOptionContext *context;
      83                 :            :   gchar *param;
      84                 :          0 :   GError *error = NULL;
      85                 :            :   GFile *source, *dest, *target;
      86                 :            :   gboolean dest_is_dir;
      87                 :            :   char *basename;
      88                 :            :   char *uri;
      89                 :            :   int i;
      90                 :            :   GFileCopyFlags flags;
      91                 :          0 :   int retval = 0;
      92                 :            : 
      93                 :          0 :   g_set_prgname ("gio move");
      94                 :            : 
      95                 :            :   /* Translators: commandline placeholder */
      96                 :          0 :   param = g_strdup_printf ("%s… %s", _("SOURCE"), _("DESTINATION"));
      97                 :          0 :   context = g_option_context_new (param);
      98                 :          0 :   g_free (param);
      99                 :          0 :   g_option_context_set_help_enabled (context, FALSE);
     100                 :          0 :   g_option_context_set_summary (context,
     101                 :          0 :       _("Move one or more files from SOURCE to DEST."));
     102                 :          0 :   g_option_context_set_description (context,
     103                 :          0 :       _("gio move is similar to the traditional mv utility, but using GIO\n"
     104                 :            :         "locations instead of local files: for example, you can use something\n"
     105                 :            :         "like smb://server/resource/file.txt as location"));
     106                 :          0 :   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
     107                 :            : 
     108         [ #  # ]:          0 :   if (do_help)
     109                 :            :     {
     110                 :          0 :       show_help (context, NULL);
     111                 :          0 :       g_option_context_free (context);
     112                 :          0 :       return 0;
     113                 :            :     }
     114                 :            : 
     115         [ #  # ]:          0 :   if (!g_option_context_parse (context, &argc, &argv, &error))
     116                 :            :     {
     117                 :          0 :       show_help (context, error->message);
     118                 :          0 :       g_error_free (error);
     119                 :          0 :       g_option_context_free (context);
     120                 :          0 :       return 1;
     121                 :            :     }
     122                 :            : 
     123         [ #  # ]:          0 :   if (argc < 3)
     124                 :            :     {
     125                 :          0 :       show_help (context, NULL);
     126                 :          0 :       g_option_context_free (context);
     127                 :          0 :       return 1;
     128                 :            :     }
     129                 :            : 
     130                 :          0 :   dest = g_file_new_for_commandline_arg (argv[argc - 1]);
     131                 :            : 
     132   [ #  #  #  # ]:          0 :   if (no_target_directory && argc > 3)
     133                 :            :     {
     134                 :          0 :       show_help (context, NULL);
     135                 :          0 :       g_object_unref (dest);
     136                 :          0 :       g_option_context_free (context);
     137                 :          0 :       return 1;
     138                 :            :     }
     139                 :            : 
     140                 :          0 :   dest_is_dir = file_is_dir (dest);
     141                 :            : 
     142   [ #  #  #  # ]:          0 :   if (!dest_is_dir && argc > 3)
     143                 :            :     {
     144                 :            :       char *message;
     145                 :          0 :       message = g_strdup_printf (_("Target %s is not a directory"), argv[argc - 1]);
     146                 :          0 :       show_help (context, message);
     147                 :          0 :       g_free (message);
     148                 :          0 :       g_object_unref (dest);
     149                 :          0 :       g_option_context_free (context);
     150                 :          0 :       return 1;
     151                 :            :     }
     152                 :            : 
     153                 :          0 :   g_option_context_free (context);
     154                 :            : 
     155         [ #  # ]:          0 :   for (i = 1; i < argc - 1; i++)
     156                 :            :     {
     157                 :          0 :       source = g_file_new_for_commandline_arg (argv[i]);
     158                 :            : 
     159   [ #  #  #  # ]:          0 :       if (dest_is_dir && !no_target_directory)
     160                 :            :         {
     161                 :          0 :           basename = g_file_get_basename (source);
     162                 :          0 :           target = g_file_get_child (dest, basename);
     163                 :          0 :           g_free (basename);
     164                 :            :         }
     165                 :            :       else
     166                 :          0 :         target = g_object_ref (dest);
     167                 :            : 
     168                 :          0 :       flags = 0;
     169         [ #  # ]:          0 :       if (backup)
     170                 :          0 :         flags |= G_FILE_COPY_BACKUP;
     171         [ #  # ]:          0 :       if (!interactive)
     172                 :          0 :         flags |= G_FILE_COPY_OVERWRITE;
     173         [ #  # ]:          0 :       if (no_copy_fallback)
     174                 :          0 :         flags |= G_FILE_COPY_NO_FALLBACK_FOR_MOVE;
     175                 :            : 
     176                 :          0 :       error = NULL;
     177                 :          0 :       start_time = g_get_monotonic_time ();
     178   [ #  #  #  # ]:          0 :       if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
     179                 :            :         {
     180   [ #  #  #  # ]:          0 :           if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
     181                 :          0 :             {
     182                 :            :               char line[16];
     183                 :            : 
     184                 :          0 :               g_error_free (error);
     185                 :          0 :               error = NULL;
     186                 :            : 
     187                 :          0 :               uri = g_file_get_uri (target);
     188                 :          0 :               g_print (_("%s: overwrite “%s”? "), argv[0], uri);
     189                 :          0 :               g_free (uri);
     190         [ #  # ]:          0 :               if (fgets (line, sizeof (line), stdin) &&
     191   [ #  #  #  # ]:          0 :                   (line[0] == 'y' || line[0] == 'Y'))
     192                 :            :                 {
     193                 :          0 :                   flags |= G_FILE_COPY_OVERWRITE;
     194                 :          0 :                   start_time = g_get_monotonic_time ();
     195   [ #  #  #  # ]:          0 :                   if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
     196                 :          0 :                     goto move_failed;
     197                 :            :                 }
     198                 :            :             }
     199                 :            :           else
     200                 :            :             {
     201                 :          0 :             move_failed:
     202                 :          0 :               print_file_error (source, error->message);
     203                 :          0 :               g_error_free (error);
     204                 :          0 :               retval = 1;
     205                 :            :             }
     206                 :            :         }
     207                 :            : 
     208   [ #  #  #  # ]:          0 :       if (progress && retval == 0)
     209                 :          0 :         g_print("\n");
     210                 :            : 
     211                 :          0 :       g_object_unref (source);
     212                 :          0 :       g_object_unref (target);
     213                 :            :     }
     214                 :            : 
     215                 :          0 :   g_object_unref (dest);
     216                 :            : 
     217                 :          0 :   return retval;
     218                 :            : }

Generated by: LCOV version 1.14