From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 849213842AE2 for ; Thu, 30 Jun 2022 13:19:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 849213842AE2 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-22-WHPzUbHvPGG_MTECGbqPZg-1; Thu, 30 Jun 2022 09:19:01 -0400 X-MC-Unique: WHPzUbHvPGG_MTECGbqPZg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1AEAD85CE18; Thu, 30 Jun 2022 13:18:42 +0000 (UTC) Received: from localhost (unknown [10.33.36.212]) by smtp.corp.redhat.com (Postfix) with ESMTP id D357B2166B26; Thu, 30 Jun 2022 13:18:41 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Improve exceptions thrown from fs::temp_directory_path Date: Thu, 30 Jun 2022 14:18:41 +0100 Message-Id: <20220630131841.1237664-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jun 2022 13:19:06 -0000 Tested x86_64-linux and x86_64-mingw, pushed to trunk. -- >8 -- Currently the throwing overload of fs::temp_directory_path() will discard the path that was obtained from the environment. When it fails because the path doesn't resolve to a directory you get an unhelpful error like: filesystem error: temp_directory_path: Not a directory It would be better to also print the path in that case, e.g. filesystem error: temp_directory_path: Not a directory [/home/bob/tmp] libstdc++-v3/ChangeLog: * src/c++17/fs_ops.cc (fs::temp_directory_path()): Include path in exception. (fs::temp_directory_path(error_code&)): Rearrange to more closely match the structure of the first overload. * src/filesystem/ops.cc (fs::temp_directory_path): Likewise. * testsuite/27_io/filesystem/operations/temp_directory_path.cc: Check that exception contains the path. * testsuite/experimental/filesystem/operations/temp_directory_path.cc: Likewise. --- libstdc++-v3/src/c++17/fs_ops.cc | 34 +++++++++++++------ libstdc++-v3/src/filesystem/ops.cc | 31 ++++++++++------- .../operations/temp_directory_path.cc | 5 +++ .../operations/temp_directory_path.cc | 5 +++ 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc index 435368fa5c5..ed5e9f7d5cf 100644 --- a/libstdc++-v3/src/c++17/fs_ops.cc +++ b/libstdc++-v3/src/c++17/fs_ops.cc @@ -1571,25 +1571,37 @@ fs::path fs::temp_directory_path() { error_code ec; - path tmp = temp_directory_path(ec); + path p = fs::get_temp_directory_from_env(ec); + if (!ec) + { + auto st = status(p, ec); + if (!ec && !is_directory(st)) + ec = std::make_error_code(std::errc::not_a_directory); + } if (ec) - _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec)); - return tmp; + { + if (p.empty()) + _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec)); + else + _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", p, ec)); + } + return p; } fs::path fs::temp_directory_path(error_code& ec) { path p = fs::get_temp_directory_from_env(ec); - if (ec) - return p; - auto st = status(p, ec); - if (ec) - p.clear(); - else if (!is_directory(st)) + if (!ec) { - p.clear(); - ec = std::make_error_code(std::errc::not_a_directory); + auto st = status(p, ec); + if (ec) + p.clear(); + else if (!is_directory(st)) + { + p.clear(); + ec = std::make_error_code(std::errc::not_a_directory); + } } return p; } diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc index 896a4918ace..ab84eb84594 100644 --- a/libstdc++-v3/src/filesystem/ops.cc +++ b/libstdc++-v3/src/filesystem/ops.cc @@ -1326,25 +1326,32 @@ fs::path fs::temp_directory_path() { error_code ec; - path tmp = temp_directory_path(ec); - if (ec.value()) - _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec)); - return tmp; + path p = fs::get_temp_directory_from_env(ec); + if (!ec) + { + auto st = status(p, ec); + if (!ec && !is_directory(st)) + ec = std::make_error_code(std::errc::not_a_directory); + } + if (ec) + _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", p, ec)); + return p; } fs::path fs::temp_directory_path(error_code& ec) { path p = fs::get_temp_directory_from_env(ec); - if (ec) - return p; - auto st = status(p, ec); - if (ec) - p.clear(); - else if (!is_directory(st)) + if (!ec) { - p.clear(); - ec = std::make_error_code(std::errc::not_a_directory); + auto st = status(p, ec); + if (ec) + p.clear(); + else if (!is_directory(st)) + { + p.clear(); + ec = std::make_error_code(std::errc::not_a_directory); + } } return p; } diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/temp_directory_path.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/temp_directory_path.cc index b4ef77f05e4..56bd7408c2d 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/operations/temp_directory_path.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/temp_directory_path.cc @@ -140,12 +140,17 @@ test04() VERIFY( r == fs::path() ); std::error_code ec2; + std::string failed_path; try { fs::temp_directory_path(); } catch (const fs::filesystem_error& e) { ec2 = e.code(); + // On Windows the returned path will be in preferred form, i.e. using L'\\' + // and will have a trailing slash, so compare generic forms. + failed_path = e.path1().generic_string(); } VERIFY( ec2 == ec ); + VERIFY( failed_path.find(f.path.generic_string()) != std::string::npos ); } int diff --git a/libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc index c2945c90866..1772f9737c1 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc @@ -141,12 +141,17 @@ test04() VERIFY( r == fs::path() ); std::error_code ec2; + std::string failed_path; try { fs::temp_directory_path(); } catch (const fs::filesystem_error& e) { ec2 = e.code(); + // On Windows the returned path will be in preferred form, i.e. using L'\\' + // and will have a trailing slash, so compare generic forms. + failed_path = e.path1().generic_string(); } VERIFY( ec2 == ec ); + VERIFY( failed_path.find(f.path.generic_string()) != std::string::npos ); } int -- 2.36.1