From patchwork Mon Nov 9 18:40:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 40969 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 07CC13836C37; Mon, 9 Nov 2020 18:40:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 07CC13836C37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1604947229; bh=qwCBkR3tNL2VnUyFrU+n7nlkt2feMMNv/OgQlTlADdQ=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=oE9lNbctxDsJMng3l+xIXGG20/klSe7JIhoIyl+TqRZsZ5n4Rz8qX5cQV+OdPcFxm Tn266ILQceQgLkNSWUzDmGm/SyltnZfsxgpgPizdAr46zWyiw9lZXi+/efH3f2MMEo EzchDn1tqTwRqtD5Ul60nA7GO3ejjrTiOkJ3IX1k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.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 A773D386EC70 for ; Mon, 9 Nov 2020 18:40:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A773D386EC70 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-171-9GTzTBH1NUa_3N20fwJ9Hw-1; Mon, 09 Nov 2020 13:40:19 -0500 X-MC-Unique: 9GTzTBH1NUa_3N20fwJ9Hw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0A0A3801FDE for ; Mon, 9 Nov 2020 18:40:19 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-113-222.ams2.redhat.com [10.36.113.222]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7BDB46EF4A for ; Mon, 9 Nov 2020 18:40:18 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 01/11] support: Add support_copy_file In-Reply-To: References: Message-Id: Date: Mon, 09 Nov 2020 19:40:16 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" --- support/Makefile | 1 + support/support.h | 5 +++++ support/support_copy_file.c | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 support/support_copy_file.c Reviewed-by: Adhemerval Zanella diff --git a/support/Makefile b/support/Makefile index 4154863511..f5f59bf8d2 100644 --- a/support/Makefile +++ b/support/Makefile @@ -46,6 +46,7 @@ libsupport-routines = \ support_capture_subprocess \ support_capture_subprocess_check \ support_chroot \ + support_copy_file \ support_copy_file_range \ support_descriptor_supports_holes \ support_descriptors \ diff --git a/support/support.h b/support/support.h index 905b5a76d8..abda3a69f1 100644 --- a/support/support.h +++ b/support/support.h @@ -119,6 +119,11 @@ extern const char support_install_rootsbindir[]; /* Corresponds to the install's compiled locale directory. */ extern const char support_complocaledir_prefix[]; +/* Copies the file at the path FROM to TO. If TO does not exist, it + is created. If TO is a regular file, it is truncated before + copying. The file mode is copied, but the permissions are not. */ +extern void support_copy_file (const char *from, const char *to); + extern ssize_t support_copy_file_range (int, off64_t *, int, off64_t *, size_t, unsigned int); diff --git a/support/support_copy_file.c b/support/support_copy_file.c new file mode 100644 index 0000000000..bf8f83803a --- /dev/null +++ b/support/support_copy_file.c @@ -0,0 +1,43 @@ +/* Copy a file from one path to another. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +void +support_copy_file (const char *from, const char *to) +{ + struct stat64 st; + xstat (from, &st); + int fd_from = xopen (from, O_RDONLY, 0); + mode_t mode = st.st_mode & 0777; + int fd_to = xopen (to, O_WRONLY | O_TRUNC | O_CREAT, mode); + ssize_t ret = support_copy_file_range (fd_from, NULL, fd_to, NULL, + st.st_size, 0); + if (ret < 0) + FAIL_EXIT1 ("copying from \"%s\" to \"%s\": %m", from, to); + if (ret != st.st_size) + FAIL_EXIT1 ("copying from \"%s\" to \"%s\": only %zd of %llu bytes copied", + from, to, ret, (unsigned long long int) st.st_size); + if (fchmod (fd_to, mode) < 0) + FAIL_EXIT1 ("fchmod on %s: %m", to); + xclose (fd_to); + xclose (fd_from); +}