From 0cafba44bbef7c5eb64979544a1ec95a389ec55f Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 18 Nov 2006 20:00:39 +0100 Subject: "ln --backup f f" produces a misleading diagnostic: ln: creating hard link `f' => `f': No such file or directory * src/ln.c (do_link): Give a better diagnostic in this unusual case. (do_link): Rename local: s/lstat_ok/dest_lstat_ok/. * tests/ln/Makefile.am (TESTS): Add hard-backup. * tests/ln/hard-backup: New test for the above. --- ChangeLog | 9 +++++++++ src/ln.c | 18 ++++++++++++------ tests/ln/Makefile.am | 2 +- tests/ln/hard-backup | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 7 deletions(-) create mode 100755 tests/ln/hard-backup diff --git a/ChangeLog b/ChangeLog index 9c1213bd5..865254e20 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2006-11-18 Jim Meyering + + "ln --backup f f" produces a misleading diagnostic: + ln: creating hard link `f' => `f': No such file or directory + * src/ln.c (do_link): Give a better diagnostic in this unusual case. + (do_link): Rename local: s/lstat_ok/dest_lstat_ok/. + * tests/ln/Makefile.am (TESTS): Add hard-backup. + * tests/ln/hard-backup: New test for the above. + 2006-11-16 Paul Eggert * bootstrap.conf (gnulib_modules): Add sys_stat, since we use it diff --git a/src/ln.c b/src/ln.c index 0aa5ac520..fae370807 100644 --- a/src/ln.c +++ b/src/ln.c @@ -133,7 +133,7 @@ do_link (const char *source, const char *dest) struct stat source_stats; struct stat dest_stats; char *dest_backup = NULL; - bool lstat_ok = false; + bool dest_lstat_ok = false; bool source_is_dir = false; bool ok; @@ -171,8 +171,8 @@ do_link (const char *source, const char *dest) if (remove_existing_files || interactive || backup_type != no_backups) { - lstat_ok = (lstat (dest, &dest_stats) == 0); - if (!lstat_ok && errno != ENOENT) + dest_lstat_ok = (lstat (dest, &dest_stats) == 0); + if (!dest_lstat_ok && errno != ENOENT) { error (0, errno, _("accessing %s"), quote (dest)); return false; @@ -184,8 +184,14 @@ do_link (const char *source, const char *dest) (with --backup, it just renames any existing destination file) But if the source and destination are the same, don't remove anything and fail right here. */ - if (remove_existing_files - && lstat_ok + if ((remove_existing_files + /* Ensure that "ln --backup f f" fails here, with the + "... same file" diagnostic, below. Otherwise, subsequent + code would give a misleading "file not found" diagnostic. + This case is different than the others handled here, since + the command in question doesn't use --force. */ + || (!symbolic_link && backup_type != no_backups)) + && dest_lstat_ok /* Allow `ln -sf --backup k k' to succeed in creating the self-referential symlink, but don't allow the hard-linking equivalent: `ln -f k k' (with or without --backup) to get @@ -205,7 +211,7 @@ do_link (const char *source, const char *dest) return false; } - if (lstat_ok) + if (dest_lstat_ok) { if (S_ISDIR (dest_stats.st_mode)) { diff --git a/tests/ln/Makefile.am b/tests/ln/Makefile.am index 8048a9852..79bedce31 100644 --- a/tests/ln/Makefile.am +++ b/tests/ln/Makefile.am @@ -1,7 +1,7 @@ ## Process this file with automake to produce Makefile.in -*-Makefile-*-. AUTOMAKE_OPTIONS = 1.2 gnits -TESTS = target-1 sf-1 misc backup-1 +TESTS = hard-backup target-1 sf-1 misc backup-1 EXTRA_DIST = $(TESTS) TESTS_ENVIRONMENT = \ PATH="$(VG_PATH_PREFIX)`pwd`/../../src$(PATH_SEPARATOR)$$PATH" diff --git a/tests/ln/hard-backup b/tests/ln/hard-backup new file mode 100755 index 000000000..e37938d38 --- /dev/null +++ b/tests/ln/hard-backup @@ -0,0 +1,54 @@ +#!/bin/sh +# Ensure that 'ln --backup F F' gives a proper diagnostic. + +# 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 + ln --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 +touch f || framework_failure=1 + +if test $framework_failure = 1; then + echo "$0: failure in testing framework" 1>&2 + (exit 1); exit 1 +fi + +fail=0 + +ln --backup f f 2> out && fail=1 +cat <<\EOF > exp || fail=1 +ln: `f' and `f' are the same file +EOF + +cmp out exp || fail=1 +test $fail = 1 && diff out exp 2> /dev/null + +(exit $fail); exit $fail -- cgit v1.2.3-54-g00ecf