4 * Do not use this for inspecting *tracked* content. When path is a
5 * symlink to a directory, we do not want to say it is a directory when
6 * dealing with tracked content in the working tree.
8 int is_directory(const char *path
)
11 return (!stat(path
, &st
) && S_ISDIR(st
.st_mode
));
14 /* removes the last path component from 'path' except if 'path' is root */
15 static void strip_last_component(struct strbuf
*path
)
17 size_t offset
= offset_1st_component(path
->buf
);
18 size_t len
= path
->len
;
20 /* Find start of the last component */
21 while (offset
< len
&& !is_dir_sep(path
->buf
[len
- 1]))
23 /* Skip sequences of multiple path-separators */
24 while (offset
< len
&& is_dir_sep(path
->buf
[len
- 1]))
27 strbuf_setlen(path
, len
);
30 /* get (and remove) the next component in 'remaining' and place it in 'next' */
31 static void get_next_component(struct strbuf
*next
, struct strbuf
*remaining
)
38 /* look for the next component */
39 /* Skip sequences of multiple path-separators */
40 for (start
= remaining
->buf
; is_dir_sep(*start
); start
++)
42 /* Find end of the path component */
43 for (end
= start
; *end
&& !is_dir_sep(*end
); end
++)
46 strbuf_add(next
, start
, end
- start
);
47 /* remove the component from 'remaining' */
48 strbuf_remove(remaining
, 0, end
- remaining
->buf
);
51 /* copies root part from remaining to resolved, canonicalizing it on the way */
52 static void get_root_part(struct strbuf
*resolved
, struct strbuf
*remaining
)
54 int offset
= offset_1st_component(remaining
->buf
);
56 strbuf_reset(resolved
);
57 strbuf_add(resolved
, remaining
->buf
, offset
);
58 #ifdef GIT_WINDOWS_NATIVE
59 convert_slashes(resolved
->buf
);
61 strbuf_remove(remaining
, 0, offset
);
64 /* We allow "recursive" symbolic links. Only within reason, though. */
66 #define MAXSYMLINKS 32
70 * If set, any number of trailing components may be missing; otherwise, only one
73 #define REALPATH_MANY_MISSING (1 << 0)
74 /* Should we die if there's an error? */
75 #define REALPATH_DIE_ON_ERROR (1 << 1)
77 static char *strbuf_realpath_1(struct strbuf
*resolved
, const char *path
,
80 struct strbuf remaining
= STRBUF_INIT
;
81 struct strbuf next
= STRBUF_INIT
;
82 struct strbuf symlink
= STRBUF_INIT
;
88 if (flags
& REALPATH_DIE_ON_ERROR
)
89 die("The empty string is not a valid path");
94 strbuf_addstr(&remaining
, path
);
95 get_root_part(resolved
, &remaining
);
98 /* relative path; can use CWD as the initial resolved path */
99 if (strbuf_getcwd(resolved
)) {
100 if (flags
& REALPATH_DIE_ON_ERROR
)
101 die_errno("unable to get current working directory");
107 /* Iterate over the remaining path components */
108 while (remaining
.len
> 0) {
109 get_next_component(&next
, &remaining
);
112 continue; /* empty component */
113 } else if (next
.len
== 1 && !strcmp(next
.buf
, ".")) {
114 continue; /* '.' component */
115 } else if (next
.len
== 2 && !strcmp(next
.buf
, "..")) {
116 /* '..' component; strip the last path component */
117 strip_last_component(resolved
);
121 /* append the next component and resolve resultant path */
122 if (!is_dir_sep(resolved
->buf
[resolved
->len
- 1]))
123 strbuf_addch(resolved
, '/');
124 strbuf_addbuf(resolved
, &next
);
126 if (lstat(resolved
->buf
, &st
)) {
127 /* error out unless this was the last component */
128 if (errno
!= ENOENT
||
129 (!(flags
& REALPATH_MANY_MISSING
) && remaining
.len
)) {
130 if (flags
& REALPATH_DIE_ON_ERROR
)
131 die_errno("Invalid path '%s'",
136 } else if (S_ISLNK(st
.st_mode
)) {
138 strbuf_reset(&symlink
);
140 if (num_symlinks
++ > MAXSYMLINKS
) {
143 if (flags
& REALPATH_DIE_ON_ERROR
)
144 die("More than %d nested symlinks "
145 "on path '%s'", MAXSYMLINKS
, path
);
150 len
= strbuf_readlink(&symlink
, resolved
->buf
,
153 if (flags
& REALPATH_DIE_ON_ERROR
)
154 die_errno("Invalid symlink '%s'",
160 if (is_absolute_path(symlink
.buf
)) {
161 /* absolute symlink; set resolved to root */
162 get_root_part(resolved
, &symlink
);
166 * strip off the last component since it will
167 * be replaced with the contents of the symlink
169 strip_last_component(resolved
);
173 * if there are still remaining components to resolve
174 * then append them to symlink
177 strbuf_addch(&symlink
, '/');
178 strbuf_addbuf(&symlink
, &remaining
);
182 * use the symlink as the remaining components that
183 * need to be resolved
185 strbuf_swap(&symlink
, &remaining
);
189 retval
= resolved
->buf
;
192 strbuf_release(&remaining
);
193 strbuf_release(&next
);
194 strbuf_release(&symlink
);
197 strbuf_reset(resolved
);
203 * Return the real path (i.e., absolute path, with symlinks resolved
204 * and extra slashes removed) equivalent to the specified path. (If
205 * you want an absolute path but don't mind links, use
206 * absolute_path().) Places the resolved realpath in the provided strbuf.
208 * The directory part of path (i.e., everything up to the last
209 * dir_sep) must denote a valid, existing directory, but the last
210 * component need not exist. If die_on_error is set, then die with an
211 * informative error message if there is a problem. Otherwise, return
212 * NULL on errors (without generating any output).
214 char *strbuf_realpath(struct strbuf
*resolved
, const char *path
,
217 return strbuf_realpath_1(resolved
, path
,
218 die_on_error
? REALPATH_DIE_ON_ERROR
: 0);
222 * Just like strbuf_realpath, but allows an arbitrary number of path
223 * components to be missing.
225 char *strbuf_realpath_forgiving(struct strbuf
*resolved
, const char *path
,
228 return strbuf_realpath_1(resolved
, path
,
229 ((die_on_error
? REALPATH_DIE_ON_ERROR
: 0) |
230 REALPATH_MANY_MISSING
));
233 char *real_pathdup(const char *path
, int die_on_error
)
235 struct strbuf realpath
= STRBUF_INIT
;
238 if (strbuf_realpath(&realpath
, path
, die_on_error
))
239 retval
= strbuf_detach(&realpath
, NULL
);
241 strbuf_release(&realpath
);
247 * Use this to get an absolute path from a relative one. If you want
248 * to resolve links, you should use strbuf_realpath.
250 const char *absolute_path(const char *path
)
252 static struct strbuf sb
= STRBUF_INIT
;
254 strbuf_add_absolute_path(&sb
, path
);
258 char *absolute_pathdup(const char *path
)
260 struct strbuf sb
= STRBUF_INIT
;
261 strbuf_add_absolute_path(&sb
, path
);
262 return strbuf_detach(&sb
, NULL
);
265 char *prefix_filename(const char *pfx
, const char *arg
)
267 struct strbuf path
= STRBUF_INIT
;
268 size_t pfx_len
= pfx
? strlen(pfx
) : 0;
271 ; /* nothing to prefix */
272 else if (is_absolute_path(arg
))
275 strbuf_add(&path
, pfx
, pfx_len
);
277 strbuf_addstr(&path
, arg
);
278 #ifdef GIT_WINDOWS_NATIVE
279 convert_slashes(path
.buf
+ pfx_len
);
281 return strbuf_detach(&path
, NULL
);