From nobody Thu Apr 04 11:38:49 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 4V9KQ20Rhcz5GrMM; Thu, 4 Apr 2024 11:38:50 +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 4V9KQ15N2Fz57NM; Thu, 4 Apr 2024 11:38:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712230729; 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=0zAKuBGftuhTwjUnGGyn90vMFXO/QHbfWetjfGvcKa8=; b=cBQbfSVncGCZua76lLUljty5HkVHglg7c05OfhgzqKXmoE6SPMRzPeQ6aX6XR650gYi2XU phQB/tNV7rYqwN1UswJAU0stoWhB6D5Wyfyg4Ybv85ut0opxlhu4ZHkWIVdgtn3b61rOIL tEbfjVTk4D16qqEGIwb3n87n1+kO9jypTyodUjzeBArGDNmAS8zRziqAJKPvQ9Ye/UGDCT I30qN8vUBuhu0JAnRKQjz/FhX4ZLusA0d9LvdiiqO5lrWnYsc58u1T31nFj3egV0Veau3S NFHo595UNxG80vSHBDn8TkTWPuw/fcJ9shJDvLQhyscpY4NWm1YpBjK+ErtYMg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712230729; a=rsa-sha256; cv=none; b=mFe7clffAJb9pyoKNk/csciY8aROXJTHHlTUxonzD5/5oHu96gWG8IW60O1fLP5t/bAI7K 1OSbZgRqye/rtFnxvF0KQexBkDDG6H7iIVfhWtajGqXS7BQkxdIc4inHwaZNk2evijMhfI hAxroVyzkCx04q0XeZL6QkpuwZ/HOEH91ch49KSIcp1zBH0fM6S27KxeLpDkCBCQ6qtAsA WObT+CHE9VpVDuQzudVF8y20bTKmx+TXgy03B1VSz1WdDUT35rDKU2p/gjhCCtbN78qBbY Wtr+G8EPHmmKtDcih/UVLNDPa6Nadm7FvPhHAXZCk8K25U2sJrBOMjl1hyhpKA== 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=1712230729; 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=0zAKuBGftuhTwjUnGGyn90vMFXO/QHbfWetjfGvcKa8=; b=NBigf3h2sgZ/fEc30WsItC8kN6XQlaxt/8+klVWOKa5K8lvxvvaMjxoC4YU6xwAWjZc/VW 5WKzQDB9vlBp/d3uu/Swjr4tu7AEZZtq7dX7ajjrYxP04QaVPhfj3k4fWGmBNJijHapVCB h7Pj3lR0soQKJ3FAGv5TjjbgkNC8gVngLxFFFyfUyk1Dd73Ggk5IcyulAf8CLfSvZI/O8h phje11TKC9gzm1PKaQTVbvvWv8KJQOu5tP7j3lHSlml33S3W93D1ij/LOVOKyzScsgqlBd dVQkgWtY4YzGEpOt3QXoO5TkICMQ1QLvHv3T0R2nqerCJuGg3/y2vYWLayBY3Q== 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 4V9KQ14z3lzFVZ; Thu, 4 Apr 2024 11:38:49 +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 434BcnCZ059748; Thu, 4 Apr 2024 11:38:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 434BcnVn059745; Thu, 4 Apr 2024 11:38:49 GMT (envelope-from git) Date: Thu, 4 Apr 2024 11:38:49 GMT Message-Id: <202404041138.434BcnVn059745@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: d738ce7cf6a3 - stable/14 - ln: Use stdbool, style nits. 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: 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: d738ce7cf6a35faa071d3945ae75a7438a7b28e6 Auto-Submitted: auto-generated The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=d738ce7cf6a35faa071d3945ae75a7438a7b28e6 commit d738ce7cf6a35faa071d3945ae75a7438a7b28e6 Author: Dag-Erling Smørgrav AuthorDate: 2024-03-27 10:03:49 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-04-04 09:43:14 +0000 ln: Use stdbool, style nits. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: imp, allanjude Differential Revision: https://reviews.freebsd.org/D44511 (cherry picked from commit 437d53daf71a357aae960881ef037564736f7441) ln: Clean up and simplify tests. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D44512 (cherry picked from commit e0afcbc85690b6464706ead57103baa0493fdfb2) ln: Add a test case for ln -sfF. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude, asomers Differential Revision: https://reviews.freebsd.org/D44513 (cherry picked from commit 2ae8d34666a6dea4c1c77a1905c7b8cebd6a21f9) --- bin/ln/ln.c | 76 ++++++++++++++++++++++--------------------- bin/ln/tests/ln_test.sh | 85 +++++++++++++++++++++---------------------------- 2 files changed, 76 insertions(+), 85 deletions(-) diff --git a/bin/ln/ln.c b/bin/ln/ln.c index 546bfba1c7c3..a0e19d702dea 100644 --- a/bin/ln/ln.c +++ b/bin/ln/ln.c @@ -49,24 +49,25 @@ static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94"; #include #include #include +#include #include #include #include #include -static int fflag; /* Unlink existing files. */ -static int Fflag; /* Remove empty directories also. */ -static int hflag; /* Check new name for symlink first. */ -static int iflag; /* Interactive mode. */ -static int Pflag; /* Create hard links to symlinks. */ -static int sflag; /* Symbolic, not hard, link. */ -static int vflag; /* Verbose output. */ -static int wflag; /* Warn if symlink target does not +static bool fflag; /* Unlink existing files. */ +static bool Fflag; /* Remove empty directories also. */ +static bool hflag; /* Check new name for symlink first. */ +static bool iflag; /* Interactive mode. */ +static bool Pflag; /* Create hard links to symlinks. */ +static bool sflag; /* Symbolic, not hard, link. */ +static bool vflag; /* Verbose output. */ +static bool wflag; /* Warn if symlink target does not * exist, and -f is not enabled. */ static char linkch; -static int linkit(const char *, const char *, int); -static void usage(void); +static int linkit(const char *, const char *, bool); +static void usage(void) __dead2; int main(int argc, char *argv[]) @@ -91,41 +92,41 @@ main(int argc, char *argv[]) argv += optind; if (argc != 2) usage(); - exit(linkit(argv[0], argv[1], 0)); + exit(linkit(argv[0], argv[1], false)); } while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1) switch (ch) { case 'F': - Fflag = 1; + Fflag = true; break; case 'L': - Pflag = 0; + Pflag = false; break; case 'P': - Pflag = 1; + Pflag = true; break; case 'f': - fflag = 1; - iflag = 0; - wflag = 0; + fflag = true; + iflag = false; + wflag = false; break; case 'h': case 'n': - hflag = 1; + hflag = true; break; case 'i': - iflag = 1; - fflag = 0; + iflag = true; + fflag = false; break; case 's': - sflag = 1; + sflag = true; break; case 'v': - vflag = 1; + vflag = true; break; case 'w': - wflag = 1; + wflag = true; break; case '?': default: @@ -136,21 +137,21 @@ main(int argc, char *argv[]) argc -= optind; linkch = sflag ? '-' : '='; - if (sflag == 0) - Fflag = 0; - if (Fflag == 1 && iflag == 0) { - fflag = 1; - wflag = 0; /* Implied when fflag != 0 */ + if (!sflag) + Fflag = false; + if (Fflag && !iflag) { + fflag = true; + wflag = false; /* Implied when fflag is true */ } - switch(argc) { + switch (argc) { case 0: usage(); /* NOTREACHED */ case 1: /* ln source */ - exit(linkit(argv[0], ".", 1)); + exit(linkit(argv[0], ".", true)); case 2: /* ln source target */ - exit(linkit(argv[0], argv[1], 0)); + exit(linkit(argv[0], argv[1], false)); default: ; } @@ -169,7 +170,7 @@ main(int argc, char *argv[]) if (!S_ISDIR(sb.st_mode)) usage(); for (exitval = 0; *argv != targetdir; ++argv) - exitval |= linkit(*argv, targetdir, 1); + exitval |= linkit(*argv, targetdir, true); exit(exitval); } @@ -220,14 +221,15 @@ samedirent(const char *path1, const char *path2) } static int -linkit(const char *source, const char *target, int isdir) +linkit(const char *source, const char *target, bool isdir) { - struct stat sb; - const char *p; - int ch, exists, first; char path[PATH_MAX]; char wbuf[PATH_MAX]; char bbuf[PATH_MAX]; + struct stat sb; + const char *p; + int ch, first; + bool exists; if (!sflag) { /* If source doesn't exist, quit now. */ @@ -290,7 +292,7 @@ linkit(const char *source, const char *target, int isdir) /* * If the file exists, first check it is not the same directory entry. */ - exists = !lstat(target, &sb); + exists = lstat(target, &sb) == 0; if (exists) { if (!sflag && samedirent(source, target)) { warnx("%s and %s are the same directory entry", diff --git a/bin/ln/tests/ln_test.sh b/bin/ln/tests/ln_test.sh index 75fda4ce2dd7..8e5dcf81e61f 100644 --- a/bin/ln/tests/ln_test.sh +++ b/bin/ln/tests/ln_test.sh @@ -1,4 +1,6 @@ # +# SPDX-License-Identifier: BSD-2-Clause +# # Copyright 2017 Shivansh Rai # All rights reserved. # @@ -23,13 +25,15 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # -# -set_umask() +atf_check_same_file() { - if ! umask 022; then - atf_fail "setting umask failed" - fi + atf_check_equal "$(stat -f %d,%i "$1")" "$(stat -f %d,%i "$2")" +} + +atf_check_symlink_to() +{ + atf_check -o inline:"$1\n" readlink "$2" } atf_test_case L_flag @@ -39,18 +43,13 @@ L_flag_head() "symbolic link, '-L' option creates a hard" \ "link to the target of the symbolic link" } - L_flag_body() { - set_umask atf_check touch A atf_check ln -s A B atf_check ln -L B C - stat_A=$(stat -f %i A) - stat_C=$(stat -f %i C) - atf_check_equal "$stat_A" "$stat_C" - atf_check -o inline:'Symbolic Link\n' stat -f %SHT B - atf_check -o inline:'A\n' readlink B + atf_check_same_file A C + atf_check_symlink_to A B } atf_test_case P_flag @@ -60,16 +59,12 @@ P_flag_head() "symbolic link, '-P' option creates a hard " \ "link to the symbolic link itself" } - P_flag_body() { - set_umask atf_check touch A atf_check ln -s A B atf_check ln -P B C - stat_B=$(stat -f %i B) - stat_C=$(stat -f %i C) - atf_check_equal "$stat_B" "$stat_C" + atf_check_same_file B C } atf_test_case f_flag @@ -78,15 +73,11 @@ f_flag_head() atf_set "descr" "Verify that if the target file already exists, " \ "'-f' option unlinks it so that link may occur" } - f_flag_body() { - set_umask atf_check touch A B atf_check ln -f A B - stat_A=$(stat -f %i A) - stat_B=$(stat -f %i B) - atf_check_equal "$stat_A" "$stat_B" + atf_check_same_file A B } atf_test_case target_exists_hard @@ -95,10 +86,8 @@ target_exists_hard_head() atf_set "descr" "Verify whether creating a hard link fails if the " \ "target file already exists" } - target_exists_hard_body() { - set_umask atf_check touch A B atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ ln A B @@ -110,10 +99,8 @@ target_exists_symbolic_head() atf_set "descr" "Verify whether creating a symbolic link fails if " \ "the target file already exists" } - target_exists_symbolic_body() { - set_umask atf_check touch A B atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ ln -s A B @@ -124,13 +111,12 @@ shf_flag_dir_head() { atf_set "descr" "Verify that if the target directory is a symbolic " \ "link, '-shf' option prevents following the link" } - shf_flag_dir_body() { atf_check mkdir -m 0777 A B atf_check ln -s A C atf_check ln -shf B C - atf_check -o inline:'Symbolic Link\n' stat -f %SHT C + atf_check test -L C atf_check -o inline:'B\n' readlink C } @@ -139,14 +125,12 @@ snf_flag_dir_head() { atf_set "descr" "Verify that if the target directory is a symbolic " \ "link, '-snf' option prevents following the link" } - snf_flag_dir_body() { atf_check mkdir -m 0777 A B atf_check ln -s A C atf_check ln -snf B C - atf_check -o inline:'Symbolic Link\n' stat -f %SHT C - atf_check -o inline:'B\n' readlink C + atf_check_symlink_to B C } atf_test_case sF_flag @@ -156,13 +140,11 @@ sF_flag_head() "and is a directory, then '-sF' option removes " \ "it so that the link may occur" } - sF_flag_body() { atf_check mkdir A B atf_check ln -sF A B - atf_check -o inline:'Symbolic Link\n' stat -f %SHT B - atf_check -o inline:'A\n' readlink B + atf_check_symlink_to A B } atf_test_case sf_flag @@ -172,14 +154,27 @@ sf_flag_head() "'-sf' option unlinks it and creates a symbolic link " \ "to the source file" } - sf_flag_body() { - set_umask atf_check touch A B atf_check ln -sf A B - atf_check -o inline:'Symbolic Link\n' stat -f %SHT B - atf_check -o inline:'A\n' readlink B + atf_check_symlink_to A B +} + +atf_test_case sfF_flag +sfF_flag_head() +{ + atf_set "descr" "Verify that if the target file already exists " \ + "and is a symlink, then '-sfF' option removes " \ + "it so that the link may occur" +} +sfF_flag_body() +{ + atf_check mkdir A B C + atf_check ln -sF A C + atf_check_symlink_to A C + atf_check ln -sfF B C + atf_check_symlink_to B C } atf_test_case s_flag @@ -187,14 +182,11 @@ s_flag_head() { atf_set "descr" "Verify that '-s' option creates a symbolic link" } - s_flag_body() { - set_umask atf_check touch A atf_check ln -s A B - atf_check -o inline:'Symbolic Link\n' stat -f %SHT B - atf_check -o inline:'A\n' readlink B + atf_check_symlink_to A B } atf_test_case s_flag_broken @@ -203,12 +195,10 @@ s_flag_broken_head() atf_set "descr" "Verify that if the source file does not exists, '-s' " \ "option creates a broken symbolic link to the source file" } - s_flag_broken_body() { atf_check ln -s A B - atf_check -o inline:'Symbolic Link\n' stat -f %SHT B - atf_check -o inline:'A\n' readlink B + atf_check_symlink_to A B } atf_test_case sw_flag @@ -217,13 +207,11 @@ sw_flag_head() atf_set "descr" "Verify that '-sw' option produces a warning if the " \ "source of a symbolic link does not currently exist" } - sw_flag_body() { atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \ ln -sw A B - atf_check -o inline:'Symbolic Link\n' stat -f %SHT B - atf_check -o inline:'A\n' readlink B + atf_check_symlink_to A B } atf_init_test_cases() @@ -237,6 +225,7 @@ atf_init_test_cases() atf_add_test_case snf_flag_dir atf_add_test_case sF_flag atf_add_test_case sf_flag + atf_add_test_case sfF_flag atf_add_test_case s_flag atf_add_test_case s_flag_broken atf_add_test_case sw_flag