From patchwork Fri Oct 1 19:42:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1535521 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=rRhqK2+n; 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 (ip-8-43-85-97.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 4HLgvb2zT4z9t1Q for ; Sat, 2 Oct 2021 06:00:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4138A3857410 for ; Fri, 1 Oct 2021 20:00:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4138A3857410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633118425; bh=CpyV0zuUkt9Wy8nzx4NJTHCzd8QHSuoYdBESVJGGcWM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=rRhqK2+np8TCnmwqtan93LcIrWR8nIuTcdPgdERgl/mTRv2NyNU4fhmpnZ6ObOl+W L5eR9gliz6Qf9t80KC4WIw37MJqCNLbbCXs2pF710M1TZI54kmUWy0u/Su2Fqy4lSb UHYVCd8xFh0yzdIMpWEKOZepEo2DhrUvClZquvSA= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 396E5385781C for ; Fri, 1 Oct 2021 19:42:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 396E5385781C Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-251-d3w0B7MgNgmOHZhNjHSBzQ-1; Fri, 01 Oct 2021 15:42:47 -0400 X-MC-Unique: d3w0B7MgNgmOHZhNjHSBzQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7E1E4100CCC1; Fri, 1 Oct 2021 19:42:46 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C76E5C1D0; Fri, 1 Oct 2021 19:42:46 +0000 (UTC) Date: Fri, 1 Oct 2021 20:42:45 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add utility for creating std::error_code from OS errors Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.8 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, RCVD_IN_MSPIKE_H2, 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" This adds a helper function to encapsulate obtaining an error code for errors from OS calls. For Windows we want to use GetLastError() and the system error category, but otherwise just use errno and the generic error category. This should not be used to replace existing uses of ec.assign(errno, generic_category()) because in those cases we really do want to get the value of errno, not a system-specific error. Only the cases that currently use GetLastError() are replace by this new function. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * src/filesystem/ops-common.h (last_error): New helper function. (filesystem::do_space): Use last_error(). * src/c++17/fs_ops.cc (fs::absolute, fs::create_hard_link) (fs::equivalent, fs::remove, fs::temp_directory_path): Use last_error(). * src/filesystem/ops.cc (fs::create_hard_link) (fs::remove, fs::temp_directory_path): Likewise. Tested powerpc64le-linux. Committed to trunk. commit d71476c9df931f3ca674941f1942b03eabea010d Author: Jonathan Wakely Date: Wed Feb 10 18:00:00 2021 libstdc++: Add utility for creating std::error_code from OS errors This adds a helper function to encapsulate obtaining an error code for errors from OS calls. For Windows we want to use GetLastError() and the system error category, but otherwise just use errno and the generic error category. This should not be used to replace existing uses of ec.assign(errno, generic_category()) because in those cases we really do want to get the value of errno, not a system-specific error. Only the cases that currently use GetLastError() are replace by this new function. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * src/filesystem/ops-common.h (last_error): New helper function. (filesystem::do_space): Use last_error(). * src/c++17/fs_ops.cc (fs::absolute, fs::create_hard_link) (fs::equivalent, fs::remove, fs::temp_directory_path): Use last_error(). * src/filesystem/ops.cc (fs::create_hard_link) (fs::remove, fs::temp_directory_path): Likewise. diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc index 2eac9977785..4f3715bbbec 100644 --- a/libstdc++-v3/src/c++17/fs_ops.cc +++ b/libstdc++-v3/src/c++17/fs_ops.cc @@ -113,7 +113,7 @@ fs::absolute(const path& p, error_code& ec) while (len > buf.size()); if (len == 0) - ec.assign((int)GetLastError(), std::system_category()); + ec = __last_system_error(); else { buf.resize(len); @@ -682,7 +682,7 @@ fs::create_hard_link(const path& to, const path& new_hard_link, if (CreateHardLinkW(new_hard_link.c_str(), to.c_str(), NULL)) ec.clear(); else - ec.assign((int)GetLastError(), system_category()); + ec = __last_system_error(); #else ec = std::make_error_code(std::errc::not_supported); #endif @@ -874,12 +874,12 @@ fs::equivalent(const path& p1, const path& p2, error_code& ec) noexcept if (!h1 || !h2) { if (!h1 && !h2) - ec.assign((int)GetLastError(), system_category()); + ec = __last_system_error(); return false; } if (!h1.get_info() || !h2.get_info()) { - ec.assign((int)GetLastError(), system_category()); + ec = __last_system_error(); return false; } return h1.info.dwVolumeSerialNumber == h2.info.dwVolumeSerialNumber @@ -1255,7 +1255,7 @@ fs::remove(const path& p, error_code& ec) noexcept return true; } else if (!ec) - ec.assign((int)GetLastError(), system_category()); + ec = __last_system_error(); } else if (status_known(st)) ec.clear(); diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h index bf26c06b7b5..e999e11b422 100644 --- a/libstdc++-v3/src/filesystem/ops-common.h +++ b/libstdc++-v3/src/filesystem/ops-common.h @@ -57,6 +57,18 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION + + // Get the last OS error (for POSIX this is just errno). + inline error_code + __last_system_error() noexcept + { +#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS + return {::GetLastError(), std::system_category()}; +#else + return {errno, std::generic_category()}; +#endif + } + namespace filesystem { namespace __gnu_posix @@ -558,7 +570,7 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM ec.clear(); } else - ec.assign((int)GetLastError(), std::system_category()); + ec = std::last_system_error(); #else ec = std::make_error_code(std::errc::not_supported); #endif @@ -583,7 +595,7 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM } while (len > buf.size()); if (len == 0) - ec.assign((int)GetLastError(), std::system_category()); + ec = __last_system_error(); else ec.clear(); diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc index b0a0f15e98f..cc7117b0cd1 100644 --- a/libstdc++-v3/src/filesystem/ops.cc +++ b/libstdc++-v3/src/filesystem/ops.cc @@ -590,7 +590,7 @@ fs::create_hard_link(const path& to, const path& new_hard_link, if (CreateHardLinkW(new_hard_link.c_str(), to.c_str(), NULL)) ec.clear(); else - ec.assign((int)GetLastError(), system_category()); + ec = __last_system_error(); #else ec = std::make_error_code(std::errc::not_supported); #endif @@ -1062,7 +1062,7 @@ fs::remove(const path& p, error_code& ec) noexcept return true; } else if (!ec) - ec.assign((int)GetLastError(), system_category()); + ec = __last_system_error(); } else if (status_known(st)) ec.clear();