From nobody Wed Apr 24 22:12:38 2024 X-Original-To: dev-commits-src-all@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 4VPtX73Cjhz5JDGb; Wed, 24 Apr 2024 22:12:39 +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 4VPtX70WfQz4c9N; Wed, 24 Apr 2024 22:12:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713996759; 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=eUWgryrVUwmiKb3xpYlFdiqSptjsnyEax81nzUYY9cE=; b=b2cbvl5baSeQ1ZIkzUshMmuVnsjL1+I1ZjfxN9Btqo8yqqErrRjXj1sJ5scBAvvSbtwaHF LkTR4o6Wg0Y2W2ChRs/Es3LcwCcHDUAPlsuVhu9xct4FClrEPtwD4mpUX+0bfQ8FHRkq0k okf29VBgpSVykyvi1YjpzQrDCJpLx8CaRXmnnNc40Gk5NVhX8e4M0lsVlrQxds5C2jaga+ 7juctk7NEFrEyFQz1DnxNGSZlusn5KaonuzFKVaXYu6Xq12Fw6Uz1PvmOOUMezV7sI9sN4 wJTO5e96nUAecAkwzSaVNPQYc1IC3SYDLft+b1frJ6LmsgQbQunqmESLPVo9Mg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713996759; a=rsa-sha256; cv=none; b=tfKiywmxElZugY4+Zt4diHn5uF1OQp9o6/n3NTE8afpSyiQOcO4xQhxyhAIg0mJlHAxUqP X8qrWjiPHnMsCi+1GgqZVEIHXodgKrj1DVnZg+QNwGQ/I+SQeG0nGy0uFdA86rc3QW0j2V cx9Vf4Zw3q6aui9FffnuYQrx/vmqdKXA99VFg4zJKueYa0vtGosnjJ4Hws6YFhHS2pW17S qJjD4tHR4BHVHtaQiXWXpQ6AGkSxWWK02/DcmcslzcmNaSynKGX5Ss38L7GWdjFPozPsbo A8Pgjwp+eeeUJwaugVImFD79bVPoh6+F0GFIUZMlrU8sNl7Me//m50xGA7wahw== 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=1713996759; 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=eUWgryrVUwmiKb3xpYlFdiqSptjsnyEax81nzUYY9cE=; b=nTIsxpRCr2/t58WeaXdO1Oj/g6moGL65SNU1IfehmxqeAUPWQsQXUKa69iv3uVLX2PfwbF JnDiZkvL+Ilxy/s+/AxWEYehqj0Sk99kvWBa3Jp4oNY9LkJ/IiXetOuBe+Fh5a3qHqcmpS RDoN7WvrhGiBERFpmDm9twLOMWFJPHQs2jUwmKtpFeuBAYs6hWlUSOHep2uJgxcd8epB9l r7M0wwMSJUHp1DlusgpXq8tmCcyyRpQU8nYrpQtlUnqA0vQXLzmr2pd7Tss/Dzes0TO9SV FrjPwS53n7clnvi6ASKb3DJ076YAGZCoDQbs0lFMhC0m7n1aDcQiYgMTcUx92A== 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 4VPtX705dnzYX4; Wed, 24 Apr 2024 22:12:39 +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 43OMCcrJ070752; Wed, 24 Apr 2024 22:12:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43OMCcMQ070749; Wed, 24 Apr 2024 22:12:38 GMT (envelope-from git) Date: Wed, 24 Apr 2024 22:12:38 GMT Message-Id: <202404242212.43OMCcMQ070749@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 760720469681 - stable/14 - cp: Clarify an obscure comment. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 760720469681a2119b4537a6603337834317383b Auto-Submitted: auto-generated The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=760720469681a2119b4537a6603337834317383b commit 760720469681a2119b4537a6603337834317383b Author: Dag-Erling Smørgrav AuthorDate: 2024-04-17 01:36:22 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-04-24 22:11:56 +0000 cp: Clarify an obscure comment. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D44805 (cherry picked from commit 64d6925d1901637125f9f739282e72c992657dc8) cp: Additional sanity check. Once we've successfully opened the file we've been asked to copy, check that it's of the same type as FTS told us it was. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude, markj Differential Revision: https://reviews.freebsd.org/D44806 (cherry picked from commit 9075d4cfad5b339aabdf8033623a2164898c2786) cp: Use warnc(). MFC after: 1 week Sponsored by: Klara, Inc. (cherry picked from commit f070188c3ad6b87ee9ce220b21718333d1bd9d52) --- bin/cp/cp.c | 13 +++++++++---- bin/cp/utils.c | 27 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/bin/cp/cp.c b/bin/cp/cp.c index af7d5ffac398..a8c57308ae96 100644 --- a/bin/cp/cp.c +++ b/bin/cp/cp.c @@ -316,8 +316,7 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) case FTS_NS: case FTS_DNR: case FTS_ERR: - warnx("%s: %s", - curr->fts_path, strerror(curr->fts_errno)); + warnc(curr->fts_errno, "%s", curr->fts_path); badcp = rval = 1; continue; case FTS_DC: /* Warn, continue. */ @@ -491,13 +490,19 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) switch (curr->fts_statp->st_mode & S_IFMT) { case S_IFLNK: - /* Catch special case of a non-dangling symlink. */ if ((fts_options & FTS_LOGICAL) || ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) { + /* + * We asked FTS to follow links but got + * here anyway, which means the target is + * nonexistent or inaccessible. Let + * copy_file() deal with the error. + */ if (copy_file(curr, dne)) badcp = rval = 1; - } else { + } else { + /* Copy the link. */ if (copy_link(curr, !dne)) badcp = rval = 1; } diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 353d35214844..a2cb2f536843 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -106,21 +106,34 @@ copy_fallback(int from_fd, int to_fd) int copy_file(const FTSENT *entp, int dne) { - struct stat *fs; + struct stat sb, *fs; ssize_t wcount; off_t wtotal; int ch, checkch, from_fd, rval, to_fd; int use_copy_file_range = 1; + fs = entp->fts_statp; from_fd = to_fd = -1; - if (!lflag && !sflag && - (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { - warn("%s", entp->fts_path); - return (1); + if (!lflag && !sflag) { + if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) < 0 || + fstat(from_fd, &sb) != 0) { + warn("%s", entp->fts_path); + return (1); + } + /* + * Check that the file hasn't been replaced with one of a + * different type. This can happen if we've been asked to + * copy something which is actively being modified and + * lost the race, or if we've been asked to copy something + * like /proc/X/fd/Y which stat(2) reports as S_IFREG but + * is actually something else once you open it. + */ + if ((sb.st_mode & S_IFMT) != (fs->st_mode & S_IFMT)) { + warnx("%s: File changed", entp->fts_path); + return (1); + } } - fs = entp->fts_statp; - /* * If the file exists and we're interactive, verify with the user. * If the file DNE, set the mode to be the from file, minus setuid