Blame gio/gio-tool-move.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
Packit ae235b
/* statics {{{1 */
Packit ae235b
Packit ae235b
static gboolean no_target_directory = FALSE;
Packit ae235b
static gboolean progress = FALSE;
Packit ae235b
static gboolean interactive = FALSE;
Packit ae235b
static gboolean backup = FALSE;
Packit ae235b
static gboolean no_copy_fallback = FALSE;
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
Packit ae235b
  { "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
Packit ae235b
  { "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
Packit ae235b
  { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
Packit ae235b
  { "no-copy-fallback", 'C', 0, G_OPTION_ARG_NONE, &no_copy_fallback, N_("Don’t use copy and delete fallback"), NULL },
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
static gint64 start_time;
Packit ae235b
static gint64 previous_time;
Packit ae235b
Packit ae235b
static void
Packit ae235b
show_progress (goffset current_num_bytes,
Packit ae235b
               goffset total_num_bytes,
Packit ae235b
               gpointer user_data)
Packit ae235b
{
Packit ae235b
  gint64 tv;
Packit ae235b
  char *current_size, *total_size, *rate;
Packit ae235b
Packit ae235b
  tv = g_get_monotonic_time ();
Packit ae235b
  if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
Packit ae235b
      current_num_bytes != total_num_bytes)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  current_size = g_format_size (current_num_bytes);
Packit ae235b
  total_size = g_format_size (total_num_bytes);
Packit ae235b
  rate = g_format_size (current_num_bytes /
Packit ae235b
                        MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
Packit ae235b
  g_print ("\r\033[K");
Packit ae235b
  g_print (_("Transferred %s out of %s (%s/s)"),
Packit ae235b
           current_size, total_size, rate);
Packit ae235b
Packit ae235b
  previous_time = tv;
Packit ae235b
Packit ae235b
  g_free (current_size);
Packit ae235b
  g_free (total_size);
Packit ae235b
  g_free (rate);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_move (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *source, *dest, *target;
Packit ae235b
  gboolean dest_is_dir;
Packit ae235b
  char *basename;
Packit ae235b
  char *uri;
Packit ae235b
  int i;
Packit ae235b
  GFileCopyFlags flags;
Packit ae235b
  int retval = 0;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio move");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("%s... %s", _("SOURCE"), _("DESTINATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
      _("Move one or more files from SOURCE to DEST."));
Packit ae235b
  g_option_context_set_description (context,
Packit ae235b
      _("gio move is similar to the traditional mv utility, but using GIO\n"
Packit ae235b
        "locations instead of local files: for example, you can use something\n"
Packit ae235b
        "like smb://server/resource/file.txt as location"));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argc < 3)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  dest = g_file_new_for_commandline_arg (argv[argc - 1]);
Packit ae235b
Packit ae235b
  if (no_target_directory && argc > 3)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_object_unref (dest);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  dest_is_dir = file_is_dir (dest);
Packit ae235b
Packit ae235b
  if (!dest_is_dir && argc > 3)
Packit ae235b
    {
Packit ae235b
      char *message;
Packit ae235b
      message = g_strdup_printf (_("Target %s is not a directory"), argv[argc - 1]);
Packit ae235b
      show_help (context, message);
Packit ae235b
      g_free (message);
Packit ae235b
      g_object_unref (dest);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  for (i = 1; i < argc - 1; i++)
Packit ae235b
    {
Packit ae235b
      source = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
Packit ae235b
      if (dest_is_dir && !no_target_directory)
Packit ae235b
        {
Packit ae235b
          basename = g_file_get_basename (source);
Packit ae235b
          target = g_file_get_child (dest, basename);
Packit ae235b
          g_free (basename);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        target = g_object_ref (dest);
Packit ae235b
Packit ae235b
      flags = 0;
Packit ae235b
      if (backup)
Packit ae235b
        flags |= G_FILE_COPY_BACKUP;
Packit ae235b
      if (!interactive)
Packit ae235b
        flags |= G_FILE_COPY_OVERWRITE;
Packit ae235b
      if (no_copy_fallback)
Packit ae235b
        flags |= G_FILE_COPY_NO_FALLBACK_FOR_MOVE;
Packit ae235b
Packit ae235b
      error = NULL;
Packit ae235b
      start_time = g_get_monotonic_time ();
Packit ae235b
      if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
Packit ae235b
        {
Packit ae235b
          if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
Packit ae235b
            {
Packit ae235b
              char line[16];
Packit ae235b
Packit ae235b
              g_error_free (error);
Packit ae235b
              error = NULL;
Packit ae235b
Packit ae235b
              uri = g_file_get_uri (target);
Packit ae235b
              g_print (_("%s: overwrite “%s”? "), argv[0], uri);
Packit ae235b
              g_free (uri);
Packit ae235b
              if (fgets (line, sizeof (line), stdin) &&
Packit ae235b
                  (line[0] == 'y' || line[0] == 'Y'))
Packit ae235b
                {
Packit ae235b
                  flags |= G_FILE_COPY_OVERWRITE;
Packit ae235b
                  start_time = g_get_monotonic_time ();
Packit ae235b
                  if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
Packit ae235b
                    goto move_failed;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
            move_failed:
Packit ae235b
              print_file_error (source, error->message);
Packit ae235b
              g_error_free (error);
Packit ae235b
              retval = 1;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (progress && retval == 0)
Packit ae235b
        g_print("\n");
Packit ae235b
Packit ae235b
      g_object_unref (source);
Packit ae235b
      g_object_unref (target);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (dest);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}