real_path: convert real_path_internal to strbuf_realpath

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Dec 12, 2016 at 10:16 UTC a1ae48410dce23c1e81e76aabaeb4eb01b065763
2 files changed +27 -28
abspath.c
+25 -28
@@ -55,21 +55,17 @@ static void get_next_component(struct strbuf *next, struct strbuf *remaining)
55 * Return the real path (i.e., absolute path, with symlinks resolved
56 * and extra slashes removed) equivalent to the specified path. (If
57 * you want an absolute path but don't mind links, use
58 - * absolute_path().) The return value is a pointer to a static
59 - * buffer.
58 + * absolute_path().) Places the resolved realpath in the provided strbuf.
59 *
60 * The directory part of path (i.e., everything up to the last
61 * dir_sep) must denote a valid, existing directory, but the last
62 * component need not exist. If die_on_error is set, then die with an
63 * informative error message if there is a problem. Otherwise, return
64 * NULL on errors (without generating any output).
66 - *
67 - * If path is our buffer, then return path, as it's already what the
68 - * user wants.
65 */
70 -static const char *real_path_internal(const char *path, int die_on_error)
66 +char *strbuf_realpath(struct strbuf *resolved, const char *path,
67 + int die_on_error)
68 {
72 - static struct strbuf resolved = STRBUF_INIT;
69 struct strbuf remaining = STRBUF_INIT;
70 struct strbuf next = STRBUF_INIT;
71 struct strbuf symlink = STRBUF_INIT;
@@ -77,10 +73,6 @@ static const char *real_path_internal(const char *path, int die_on_error)
73 int num_symlinks = 0;
74 struct stat st;
75
80 - /* We've already done it */
81 - if (path == resolved.buf)
82 - return path;
83 -
76 if (!*path) {
77 if (die_on_error)
78 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)
80 goto error_out;
81 }
82
91 - strbuf_reset(&resolved);
83 + strbuf_reset(resolved);
84
85 if (is_absolute_path(path)) {
86 /* absolute path; start with only root as being resolved */
87 int offset = offset_1st_component(path);
96 - strbuf_add(&resolved, path, offset);
88 + strbuf_add(resolved, path, offset);
89 strbuf_addstr(&remaining, path + offset);
90 } else {
91 /* relative path; can use CWD as the initial resolved path */
100 - if (strbuf_getcwd(&resolved)) {
92 + if (strbuf_getcwd(resolved)) {
93 if (die_on_error)
94 die_errno("unable to get current working directory");
95 else
@@ -116,21 +108,21 @@ static const char *real_path_internal(const char *path, int die_on_error)
108 continue; /* '.' component */
109 } else if (next.len == 2 && !strcmp(next.buf, "..")) {
110 /* '..' component; strip the last path component */
119 - strip_last_component(&resolved);
111 + strip_last_component(resolved);
112 continue;
113 }
114
115 /* append the next component and resolve resultant path */
124 - if (!is_dir_sep(resolved.buf[resolved.len - 1]))
125 - strbuf_addch(&resolved, '/');
126 - strbuf_addbuf(&resolved, &next);
116 + if (!is_dir_sep(resolved->buf[resolved->len - 1]))
117 + strbuf_addch(resolved, '/');
118 + strbuf_addbuf(resolved, &next);
119
128 - if (lstat(resolved.buf, &st)) {
120 + if (lstat(resolved->buf, &st)) {
121 /* error out unless this was the last component */
122 if (errno != ENOENT || remaining.len) {
123 if (die_on_error)
124 die_errno("Invalid path '%s'",
133 - resolved.buf);
125 + resolved->buf);
126 else
127 goto error_out;
128 }
@@ -146,12 +138,12 @@ static const char *real_path_internal(const char *path, int die_on_error)
138 goto error_out;
139 }
140
149 - len = strbuf_readlink(&symlink, resolved.buf,
141 + len = strbuf_readlink(&symlink, resolved->buf,
142 st.st_size);
143 if (len < 0) {
144 if (die_on_error)
145 die_errno("Invalid symlink '%s'",
154 - resolved.buf);
146 + resolved->buf);
147 else
148 goto error_out;
149 }
@@ -159,8 +151,8 @@ static const char *real_path_internal(const char *path, int die_on_error)
151 if (is_absolute_path(symlink.buf)) {
152 /* absolute symlink; set resolved to root */
153 int offset = offset_1st_component(symlink.buf);
162 - strbuf_reset(&resolved);
163 - strbuf_add(&resolved, symlink.buf, offset);
154 + strbuf_reset(resolved);
155 + strbuf_add(resolved, symlink.buf, offset);
156 strbuf_remove(&symlink, 0, offset);
157 } else {
158 /*
@@ -168,7 +160,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
160 * strip off the last component since it will
161 * be replaced with the contents of the symlink
162 */
171 - strip_last_component(&resolved);
163 + strip_last_component(resolved);
164 }
165
166 /*
@@ -188,24 +180,29 @@ static const char *real_path_internal(const char *path, int die_on_error)
180 }
181 }
182
191 - retval = resolved.buf;
183 + retval = resolved->buf;
184
185 error_out:
186 strbuf_release(&remaining);
187 strbuf_release(&next);
188 strbuf_release(&symlink);
189
190 + if (!retval)
191 + strbuf_reset(resolved);
192 +
193 return retval;
194 }
195
196 const char *real_path(const char *path)
197 {
203 - return real_path_internal(path, 1);
198 + static struct strbuf realpath = STRBUF_INIT;
199 + return strbuf_realpath(&realpath, path, 1);
200 }
201
202 const char *real_path_if_valid(const char *path)
203 {
208 - return real_path_internal(path, 0);
204 + static struct strbuf realpath = STRBUF_INIT;
205 + return strbuf_realpath(&realpath, path, 0);
206 }
207
208 /*
cache.h
+2
@@ -1064,6 +1064,8 @@ static inline int is_absolute_path(const char *path)
1064 return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
1065 }
1066 int is_directory(const char *);
1067 +char *strbuf_realpath(struct strbuf *resolved, const char *path,
1068 + int die_on_error);
1069 const char *real_path(const char *path);
1070 const char *real_path_if_valid(const char *path);
1071 const char *absolute_path(const char *path);