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

[PATCH v4 2/5] real_path: convert real_path_internal to strbuf_realpath

From
BWBrandon Williams <bmwill@google.com>
Date
Jan 3, 2017, 19:09 UTC
Message-ID
<20170103190923.11882-3-bmwill@google.com>
In-Reply-To
<20170103190923.11882-1-bmwill@google.com>

Change the name of real_path_internal to strbuf_realpath. In addition push the static strbuf up to its callers and instead take as a parameter a pointer to a strbuf to use for the final result.

This change makes strbuf_realpath reentrant.
Signed-off-by: Brandon Williams <bmwill@google.com>
---
 abspath.c | 53 +++++++++++++++++++++++++----------------------------
 cache.h   |  2 ++
 2 files changed, 27 insertions(+), 28 deletions(-)
diff --git a/abspath.c b/abspath.c
index 0f34636a8..c3a6acd4d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -55,21 +55,17 @@ static void get_next_component(struct strbuf *next, struct strbuf *remaining)
  * Return the real path (i.e., absolute path, with symlinks resolved
  * and extra slashes removed) equivalent to the specified path.  (If
  * you want an absolute path but don't mind links, use
- * absolute_path().)  The return value is a pointer to a static
- * buffer.
+ * absolute_path().)  Places the resolved realpath in the provided strbuf.
  *
  * The directory part of path (i.e., everything up to the last
  * dir_sep) must denote a valid, existing directory, but the last
  * component need not exist.  If die_on_error is set, then die with an
  * informative error message if there is a problem.  Otherwise, return
  * NULL on errors (without generating any output).
- *
- * If path is our buffer, then return path, as it's already what the
- * user wants.
  */
-static const char *real_path_internal(const char *path, int die_on_error)
+char *strbuf_realpath(struct strbuf *resolved, const char *path,
+		      int die_on_error)
 {
-	static struct strbuf resolved = STRBUF_INIT;
 	struct strbuf remaining = STRBUF_INIT;
 	struct strbuf next = STRBUF_INIT;
 	struct strbuf symlink = STRBUF_INIT;
@@ -77,10 +73,6 @@ static const char *real_path_internal(const char *path, int die_on_error)
 	int num_symlinks = 0;
 	struct stat st;
 
-	/* We've already done it */
-	if (path == resolved.buf)
-		return path;
-
 	if (!*path) {
 		if (die_on_error)
 			die("The empty string is not a valid path");
@@ -88,16 +80,16 @@ static const char *real_path_internal(const char *path, int die_on_error)
 			goto error_out;
 	}
 
-	strbuf_reset(&resolved);
+	strbuf_reset(resolved);
 
 	if (is_absolute_path(path)) {
 		/* absolute path; start with only root as being resolved */
 		int offset = offset_1st_component(path);
-		strbuf_add(&resolved, path, offset);
+		strbuf_add(resolved, path, offset);
 		strbuf_addstr(&remaining, path + offset);
 	} else {
 		/* relative path; can use CWD as the initial resolved path */
-		if (strbuf_getcwd(&resolved)) {
+		if (strbuf_getcwd(resolved)) {
 			if (die_on_error)
 				die_errno("unable to get current working directory");
 			else
@@ -116,21 +108,21 @@ static const char *real_path_internal(const char *path, int die_on_error)
 			continue; /* '.' component */
 		} else if (next.len == 2 && !strcmp(next.buf, "..")) {
 			/* '..' component; strip the last path component */
-			strip_last_component(&resolved);
+			strip_last_component(resolved);
 			continue;
 		}
 
 		/* append the next component and resolve resultant path */
-		if (!is_dir_sep(resolved.buf[resolved.len - 1]))
-			strbuf_addch(&resolved, '/');
-		strbuf_addbuf(&resolved, &next);
+		if (!is_dir_sep(resolved->buf[resolved->len - 1]))
+			strbuf_addch(resolved, '/');
+		strbuf_addbuf(resolved, &next);
 
-		if (lstat(resolved.buf, &st)) {
+		if (lstat(resolved->buf, &st)) {
 			/* error out unless this was the last component */
 			if (errno != ENOENT || remaining.len) {
 				if (die_on_error)
 					die_errno("Invalid path '%s'",
-						  resolved.buf);
+						  resolved->buf);
 				else
 					goto error_out;
 			}
@@ -146,12 +138,12 @@ static const char *real_path_internal(const char *path, int die_on_error)
 					goto error_out;
 			}
 
-			len = strbuf_readlink(&symlink, resolved.buf,
+			len = strbuf_readlink(&symlink, resolved->buf,
 					      st.st_size);
 			if (len < 0) {
 				if (die_on_error)
 					die_errno("Invalid symlink '%s'",
-						  resolved.buf);
+						  resolved->buf);
 				else
 					goto error_out;
 			}
@@ -159,8 +151,8 @@ static const char *real_path_internal(const char *path, int die_on_error)
 			if (is_absolute_path(symlink.buf)) {
 				/* absolute symlink; set resolved to root */
 				int offset = offset_1st_component(symlink.buf);
-				strbuf_reset(&resolved);
-				strbuf_add(&resolved, symlink.buf, offset);
+				strbuf_reset(resolved);
+				strbuf_add(resolved, symlink.buf, offset);
 				strbuf_remove(&symlink, 0, offset);
 			} else {
 				/*
@@ -168,7 +160,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
 				 * strip off the last component since it will
 				 * be replaced with the contents of the symlink
 				 */
-				strip_last_component(&resolved);
+				strip_last_component(resolved);
 			}
 
 			/*
@@ -188,24 +180,29 @@ static const char *real_path_internal(const char *path, int die_on_error)
 		}
 	}
 
-	retval = resolved.buf;
+	retval = resolved->buf;
 
 error_out:
 	strbuf_release(&remaining);
 	strbuf_release(&next);
 	strbuf_release(&symlink);
 
+	if (!retval)
+		strbuf_reset(resolved);
+
 	return retval;
 }
 
 const char *real_path(const char *path)
 {
-	return real_path_internal(path, 1);
+	static struct strbuf realpath = STRBUF_INIT;
+	return strbuf_realpath(&realpath, path, 1);
 }
 
 const char *real_path_if_valid(const char *path)
 {
-	return real_path_internal(path, 0);
+	static struct strbuf realpath = STRBUF_INIT;
+	return strbuf_realpath(&realpath, path, 0);
 }
 
 /*
diff --git a/cache.h b/cache.h
index a50a61a19..7a8129403 100644
--- a/cache.h
+++ b/cache.h
@@ -1064,6 +1064,8 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 int is_directory(const char *);
+char *strbuf_realpath(struct strbuf *resolved, const char *path,
+		      int die_on_error);
 const char *real_path(const char *path);
 const char *real_path_if_valid(const char *path);
 const char *absolute_path(const char *path);
-- 
2.11.0.390.gc69c2f50cf-goog
Previous: Brandon WilliamsNext: Brandon Williams
Message 57 of 83 in “making real_path thread-safe”
  1. making real_path thread-safeBrandon Williams, Dec 5, 2016
  2. real_path: make real_path thread-safeBrandon Williams, Dec 5, 2016
  3. Stefan BellerDec 5, 2016
  4. Brandon WilliamsDec 5, 2016
  5. Stefan BellerDec 5, 2016
  6. Stefan BellerDec 5, 2016
  7. Brandon WilliamsDec 5, 2016
  8. Duy NguyenDec 8, 2016
  9. Brandon WilliamsDec 8, 2016
  10. Junio C HamanoDec 6, 2016
  11. Brandon WilliamsDec 7, 2016
  12. Ramsay JonesDec 7, 2016
  13. Torsten BögershausenDec 7, 2016
  14. Junio C HamanoDec 7, 2016
  15. Brandon WilliamsDec 7, 2016
  16. Torsten BögershausenDec 8, 2016
  17. Johannes SixtDec 8, 2016
  18. Brandon WilliamsDec 8, 2016
  19. Johannes SixtDec 7, 2016
  20. Brandon WilliamsDec 7, 2016
  21. Johannes SixtDec 8, 2016
  22. Junio C HamanoDec 8, 2016
  23. 0/4 road to reentrant real_pathBrandon Williams, Dec 8, 2016
  24. 1/4 real_path: resolve symlinks by handBrandon Williams, Dec 8, 2016
  25. Jacob KellerDec 9, 2016
  26. Johannes SixtDec 9, 2016
  27. Brandon WilliamsDec 9, 2016
  28. 2/4 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Dec 8, 2016
  29. 3/4 real_path: create real_pathdupBrandon Williams, Dec 8, 2016
  30. Johannes SixtDec 9, 2016
  31. 4/4 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Dec 8, 2016
  32. Duy NguyenDec 9, 2016
  33. Brandon WilliamsDec 9, 2016
  34. Duy NguyenDec 10, 2016
  35. 0/4 road to reentrant real_pathBrandon Williams, Dec 12, 2016
  36. 1/4 real_path: resolve symlinks by handBrandon Williams, Dec 12, 2016
  37. Junio C HamanoDec 12, 2016
  38. Brandon WilliamsDec 12, 2016
  39. Junio C HamanoDec 12, 2016
  40. 4/4 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Dec 12, 2016
  41. Junio C HamanoDec 12, 2016
  42. Junio C HamanoDec 12, 2016
  43. Stefan BellerDec 12, 2016
  44. Brandon WilliamsDec 13, 2016
  45. Junio C HamanoDec 13, 2016
  46. 2/4 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Dec 12, 2016
  47. Junio C HamanoDec 12, 2016
  48. 3/4 real_path: create real_pathdupBrandon Williams, Dec 12, 2016
  49. Junio C HamanoDec 12, 2016
  50. real_path: canonicalize directory separators in root partsJohannes Sixt, Dec 21, 2016
  51. Brandon WilliamsDec 21, 2016
  52. Johannes SixtDec 22, 2016
  53. Brandon WilliamsDec 22, 2016
  54. Johannes SixtDec 22, 2016
  55. Junio C HamanoDec 22, 2016
  56. 0/5 road to reentrant real_pathBrandon Williams, Jan 3, 2017
  57. 2/5 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Jan 3, 2017
  58. 4/5 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Jan 3, 2017
  59. Jacob KellerJan 4, 2017
  60. Brandon WilliamsJan 4, 2017
  61. 5/5 real_path: canonicalize directory separators in root partsBrandon Williams, Jan 3, 2017
  62. 1/5 real_path: resolve symlinks by handBrandon Williams, Jan 3, 2017
  63. 3/5 real_path: create real_pathdupBrandon Williams, Jan 3, 2017
  64. Jeff KingJan 4, 2017
  65. Torsten BögershausenJan 4, 2017
  66. Jeff KingJan 4, 2017
  67. Brandon WilliamsJan 4, 2017
  68. Stefan BellerJan 4, 2017
  69. Jacob KellerJan 4, 2017
  70. Brandon WilliamsJan 4, 2017
  71. 0/5 road to reentrant real_pathBrandon Williams, Jan 4, 2017
  72. 3/5 real_path: create real_pathdupBrandon Williams, Jan 4, 2017
  73. 4/5 real_path: have callers use real_pathdup and strbuf_realpathBrandon Williams, Jan 4, 2017
  74. 5/5 real_path: canonicalize directory separators in root partsBrandon Williams, Jan 4, 2017
  75. 2/5 real_path: convert real_path_internal to strbuf_realpathBrandon Williams, Jan 4, 2017
  76. 1/5 real_path: resolve symlinks by handBrandon Williams, Jan 4, 2017
  77. Junio C HamanoJan 8, 2017
  78. Brandon WilliamsJan 9, 2017
  79. Junio C HamanoJan 9, 2017
  80. Brandon WilliamsJan 9, 2017
  81. Junio C HamanoJan 9, 2017
  82. 1/2 real_path: prevent redefinition of MAXSYMLINKSBrandon Williams, Jan 9, 2017
  83. 2/2 real_path: set errno when max number of symlinks is exceededBrandon Williams, Jan 9, 2017

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.