git: 9front

Download patch

ref: fceed1b4dd4ad2d495c9fde89865444166395962
parent: c59d04ae0cc5aaed5eef3381b9c01b3f2572f500
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Tue Aug 4 12:21:37 EDT 2020

rc: avoid stat calls for directory globbing

On Plan9, we can count on Readdir() onlydirs argument
to work, which allows us to avoid stating every single
file to see if it is a directory.

--- a/sys/src/cmd/rc/glob.c
+++ b/sys/src/cmd/rc/glob.c
@@ -60,9 +60,15 @@
 {
 	char *t, *newp;
 	int f;
-	/* scan the pattern looking for a component with a metacharacter in it */
+
+	/* append slashes, Readdir() already filtered directories */
+	while(*p=='/'){
+		*namep++=*p++;
+		*namep='\0';
+	}
 	if(*p=='\0')
 		return newword(name, list);
+	/* scan the pattern looking for a component with a metacharacter in it */
 	t = namep;
 	newp = p;
 	while(*newp){
--- a/sys/src/cmd/rc/plan9.c
+++ b/sys/src/cmd/rc/plan9.c
@@ -345,23 +345,15 @@
 int
 Opendir(char *name)
 {
-	Dir *db;
 	int f;
-	f = open(name, 0);
-	if(f==-1)
+
+	if((f = open(name, 0)) < 0)
 		return f;
-	db = dirfstat(f);
-	if(db!=nil && (db->mode&DMDIR)){
-		if(f<NFD){
-			dir[f].i = 0;
-			dir[f].n = 0;
-		}
-		free(db);
-		return f;
+	if(f<NFD){
+		dir[f].i = 0;
+		dir[f].n = 0;
 	}
-	free(db);
-	close(f);
-	return -1;
+	return f;
 }
 
 static int
@@ -375,13 +367,6 @@
 	return w;
 }
 
-/*
- * onlydirs is advisory -- it means you only
- * need to return the directories.  it's okay to
- * return files too (e.g., on unix where you can't
- * tell during the readdir), but that just makes 
- * the globber work harder.
- */
 int
 Readdir(int f, void *p, int onlydirs)
 {
--