From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 600341F670 for ; Fri, 11 Mar 2022 21:43:55 +0000 (UTC) Received: from localhost ([::1]:35206 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nSn3C-0002K1-AP for normalperson@yhbt.net; Fri, 11 Mar 2022 16:43:54 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nSn38-0002Fo-GD for bug-gnulib@gnu.org; Fri, 11 Mar 2022 16:43:50 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:46014) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nSn34-0002Qh-9x for bug-gnulib@gnu.org; Fri, 11 Mar 2022 16:43:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647035023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=Z1vPujy+5WhwWnU/vAOEdHSAlye/yNyHd2371ITWDG0=; b=d8q2nEgo8JGoeIgIyvUcKqCGzlkJOxWap+P90XK9/uYxXWs5wqhEPXvWKYgJ1seVKwR3cP NoHj3jc88bKSX5Iu/ZAP+jS3EBDU3U+4FIVzs3KCjb0H1QPLzQcw0xW8ZcrvuwKcWMTL1S JbkJx1HI9nN88sHPlYoQvLKDP20Vpgc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-611-Cvqd7UZVMKGCLYcQ9PrLnw-1; Fri, 11 Mar 2022 16:43:41 -0500 X-MC-Unique: Cvqd7UZVMKGCLYcQ9PrLnw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B72085238 for ; Fri, 11 Mar 2022 21:43:40 +0000 (UTC) Received: from greed.delorie.com (ovpn-112-4.rdu2.redhat.com [10.10.112.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 82B225DB8E for ; Fri, 11 Mar 2022 21:43:40 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 22BLhdG31672223 for ; Fri, 11 Mar 2022 16:43:39 -0500 From: DJ Delorie To: bug-gnulib@gnu.org Subject: [patch v2] glob: resolve DT_UNKNOWN via is_dir In-Reply-To: (message from DJ Delorie on Tue, 16 Nov 2021 22:45:12 -0500) Date: Fri, 11 Mar 2022 16:43:39 -0500 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dj@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Received-SPF: pass client-ip=170.10.129.124; envelope-from=dj@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" [v2: changed malloc failure from ignore to error; added support for alloca; tested by copying to glibc and testing there] The DT_* values returned by getdents (readdir) are only hints and not required. In fact, some Linux filesystems return DT_UNKNOWN for most entries, regardless of actual type. This causes make to mis-match patterns with a trailing slash (via GLOB_ONLYDIR) (see make's functions/wildcard test case). Thus, this patch detects that case and uses is_dir() to make the type known enough for proper operation. Performance in non-DT_UNKNOWN cases is not affected. The lack of DT_* is a well known issue on older XFS installations (for example, RHEL 7 and 8, Fedora 28) but can be recreated by creating an XFS filesystem with flags that mimic older behavior: $ fallocate -l 10G /xfs.fs $ mkfs.xfs -n ftype=0 -m crc=0 -f /xfs.fs $ mkdir /xfs $ mount -o loop /xfs.fs /xfs diff --git a/lib/glob.c b/lib/glob.c index f8d8a306f2..c28c92a42c 100644 --- a/lib/glob.c +++ b/lib/glob.c @@ -1381,7 +1381,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: case DT_LNK: break; + case DT_UNKNOWN: + { + /* The filesystem was too lazy to give us a hint, + so we have to do it the hard way. */ + char *fullpath, *p; + bool isdir; + int need = strlen (directory) + strlen (d.name) + 2; + int use_alloca = glob_use_alloca (alloca_used, need); + if (use_alloca) + fullpath = alloca_account (need, alloca_used); + else + { + fullpath = malloc (need); + if (fullpath == NULL) + goto memory_error; + } + p = stpcpy (fullpath, directory); + *p++ = '/'; + strcpy (p, d.name); + isdir = is_dir (fullpath, flags, pglob); + if (!use_alloca) + free (fullpath); + if (isdir) + break; + continue; + } default: continue; }