From patchwork Thu Apr 28 12:34:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1623646 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=XQWccG3g; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kpw7J6FxRz9s2R for ; Thu, 28 Apr 2022 22:35:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1092B3857378 for ; Thu, 28 Apr 2022 12:35:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1092B3857378 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651149306; bh=236aVhulBVIPYonb8ozRkruv9YxcHLuzIHC/OPlRdgw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=XQWccG3geddTPGggS2FaY80KcpBAKnTTHYXggCxwAsBHQqckTPUx9OOdVN/Pof8EI HPX//P4+PFcFN1N7sLjN+mjyGzl/WvPrf4rNTl5edJwmk0kTy0Ng1pgJsVlyF7qSC8 UgeMZwXj0+Oli1pDOBkAcrPIgpWBrLv1jpFCAO2U= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id D5C463856DC2 for ; Thu, 28 Apr 2022 12:34:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D5C463856DC2 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-658-IunzYr6YMEK1nSyHfNVkhQ-1; Thu, 28 Apr 2022 08:34:16 -0400 X-MC-Unique: IunzYr6YMEK1nSyHfNVkhQ-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 B627685A5BC; Thu, 28 Apr 2022 12:34:15 +0000 (UTC) Received: from localhost (unknown [10.33.36.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7CB11401474; Thu, 28 Apr 2022 12:34:15 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix error reporting in filesystem::copy [PR99290] Date: Thu, 28 Apr 2022 13:34:14 +0100 Message-Id: <20220428123414.2354532-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=-12.4 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 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux, pushed to trunk. I'll backport this too. -- >8 -- The recursive calls to filesystem::copy should stop if any of them reports an error. libstdc++-v3/ChangeLog: PR libstdc++/99290 * src/c++17/fs_ops.cc (fs::copy): Pass error_code to directory_iterator constructor, and check on each iteration. * src/filesystem/ops.cc (fs::copy): Likewise. * testsuite/27_io/filesystem/operations/copy.cc: Check for errors during recursion. * testsuite/experimental/filesystem/operations/copy.cc: Likewise. --- libstdc++-v3/src/c++17/fs_ops.cc | 8 +++-- libstdc++-v3/src/filesystem/ops.cc | 8 +++-- .../27_io/filesystem/operations/copy.cc | 29 +++++++++++++++++++ .../filesystem/operations/copy.cc | 29 +++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc index 4552a730bf2..435368fa5c5 100644 --- a/libstdc++-v3/src/c++17/fs_ops.cc +++ b/libstdc++-v3/src/c++17/fs_ops.cc @@ -408,8 +408,12 @@ fs::copy(const path& from, const path& to, copy_options options, // set an unused bit in options to disable further recursion if (!is_set(options, copy_options::recursive)) options |= static_cast(4096); - for (const directory_entry& x : directory_iterator(from)) - copy(x.path(), to/x.path().filename(), options, ec); + for (const directory_entry& x : directory_iterator(from, ec)) + { + copy(x.path(), to/x.path().filename(), options, ec); + if (ec) + return; + } } // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2683. filesystem::copy() says "no effects" diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc index e2a2cefdf49..98ddff5a66e 100644 --- a/libstdc++-v3/src/filesystem/ops.cc +++ b/libstdc++-v3/src/filesystem/ops.cc @@ -350,8 +350,12 @@ fs::copy(const path& from, const path& to, copy_options options, // set an unused bit in options to disable further recursion if (!is_set(options, copy_options::recursive)) options |= static_cast(4096); - for (const directory_entry& x : directory_iterator(from)) - copy(x.path(), to/x.path().filename(), options, ec); + for (const directory_entry& x : directory_iterator(from, ec)) + { + copy(x.path(), to/x.path().filename(), options, ec); + if (ec) + return; + } } // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2683. filesystem::copy() says "no effects" diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc index b85628cdf30..b936e04493b 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc @@ -193,6 +193,34 @@ test05() VERIFY( !ec ); // Previous value should be cleared (LWG 2683) } +void +test_pr99290() +{ + auto dir = __gnu_test::nonexistent_path(); + auto source = dir/"source"; + auto dest = dir/"dest"; + create_directories(source/"emptydir"); + create_directories(dest/"emptydir"); + std::ofstream{source/"file"} << 'a'; + std::ofstream{dest/"file"} << 'b'; + // PR libstdc++/99290 + // std::filesystem::copy does not always report errors for recursion + std::error_code ec; + copy(source, dest, ec); + VERIFY( ec == std::errc::file_exists ); + +#if __cpp_exceptions + try { + copy(source, dest); + VERIFY( false ); + } catch (const fs::filesystem_error& e) { + VERIFY( e.code() == std::errc::file_exists ); + } +#endif + + remove_all(dir); +} + int main() { @@ -201,4 +229,5 @@ main() test03(); test04(); test05(); + test_pr99290(); } diff --git a/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc index 319632a5a76..5cd6b483c26 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc @@ -190,6 +190,34 @@ test05() VERIFY( !ec ); // Previous value should be cleared (LWG 2683) } +void +test_pr99290() +{ + auto dir = __gnu_test::nonexistent_path(); + auto source = dir/"source"; + auto dest = dir/"dest"; + create_directories(source/"emptydir"); + create_directories(dest/"emptydir"); + std::ofstream{source/"file"} << 'a'; + std::ofstream{dest/"file"} << 'b'; + // PR libstdc++/99290 + // std::filesystem::copy does not always report errors for recursion + std::error_code ec; + copy(source, dest, ec); + VERIFY( ec == std::errc::file_exists ); + +#if __cpp_exceptions + try { + copy(source, dest); + VERIFY( false ); + } catch (const fs::filesystem_error& e) { + VERIFY( e.code() == std::errc::file_exists ); + } +#endif + + remove_all(dir); +} + int main() { @@ -198,4 +226,5 @@ main() test03(); test04(); test05(); + test_pr99290(); }