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

[PATCH] create_directories: simplify and avoid repeated copying

From
GBGeert Bosch <bosch@gnat.com>
Date
Apr 12, 2007, 20:29 UTC
Message-ID
<20070414013003.B0B4C2F1DC3@geert-boschs-computer.local>
Signed-off-by: Geert Bosch <bosch@gnat.com>
---
I needed to change the logic of this function a bit for
doing some local hacks^Wmodifications and thought it might
be a slight improvement/simplification.
No change in functionality and passed 'make test'.
 entry.c |   23 +++++++++--------------
 1 files changed, 9 insertions(+), 14 deletions(-)
diff --git a/entry.c b/entry.c
index d72f811..f966c59 100644
--- a/entry.c
+++ b/entry.c
@@ -3,24 +3,19 @@
 
 static void create_directories(const char *path, struct checkout *state)
 {
-	int len = strlen(path);
-	char *buf = xmalloc(len + 1);
-	const char *slash = path;
+	char *buf = xstrdup(path);
+	char *slash = buf;
 
 	while ((slash = strchr(slash+1, '/')) != NULL) {
-		len = slash - path;
-		memcpy(buf, path, len);
-		buf[len] = 0;
+		*slash = 0;
 		if (mkdir(buf, 0777)) {
-			if (errno == EEXIST) {
-				struct stat st;
-				if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
-					continue;
-				if (!stat(buf, &st) && S_ISDIR(st.st_mode))
-					continue; /* ok */
-			}
-			die("cannot create directory at %s", buf);
+			struct stat st;
+			int force = (slash - buf) > state->base_dir_len && state->force;
+			if (errno != EEXIST || ((!force || unlink(buf) || mkdir(buf, 0777)) &&
+			    (stat(buf, &st) || !S_ISDIR(st.st_mode))))
+				die("cannot create directory at %s", buf);
 		}
+		*slash='/';
 	}
 	free(buf);
 }
-- 
1.5.1
Next: Junio C Hamano
Message 1 of 2 in “create_directories: simplify and avoid repeated copying”
  1. create_directories: simplify and avoid repeated copyingGeert Bosch, Apr 12, 2007
  2. Junio C HamanoApr 14, 2007

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.