git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] Simplify read_directory[_recursive]() arguments

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Jul 9, 2009, 02:42 UTC
Message-ID
<alpine.LFD.2.01.0907081940220.3352@localhost.localdomain>
In-Reply-To
<alpine.LFD.2.01.0907081936470.3352@localhost.localdomain>
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Wed, 8 Jul 2009 19:24:39 -0700
Subject: [PATCH 2/3] Simplify read_directory[_recursive]() arguments

Stop the insanity with separate 'path' and 'base' arguments that must match. We don't need that crazy interface any more, since we cleaned up handling of 'path' in commit da4b3e8c28b1dc2b856d2555ac7bb47ab712598c.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

The diffstat says it only removes a single line, but it _simplifies_ a lot of them, and gets rid of the horrible confusion about what 'path' vs 'base' means.

 dir.c          |   57 +++++++++++++++++++++++++++----------------------------
 dir.h          |    2 +-
 unpack-trees.c |    2 +-
 3 files changed, 30 insertions(+), 31 deletions(-)
diff --git a/dir.c b/dir.c
index 0c8553b..b0671f5 100644
--- a/dir.c
+++ b/dir.c
@@ -14,8 +14,7 @@ struct path_simplify {
 	const char *path;
 };
 
-static int read_directory_recursive(struct dir_struct *dir,
-	const char *path, const char *base, int baselen,
+static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
 	int check_only, const struct path_simplify *simplify);
 static int get_dtype(struct dirent *de, const char *path);
 
@@ -54,23 +53,22 @@ static int common_prefix(const char **pathspec)
 
 int fill_directory(struct dir_struct *dir, const char **pathspec)
 {
-	const char *path, *base;
-	int baselen;
+	const char *path;
+	int len;
 
 	/*
 	 * Calculate common prefix for the pathspec, and
 	 * use that to optimize the directory walk
 	 */
-	baselen = common_prefix(pathspec);
+	len = common_prefix(pathspec);
 	path = "";
-	base = "";
 
-	if (baselen)
-		path = base = xmemdupz(*pathspec, baselen);
+	if (len)
+		path = xmemdupz(*pathspec, len);
 
 	/* Read the directory and prune it */
-	read_directory(dir, path, base, baselen, pathspec);
-	return baselen;
+	read_directory(dir, path, len, pathspec);
+	return len;
 }
 
 /*
@@ -526,7 +524,7 @@ static enum directory_treatment treat_directory(struct dir_struct *dir,
 	/* This is the "show_other_directories" case */
 	if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
 		return show_directory;
-	if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
+	if (!read_directory_recursive(dir, dirname, len, 1, simplify))
 		return ignore_directory;
 	return show_directory;
 }
@@ -595,15 +593,15 @@ static int get_dtype(struct dirent *de, const char *path)
  * Also, we ignore the name ".git" (even if it is not a directory).
  * That likely will not change.
  */
-static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
+static int read_directory_recursive(struct dir_struct *dir, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
 {
-	DIR *fdir = opendir(*path ? path : ".");
+	DIR *fdir = opendir(*base ? base : ".");
 	int contents = 0;
 
 	if (fdir) {
 		struct dirent *de;
-		char fullname[PATH_MAX + 1];
-		memcpy(fullname, base, baselen);
+		char path[PATH_MAX + 1];
+		memcpy(path, base, baselen);
 
 		while ((de = readdir(fdir)) != NULL) {
 			int len, dtype;
@@ -614,17 +612,18 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
 				continue;
 			len = strlen(de->d_name);
 			/* Ignore overly long pathnames! */
-			if (len + baselen + 8 > sizeof(fullname))
+			if (len + baselen + 8 > sizeof(path))
 				continue;
-			memcpy(fullname + baselen, de->d_name, len+1);
-			if (simplify_away(fullname, baselen + len, simplify))
+			memcpy(path + baselen, de->d_name, len+1);
+			len = baselen + len;
+			if (simplify_away(path, len, simplify))
 				continue;
 
 			dtype = DTYPE(de);
-			exclude = excluded(dir, fullname, &dtype);
+			exclude = excluded(dir, path, &dtype);
 			if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
-			    && in_pathspec(fullname, baselen + len, simplify))
-				dir_add_ignored(dir, fullname, baselen + len);
+			    && in_pathspec(path, len, simplify))
+				dir_add_ignored(dir, path,len);
 
 			/*
 			 * Excluded? If we don't explicitly want to show
@@ -634,7 +633,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
 				continue;
 
 			if (dtype == DT_UNKNOWN)
-				dtype = get_dtype(de, fullname);
+				dtype = get_dtype(de, path);
 
 			/*
 			 * Do we want to see just the ignored files?
@@ -651,9 +650,9 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
 			default:
 				continue;
 			case DT_DIR:
-				memcpy(fullname + baselen + len, "/", 2);
+				memcpy(path + len, "/", 2);
 				len++;
-				switch (treat_directory(dir, fullname, baselen + len, simplify)) {
+				switch (treat_directory(dir, path, len, simplify)) {
 				case show_directory:
 					if (exclude != !!(dir->flags
 							& DIR_SHOW_IGNORED))
@@ -661,7 +660,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
 					break;
 				case recurse_into_directory:
 					contents += read_directory_recursive(dir,
-						fullname, fullname, baselen + len, 0, simplify);
+						path, len, 0, simplify);
 					continue;
 				case ignore_directory:
 					continue;
@@ -675,7 +674,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
 			if (check_only)
 				goto exit_early;
 			else
-				dir_add_name(dir, fullname, baselen + len);
+				dir_add_name(dir, path, len);
 		}
 exit_early:
 		closedir(fdir);
@@ -738,15 +737,15 @@ static void free_simplify(struct path_simplify *simplify)
 	free(simplify);
 }
 
-int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
+int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
 {
 	struct path_simplify *simplify;
 
-	if (has_symlink_leading_path(path, strlen(path)))
+	if (has_symlink_leading_path(path, len))
 		return dir->nr;
 
 	simplify = create_simplify(pathspec);
-	read_directory_recursive(dir, path, base, baselen, 0, simplify);
+	read_directory_recursive(dir, path, len, 0, simplify);
 	free_simplify(simplify);
 	qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
 	qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
diff --git a/dir.h b/dir.h
index f9d69dd..a631446 100644
--- a/dir.h
+++ b/dir.h
@@ -67,7 +67,7 @@ struct dir_struct {
 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
 
 extern int fill_directory(struct dir_struct *dir, const char **pathspec);
-extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec);
+extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);
 
 extern int excluded(struct dir_struct *, const char *, int *);
 extern void add_excludes_from_file(struct dir_struct *, const char *fname);
diff --git a/unpack-trees.c b/unpack-trees.c
index 05d0bb1..42c7d7d 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -551,7 +551,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
 	memset(&d, 0, sizeof(d));
 	if (o->dir)
 		d.exclude_per_dir = o->dir->exclude_per_dir;
-	i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
+	i = read_directory(&d, pathbuf, namelen+1, NULL);
 	if (i)
 		return o->gently ? -1 :
 			error(ERRORMSG(o, not_uptodate_dir), ce->name);
-- 
1.6.3.3.412.gf581d
Previous: Linus TorvaldsNext: Linus Torvalds
Message 6 of 39 in “Too many 'stat' calls by git-status on Windows”
  1. Dmitry PotapovJul 7, 2009
  2. Ramsay JonesJul 8, 2009
  3. Linus TorvaldsJul 9, 2009
  4. Linus TorvaldsJul 9, 2009
  5. 1/3 Add 'fill_directory()' helper function for directory traversalLinus Torvalds, Jul 9, 2009
  6. 2/3 Simplify read_directory[_recursive]() argumentsLinus Torvalds, Jul 9, 2009
  7. 3/3 Avoid doing extra 'lstat()'s for d_type if we have an up-to-date cache entryLinus Torvalds, Jul 9, 2009
  8. Junio C HamanoJul 9, 2009
  9. Linus TorvaldsJul 9, 2009
  10. Junio C HamanoJul 9, 2009
  11. Linus TorvaldsJul 9, 2009
  12. Junio C HamanoJul 9, 2009
  13. Linus TorvaldsJul 9, 2009
  14. Linus TorvaldsJul 9, 2009
  15. Junio C HamanoJul 9, 2009
  16. Linus TorvaldsJul 9, 2009
  17. 4/3 Avoid using 'lstat()' to figure out directoriesLinus Torvalds, Jul 9, 2009
  18. 5/3 Prepare symlink caching for thread-safetyLinus Torvalds, Jul 9, 2009
  19. 6/3 Export thread-safe version of 'has_symlink_leading_path()'Linus Torvalds, Jul 9, 2009
  20. 7/3 Make index preloading check the whole path to the fileLinus Torvalds, Jul 9, 2009
  21. Linus TorvaldsJul 9, 2009
  22. Junio C HamanoJul 10, 2009
  23. Linus TorvaldsJul 10, 2009
  24. Linus TorvaldsJul 10, 2009
  25. Junio C HamanoJul 11, 2009
  26. Linus TorvaldsJul 11, 2009
  27. Kjetil BarvikJul 12, 2009
  28. Junio C HamanoJul 12, 2009
  29. Paolo BonziniJul 9, 2009
  30. Linus TorvaldsJul 9, 2009
  31. Linus TorvaldsJul 9, 2009
  32. Linus TorvaldsJul 10, 2009
  33. Junio C HamanoJul 9, 2009
  34. Dmitry PotapovJul 9, 2009
  35. Eric BlakeJul 9, 2009
  36. Dmitry PotapovJul 9, 2009
  37. Dmitry PotapovJul 10, 2009
  38. Dmitry PotapovJul 9, 2009
  39. Dmitry PotapovJul 9, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.