From d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc Mon Sep 17 00:00:00 2001 From: Conrad Meyer Date: Fri, 29 Dec 2017 22:08:43 +0000 Subject: [PATCH] find(1): Fix -newer and -samefile to conform to POSIX[0] By default, or with the -P flag, find(1) should evaluate paths "physically." For symlinks, this means using the link itself instead of the target. Historically (since the import of BSD 4.4-lite from CSRG), find(1) has failed to refer to the link itself, at least for -newer and -samefile. [0]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html PR: 222698 Reported by: Harald Schmalzbauer Sponsored by: Dell EMC Isilon --- usr.bin/find/Makefile | 3 ++ usr.bin/find/function.c | 14 ++++++- usr.bin/find/tests/Makefile | 5 +++ usr.bin/find/tests/find_test.sh | 73 +++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 usr.bin/find/tests/Makefile create mode 100755 usr.bin/find/tests/find_test.sh diff --git a/usr.bin/find/Makefile b/usr.bin/find/Makefile index cd10f123045..b18b7c8e918 100644 --- a/usr.bin/find/Makefile +++ b/usr.bin/find/Makefile @@ -8,4 +8,7 @@ YFLAGS= NO_WMISSING_VARIABLE_DECLARATIONS= +#HAS_TESTS= +#SUBDIR.${MK_TESTS}+= tests + .include diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index cc724ba7c77..72bc8fee275 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -1066,12 +1066,17 @@ c_samefile(OPTION *option, char ***argvp) char *fn; PLAN *new; struct stat sb; + int error; fn = nextarg(option, argvp); ftsoptions &= ~FTS_NOSTAT; new = palloc(option); - if (stat(fn, &sb)) + if (ftsoptions & FTS_PHYSICAL) + error = lstat(fn, &sb); + else + error = stat(fn, &sb); + if (error != 0) err(1, "%s", fn); new->i_data = sb.st_ino; return new; @@ -1201,6 +1206,7 @@ c_newer(OPTION *option, char ***argvp) char *fn_or_tspec; PLAN *new; struct stat sb; + int error; fn_or_tspec = nextarg(option, argvp); ftsoptions &= ~FTS_NOSTAT; @@ -1214,7 +1220,11 @@ c_newer(OPTION *option, char ***argvp) /* Use the seconds only in the comparison. */ new->t_data.tv_nsec = 999999999; } else { - if (stat(fn_or_tspec, &sb)) + if (ftsoptions & FTS_PHYSICAL) + error = lstat(fn_or_tspec, &sb); + else + error = stat(fn_or_tspec, &sb); + if (error != 0) err(1, "%s", fn_or_tspec); if (option->flags & F_TIME2_C) new->t_data = sb.st_ctim; diff --git a/usr.bin/find/tests/Makefile b/usr.bin/find/tests/Makefile new file mode 100644 index 00000000000..e8c22a36a5f --- /dev/null +++ b/usr.bin/find/tests/Makefile @@ -0,0 +1,5 @@ +# $FreeBSD$ + +ATF_TESTS_SH= find_test + +.include diff --git a/usr.bin/find/tests/find_test.sh b/usr.bin/find/tests/find_test.sh new file mode 100755 index 00000000000..604e98795a1 --- /dev/null +++ b/usr.bin/find/tests/find_test.sh @@ -0,0 +1,73 @@ +# +# Copyright 2017, Conrad Meyer . +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# $FreeBSD$ +# + +atf_test_case find_newer_link +find_newer_link_head() +{ + atf_set "descr" "Verifies that -newer correctly uses a symlink, " \ + "rather than its target, for comparison" +} +find_newer_link_body() +{ + atf_check -s exit:0 mkdir test + atf_check -s exit:0 ln -s file1 test/link + atf_check -s exit:0 sleep 1.1 + atf_check -s exit:0 touch test/file2 + atf_check -s exit:0 sleep 1.1 + atf_check -s exit:0 touch test/file1 + + # find(1) should evaluate 'link' as a symlink rather than its target + # (with -P / without -L flags). Since link was created first, the + # other two files should be newer. + echo -e "test\ntest/file1\ntest/file2" > expout + atf_check -s exit:0 -o save:output find test -newer test/link + atf_check -s exit:0 -o file:expout sort < output +} + +atf_test_case find_samefile_link +find_samefile_link_head() +{ + atf_set "descr" "Verifies that -samefile correctly uses a symlink, " \ + "rather than its target, for comparison" +} +find_samefile_link_body() +{ + atf_check -s exit:0 mkdir test + atf_check -s exit:0 touch test/file3 + atf_check -s exit:0 ln -s file3 test/link2 + + # find(1) should evaluate 'link' as a symlink rather than its target + # (with -P / without -L flags). + atf_check -s exit:0 -o "inline:test/link2\n" find test -samefile test/link2 +} + +atf_init_test_cases() +{ + atf_add_test_case find_newer_link + atf_add_test_case find_samefile_link +}