(root)/
glib-2.79.0/
gio/
gio-tool-move.c
       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  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    tv = g_get_monotonic_time ();
      60    if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
      61        current_num_bytes != total_num_bytes)
      62      return;
      63  
      64    current_size = g_format_size (current_num_bytes);
      65    total_size = g_format_size (total_num_bytes);
      66    rate = g_format_size (current_num_bytes /
      67                          MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
      68    g_print ("\r\033[K");
      69    g_print (_("Transferred %s out of %s (%s/s)"),
      70             current_size, total_size, rate);
      71  
      72    previous_time = tv;
      73  
      74    g_free (current_size);
      75    g_free (total_size);
      76    g_free (rate);
      77  }
      78  
      79  int
      80  handle_move (int argc, char *argv[], gboolean do_help)
      81  {
      82    GOptionContext *context;
      83    gchar *param;
      84    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    int retval = 0;
      92  
      93    g_set_prgname ("gio move");
      94  
      95    /* Translators: commandline placeholder */
      96    param = g_strdup_printf ("%s%s", _("SOURCE"), _("DESTINATION"));
      97    context = g_option_context_new (param);
      98    g_free (param);
      99    g_option_context_set_help_enabled (context, FALSE);
     100    g_option_context_set_summary (context,
     101        _("Move one or more files from SOURCE to DEST."));
     102    g_option_context_set_description (context,
     103        _("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    g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
     107  
     108    if (do_help)
     109      {
     110        show_help (context, NULL);
     111        g_option_context_free (context);
     112        return 0;
     113      }
     114  
     115    if (!g_option_context_parse (context, &argc, &argv, &error))
     116      {
     117        show_help (context, error->message);
     118        g_error_free (error);
     119        g_option_context_free (context);
     120        return 1;
     121      }
     122  
     123    if (argc < 3)
     124      {
     125        show_help (context, NULL);
     126        g_option_context_free (context);
     127        return 1;
     128      }
     129  
     130    dest = g_file_new_for_commandline_arg (argv[argc - 1]);
     131  
     132    if (no_target_directory && argc > 3)
     133      {
     134        show_help (context, NULL);
     135        g_object_unref (dest);
     136        g_option_context_free (context);
     137        return 1;
     138      }
     139  
     140    dest_is_dir = file_is_dir (dest);
     141  
     142    if (!dest_is_dir && argc > 3)
     143      {
     144        char *message;
     145        message = g_strdup_printf (_("Target %s is not a directory"), argv[argc - 1]);
     146        show_help (context, message);
     147        g_free (message);
     148        g_object_unref (dest);
     149        g_option_context_free (context);
     150        return 1;
     151      }
     152  
     153    g_option_context_free (context);
     154  
     155    for (i = 1; i < argc - 1; i++)
     156      {
     157        source = g_file_new_for_commandline_arg (argv[i]);
     158  
     159        if (dest_is_dir && !no_target_directory)
     160          {
     161            basename = g_file_get_basename (source);
     162            target = g_file_get_child (dest, basename);
     163            g_free (basename);
     164          }
     165        else
     166          target = g_object_ref (dest);
     167  
     168        flags = 0;
     169        if (backup)
     170          flags |= G_FILE_COPY_BACKUP;
     171        if (!interactive)
     172          flags |= G_FILE_COPY_OVERWRITE;
     173        if (no_copy_fallback)
     174          flags |= G_FILE_COPY_NO_FALLBACK_FOR_MOVE;
     175  
     176        error = NULL;
     177        start_time = g_get_monotonic_time ();
     178        if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
     179          {
     180            if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
     181              {
     182                char line[16];
     183  
     184                g_error_free (error);
     185                error = NULL;
     186  
     187                uri = g_file_get_uri (target);
     188                g_print (_("%s: overwrite “%s”? "), argv[0], uri);
     189                g_free (uri);
     190                if (fgets (line, sizeof (line), stdin) &&
     191                    (line[0] == 'y' || line[0] == 'Y'))
     192                  {
     193                    flags |= G_FILE_COPY_OVERWRITE;
     194                    start_time = g_get_monotonic_time ();
     195                    if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
     196                      goto move_failed;
     197                  }
     198              }
     199            else
     200              {
     201              move_failed:
     202                print_file_error (source, error->message);
     203                g_error_free (error);
     204                retval = 1;
     205              }
     206          }
     207  
     208        if (progress && retval == 0)
     209          g_print("\n");
     210  
     211        g_object_unref (source);
     212        g_object_unref (target);
     213      }
     214  
     215    g_object_unref (dest);
     216  
     217    return retval;
     218  }