bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mv: minor bug fix with --verbose --backup and directories


From: Jim Meyering
Subject: mv: minor bug fix with --verbose --backup and directories
Date: Sat, 26 Aug 2006 20:43:41 +0200

This fixes a minor inconsistency.
With --verbose, mv would report the backup file name
for a non-directory destination, but not for directories:

  $ rm -rf a b; touch a b; mv --verbose --backup -T a b
  `a' -> `b' (backup: `b.~2~')
             ^^^^^^^^^^^^^^^^^
But for directories, no "(backup: NAME)" suffix:

  $ rm -rf a b; mkdir a b; mv --verbose --backup -T a b
  `a' -> `b'
  $ ls -d b*~
  b.~1~

Note that although I mentioned mv's -T option below,
you can reproduce the problem without it:

   mkdir -p A B/A; mv --verbose --backup A B


2006-08-26  Jim Meyering  <address@hidden>

        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.
        * tests/mv/Makefile.am (TESTS): Add backup-dir.

Index: NEWS
===================================================================
RCS file: /fetish/cu/NEWS,v
retrieving revision 1.411
diff -u -r1.411 NEWS
--- NEWS        26 Aug 2006 06:46:17 -0000      1.411
+++ NEWS        26 Aug 2006 14:22:59 -0000
@@ -15,6 +15,9 @@
   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: src/copy.c
===================================================================
RCS file: /fetish/cu/src/copy.c,v
retrieving revision 1.205
diff -u -r1.205 copy.c
--- src/copy.c  19 Aug 2006 14:01:30 -0000      1.205
+++ src/copy.c  26 Aug 2006 14:12:08 -0000
@@ -933,6 +933,18 @@
               && ! 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 @@
    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 @@
                }
            }
 
-         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 @@
      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 @@
       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 @@
          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?  */
Index: tests/mv/backup-dir
===================================================================
RCS file: tests/mv/backup-dir
diff -N tests/mv/backup-dir
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/mv/backup-dir 26 Aug 2006 15:52:30 -0000
@@ -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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]