From nobody Wed Dec 13 23:41:58 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SrBpZ3jvqz54VC1; Wed, 13 Dec 2023 23:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SrBpZ3F7Wz4D8r; Wed, 13 Dec 2023 23:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702510918; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Z+e0b6uG/YkbIYQqtC8POkLXxg0uzdgBvMTTxprq6fQ=; b=DXQ/7voEis7mykEzHkXaewZjibiaTCr/YvyFGKpqrDtXYvG5CdvIJhzfk+Z99+1cas4Od4 hgtvPQ+/IXUAd0/WlhU73xX6aDAgFmMOvsGPeAZMDlOoHusxaWAQdlHD7nSMgdzpR1C0/f D/GCtv6tipcM5eAgqeKVUajJnsuI3FfqTgqZnIsptOwJv0bJUp5biKC/FXtuUeW3gA84dM 1A60UgjYJ+5MtgByMBQyrEFeY67vxwbdrmOE5U7rZ95WDZoRqWi/Io5s3Ec4QndS4h3Pkb tmE7qbRrTq3OZl7s8fOIfd/kh+noAU/6052UQDZSL17MwGiOSYOlCEm0PiSVYg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702510918; a=rsa-sha256; cv=none; b=ogWQWjOPK4wLLGfaaYxmqO84EGE2XKyGF2ZRZp+vN/QqlJz9mJ+hQIVyy9ZOO5j73360PC tl15VjGWV1hcO7S+tDDmCSR/Z1h9JRBvtStCsaGbusXQBD09NHVjiwVBE/FK+PTYFULrME NdyzIf3tChIxsTIVkqFNmAof8pSzvcwekHbFC0s+dpampGRD0jkJlyB8/XaNGp1udBvauf +2UN71a+HR/Jp6X1UmDU/nldL175WakqjothrLCQCl0pdLCw+UACAwHq2mQIN7jZ72WYhD f7VZXb2y2kmsomi6Kf0q1OfSSeJNIM368A4V1QbbYdScOqPKS7eaNs8lhwx/Yw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702510918; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Z+e0b6uG/YkbIYQqtC8POkLXxg0uzdgBvMTTxprq6fQ=; b=RJ31u3avEmVPT7x0KDCwazA/GwMDy0I+PUCB29FY+IZyTJutSxzj315MMOxYdKTvJ1aqLI sQd9QGwIt+IQjsCVdKrBUApo/7yZ0wR7Oru5WOJt0tZufCd9Z3gYUeAbAlP+ffkfajsAGk YfF1JI4SHNqIBPFaPDH3ZenoldZfBaujvIAjuqNBWQgvmmttbuxhf4DdbPKrRutgVF+feb bTXJeePWEUgw+XPGjzpgYlGUew4I3WKfEZVQDoI+hjPW0MJGxmp7Q0s+2wMIwMZ0hNl9qx gwhAtXf5ZWgbgkcjsldJceDFAzh0O2/ExhKN3Ex2rsfg9SSG1I0Vpgm2BeSCMw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SrBpZ2Hq8z1JMq; Wed, 13 Dec 2023 23:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BDNfwZf057526; Wed, 13 Dec 2023 23:41:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BDNfwDU057523; Wed, 13 Dec 2023 23:41:58 GMT (envelope-from git) Date: Wed, 13 Dec 2023 23:41:58 GMT Message-Id: <202312132341.3BDNfwDU057523@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: d002316fd7bf - main - cp: Refactor the core logic. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d002316fd7bf0b359ea2f5518f3c10f6ad89a9ac Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=d002316fd7bf0b359ea2f5518f3c10f6ad89a9ac commit d002316fd7bf0b359ea2f5518f3c10f6ad89a9ac Author: Dag-Erling Smørgrav AuthorDate: 2023-12-13 23:40:45 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-12-13 23:40:55 +0000 cp: Refactor the core logic. Rewrite `copy_file()` so the lflag and sflag are handled as early as possible instead of constantly checking that they're not set and then handling them at the end. This also opens the door to changing the failure logic at some future point (for instance, we might decide to fall back to copying if `errno` indicates that the file system does not support links). MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans, allanjude Differential Revision: https://reviews.freebsd.org/D43055 --- bin/cp/utils.c | 121 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 60 insertions(+), 61 deletions(-) diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 3621c89dd2f2..891360604631 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -62,6 +62,11 @@ */ #define BUFSIZE_SMALL (MAXPHYS) +/* + * Prompt used in -i case. + */ +#define YESNO "(y/n [n]) " + static ssize_t copy_fallback(int from_fd, int to_fd) { @@ -119,7 +124,6 @@ copy_file(const FTSENT *entp, int dne) * modified by the umask.) */ if (!dne) { -#define YESNO "(y/n [n]) " if (nflag) { if (vflag) printf("%s not overwritten\n", to.p_path); @@ -139,70 +143,68 @@ copy_file(const FTSENT *entp, int dne) } if (fflag) { - /* - * Remove existing destination file name create a new - * file. - */ + /* remove existing destination file */ (void)unlink(to.p_path); - if (!lflag && !sflag) { - to_fd = open(to.p_path, - O_WRONLY | O_TRUNC | O_CREAT, - fs->st_mode & ~(S_ISUID | S_ISGID)); - } - } else if (!lflag && !sflag) { - /* Overwrite existing destination file name. */ - to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0); } - } else if (!lflag && !sflag) { + } + + rval = 0; + + if (lflag) { + if (link(entp->fts_path, to.p_path) != 0) { + warn("%s", to.p_path); + rval = 1; + } + goto done; + } + + if (sflag) { + if (symlink(entp->fts_path, to.p_path) != 0) { + warn("%s", to.p_path); + rval = 1; + } + goto done; + } + + if (!dne && !fflag) { + /* overwrite existing destination file */ + to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0); + } else { + /* create new destination file */ to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, fs->st_mode & ~(S_ISUID | S_ISGID)); } - - if (!lflag && !sflag && to_fd == -1) { + if (to_fd == -1) { warn("%s", to.p_path); rval = 1; goto done; } - rval = 0; - - if (!lflag && !sflag) { - wtotal = 0; - do { - if (use_copy_file_range) { - wcount = copy_file_range(from_fd, NULL, - to_fd, NULL, SSIZE_MAX, 0); - if (wcount < 0 && errno == EINVAL) { - /* Prob a non-seekable FD */ - use_copy_file_range = 0; - } - } - if (!use_copy_file_range) { - wcount = copy_fallback(from_fd, to_fd); - } - wtotal += wcount; - if (info) { - info = 0; - (void)fprintf(stderr, - "%s -> %s %3d%%\n", - entp->fts_path, to.p_path, - cp_pct(wtotal, fs->st_size)); + wtotal = 0; + do { + if (use_copy_file_range) { + wcount = copy_file_range(from_fd, NULL, + to_fd, NULL, SSIZE_MAX, 0); + if (wcount < 0 && errno == EINVAL) { + /* probably a non-seekable descriptor */ + use_copy_file_range = 0; } - } while (wcount > 0); - if (wcount < 0) { - warn("%s", entp->fts_path); - rval = 1; } - } else if (lflag) { - if (link(entp->fts_path, to.p_path)) { - warn("%s", to.p_path); - rval = 1; + if (!use_copy_file_range) { + wcount = copy_fallback(from_fd, to_fd); } - } else if (sflag) { - if (symlink(entp->fts_path, to.p_path)) { - warn("%s", to.p_path); - rval = 1; + wtotal += wcount; + if (info) { + info = 0; + (void)fprintf(stderr, + "%s -> %s %3d%%\n", + entp->fts_path, to.p_path, + cp_pct(wtotal, fs->st_size)); } + } while (wcount > 0); + if (wcount < 0) { + warn("%s", entp->fts_path); + rval = 1; } /* @@ -211,16 +213,13 @@ copy_file(const FTSENT *entp, int dne) * or its contents might be irreplaceable. It would only be safe * to remove it if we created it and its length is 0. */ - - if (!lflag && !sflag) { - if (pflag && setfile(fs, to_fd)) - rval = 1; - if (pflag && preserve_fd_acls(from_fd, to_fd) != 0) - rval = 1; - if (close(to_fd)) { - warn("%s", to.p_path); - rval = 1; - } + if (pflag && setfile(fs, to_fd)) + rval = 1; + if (pflag && preserve_fd_acls(from_fd, to_fd) != 0) + rval = 1; + if (close(to_fd)) { + warn("%s", to.p_path); + rval = 1; } done: