Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 184 lines (164 sloc) 4.502 kb
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
1 #include "cache.h"
2
90b4a71 @gitster is_directory(): a generic helper function
gitster authored
3 /*
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.
7 */
8 int is_directory(const char *path)
9 {
10 struct stat st;
11 return (!stat(path, &st) && S_ISDIR(st.st_mode));
12 }
13
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
14 /* We allow "recursive" symbolic links. Only within reason, though. */
15 #define MAXDEPTH 5
16
e2a57aa @carlosmn Name make_*_path functions more accurately
carlosmn authored
17 /*
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
18 * Return the real path (i.e., absolute path, with symlinks resolved
19 * and extra slashes removed) equivalent to the specified path. (If
20 * you want an absolute path but don't mind links, use
21 * absolute_path().) The return value is a pointer to a static
22 * buffer.
23 *
24 * The input and all intermediate paths must be shorter than MAX_PATH.
25 * The directory part of path (i.e., everything up to the last
26 * dir_sep) must denote a valid, existing directory, but the last
27 * component need not exist. If die_on_error is set, then die with an
28 * informative error message if there is a problem. Otherwise, return
29 * NULL on errors (without generating any output).
e2a57aa @carlosmn Name make_*_path functions more accurately
carlosmn authored
30 *
31 * If path is our buffer, then return path, as it's already what the
32 * user wants.
33 */
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
34 static const char *real_path_internal(const char *path, int die_on_error)
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
35 {
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
36 static struct strbuf sb = STRBUF_INIT;
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
37 char *retval = NULL;
d6052ab @mhagger real_path_internal(): add comment explaining use of cwd
mhagger authored
38
39 /*
40 * If we have to temporarily chdir(), store the original CWD
41 * here so that we can chdir() back to it at the end of the
42 * function:
43 */
251277a abspath: use strbuf_getcwd() to remember original working directory
René Scharfe authored
44 struct strbuf cwd = STRBUF_INIT;
d6052ab @mhagger real_path_internal(): add comment explaining use of cwd
mhagger authored
45
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
46 int depth = MAXDEPTH;
47 char *last_elem = NULL;
48 struct stat st;
49
1d679de @carlosmn make_absolute_path: return the input path if it points to our buffer
carlosmn authored
50 /* We've already done it */
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
51 if (path == sb.buf)
1d679de @carlosmn make_absolute_path: return the input path if it points to our buffer
carlosmn authored
52 return path;
53
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
54 if (!*path) {
55 if (die_on_error)
56 die("The empty string is not a valid path");
57 else
58 goto error_out;
59 }
3efe5d1 @mhagger real_path(): reject the empty string
mhagger authored
60
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
61 strbuf_reset(&sb);
62 strbuf_addstr(&sb, path);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
63
64 while (depth--) {
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
65 if (!is_directory(sb.buf)) {
66 char *last_slash = find_last_dir_sep(sb.buf);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
67 if (last_slash) {
68 last_elem = xstrdup(last_slash + 1);
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
69 strbuf_setlen(&sb, last_slash - sb.buf + 1);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
70 } else {
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
71 last_elem = xmemdupz(sb.buf, sb.len);
72 strbuf_reset(&sb);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
73 }
74 }
75
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
76 if (sb.len) {
251277a abspath: use strbuf_getcwd() to remember original working directory
René Scharfe authored
77 if (!cwd.len && strbuf_getcwd(&cwd)) {
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
78 if (die_on_error)
79 die_errno("Could not get current working directory");
80 else
81 goto error_out;
82 }
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
83
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
84 if (chdir(sb.buf)) {
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
85 if (die_on_error)
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
86 die_errno("Could not switch to '%s'",
87 sb.buf);
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
88 else
89 goto error_out;
90 }
91 }
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
92 if (strbuf_getcwd(&sb)) {
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
93 if (die_on_error)
94 die_errno("Could not get current working directory");
95 else
96 goto error_out;
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
97 }
98
99 if (last_elem) {
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
100 if (sb.len && !is_dir_sep(sb.buf[sb.len - 1]))
101 strbuf_addch(&sb, '/');
102 strbuf_addstr(&sb, last_elem);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
103 free(last_elem);
104 last_elem = NULL;
105 }
106
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
107 if (!lstat(sb.buf, &st) && S_ISLNK(st.st_mode)) {
108 struct strbuf next_sb = STRBUF_INIT;
109 ssize_t len = strbuf_readlink(&next_sb, sb.buf, 0);
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
110 if (len < 0) {
111 if (die_on_error)
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
112 die_errno("Invalid symlink '%s'",
113 sb.buf);
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
114 else
115 goto error_out;
116 }
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
117 strbuf_swap(&sb, &next_sb);
118 strbuf_release(&next_sb);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
119 } else
120 break;
121 }
122
2fdb9ce abspath: convert real_path_internal() to strbuf
René Scharfe authored
123 retval = sb.buf;
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
124 error_out:
125 free(last_elem);
251277a abspath: use strbuf_getcwd() to remember original working directory
René Scharfe authored
126 if (cwd.len && chdir(cwd.buf))
127 die_errno("Could not change back to '%s'", cwd.buf);
128 strbuf_release(&cwd);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
129
038e55f @mhagger Introduce new static function real_path_internal()
mhagger authored
130 return retval;
131 }
132
133 const char *real_path(const char *path)
134 {
135 return real_path_internal(path, 1);
5b8e6f8 @dmpot shrink git-shell by avoiding redundant dependencies
dmpot authored
136 }
10c4c88 Allow add_path() to add non-existent directories to the path
Johannes Sixt authored
137
e3e46cd @mhagger Introduce new function real_path_if_valid()
mhagger authored
138 const char *real_path_if_valid(const char *path)
139 {
140 return real_path_internal(path, 0);
141 }
142
e2a57aa @carlosmn Name make_*_path functions more accurately
carlosmn authored
143 /*
144 * Use this to get an absolute path from a relative one. If you want
145 * to resolve links, you should use real_path.
146 */
147 const char *absolute_path(const char *path)
10c4c88 Allow add_path() to add non-existent directories to the path
Johannes Sixt authored
148 {
679eebe abspath: convert absolute_path() to strbuf
René Scharfe authored
149 static struct strbuf sb = STRBUF_INIT;
150 strbuf_reset(&sb);
151 strbuf_add_absolute_path(&sb, path);
152 return sb.buf;
10c4c88 Allow add_path() to add non-existent directories to the path
Johannes Sixt authored
153 }
0687628 @divanorama Reduce parse-options.o dependencies
divanorama authored
154
155 /*
156 * Unlike prefix_path, this should be used if the named file does
157 * not have to interact with index entry; i.e. name of a random file
158 * on the filesystem.
159 */
160 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
161 {
fc2b621 @apelisse Prevent buffer overflows when path is too long
apelisse authored
162 static struct strbuf path = STRBUF_INIT;
380395d @jrn mingw: rename WIN32 cpp macro to GIT_WINDOWS_NATIVE
jrn authored
163 #ifndef GIT_WINDOWS_NATIVE
0687628 @divanorama Reduce parse-options.o dependencies
divanorama authored
164 if (!pfx_len || is_absolute_path(arg))
165 return arg;
fc2b621 @apelisse Prevent buffer overflows when path is too long
apelisse authored
166 strbuf_reset(&path);
167 strbuf_add(&path, pfx, pfx_len);
168 strbuf_addstr(&path, arg);
0687628 @divanorama Reduce parse-options.o dependencies
divanorama authored
169 #else
170 char *p;
171 /* don't add prefix to absolute paths, but still replace '\' by '/' */
fc2b621 @apelisse Prevent buffer overflows when path is too long
apelisse authored
172 strbuf_reset(&path);
0687628 @divanorama Reduce parse-options.o dependencies
divanorama authored
173 if (is_absolute_path(arg))
174 pfx_len = 0;
175 else if (pfx_len)
fc2b621 @apelisse Prevent buffer overflows when path is too long
apelisse authored
176 strbuf_add(&path, pfx, pfx_len);
177 strbuf_addstr(&path, arg);
178 for (p = path.buf + pfx_len; *p; p++)
0687628 @divanorama Reduce parse-options.o dependencies
divanorama authored
179 if (*p == '\\')
180 *p = '/';
181 #endif
fc2b621 @apelisse Prevent buffer overflows when path is too long
apelisse authored
182 return path.buf;
0687628 @divanorama Reduce parse-options.o dependencies
divanorama authored
183 }
Something went wrong with that request. Please try again.