From patchwork Thu Nov 30 05:01:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Goncharov X-Patchwork-Id: 24625 Received: (qmail 68547 invoked by alias); 30 Nov 2017 05:06:03 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 49530 invoked by uid 89); 30 Nov 2017 05:01:26 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.7 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f176.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=42natbVDSX4irhcvoPijbDT/Wb8kabSAWbeiS9O1uQU=; b=a0xumo2Tjqt3YANtIxgoPYXMJuZEz3NuEyYvyNGasvhyUKbyG+JIreqDjpcD+5NWn7 LTm0rBpNgKWC77tDxBIyFC8CW+RpaddaUIafLKoRqXN7bQ6u8uac7S7Xewm2CfxnZ2Mv JLbII//Has5616NzPIud+LhbuasGjh296vSnQ+S/8TNzmwiUQ70uYs6kbQpyz56o8gKo WZ9lgtNSfcioosHwd4nnKpCYV+Lln0+4g0+0/LEkxQFiillqBVUZQslKj3DgHv0aGqwF BSC7bKG63c5k0t6HUS7OLoZaZjTcMuz4LsvaVKd8sfseEdk7fDZLGxDL6VY3u3TGeg/n 2PsQ== X-Gm-Message-State: AKGB3mKh2TU00PmdNAALYLT15ahHB95e2Ji3ONMsHSYWNZZqgt3CxNpE dptSYTL3ZR4A8iniKI6LuHT7+Q== X-Google-Smtp-Source: AGs4zMbbhUQUoPyy1eaPyVcGAoxUtYcUrK6Tt6DX3zn+LBZdB+wUeW+Yul7UEEc2SJ52JkLMJ4eNVw== X-Received: by 10.200.4.154 with SMTP id s26mr1934006qtg.156.1512018080264; Wed, 29 Nov 2017 21:01:20 -0800 (PST) Date: Thu, 30 Nov 2017 00:01:19 -0500 From: Dmitry Goncharov To: Jonathan Nieder Cc: libc-alpha@sourceware.org, bug-gnulib@gnu.org Subject: Re: [PATCH] posix: if glob has a trailing slash match directories only. Message-ID: <20171130050118.GA21476@madrid> References: <20171128210857.GC2745@madrid> <20171128220453.GC27469@aiede.mtv.corp.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20171128220453.GC27469@aiede.mtv.corp.google.com> User-Agent: Mutt/1.5.21 (2010-09-15) On Tue, Nov 28, 2017 at 02:04:53PM -0800, Jonathan Nieder wrote: > > What should happen in the DT_LNK case? Should the same logic trip for > it as well so we can distinguish between a symlink to a directory and > other symlinks? > The filesystems that i tested on all return DT_UNKNOWN for a symlink or hardlink. Even when they return proper type for a file or directory. However, if the filesystem ever sets type to DT_LNK glob needs to treat it like DT_UNKNOWN. regards, Dmitry diff --git a/ChangeLog b/ChangeLog index ffeb208132..fb76fc1415 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2017-11-28 Dmitry Goncharov + + [BZ #22513] + * posix/glob.c (glob_in_dir): Make pattern with a trailing slash + match directores only. + * posix/globtest.sh: Add tests. + 2017-11-13 Florian Weimer * support/next_to_fault.h, support/next_to_fault.c: New files. diff --git a/posix/glob.c b/posix/glob.c index cb39779d07..47e67907cd 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -1342,7 +1342,33 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (flags & GLOB_ONLYDIR) switch (readdir_result_type (d)) { - case DT_DIR: case DT_LNK: case DT_UNKNOWN: break; + case DT_DIR: break; + case DT_LNK: case DT_UNKNOWN: + { + int dir; + size_t namlen = strlen (d.name); + size_t fullsize; + bool alloca_fullname + = (! size_add_wrapv (dirlen + 1, namlen + 1, &fullsize) + && glob_use_alloca (alloca_used, fullsize)); + char *fullname; + if (alloca_fullname) + fullname = alloca_account (fullsize, alloca_used); + else + { + fullname = malloc (fullsize); + if (fullname == NULL) + return GLOB_NOSPACE; + } + mempcpy (mempcpy (mempcpy (fullname, directory, dirlen), + "/", 1), + d.name, namlen + 1); + dir = is_dir (fullname, flags, pglob); + if (__glibc_unlikely (!alloca_fullname)) + free (fullname); + if (dir) + break; + } default: continue; } diff --git a/posix/globtest.sh b/posix/globtest.sh index 73f7ae31cc..4a062ea507 100755 --- a/posix/globtest.sh +++ b/posix/globtest.sh @@ -43,13 +43,22 @@ export LC_ALL # Create the arena testdir=${common_objpfx}posix/globtest-dir +testdir2=${common_objpfx}posix/globtest-dir2 testout=${common_objpfx}posix/globtest-out rm -rf $testdir $testout mkdir $testdir +mkdir $testdir2 +mkdir $testdir2/hello1d +ln -s $testdir2/hello1d $testdir2/hello1ds +mkdir $testdir2/hello2d +echo 1 > $testdir2/hello1f +ln -s $testdir2/hello1f $testdir2/hello1fs +echo 2 > $testdir2/hello2f +ln $testdir2/hello2f $testdir2/hello2fl cleanup() { chmod 777 $testdir/noread - rm -fr $testdir $testout + rm -fr $testdir $testdir2 $testout } trap cleanup 0 HUP INT QUIT TERM @@ -815,6 +824,45 @@ if test $failed -ne 0; then result=1 fi +# Test that if the specified glob ends with a slash then only directories are +# matched. +# First run with the glob that has no slash to demonstrate presence of a slash +# makes a difference. +failed=0 +${test_program_prefix} \ +${common_objpfx}posix/globtest "$testdir2" "hello*" | +sort > $testout +cat <<"EOF" | $CMP - $testout >> $logfile || failed=1 +`hello1d' +`hello1ds' +`hello1f' +`hello1fs' +`hello2d' +`hello2f' +`hello2fl' +EOF + +if test $failed -ne 0; then + echo "pattern+meta test failed" >> $logfile + result=1 +fi + +# The same pattern+meta test with a slash this time. +failed=0 +${test_program_prefix} \ +${common_objpfx}posix/globtest "$testdir2" "hello*/" | +sort > $testout +cat <<"EOF" | $CMP - $testout >> $logfile || failed=1 +`hello1d/' +`hello1ds/' +`hello2d/' +EOF + +if test $failed -ne 0; then + echo "pattern+meta+slash test failed" >> $logfile + result=1 +fi + if test $result -eq 0; then echo "All OK." > $logfile fi