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 /* We allow "recursive" symbolic links. Only within reason, though. */
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().) Places the resolved realpath in the provided strbuf.
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 char *strbuf_realpath(struct strbuf *resolved, const char *path,
69 struct strbuf remaining = STRBUF_INIT;
70 struct strbuf next = STRBUF_INIT;
71 struct strbuf symlink = STRBUF_INIT;
78 die("The empty string is not a valid path");
83 strbuf_reset(resolved);
85 if (is_absolute_path(path)) {
86 /* absolute path; start with only root as being resolved */
87 int offset = offset_1st_component(path);
88 strbuf_add(resolved, path, offset);
89 strbuf_addstr(&remaining, path + offset);
91 /* relative path; can use CWD as the initial resolved path */
92 if (strbuf_getcwd(resolved)) {
94 die_errno("unable to get current working directory");
98 strbuf_addstr(&remaining, path);
101 /* Iterate over the remaining path components */
102 while (remaining.len > 0) {
103 get_next_component(&next, &remaining);
106 continue; /* empty component */
107 } else if (next.len == 1 && !strcmp(next.buf, ".")) {
108 continue; /* '.' component */
109 } else if (next.len == 2 && !strcmp(next.buf, "..")) {
110 /* '..' component; strip the last path component */
111 strip_last_component(resolved);
115 /* append the next component and resolve resultant path */
116 if (!is_dir_sep(resolved->buf[resolved->len - 1]))
117 strbuf_addch(resolved, '/');
118 strbuf_addbuf(resolved, &next);
120 if (lstat(resolved->buf, &st)) {
121 /* error out unless this was the last component */
122 if (errno != ENOENT || remaining.len) {
124 die_errno("Invalid path '%s'",
129 } else if (S_ISLNK(st.st_mode)) {
131 strbuf_reset(&symlink);
133 if (num_symlinks++ > MAXSYMLINKS) {
135 die("More than %d nested symlinks "
136 "on path '%s'", MAXSYMLINKS, path);
141 len = strbuf_readlink(&symlink, resolved->buf,
145 die_errno("Invalid symlink '%s'",
151 if (is_absolute_path(symlink.buf)) {
152 /* absolute symlink; set resolved to root */
153 int offset = offset_1st_component(symlink.buf);
154 strbuf_reset(resolved);
155 strbuf_add(resolved, symlink.buf, offset);
156 strbuf_remove(&symlink, 0, offset);
160 * strip off the last component since it will
161 * be replaced with the contents of the symlink
163 strip_last_component(resolved);
167 * if there are still remaining components to resolve
168 * then append them to symlink
171 strbuf_addch(&symlink, '/');
172 strbuf_addbuf(&symlink, &remaining);
176 * use the symlink as the remaining components that
177 * need to be resloved
179 strbuf_swap(&symlink, &remaining);
183 retval = resolved->buf;
186 strbuf_release(&remaining);
187 strbuf_release(&next);
188 strbuf_release(&symlink);
191 strbuf_reset(resolved);
196 const char *real_path(const char *path)
198 static struct strbuf realpath = STRBUF_INIT;
199 return strbuf_realpath(&realpath, path, 1);
202 const char *real_path_if_valid(const char *path)
204 static struct strbuf realpath = STRBUF_INIT;
205 return strbuf_realpath(&realpath, path, 0);
208 char *real_pathdup(const char *path)
210 struct strbuf realpath = STRBUF_INIT;
213 if (strbuf_realpath(&realpath, path, 0))
214 retval = strbuf_detach(&realpath, NULL);
216 strbuf_release(&realpath);
222 * Use this to get an absolute path from a relative one. If you want
223 * to resolve links, you should use real_path.
225 const char *absolute_path(const char *path)
227 static struct strbuf sb = STRBUF_INIT;
229 strbuf_add_absolute_path(&sb, path);
234 * Unlike prefix_path, this should be used if the named file does
235 * not have to interact with index entry; i.e. name of a random file
238 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
240 static struct strbuf path = STRBUF_INIT;
241 #ifndef GIT_WINDOWS_NATIVE
242 if (!pfx_len || is_absolute_path(arg))
245 strbuf_add(&path, pfx, pfx_len);
246 strbuf_addstr(&path, arg);
248 /* don't add prefix to absolute paths, but still replace '\' by '/' */
250 if (is_absolute_path(arg))
253 strbuf_add(&path, pfx, pfx_len);
254 strbuf_addstr(&path, arg);
255 convert_slashes(path.buf + pfx_len);