From patchwork Thu Jun 30 13:18:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 55604 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B5BD383F962 for ; Thu, 30 Jun 2022 13:18:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B5BD383F962 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656595131; bh=xMlHwHXBPB9Ma3OEqwn+kpfwsrgjusECTZ8+zlg6kM0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=bhINaRThnYlk09TtMVgeua/UQiu051EbM/lYcbpIgujgYDkyAytwieVEy5jGeB2WH ZnXwBr9puni4oqxDZowpzcW0aWtw201TSB4II8LOBmqAITWiK7/UOPC6LQy/nhSI30 g6qfFxtDUr/ZJqwRjPpyJmfUrGVRWGgNOaA8bFN4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id EC2E33842ADB for ; Thu, 30 Jun 2022 13:18:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EC2E33842ADB Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-479-nx63c46BOVKCVD8tggzHZA-1; Thu, 30 Jun 2022 09:18:21 -0400 X-MC-Unique: nx63c46BOVKCVD8tggzHZA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2E0F4280229E; Thu, 30 Jun 2022 13:18:04 +0000 (UTC) Received: from localhost (unknown [10.33.36.212]) by smtp.corp.redhat.com (Postfix) with ESMTP id E851641637B; Thu, 30 Jun 2022 13:18:03 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix experimental::filesystem::status on Windows [PR88881] Date: Thu, 30 Jun 2022 14:18:03 +0100 Message-Id: <20220630131803.1237546-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.5 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux and x86_64-mingw, pushed to trunk. -- >8 -- Although the Filesystem TS isn't properly supported on Windows (unlike the C++17 Filesystem lib), most tests do pass. Two of the failures are due to PR 88881 which was only fixed for std::filesystem not the TS. This applies the fix to the TS implementation too. libstdc++-v3/ChangeLog: PR libstdc++/88881 * src/filesystem/ops.cc (has_trailing_slash): New helper function. (fs::status): Strip trailing slashes. (fs::symlink_status): Likewise. * testsuite/experimental/filesystem/operations/temp_directory_path.cc: Clean the environment before each test and use TMP instead of TMPDIR so the test passes on Windows. --- libstdc++-v3/src/filesystem/ops.cc | 56 ++++++++++++++++++- .../operations/temp_directory_path.cc | 6 +- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc index 98ddff5a66e..896a4918ace 100644 --- a/libstdc++-v3/src/filesystem/ops.cc +++ b/libstdc++-v3/src/filesystem/ops.cc @@ -1176,13 +1176,43 @@ fs::space(const path& p, error_code& ec) noexcept return info; } +#if _GLIBCXX_FILESYSTEM_IS_WINDOWS +static bool has_trailing_slash(const fs::path& p) +{ + wchar_t c = p.native().back(); + return c == '/' || c == L'\\'; +} +#endif + #ifdef _GLIBCXX_HAVE_SYS_STAT_H fs::file_status fs::status(const fs::path& p, error_code& ec) noexcept { file_status status; + + auto str = p.c_str(); + +#if _GLIBCXX_FILESYSTEM_IS_WINDOWS + // stat() fails if there's a trailing slash (PR 88881) + path p2; + if (p.has_relative_path() && has_trailing_slash(p)) + { + __try + { + p2 = p.parent_path(); + str = p2.c_str(); + } + __catch(const bad_alloc&) + { + ec = std::make_error_code(std::errc::not_enough_memory); + return status; + } + str = p2.c_str(); + } +#endif + stat_type st; - if (posix::stat(p.c_str(), &st)) + if (posix::stat(str, &st)) { int err = errno; ec.assign(err, std::generic_category()); @@ -1205,8 +1235,30 @@ fs::file_status fs::symlink_status(const fs::path& p, std::error_code& ec) noexcept { file_status status; + + auto str = p.c_str(); + +#if _GLIBCXX_FILESYSTEM_IS_WINDOWS + // stat() fails if there's a trailing slash (PR 88881) + path p2; + if (p.has_relative_path() && has_trailing_slash(p)) + { + __try + { + p2 = p.parent_path(); + str = p2.c_str(); + } + __catch(const bad_alloc&) + { + ec = std::make_error_code(std::errc::not_enough_memory); + return status; + } + str = p2.c_str(); + } +#endif + stat_type st; - if (posix::lstat(p.c_str(), &st)) + if (posix::lstat(str, &st)) { int err = errno; ec.assign(err, std::generic_category()); 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 9e9cd44d460..c2945c90866 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc @@ -105,6 +105,8 @@ test03() if (!__gnu_test::permissions_are_testable()) return; + clean_env(); + auto p = __gnu_test::nonexistent_path(); create_directories(p/"tmp"); permissions(p, fs::perms::none); @@ -129,8 +131,10 @@ test03() void test04() { + clean_env(); + __gnu_test::scoped_file f; - set_env("TMPDIR", f.path.string()); + set_env("TMP", f.path.string()); std::error_code ec; auto r = fs::temp_directory_path(ec); VERIFY( ec == std::make_error_code(std::errc::not_a_directory) );