From nobody Mon Apr 08 10:11:42 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 4VClHg0HqMz5GjSg; Mon, 8 Apr 2024 10:11:43 +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 4VClHf4WGPz4WJq; Mon, 8 Apr 2024 10:11:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712571102; 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=YuFr8oPzh7YJkPmOix+lXv+e9nsZg9egeISRmKoBOi8=; b=I1MOsueTjzD8AyY/GtNBmDfnhRzyhJRuaAWfVV1aTQufn0Fqdt4BrQN/aRoX9r/TjZQLzy GERolG2eTyVdIgd2DQUomQLNPHYxIP6OMDniI1aDrUA89MV3P2P9JqsZD/ys7l+GHVpp3d lE6yxoXTyvE4yu+QfEH06s4x90W35HYeUIEOMe/8YmGSoA1HMd2jhrYG2f1pilBd6TjKJM VDdG8be2MkvmCI/+I/+yxZWV4IXx5/HSuwIRjWlgSun0tbrl0SF5YlUJs6ewscUSFyFHyz nbcyYtAFfT1DR5njU81sg86fML7HLZzRxSkiyBe79zUOacH9m304FLXdhqlByQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712571102; a=rsa-sha256; cv=none; b=JJEbKWiTLeVAuyknbrz6gGRPiOkiuaDVGmvdss6CZPmde3NSrTduz/BnGaCRabYT23sbng Zp7zU0Tyf6bOvN5Jiipq4rTQSZ6Fa/8f7V0XXJpmW1lyLs4M4lYqBw7OySXQN8NrIsUe/5 bZdT7REU7zpmpFflRz36hLECrEwhpBDHdNOTFKyMmVl+1mIDRxLLiuGSv0CqrEsPRo7UN7 rzLxHycpeT4IWvyXojvThRx/RO3UeyhLtYPMfwA3kmaJ2G4Sghyv+LTV/W656OrN8XqwJs dK11dE1uk6QdrgU5aQffS/sp3Ng6MBuXKDQ6hMeOawJwFa8gzayLNC0BzqJS7A== 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=1712571102; 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=YuFr8oPzh7YJkPmOix+lXv+e9nsZg9egeISRmKoBOi8=; b=RG8smckG3EjEcpBfxf9oh9Oxk4DtUhQDOtr5a2Y2qIcvfnaBt9MNLoEIO/nB/BqfOF3t1U 7pAQrslW8UkEnZ8IKYgRYBA0D3i7u1oSKgmpeaKa6EpSgbbjvuQWURkKK4mOFHtOQ/HrMZ URDLVBHk0burlKlgSexqV86pUP/y17KbPB5VWMwDQ3MRAZoDvuKU/h4w/21N9bfQRwdwWf 1oyd8bFAyMkziIGlaBV8Uu/Qfv0T9r+Ew2KMYn+wDplxpPvtjLFyvnVJYURQfwte89oJe1 tlWtkmN+fw48MtfyALFRC3p+i7Ch0sfIcD9UrM0GspKrsIaD46HUmza2XO9fNw== 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 4VClHf46zSz16sN; Mon, 8 Apr 2024 10:11:42 +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 438ABgId093417; Mon, 8 Apr 2024 10:11:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 438ABgHf093414; Mon, 8 Apr 2024 10:11:42 GMT (envelope-from git) Date: Mon, 8 Apr 2024 10:11:42 GMT Message-Id: <202404081011.438ABgHf093414@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: dc0087a3aa78 - stable/13 - ln: Improve link(1) variant of ln(1). 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/13 X-Git-Reftype: branch X-Git-Commit: dc0087a3aa788dbee4f3b4ab0270fdbb48bc20c9 Auto-Submitted: auto-generated The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=dc0087a3aa788dbee4f3b4ab0270fdbb48bc20c9 commit dc0087a3aa788dbee4f3b4ab0270fdbb48bc20c9 Author: Dag-Erling Smørgrav AuthorDate: 2024-04-04 14:14:50 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-04-08 10:10:15 +0000 ln: Improve link(1) variant of ln(1). * Give link(1) its own usage message. * Use getprogname(3) instead of rolling our own. * Verify that the target file does not already exist. * Add tests specific to link(1). MFC after: 3 days Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D44635 (cherry picked from commit bee7cf9e97f6d7bdc918421a93270fa88659808b) --- bin/ln/ln.c | 32 +++++++++++++++--------- bin/ln/tests/ln_test.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/bin/ln/ln.c b/bin/ln/ln.c index a0e19d702dea..3919494c3d4c 100644 --- a/bin/ln/ln.c +++ b/bin/ln/ln.c @@ -67,13 +67,14 @@ static bool wflag; /* Warn if symlink target does not static char linkch; static int linkit(const char *, const char *, bool); +static void link_usage(void) __dead2; static void usage(void) __dead2; int main(int argc, char *argv[]) { struct stat sb; - char *p, *targetdir; + char *targetdir; int ch, exitval; /* @@ -81,17 +82,20 @@ main(int argc, char *argv[]) * "link", for which the functionality provided is greatly * simplified. */ - if ((p = strrchr(argv[0], '/')) == NULL) - p = argv[0]; - else - ++p; - if (strcmp(p, "link") == 0) { + if (strcmp(getprogname(), "link") == 0) { while (getopt(argc, argv, "") != -1) - usage(); + link_usage(); argc -= optind; argv += optind; if (argc != 2) - usage(); + link_usage(); + if (lstat(argv[1], &sb) == 0) + errc(1, EEXIST, "%s", argv[1]); + /* + * We could simply call link(2) here, but linkit() + * performs additional checks and gives better + * diagnostics. + */ exit(linkit(argv[0], argv[1], false)); } @@ -349,12 +353,18 @@ linkit(const char *source, const char *target, bool isdir) return (0); } +static void +link_usage(void) +{ + (void)fprintf(stderr, "usage: link source_file target_file\n"); + exit(1); +} + static void usage(void) { - (void)fprintf(stderr, "%s\n%s\n%s\n", + (void)fprintf(stderr, "%s\n%s\n", "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]", - " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir", - " link source_file target_file"); + " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir"); exit(1); } diff --git a/bin/ln/tests/ln_test.sh b/bin/ln/tests/ln_test.sh index 8e5dcf81e61f..82bc556842d8 100644 --- a/bin/ln/tests/ln_test.sh +++ b/bin/ln/tests/ln_test.sh @@ -90,7 +90,7 @@ target_exists_hard_body() { atf_check touch A B atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ - ln A B + ln A B } atf_test_case target_exists_symbolic @@ -103,7 +103,7 @@ target_exists_symbolic_body() { atf_check touch A B atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ - ln -s A B + ln -s A B } atf_test_case shf_flag_dir @@ -210,10 +210,65 @@ sw_flag_head() sw_flag_body() { atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \ - ln -sw A B + ln -sw A B atf_check_symlink_to A B } +atf_test_case link_argc +link_argc_head() { + atf_set "descr" "Verify that link(1) requires exactly two arguments" +} +link_argc_body() { + atf_check -s exit:1 -e match:"usage: link" \ + link foo + atf_check -s exit:1 -e match:"No such file" \ + link foo bar + atf_check -s exit:1 -e match:"No such file" \ + link -- foo bar + atf_check -s exit:1 -e match:"usage: link" \ + link foo bar baz +} + +atf_test_case link_basic +link_basic_head() { + atf_set "descr" "Verify that link(1) creates a link" +} +link_basic_body() { + touch foo + atf_check link foo bar + atf_check_same_file foo bar + rm bar + ln -s foo bar + atf_check link bar baz + atf_check_same_file foo baz +} + +atf_test_case link_eexist +link_eexist_head() { + atf_set "descr" "Verify that link(1) fails if the target exists" +} +link_eexist_body() { + touch foo bar + atf_check -s exit:1 -e match:"bar.*exists" \ + link foo bar + ln -s non-existent baz + atf_check -s exit:1 -e match:"baz.*exists" \ + link foo baz +} + +atf_test_case link_eisdir +link_eisdir_head() { + atf_set "descr" "Verify that link(1) fails if the source is a directory" +} +link_eisdir_body() { + mkdir foo + atf_check -s exit:1 -e match:"foo.*directory" \ + link foo bar + ln -s foo bar + atf_check -s exit:1 -e match:"bar.*directory" \ + link bar baz +} + atf_init_test_cases() { atf_add_test_case L_flag @@ -229,4 +284,8 @@ atf_init_test_cases() atf_add_test_case s_flag atf_add_test_case s_flag_broken atf_add_test_case sw_flag + atf_add_test_case link_argc + atf_add_test_case link_basic + atf_add_test_case link_eexist + atf_add_test_case link_eisdir }