From d2ba554507dd3b04d61aaa2e387b5ffd49d3e613 Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Sun, 15 Dec 2019 16:10:05 +0100 Subject: [PATCH] tmpfile: improve error message * spot/misc/tmpfile.cc: Display strerror(errno) plus some suggestions that depend on the error. Based on a report from Shengping Shaw. * THANKS: Add reporter. * tests/core/ltlcross5.test: New file. * tests/Makefile.am: Add it. --- THANKS | 1 + spot/misc/tmpfile.cc | 26 ++++++++++++++-- tests/Makefile.am | 1 + tests/core/ltlcross5.test | 65 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/core/ltlcross5.test diff --git a/THANKS b/THANKS index a18ea92d0..841832947 100644 --- a/THANKS +++ b/THANKS @@ -42,6 +42,7 @@ Reuben Rowe Rüdiger Ehlers Silien Hong Simon Jantsch +Shengping Shaw Shufang Zhu Sonali Dutta Tereza Šťastná diff --git a/spot/misc/tmpfile.cc b/spot/misc/tmpfile.cc index 3f5842a98..b068d2abe 100644 --- a/spot/misc/tmpfile.cc +++ b/spot/misc/tmpfile.cc @@ -1,5 +1,5 @@ // -*- coding: utf-8 -*- -// Copyright (C) 2013, 2015, 2017, 2018 Laboratoire de Recherche et +// Copyright (C) 2013, 2015, 2017-2019 Laboratoire de Recherche et // Développement de l'Epita (LRDE). // // This file is part of Spot, a model checking library. @@ -75,7 +75,29 @@ namespace spot fd = mkstemp(*name); } if (fd < 0) - throw std::runtime_error("failed to create "s + *name); + { + std::string err = ("failed to create temporary file "s + *name + + ": " + strerror(errno)); + if (errno == EACCES) + { + if (tmpdir) + err += ("\nConsider setting the SPOT_TMPDIR environment " + "variable to a writable directory."); + else + err += ("\nConsider executing this from a writable " + "directory, or setting\nthe SPOT_TMPDIR environment " + "variable to such a directory."); + } + else if (tmpdir) + { + const char* dir = + secure_getenv("SPOT_TMPDIR") ? "SPOT_TMPDIR" : "TMPDIR"; + err += ("\nNote that the directory comes from the "s + + dir + + " environment variable."); + } + throw std::runtime_error(err); + } return fd; } } diff --git a/tests/Makefile.am b/tests/Makefile.am index 84113c3eb..26d5f1b46 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -214,6 +214,7 @@ TESTS_twa = \ core/bdddict.test \ core/alternating.test \ core/ltlcross3.test \ + core/ltlcross5.test \ core/taatgba.test \ core/renault.test \ core/nondet.test \ diff --git a/tests/core/ltlcross5.test b/tests/core/ltlcross5.test new file mode 100644 index 000000000..82e9fdc89 --- /dev/null +++ b/tests/core/ltlcross5.test @@ -0,0 +1,65 @@ +#!/bin/sh +# -*- coding: utf-8 -*- +# Copyright (C) 2019 Laboratoire de Recherche et Développement de +# l'Epita (LRDE). +# +# This file is part of Spot, a model checking library. +# +# Spot 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. +# +# Spot 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 . + +. ./defs +set -e + +unset TMPDIR +unset SPOT_TMPDIR + +mkdir foo +chmod a-w foo +cd foo +err=0 +if touch bar; then + # We are likely running as root, so we cannot detect permission + # issues. + cd .. + rm -rf foo + exit 77 +fi + +ltlcross ltl2tgba -f GFa 2>../err && err=1 +cd .. +cat err +grep 'failed to create temporary file' err || err=1 +grep 'executing this from a writable' err || err=1 +grep 'SPOT_TMPDIR' err || err=1 + +SPOT_TMPDIR=foo ltlcross ltl2tgba -f GFa 2>err && err=2 +cat err +grep 'failed to create temporary file' err || err=2 +grep 'executing this from a writable' err && err=2 +grep 'SPOT_TMPDIR' err + +chmod a+w foo +rmdir foo + +SPOT_TMPDIR=bar ltlcross ltl2tgba -f GFa 2>err && err=3 +cat err +grep 'failed to create temporary file' err +grep 'Note that the directory.*SPOT_TMPDIR ' err + +TMPDIR=bar ltlcross ltl2tgba -f GFa 2>err && err=4 +cat err +grep 'failed to create temporary file' err +grep 'Note that the directory.* TMPDIR ' err + +exit $err