From 44fbd3fd862e34d42006f8b74cb11c9c56346417 Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Sun, 1 Jul 2012 01:14:42 +0100 Subject: sort: avoid redundant processing with inaccessible inputs or output * src/sort.c (check_inputs): A new function to verify all inputs are accessible before further processing. (check_output): A new function to open or create a specified output file, before futher processing. (stream_open): Adjust to truncating the previously opened output file rather than opening directly. (avoid_trashing_input): Optimize to stat the output file descriptor, rather than the file name. (main): Call the new functions to check accessibility of inputs and output, before processing starts. * tests/misc/sort: Adjust to the changed error message. * tests/misc/sort-merge-fdlimit: Account for the earlier opened file descriptor of the specified output file. * tests/misc/sort-exit-early: A new test to exercise the improvements. * tests/Makefile.am: Reference the new test. * NEWS: Mention the improvement. Suggested-by: Bernhard Voelker --- tests/Makefile.am | 1 + tests/misc/sort | 2 +- tests/misc/sort-exit-early | 36 ++++++++++++++++++++++++++++++++++++ tests/misc/sort-merge-fdlimit | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100755 tests/misc/sort-exit-early (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 2155cee83..dfac9a0f3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -251,6 +251,7 @@ TESTS = \ misc/sort-merge \ misc/sort-merge-fdlimit \ misc/sort-month \ + misc/sort-exit-early \ misc/sort-rand \ misc/sort-spinlock-abuse \ misc/sort-stale-thread-mem \ diff --git a/tests/misc/sort b/tests/misc/sort index 5be00a017..5d15d7572 100755 --- a/tests/misc/sort +++ b/tests/misc/sort @@ -33,7 +33,7 @@ my $mb_locale = $ENV{LOCALE_FR_UTF8}; # Normalize each diagnostic to use '-'. my $normalize_filename = {ERR_SUBST => 's/^$prog: .*?:/$prog: -:/'}; -my $no_file = "$prog: open failed: no-file: No such file or directory\n"; +my $no_file = "$prog: cannot read: no-file: No such file or directory\n"; my @Tests = ( diff --git a/tests/misc/sort-exit-early b/tests/misc/sort-exit-early new file mode 100755 index 000000000..7470aad52 --- /dev/null +++ b/tests/misc/sort-exit-early @@ -0,0 +1,36 @@ +#!/bin/sh +# Test 'sort' exits early on inaccessible inputs or output + +# Copyright (C) 2012 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 3 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, see . + +. "${srcdir=.}/init.sh"; path_prepend_ ../src +print_ver_ sort + +# Check output is writable before starting to sort +touch input +chmod a-w input +timeout 10 sort -o input && fail=1 +test $? = 124 && fail=1 + +# Check all inputs are readable before starting to sort +# Also ensure the output isn't created in this case +touch output +chmod a-r output +timeout 10 sort -o typo - output && fail=1 +test $? = 124 && fail=1 +test -e typo && fail=1 + +Exit $fail diff --git a/tests/misc/sort-merge-fdlimit b/tests/misc/sort-merge-fdlimit index 7ce109c1a..ee1575aed 100755 --- a/tests/misc/sort-merge-fdlimit +++ b/tests/misc/sort-merge-fdlimit @@ -65,7 +65,7 @@ done (seq 6 && echo 6) >exp || fail=1 echo 6 >out || fail=1 (exec 3<&- 4<&- 5<&- 6