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

[PATCH 1/4] is_directory(): a generic helper function

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 9, 2008, 08:27 UTC
Message-ID
<1220948830-3275-2-git-send-email-gitster@pobox.com>
In-Reply-To
<1220948830-3275-1-git-send-email-gitster@pobox.com>

A simple "grep -e stat --and -e S_ISDIR" revealed there are many open-coded implementations of this function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 abspath.c       |   13 ++++++++++++-
 builtin-clone.c |    9 +--------
 cache.h         |    1 +
 daemon.c        |   10 +++-------
 rerere.c        |    3 +--
 sha1_file.c     |    3 +--
 6 files changed, 19 insertions(+), 20 deletions(-)
diff --git a/abspath.c b/abspath.c
index 0d56124..8c06782 100644
--- a/abspath.c
+++ b/abspath.c
@@ -1,5 +1,16 @@
 #include "cache.h"
 
+/*
+ * Do not use this for inspecting *tracked* content.  When path is a
+ * symlink to a directory, we do not want to say it is a directory.
+ * Worth yet, leading components in path could contain symbolic links.
+ */
+int is_directory(const char *path)
+{
+	struct stat st;
+	return (!stat(path, &st) && S_ISDIR(st.st_mode));
+}
+
 /* We allow "recursive" symbolic links. Only within reason, though. */
 #define MAXDEPTH 5
 
@@ -17,7 +28,7 @@ const char *make_absolute_path(const char *path)
 		die ("Too long path: %.*s", 60, path);
 
 	while (depth--) {
-		if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
+		if (!is_directory(buf)) {
 			char *last_slash = strrchr(buf, '/');
 			if (last_slash) {
 				*last_slash = '\0';
diff --git a/builtin-clone.c b/builtin-clone.c
index c843529..a4b8790 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c
@@ -77,7 +77,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
 	for (i = 0; i < ARRAY_SIZE(suffix); i++) {
 		const char *path;
 		path = mkpath("%s%s", repo, suffix[i]);
-		if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
+		if (is_directory(path)) {
 			*is_bundle = 0;
 			return xstrdup(make_nonrelative_path(path));
 		}
@@ -140,13 +140,6 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
 	return xstrndup(start, end - start);
 }
 
-static int is_directory(const char *path)
-{
-	struct stat buf;
-
-	return !stat(path, &buf) && S_ISDIR(buf.st_mode);
-}
-
 static void strip_trailing_slashes(char *dir)
 {
 	char *end = dir + strlen(dir);
diff --git a/cache.h b/cache.h
index de8c2b6..ce1f630 100644
--- a/cache.h
+++ b/cache.h
@@ -533,6 +533,7 @@ static inline int is_absolute_path(const char *path)
 {
 	return path[0] == '/' || has_dos_drive_prefix(path);
 }
+int is_directory(const char *);
 const char *make_absolute_path(const char *path);
 const char *make_nonrelative_path(const char *path);
 const char *make_relative_path(const char *abs, const char *base);
diff --git a/daemon.c b/daemon.c
index c315932..ab7a273 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1115,13 +1115,9 @@ int main(int argc, char **argv)
 	if (strict_paths && (!ok_paths || !*ok_paths))
 		die("option --strict-paths requires a whitelist");
 
-	if (base_path) {
-		struct stat st;
-
-		if (stat(base_path, &st) || !S_ISDIR(st.st_mode))
-			die("base-path '%s' does not exist or "
-			    "is not a directory", base_path);
-	}
+	if (base_path && !is_directory(base_path))
+		die("base-path '%s' does not exist or is not a directory",
+		    base_path);
 
 	if (inetd_mode) {
 		struct sockaddr_storage ss;
diff --git a/rerere.c b/rerere.c
index 323e493..c38886b 100644
--- a/rerere.c
+++ b/rerere.c
@@ -319,7 +319,6 @@ static int git_rerere_config(const char *var, const char *value, void *cb)
 
 static int is_rerere_enabled(void)
 {
-	struct stat st;
 	const char *rr_cache;
 	int rr_cache_exists;
 
@@ -327,7 +326,7 @@ static int is_rerere_enabled(void)
 		return 0;
 
 	rr_cache = git_path("rr-cache");
-	rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
+	rr_cache_exists = is_directory(rr_cache);
 	if (rerere_enabled < 0)
 		return rr_cache_exists;
 
diff --git a/sha1_file.c b/sha1_file.c
index 9ee1ed1..ae550e8 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -250,7 +250,6 @@ static void read_info_alternates(const char * alternates, int depth);
  */
 static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
 {
-	struct stat st;
 	const char *objdir = get_object_directory();
 	struct alternate_object_database *ent;
 	struct alternate_object_database *alt;
@@ -281,7 +280,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 	ent->base[pfxlen] = ent->base[entlen-1] = 0;
 
 	/* Detect cases where alternate disappeared */
-	if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
+	if (!is_directory(ent->base)) {
 		error("object directory %s does not exist; "
 		      "check .git/objects/info/alternates.",
 		      ent->base);
-- 
1.6.0.1.415.g7bb82
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 12 in “Pushing into a repository with alternates”
  1. 0/4 Pushing into a repository with alternatesJunio C Hamano, Sep 9, 2008
  2. 1/4 is_directory(): a generic helper functionJunio C Hamano, Sep 9, 2008
  3. 2/4 receive-pack: make it a builtinJunio C Hamano, Sep 9, 2008
  4. 3/4 push: prepare sender to receive extended ref information from the receiverJunio C Hamano, Sep 9, 2008
  5. 4/4 push: receiver end advertises refs from alternate repositoriesJunio C Hamano, Sep 9, 2008
  6. Jeff KingSep 9, 2008
  7. Paolo BonziniSep 9, 2008
  8. Junio C HamanoSep 9, 2008
  9. Paolo BonziniSep 9, 2008
  10. Johannes SixtSep 9, 2008
  11. Jeff KingSep 9, 2008
  12. Junio C HamanoSep 9, 2008

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.