From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arthur Miller Newsgroups: gmane.emacs.devel Subject: Re: empty-directory predicate, native implementation Date: Tue, 13 Oct 2020 13:42:07 +0200 Message-ID: References: <87r1q2mu4t.fsf@gmx.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16788"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Michael Albinus Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Oct 13 13:45:05 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kSIjo-0004Gb-Ni for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 13:45:04 +0200 Original-Received: from localhost ([::1]:43502 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kSIjn-0002q1-Lm for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 07:45:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSIhF-0001h6-Ej for emacs-devel@gnu.org; Tue, 13 Oct 2020 07:42:25 -0400 Original-Received: from mail-vi1eur05olkn2010.outbound.protection.outlook.com ([40.92.90.10]:42336 helo=EUR05-VI1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSIh4-0003r6-DD for emacs-devel@gnu.org; Tue, 13 Oct 2020 07:42:24 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZRrAUuBMxsV6D6ZGELVWy/gdSb/oGSxKYbbHHkGpqTIbahoBLuxxvTvy5IP1ONoLUJGff3b+adm22+opj0eo/Hiz7CsSpjsOWxXTCSpyCIhEQf5MzoaVAOkyG25Vverk+k1ImFIDBPnaLuK9bhncEGNSMW+jOsxJbrMGaFWGUKqNSO6xusdvWUKsd5jfBL7NwfJI79ZgmM8tGuWuNTkBDGEBS/FBfAX74b0jEzNXCU5hyqSN/SRO9dsyYt3fTigHsDi1PGJfhlRDonoJzYneoCSpaiCHJS2j1RDQA8y9coet+58fVwuGOrG4VY2I12JVg8OpxVWzWoXJ+P1A+grDRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iTXdOd1mLersf4smFTUSwjO2f1nd8nGE3LAejoKnsuQ=; b=awfRqg6Wclrop99e9S7c7M/prkOabUDgPnLXzWedFhv1Y8MVc8hMP/h/19L0jzCFUy7FuXiHHLQYyc1wx9iCstZ60TVaBpUBvj3FrAGiSeIoAkK+2nC2CVW6WqMQNNUwFslpDlVzDsUstOnLSRB3VTKYKSlklrkf+7Om4wVJdgc9jj0ONu/bqONWSQMRS/gAlTDvHfniviqvruTydwyPFWE7FwfBWZxfVziJYyWUlkzF1mXgUkovzv5tn+ih8NeAXqiU0uRfuUStQDGEXfkiQrnf4UcdrPAy++AgYF5vhPcGhsDnfdjIGNrFo8EMuHUOAx7qAtSl+4umf8UnM2I2Dg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iTXdOd1mLersf4smFTUSwjO2f1nd8nGE3LAejoKnsuQ=; b=p5r7XZ2EU744IDSuLpk9pXupspQV9pR5OBg+a8OoLVZDByf6EO1WgQHLt024stk5/WgDQHHGZl/sc7BdC/GukYH+7kuJrlERWBGa0eIM4J7tgHMFW4ypx//YpzQXJkUWsFXbR7285AMJvg0QYfPUNm4iQffMUMncoyUTyUj2Naxs0b2V4gIu2JX1CCTa+e7neR8uC/UqboeS4+6vFpLp5xM1IJoTLuTjftS6YI00PgnRiboJ7qfqttgu1AJGADR/MqQZGUCz97w5FUPk88GzhCeghtd6rooeVLNaeYuo0WV4NRt1VDZ5hfQ41iHEod8KmJwar5XWn+C5+fLWofy/RQ== Original-Received: from AM6EUR05FT032.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc11::53) by AM6EUR05HT160.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc11::139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3455.23; Tue, 13 Oct 2020 11:42:09 +0000 Original-Received: from VI1PR06MB4526.eurprd06.prod.outlook.com (2a01:111:e400:fc11::4c) by AM6EUR05FT032.mail.protection.outlook.com (2a01:111:e400:fc11::101) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3455.23 via Frontend Transport; Tue, 13 Oct 2020 11:42:09 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:D83C7489507D060247F5E0704C7FB06D29931BB2921BDEF66B9C92E6E2E086BE; UpperCasedChecksum:072BA830120AC687F2FBC4F0E3CD0CA91D1CE131B81D2B545A7E177A8535AAD8; SizeAsReceived:7492; Count:46 Original-Received: from VI1PR06MB4526.eurprd06.prod.outlook.com ([fe80::b547:51cd:16c5:4487]) by VI1PR06MB4526.eurprd06.prod.outlook.com ([fe80::b547:51cd:16c5:4487%7]) with mapi id 15.20.3455.031; Tue, 13 Oct 2020 11:42:09 +0000 In-Reply-To: <87r1q2mu4t.fsf@gmx.de> (Michael Albinus's message of "Tue, 13 Oct 2020 10:01:54 +0200") X-TMN: [7+rI+mP56r7wf1zOs3PnQd09SBW48zvN] X-ClientProxiedBy: AM6PR10CA0099.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:8c::40) To VI1PR06MB4526.eurprd06.prod.outlook.com (2603:10a6:803:ac::17) X-Microsoft-Original-Message-ID: <87ft6ibbe8.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (90.230.29.56) by AM6PR10CA0099.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:8c::40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3455.23 via Frontend Transport; Tue, 13 Oct 2020 11:42:08 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: dd986737-a20e-4d2d-438f-08d86f6d0448 X-MS-TrafficTypeDiagnostic: AM6EUR05HT160: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HuQ9mr1DdmMsFYPBEQACTj2x8trs0ZVUrd08utOzYzjUDl2ehot8GV0B1ZheKF+Cdouy9QmNJlRd2zKGr2hWDvcON0/hhVnhzgdkU5dFY2tU3I85HauIqjkdqBFbSoldeW70FtknTNnTab9A/uXWhrPp9F8zKZqqV606Q7Ea017/BgNbDv4/7rgKguLuMT73ntbQtc7rSjJRTHg2RRdoYA== X-MS-Exchange-AntiSpam-MessageData: MB1IS4qn7ECpa/zw9ZmTAs0H7chAukZuqgEP5dzgjMBK+/SxDTbzsRbddvwlAcEazvyKbUEQzhHTx1Z4z0rDVJGlySQFHx+Ong6ACXM3msQTFjbVUEAcblIJWS/GL6nxPEdXBYmM725kihCY+k88Kw== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: dd986737-a20e-4d2d-438f-08d86f6d0448 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2020 11:42:09.1295 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: AM6EUR05FT032.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6EUR05HT160 Received-SPF: pass client-ip=40.92.90.10; envelope-from=arthur.miller@live.com; helo=EUR05-VI1-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/13 07:42:10 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:257526 Archived-At: --=-=-= Content-Type: text/plain Michael Albinus writes: > Arthur Miller writes: > > Hi Arthur, > >> This isn't any kind of drammatic difference for most use; file I/O >> is dominated by disk access anyway, but i still don't like to spend cpu >> on unnecessary evaluations, so I wonder if we could get native predicate >> in elisp? > > I don't know whether we will add this function, but in case of: you > shall call a file name handler first. See for example Fdirectory_files. > > Best regards, Michael. Thanks for looking at this. I have seen in the source code calls to Ffind_file_handler, and have been looking at the function in fileio.c, but I didn't figured out what it really does, so I didn't use it. I tested without and it worked fine :-). Anyway, I have added it now, but I am not sure if I using it correctly, since I am not sure what it does. By the way, what are "special constructs" referred to in the comment? --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=dired.patch Content-Description: dired.c patch --- src/dired.c 2020-10-13 13:36:27.069967986 +0200 +++ ../dired.c 2020-10-13 13:35:47.695916558 +0200 @@ -21,6 +21,7 @@ #include #include +#include #ifdef HAVE_PWD_H #include @@ -39,6 +40,7 @@ #include "systime.h" #include "buffer.h" #include "coding.h" +#include "blockinput.h" #ifdef MSDOS #include "msdos.h" /* for fstatat */ @@ -929,7 +931,7 @@ struct stat s; /* An array to hold the mode string generated by filemodestring, - including its terminating space and null byte. */ + including its terminating space and NUL byte. */ char modes[sizeof "-rwxr-xr-x "]; char *uname = NULL, *gname = NULL; @@ -1078,6 +1080,58 @@ return groups; } +typedef struct dirent* pdirent; +DEFUN ("directory-empty-p", Fdirectory_empty_p, + Sdirectory_empty_p, 1, 1, 0, + doc: /* Returns t if directory DIRNAME does not contain any + user files (special files . and .. are excluded + automatically), nil otherwise. */) +(Lisp_Object dirname) +{ + #define BSIZE 1024 + char buf[BSIZE]; + const char* name; + int fd, n = 0, p = 0, c = 0; + pdirent d; + Lisp_Object handler; + + if(!STRINGP(dirname)) + error("Directory name not a string object."); + + dirname = Fexpand_file_name(dirname, Qnil); + + /* If the file name has special constructs in it, + call the corresponding file name handler. */ + handler = Ffind_file_name_handler (dirname, Qdirectory_empty_p); + if (!NILP (handler)) + return call2 (handler, Qdirectory_empty_p, dirname); + + name = SSDATA(dirname); + + fd = open (name, O_RDONLY | O_DIRECTORY); + + if( fd == -1 ) + error("Can't open directory."); + + //block_input(); + /* 32-bit version of getdents should be good enough; + we are just looking at first 3 files*/ + n = syscall(SYS_getdents,fd,buf, BSIZE); + if(n == -1) + error("Can't read directory data."); + + while(p < n && c < 3) { + d = (pdirent) (buf + p); + p += d->d_reclen; + c++; + } + //unblock_input(); + + close(fd); + return (c > 2) ? Qnil : Qt; +} + + void syms_of_dired (void) { @@ -1089,7 +1143,8 @@ DEFSYM (Qfile_attributes_lessp, "file-attributes-lessp"); DEFSYM (Qdefault_directory, "default-directory"); DEFSYM (Qdecomposed_characters, "decomposed-characters"); - + DEFSYM (Qdirectory_empty_p, "directory-empty-p") + defsubr (&Sdirectory_files); defsubr (&Sdirectory_files_and_attributes); defsubr (&Sfile_name_completion); @@ -1098,6 +1153,7 @@ defsubr (&Sfile_attributes_lessp); defsubr (&Ssystem_users); defsubr (&Ssystem_groups); + defsubr (&Sdirectory_empty_p); DEFVAR_LISP ("completion-ignored-extensions", Vcompletion_ignored_extensions, doc: /* Completion ignores file names ending in any string in this list. --=-=-=--