From nobody Fri Feb 18 15:18:24 2022 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 4A6DD19DE85C; Fri, 18 Feb 2022 15:18:25 +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 4K0b1Y1Z4mz3NwQ; Fri, 18 Feb 2022 15:18:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645197505; 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=kIIseYnHuzSIkrD3d9jM6IaKuSvfLcnIAwrueEYgiLQ=; b=E9r3iilloqUxacqRL7ghGRXiGgLwR34wJKGFHTdVpfZLX/vjCM/PVQ5FC8O7AgMpSFM75L 8cgIUsRQMlRObGiCcoRHAxRnJlCaRwEtZshnoOu1/ZTJxN0yPrDI0176JgmhQudpKqGPM0 ZqSNKqTxUgDixPAeMGWZG1R7Tb4/zdrHWFBC1vHeKoA83Qryx6z+uhLZjfd0GWFPA4VA3R BFBxZfBsujDzGRCDKOcm9wszciXx7YPH0rjaUQrbZYKnvjnCTU1vqXPDDlpegsjYfOAE0k SQid1GNvWUU0wZ3I2QSPdnAfvQ7t5qEEkXO9mn5qO9qwhvUZglK2kVwq9pC+Sw== 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 15EC01F768; Fri, 18 Feb 2022 15:18:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 21IFIOLw005849; Fri, 18 Feb 2022 15:18:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21IFIOmt005848; Fri, 18 Feb 2022 15:18:24 GMT (envelope-from git) Date: Fri, 18 Feb 2022 15:18:24 GMT Message-Id: <202202181518.21IFIOmt005848@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Tom Jones Subject: git: f4be3645a14d - main - diff: add --no-dereference flag 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: thj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f4be3645a14d4faa5a51846054318ccf77c9cd5e Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645197505; 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=kIIseYnHuzSIkrD3d9jM6IaKuSvfLcnIAwrueEYgiLQ=; b=I8BuRD0Yr7Zg6dpC8+J6YB58PeCUMhIY2+j6D3uwdtM6l6vTQEG5NOShS5e+ZYGrc8K/Lu O5JFVmtyDSdvRFymdtJ8nRXxBoClGCnJ0xfOvv+340IPlRvafSOoEkKsYZBJCiKJes5A45 lBzFZEv7OIbuOoZtHmmfTZZXFtnBFtv76tNUONzQ+YZBp5EFyIsnaR/kf+fjdCNvYaOvSc cr8vW/gcdANxyMTQGlaHPzGtMotZ0jhrJtdNmc69ia0c0XTVHoHrlVCD7Z0I6FThzl2WLj d1omSkTQ9oocADEtNDM5Akb9U2K6nrSE5quw06uZNU+e+AaPb75BqDvOmEka/A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1645197505; a=rsa-sha256; cv=none; b=Hscr6vAQIxG0GhyzHEKHieHfpgRbt3G8UAmgemO38UzjJ0pj4TBYYs0gIIw0V7C/9Dc6FN uH/Qrlr6v3lEL3t/1JsEI4HrChsP20l6Hl8yTh07DYLsqMktffjda+wng1yodifHmK4hAE vo44MMF6IeHg+pZzcXuQnpP/iPNJEcLqequl/drojs81CWnTVlr2SSz8Fk4N6+/WnBms/T 4ZMKn2HMeJIr8G5iY1RNGNWPruA2TYVmndwcFmmyA7dYg7yDFSGvUCO/u/yIAWrauyTRRu VH/X9BBQWCbhzr3TvRfUqh6WO72QU++Zoio+T9quybeSlHg6roSb1MBTlvfYqA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by thj: URL: https://cgit.FreeBSD.org/src/commit/?id=f4be3645a14d4faa5a51846054318ccf77c9cd5e commit f4be3645a14d4faa5a51846054318ccf77c9cd5e Author: Tom Jones AuthorDate: 2022-02-18 15:13:13 +0000 Commit: Tom Jones CommitDate: 2022-02-18 15:17:13 +0000 diff: add --no-dereference flag When diffing files and directories, don't follow symbolic links, instead compare where the links point to. Reviewed by: bapt Sponsored by: Klara Inc. Differential Revision: https://reviews.freebsd.org/D34203 --- usr.bin/diff/diff.c | 8 +++- usr.bin/diff/diff.h | 2 +- usr.bin/diff/diffdir.c | 94 +++++++++++++++++++++++++++++++++-------- usr.bin/diff/tests/diff_test.sh | 31 ++++++++++++++ 4 files changed, 116 insertions(+), 19 deletions(-) diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index 8074261742ae..224301d56d18 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$"); #include "xmalloc.h" bool lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag; -bool ignore_file_case, suppress_common, color; +bool ignore_file_case, suppress_common, color, noderef; int diff_format, diff_context, status; int tabsize = 8, width = 130; static int colorflag = COLORFLAG_NEVER; @@ -62,6 +62,7 @@ enum { OPT_CHANGED_GROUP_FORMAT, OPT_SUPPRESS_COMMON, OPT_COLOR, + OPT_NO_DEREFERENCE, }; static struct option longopts[] = { @@ -97,6 +98,7 @@ static struct option longopts[] = { { "side-by-side", no_argument, NULL, 'y' }, { "ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE }, { "horizon-lines", required_argument, NULL, OPT_HORIZON_LINES }, + { "no-dereference", no_argument, NULL, OPT_NO_DEREFERENCE}, { "no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE }, { "normal", no_argument, NULL, OPT_NORMAL }, { "strip-trailing-cr", no_argument, NULL, OPT_STRIPCR }, @@ -328,6 +330,10 @@ main(int argc, char **argv) errx(2, "unsupported --color value '%s' (must be always, auto, or never)", optarg); break; + case OPT_NO_DEREFERENCE: + rflag = true; + noderef = true; + break; default: usage(); break; diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h index 4a7d19ee8982..fd649b017211 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -101,7 +101,7 @@ struct excludes { }; extern bool lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag; -extern bool ignore_file_case, suppress_common, color; +extern bool ignore_file_case, suppress_common, color, noderef; extern int diff_format, diff_context, status; extern int tabsize, width; extern char *start, *ifdefname, *diffargs, *label[2]; diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c index ecb7c4a6c4ee..527cd31a8c30 100644 --- a/usr.bin/diff/diffdir.c +++ b/usr.bin/diff/diffdir.c @@ -33,6 +33,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include "diff.h" @@ -175,28 +176,87 @@ diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2, { flags |= D_HEADER; strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1); - if (stat(path1, &stb1) != 0) { - if (!(Nflag || Pflag) || errno != ENOENT) { - warn("%s", path1); - return; + strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2); + + if (noderef) { + if (lstat(path1, &stb1) != 0) { + if (!(Nflag || Pflag) || errno != ENOENT) { + warn("%s", path1); + return; + } + flags |= D_EMPTY1; + memset(&stb1, 0, sizeof(stb1)); } - flags |= D_EMPTY1; - memset(&stb1, 0, sizeof(stb1)); - } - strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2); - if (stat(path2, &stb2) != 0) { - if (!Nflag || errno != ENOENT) { - warn("%s", path2); + if (lstat(path2, &stb2) != 0) { + if (!Nflag || errno != ENOENT) { + warn("%s", path2); + return; + } + flags |= D_EMPTY2; + memset(&stb2, 0, sizeof(stb2)); + stb2.st_mode = stb1.st_mode; + } + if (stb1.st_mode == 0) + stb1.st_mode = stb2.st_mode; + if (S_ISLNK(stb1.st_mode) || S_ISLNK(stb2.st_mode)) { + if (S_ISLNK(stb1.st_mode) && S_ISLNK(stb2.st_mode)) { + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + ssize_t len1 = 0; + ssize_t len2 = 0; + + len1 = readlink(path1, buf1, sizeof(buf1)); + len2 = readlink(path2, buf2, sizeof(buf2)); + + if (len1 < 0 || len2 < 0) { + perror("reading links"); + return; + } + buf1[len1] = '\0'; + buf2[len2] = '\0'; + + if (len1 != len2 || strncmp(buf1, buf2, len1) != 0) { + printf("Symbolic links %s and %s differ\n", + path1, path2); + status |= 1; + } + + return; + } + + printf("File %s is a %s while file %s is a %s\n", + path1, S_ISLNK(stb1.st_mode) ? "symbolic link" : + (S_ISDIR(stb1.st_mode) ? "directory" : + (S_ISREG(stb1.st_mode) ? "file" : "error")), + path2, S_ISLNK(stb2.st_mode) ? "symbolic link" : + (S_ISDIR(stb2.st_mode) ? "directory" : + (S_ISREG(stb2.st_mode) ? "file" : "error"))); + status |= 1; return; } - flags |= D_EMPTY2; - memset(&stb2, 0, sizeof(stb2)); - stb2.st_mode = stb1.st_mode; - } - if (stb1.st_mode == 0) - stb1.st_mode = stb2.st_mode; + } else { + if (stat(path1, &stb1) != 0) { + if (!(Nflag || Pflag) || errno != ENOENT) { + warn("%s", path1); + return; + } + flags |= D_EMPTY1; + memset(&stb1, 0, sizeof(stb1)); + } + if (stat(path2, &stb2) != 0) { + if (!Nflag || errno != ENOENT) { + warn("%s", path2); + return; + } + flags |= D_EMPTY2; + memset(&stb2, 0, sizeof(stb2)); + stb2.st_mode = stb1.st_mode; + } + if (stb1.st_mode == 0) + stb1.st_mode = stb2.st_mode; + } if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { if (rflag) diffdir(path1, path2, flags); diff --git a/usr.bin/diff/tests/diff_test.sh b/usr.bin/diff/tests/diff_test.sh index 8c0219712db7..a024016edb10 100755 --- a/usr.bin/diff/tests/diff_test.sh +++ b/usr.bin/diff/tests/diff_test.sh @@ -20,6 +20,7 @@ atf_test_case report_identical atf_test_case non_regular_file atf_test_case binary atf_test_case functionname +atf_test_case noderef simple_body() { @@ -296,6 +297,35 @@ functionname_body() "$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcclassm.in" } +noderef_body() +{ + atf_check mkdir A B + + atf_check -x "echo 1 > A/test-file" + atf_check -x "echo 1 > test-file" + atf_check -x "echo 1 > test-file2" + + atf_check ln -s $(pwd)/test-file B/test-file + + atf_check -o empty -s exit:0 diff -r A B + atf_check -o inline:"File A/test-file is a file while file B/test-file is a symbolic link\n" \ + -s exit:1 diff -r --no-dereference A B + + # both test files are now the same symbolic link + atf_check rm A/test-file + + atf_check ln -s $(pwd)/test-file A/test-file + atf_check -o empty -s exit:0 diff -r A B + atf_check -o empty -s exit:0 diff -r --no-dereference A B + + # make test files different symbolic links, but same contents + atf_check unlink A/test-file + atf_check ln -s $(pwd)/test-file2 A/test-file + + atf_check -o empty -s exit:0 diff -r A B + atf_check -o inline:"Symbolic links A/test-file and B/test-file differ\n" -s exit:1 diff -r --no-dereference A B +} + atf_init_test_cases() { atf_add_test_case simple @@ -318,4 +348,5 @@ atf_init_test_cases() atf_add_test_case non_regular_file atf_add_test_case binary atf_add_test_case functionname + atf_add_test_case noderef }