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 21:59:47 +0200 Message-ID: References: <83y2ka18t7.fsf@gnu.org> <83d01m0wjp.fsf@gnu.org> 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="24446"; 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: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Oct 13 22:01:01 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 1kSQTl-0006Gy-4O for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 22:01:01 +0200 Original-Received: from localhost ([::1]:48686 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kSQTj-0001fe-AL for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 16:01:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47226) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSQSg-0001BQ-I3 for emacs-devel@gnu.org; Tue, 13 Oct 2020 15:59:54 -0400 Original-Received: from mail-am6eur05olkn2061.outbound.protection.outlook.com ([40.92.91.61]:64917 helo=EUR05-AM6-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 1kSQSd-0004Pv-Tk; Tue, 13 Oct 2020 15:59:54 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iB9ASxhoFftzfBD7sm4t+YrZMhMEo4qggYC5RDlSSjda2lAU92VyIRC8NLLYPgWJFlbvjb1w6cnAHi6fwLlgbedYIp7eelzl8+nkLTu+SIuK0omx0niXa2D83UiR9UXsfStqRMDzFs/jnzDP5InWDysZW9CNmsFUE8pC1Q6qEyItAT5QTKPbgiYS7VhW9Tk2hO4J4OsaLpCG/mpI3wOwCxvFb0SC1L2DjerQeqkqP/+8IocnN5Yv2JI5gNpsa5zjr6XmYTsMKimm6RDaP7u9NTAUjs+xLtRPf5i/++QGr/sI1ButvPRjH9Gf2xRZ+CskW2CgS//5DnWGqewsPbD2eQ== 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=n3YnE7GlqJPOtU9u4rsOhKSZ9zNRtj8PHN8j7hBQ3LE=; b=EOEPZytwdq+LYpSrPEXj5S0pQ8ckvS+Uc2wcKo14XRfoi/sQvX5ny3cgh8Q1rtwKckVSGtFAWhofEinYAWOmphjISGqA8NnXhX4fB4vEBic3H0Sb6pRywhUOGatNiuQ+J/uG8+jXgmZqAqnyP3Y9eZ6LJLm4FmxOEfoadOWzGpxZdyqBYDpb+zVyC5w894YreRE1q8heJ06oeCl/h94Gm+lavoyRe6AtS03zIbJAN5ggK7MYhLqpyHXUJVvMGB/UlEuXzm/n2VEjoL2iP9VAhjwFd2+Nu0ou3r3x36SWQfWB5J+e3w4YcIY+uBmTGSqGAc6gB3pU6Lh3IZSW7J6mbw== 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=n3YnE7GlqJPOtU9u4rsOhKSZ9zNRtj8PHN8j7hBQ3LE=; b=oqRY1BiY6fGCMbXcA85kcB5/QkRXPpFqhgEkHLZYyU8DsrAuPu6XkStP5UWakOzMmGHIt6E5jhkafu4HPjuZpeCQlvPixVGa/ZU3Va8qX1fqQOEhDR52PpTqF2wmSr4lYf4W23FmbeBbdX8XIaj8uV9EjSPUt/hnDPC4V3Wwlkmgex/jYDOs+GhdHFmHMoBw9WTF/V0UEOWwqBWOXb0FkQtulS8Ialicq88HiexUVfOBANqKcye/5PHf8BlUOXVBGTA+ILqsbF97eVcuOOC3M3EaLWQM40VVXwg8dyr7/QIUBoncqHnTeXntL4bYklfd3VeieLr8jUwFFeYaMrPBtA== Original-Received: from VI1EUR05FT017.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc12::52) by VI1EUR05HT041.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc12::187) 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 19:59:48 +0000 Original-Received: from VI1PR06MB4526.eurprd06.prod.outlook.com (2a01:111:e400:fc12::4d) by VI1EUR05FT017.mail.protection.outlook.com (2a01:111:e400:fc12::438) 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 19:59:48 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:2569DE6537E730D3BC5947E0D2FF58E778E2746EC7F12D2B4F6CD45C02F26AA4; UpperCasedChecksum:CC38F2AC13E19613C457B6E7E4F09ECA2EAF18363127E36302F1B5B486EE32B9; SizeAsReceived:7590; 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 19:59:48 +0000 In-Reply-To: <83d01m0wjp.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 13 Oct 2020 22:12:58 +0300") X-TMN: [+GYDVCSkhXSUbt41/NT5+rR1foNeX16t] X-ClientProxiedBy: AM5PR0701CA0061.eurprd07.prod.outlook.com (2603:10a6:203:2::23) To VI1PR06MB4526.eurprd06.prod.outlook.com (2603:10a6:803:ac::17) X-Microsoft-Original-Message-ID: <871ri1dhho.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (90.230.29.56) by AM5PR0701CA0061.eurprd07.prod.outlook.com (2603:10a6:203:2::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3477.13 via Frontend Transport; Tue, 13 Oct 2020 19:59:48 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: fb617cf9-4f56-478b-c444-08d86fb28a14 X-MS-TrafficTypeDiagnostic: VI1EUR05HT041: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oZXfKWaQPQ1OGe6YgnGam9sGSobFRsk9TdYOINK7Ddw+lAF0juXYpYidBBvOge3walp1JraWtVAKdRb772jX3nkFZav4KzYCXR/D9MxCsv3B3vlMyjIWfYiDlUFPQNa82xSDuwygBZH2nojOMaCZ8psPlmjkfLWWW3xvzvgSRADX3+zMKQLsaW6N+pp4Xg3WkrS/kvj6Z5TAlpzfFbUh/w== X-MS-Exchange-AntiSpam-MessageData: Rcb0/VdA5rRBqUfnnxOpGEDJ4JERIH1DqKMe0Q/WSyqc5ElhcUG/AZjqpkXXKWDzq+equpWIMyCgk6+jXuY1+r4SApsG8LdhG+y0FiikLo/2HNGF5GaUi3FhXwDKUeJigVgl7CXofGSMl+KvGNB6gg== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: fb617cf9-4f56-478b-c444-08d86fb28a14 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2020 19:59:48.8251 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: VI1EUR05FT017.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: VI1EUR05HT041 Received-SPF: pass client-ip=40.92.91.61; envelope-from=arthur.miller@live.com; helo=EUR05-AM6-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/13 15:59:49 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:257573 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Arthur Miller >> Cc: emacs-devel@gnu.org >> Date: Tue, 13 Oct 2020 20:43:41 +0200 >> >> > directory-files (and which you missed). For example, file names need >> > to be encoded before they are passed to libc functions (or any >> > external APIs that expect file names). >> Actually I didn't; I first tested with ENCODE_FILE but I didn't know >> what it does, and it appeared like it is working without, so I ditched >> it. > > It worked without ENCODE_FILE by sheer luck: you must have tried that > on a system where file names are encoded in UTF-8, and so most file > names can be used without any conversion (but ENCODE_FILE already > knows that). I see :-). I spent quite a lot time earlier today to get it to work on windows, mostly for the conversion between utf8 and unicode; tried first to manually convert between utf/wide, and after several tries discovered some functions in w32.c I could reuse: filename_to_utf16 and filename_to_ansi, which worked for me (minus tramp of course). Can I ask two questions, just for my learning; you seem to handle ascii/unicode "manually"; in w32.c I have seen quite a few places where switch is done on if(w32_unicode_filenames); is it possible to somehow pass to configure script if Emacs should be compiled with wide or asci version of win api? Second question: block_input; is there some general rule(s) what calls you enclose with block/unblock_input? I guess OS will anyway block the process after call to SYS_getdents so I didn't enclosed it; but I am not sure if that would be needed at all or not. I have attached my patch with win code as illustration to those questions. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=dired.patch Content-Description: dired.c --- src/dired.c 2020-10-13 20:09:48.235962328 +0200 +++ ../dired.c 2020-10-13 20:09:10.865527164 +0200 @@ -22,6 +22,12 @@ #include +#if __linux__ +#include +#elif _WIN32 +#include +#endif + #ifdef HAVE_PWD_H #include #endif @@ -39,6 +45,7 @@ #include "systime.h" #include "buffer.h" #include "coding.h" +#include "blockinput.h" #ifdef MSDOS #include "msdos.h" /* for fstatat */ @@ -929,7 +936,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 +1085,119 @@ return groups; } +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) +{ + const char* name; + 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); + +#ifdef __linux__ + { + typedef struct dirent* pdirent; +#define BSIZE 1024 + pdirent d; + char buf[BSIZE]; + int fd, n = 0, p = 0, c = 0; + + 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; + } +#elif _WIN32 + { + int len, c = 0; + HANDLE h = INVALID_HANDLE_VALUE; + + if(w32_unicode_filenames){ + WIN32_FIND_DATAW ffd; + wchar_t wname[MAX_PATH]; + filename_to_utf16(name, wname); + len = wcslen(wname); + + if (!len) + error(TEXT("Can't read directory name.")); + + if(len > MAX_PATH - 3) + error(TEXT("Directory path too long.")); + + wcscat(wname, L"\\*"); + + h = FindFirstFileW(wname, &ffd); + if (h == INVALID_HANDLE_VALUE) + error(TEXT("Can't open directory.")); + + while (FindNextFileW(h, &ffd) && c < 1){ + if(wcscmp(ffd.cFileName, L".") && + wcscmp(ffd.cFileName, L"..")) + c++; + } + }else { + WIN32_FIND_DATAA ffd; + char aname[MAX_UTF8_PATH]; + filename_to_ansi(name, aname); + len = strlen(aname); + + if (!len) + error(TEXT("Can't read directory name.")); + + if(len > MAX_PATH - 3) + error(TEXT("Directory path too long.")); + + strcat(aname, "\\*"); + + h = FindFirstFileA(aname, &ffd); + if (h == INVALID_HANDLE_VALUE) + error(TEXT("Can't open directory.")); + + while (FindNextFileA(h, &ffd) && c < 1){ + if(strcmp(ffd.cFileName, ".") && + strcmp(ffd.cFileName, "..")) + c++; + } + } + + FindClose(h); + return (c > 0) ? Qnil : Qt; + } +#endif +} + + void syms_of_dired (void) { @@ -1089,7 +1209,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 +1219,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. --=-=-=--