]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Fix "mv --verbose --backup" so its output includes the
authorJim Meyering <jim@meyering.net>
Sat, 26 Aug 2006 15:53:24 +0000 (15:53 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 26 Aug 2006 15:53:24 +0000 (15:53 +0000)
" (backup: foo.~1~)" suffix also when backing up a directory.
* NEWS: Report this bug fix.
* src/copy.c (emit_verbose): New function, factored out of...
(copy_internal): ...here.  Use the new function.
* tests/mv/backup-dir: Test for the above fix.

ChangeLog
NEWS
src/copy.c
tests/mv/backup-dir [new file with mode: 0755]

index 79f7133a65f09cc0568374181eef078359b9ab06..898f8748dc41b46c10177039d16e12ea468a96d2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2006-08-26  Jim Meyering  <jim@meyering.net>
+
+       Fix "mv --verbose --backup" so its output includes the
+       " (backup: foo.~1~)" suffix also when backing up a directory.
+       * NEWS: Report this bug fix.
+       * src/copy.c (emit_verbose): New function, factored out of...
+       (copy_internal): ...here.  Use the new function.
+       * tests/mv/backup-dir: Test for the above fix.
+
 2006-08-25  Paul Eggert  <eggert@cs.ucla.edu>
 
        * .x-sc_no_if_have_config_h: Remove; no longer needed.
diff --git a/NEWS b/NEWS
index d98f13bf4b51c727cce282d5b395dcb125dd2810..b9c984bbe6b4d90e1b3d19f46e7a27b022d01ae9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   no differently than regular directories on a file system with
   dirent.d_type support.
 
+  "mv -T --verbose --backup=t A B" now prints the " (backup: B.~1~)"
+  suffix when A and B are directories as well as when they are not.
+
 
 * Major changes in release 6.1 (2006-08-19) [unstable]
 
index e11dd77f840fe00e1df7279b1fa414d6f6ffa1e4..9ca2ae9df07afe7bb47beb44327fa737520f61e3 100644 (file)
@@ -933,6 +933,18 @@ abandon_move (const struct cp_options *x,
               && ! yesno ()));
 }
 
+/* Print --verbose output on standard output, e.g. `new' -> `old'.
+   If BACKUP_DST_NAME is non-NULL, then also indicate that it is
+   the name of a backup file.  */
+static void
+emit_verbose (char const *src, char const *dst, char const *backup_dst_name)
+{
+  printf ("%s -> %s", quote_n (0, src), quote_n (1, dst));
+  if (backup_dst_name)
+    printf (_(" (backup: %s)"), quote (backup_dst_name));
+  putchar ('\n');
+}
+
 /* Copy the file SRC_NAME to the file DST_NAME.  The files may be of
    any type.  NEW_DST should be true if the file DST_NAME cannot
    exist because its parent directory was just created; NEW_DST should
@@ -944,7 +956,6 @@ abandon_move (const struct cp_options *x,
    Set *COPY_INTO_SELF if SRC_NAME is a parent of (or the
    same as) DST_NAME; otherwise, clear it.
    Return true if successful.  */
-
 static bool
 copy_internal (char const *src_name, char const *dst_name,
               bool new_dst,
@@ -1169,9 +1180,7 @@ copy_internal (char const *src_name, char const *dst_name,
                }
            }
 
-         bool backup_directories = true;
-         if (x->backup_type != no_backups
-             && (!S_ISDIR (dst_sb.st_mode) || backup_directories))
+         if (x->backup_type != no_backups)
            {
              char *tmp_backup = find_backup_file_name (dst_name,
                                                        x->backup_type);
@@ -1242,12 +1251,7 @@ copy_internal (char const *src_name, char const *dst_name,
      directory.  So --verbose should not announce anything until we're
      sure we'll create a directory. */
   if (x->verbose && !S_ISDIR (src_type))
-    {
-      printf ("%s -> %s", quote_n (0, src_name), quote_n (1, dst_name));
-      if (backup_succeeded)
-       printf (_(" (backup: %s)"), quote (dst_backup));
-      putchar ('\n');
-    }
+    emit_verbose (src_name, dst_name, backup_succeeded ? dst_backup : NULL);
 
   /* Associate the destination file name with the source device and inode
      so that if we encounter a matching dev/ino pair in the source tree
@@ -1362,7 +1366,9 @@ copy_internal (char const *src_name, char const *dst_name,
       if (rename (src_name, dst_name) == 0)
        {
          if (x->verbose && S_ISDIR (src_type))
-           printf ("%s -> %s\n", quote_n (0, src_name), quote_n (1, dst_name));
+           emit_verbose (src_name, dst_name,
+                         backup_succeeded ? dst_backup : NULL);
+
          if (rename_succeeded)
            *rename_succeeded = true;
 
@@ -1526,7 +1532,7 @@ copy_internal (char const *src_name, char const *dst_name,
          remember_copied (dst_name, dst_sb.st_ino, dst_sb.st_dev);
 
          if (x->verbose)
-           printf ("%s -> %s\n", quote_n (0, src_name), quote_n (1, dst_name));
+           emit_verbose (src_name, dst_name, NULL);
        }
 
       /* Are we crossing a file system boundary?  */
diff --git a/tests/mv/backup-dir b/tests/mv/backup-dir
new file mode 100755 (executable)
index 0000000..ea2c6b7
--- /dev/null
@@ -0,0 +1,57 @@
+#!/bin/sh
+# Ensure "mv --verbose --backup" works the same for dirs and non-dirs.
+
+# Copyright (C) 2006 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  mv --version
+fi
+
+. $srcdir/../envvar-check
+. $srcdir/../lang-default
+
+pwd=`pwd`
+t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
+trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
+trap '(exit $?); exit $?' 1 2 13 15
+
+framework_failure=0
+mkdir -p $tmp || framework_failure=1
+cd $tmp || framework_failure=1
+
+mkdir A B || framework_failure=1
+touch X Y || framework_failure=1
+
+if test $framework_failure = 1; then
+  echo "$0: failure in testing framework" 1>&2
+  (exit 1); exit 1
+fi
+
+fail=0
+
+# Before coreutils-6.2, the " (backup: `B.~1~')" suffix was not printed.
+mv --verbose --backup=numbered -T A B > out || fail=1
+cat <<\EOF > exp || fail=1
+`A' -> `B' (backup: `B.~1~')
+EOF
+
+cmp out exp || fail=1
+test $fail = 1 && diff out exp 2> /dev/null
+
+(exit $fail); exit $fail