/* Nonzero if any of the input files are the standard input. */
static bool have_read_stdin;
+/* File descriptor associated with -o. */
+static int outfd = -1;
+
/* List of key field comparisons to be tried. */
static struct keyfield *keylist;
static FILE *
stream_open (char const *file, char const *how)
{
+ FILE *fp;
+
if (!file)
return stdout;
if (*how == 'r')
{
- FILE *fp;
if (STREQ (file, "-"))
{
have_read_stdin = true;
else
fp = fopen (file, how);
fadvise (fp, FADVISE_SEQUENTIAL);
- return fp;
}
- return fopen (file, how);
+ else if (*how == 'w')
+ {
+ assert (outfd != -1);
+ if (ftruncate (outfd, 0) != 0)
+ error (EXIT_FAILURE, errno, _("%s: error truncating"), quote (file));
+ fp = fdopen (outfd, how);
+ }
+ else
+ assert (!"unexpected mode passed to stream_open");
+
+ return fp;
}
/* Same as stream_open, except always return a non-null value; die on
if (! got_outstat)
{
if ((outfile
- ? stat (outfile, &outstat)
+ ? fstat (outfd, &outstat)
: fstat (STDOUT_FILENO, &outstat))
!= 0)
break;
}
}
+/* Scan the input files to ensure all are accessible.
+ Otherwise exit with a diagnostic.
+
+ Note this will catch common issues with permissions etc.
+ but will fail to notice issues where you can open() but not read(),
+ like when a directory is specified on some systems.
+ Catching these obscure cases could slow down performance in
+ common cases. */
+
+static void
+check_inputs (char *const *files, size_t nfiles)
+{
+ size_t i;
+ for (i = 0; i < nfiles; i++)
+ {
+ if (STREQ (files[i], "-"))
+ continue;
+
+ if (euidaccess (files[i], R_OK) != 0)
+ die (_("cannot read"), files[i]);
+ }
+}
+
+/* Ensure a specified output file can be created or written to,
+ and cache the returned descriptor in the global OUTFD variable.
+ Otherwise exit with a diagnostic. */
+
+static void
+check_output (char const *outfile)
+{
+ if (outfile)
+ {
+ outfd = open (outfile, O_WRONLY | O_CREAT | O_BINARY, MODE_RW_UGO);
+ if (outfd < 0)
+ die (_("open failed"), outfile);
+ }
+}
+
/* Merge the input FILES. NTEMPS is the number of files at the
start of FILES that are temporary; it is zero at the top level.
NFILES is the total number of files. Put the output in
exit (check (files[0], checkonly) ? EXIT_SUCCESS : SORT_OUT_OF_ORDER);
}
+ /* Check all inputs are accessible, or exit immediately. */
+ check_inputs (files, nfiles);
+
+ /* Check output is writable, or exit immediately. */
+ check_output (outfile);
+
if (mergeonly)
{
struct sortfile *sortfiles = xcalloc (nfiles, sizeof *sortfiles);
--- /dev/null
+#!/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 <http://www.gnu.org/licenses/>.
+
+. "${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