@@ -11,133 +11,216 @@ int is_directory(const char *path)
1111 return (!stat (path , & st ) && S_ISDIR (st .st_mode ));
1212}
1313
14+ /* removes the last path component from 'path' except if 'path' is root */
15+ static void strip_last_component (struct strbuf * path )
16+ {
17+ size_t offset = offset_1st_component (path -> buf );
18+ size_t len = path -> len ;
19+
20+ /* Find start of the last component */
21+ while (offset < len && !is_dir_sep (path -> buf [len - 1 ]))
22+ len -- ;
23+ /* Skip sequences of multiple path-separators */
24+ while (offset < len && is_dir_sep (path -> buf [len - 1 ]))
25+ len -- ;
26+
27+ strbuf_setlen (path , len );
28+ }
29+
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 )
32+ {
33+ char * start = NULL ;
34+ char * end = NULL ;
35+
36+ strbuf_reset (next );
37+
38+ /* look for the next component */
39+ /* Skip sequences of multiple path-separators */
40+ for (start = remaining -> buf ; is_dir_sep (* start ); start ++ )
41+ ; /* nothing */
42+ /* Find end of the path component */
43+ for (end = start ; * end && !is_dir_sep (* end ); end ++ )
44+ ; /* nothing */
45+
46+ strbuf_add (next , start , end - start );
47+ /* remove the component from 'remaining' */
48+ strbuf_remove (remaining , 0 , end - remaining -> buf );
49+ }
50+
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 )
53+ {
54+ int offset = offset_1st_component (remaining -> buf );
55+
56+ strbuf_reset (resolved );
57+ strbuf_add (resolved , remaining -> buf , offset );
58+ #ifdef GIT_WINDOWS_NATIVE
59+ convert_slashes (resolved -> buf );
60+ #endif
61+ strbuf_remove (remaining , 0 , offset );
62+ }
63+
1464/* We allow "recursive" symbolic links. Only within reason, though. */
15- #define MAXDEPTH 5
65+ #define MAXSYMLINKS 5
1666
1767/*
1868 * Return the real path (i.e., absolute path, with symlinks resolved
1969 * and extra slashes removed) equivalent to the specified path. (If
2070 * 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.
71+ * absolute_path().) Places the resolved realpath in the provided strbuf.
2372 *
24- * The input and all intermediate paths must be shorter than MAX_PATH.
2573 * The directory part of path (i.e., everything up to the last
2674 * dir_sep) must denote a valid, existing directory, but the last
2775 * component need not exist. If die_on_error is set, then die with an
2876 * informative error message if there is a problem. Otherwise, return
2977 * NULL on errors (without generating any output).
30- *
31- * If path is our buffer, then return path, as it's already what the
32- * user wants.
3378 */
34- static const char * real_path_internal (const char * path , int die_on_error )
79+ char * strbuf_realpath (struct strbuf * resolved , const char * path ,
80+ int die_on_error )
3581{
36- static struct strbuf sb = STRBUF_INIT ;
82+ struct strbuf remaining = STRBUF_INIT ;
83+ struct strbuf next = STRBUF_INIT ;
84+ struct strbuf symlink = STRBUF_INIT ;
3785 char * retval = NULL ;
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- */
44- struct strbuf cwd = STRBUF_INIT ;
45-
46- int depth = MAXDEPTH ;
47- char * last_elem = NULL ;
86+ int num_symlinks = 0 ;
4887 struct stat st ;
4988
50- /* We've already done it */
51- if (path == sb .buf )
52- return path ;
53-
5489 if (!* path ) {
5590 if (die_on_error )
5691 die ("The empty string is not a valid path" );
5792 else
5893 goto error_out ;
5994 }
6095
61- strbuf_reset (& sb );
62- strbuf_addstr (& sb , path );
63-
64- while (depth -- ) {
65- if (!is_directory (sb .buf )) {
66- char * last_slash = find_last_dir_sep (sb .buf );
67- if (last_slash ) {
68- last_elem = xstrdup (last_slash + 1 );
69- strbuf_setlen (& sb , last_slash - sb .buf + 1 );
70- } else {
71- last_elem = xmemdupz (sb .buf , sb .len );
72- strbuf_reset (& sb );
73- }
96+ strbuf_addstr (& remaining , path );
97+ get_root_part (resolved , & remaining );
98+
99+ if (!resolved -> len ) {
100+ /* relative path; can use CWD as the initial resolved path */
101+ if (strbuf_getcwd (resolved )) {
102+ if (die_on_error )
103+ die_errno ("unable to get current working directory" );
104+ else
105+ goto error_out ;
74106 }
107+ }
75108
76- if (sb .len ) {
77- if (!cwd .len && strbuf_getcwd (& cwd )) {
109+ /* Iterate over the remaining path components */
110+ while (remaining .len > 0 ) {
111+ get_next_component (& next , & remaining );
112+
113+ if (next .len == 0 ) {
114+ continue ; /* empty component */
115+ } else if (next .len == 1 && !strcmp (next .buf , "." )) {
116+ continue ; /* '.' component */
117+ } else if (next .len == 2 && !strcmp (next .buf , ".." )) {
118+ /* '..' component; strip the last path component */
119+ strip_last_component (resolved );
120+ continue ;
121+ }
122+
123+ /* 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 );
127+
128+ if (lstat (resolved -> buf , & st )) {
129+ /* error out unless this was the last component */
130+ if (errno != ENOENT || remaining .len ) {
78131 if (die_on_error )
79- die_errno ("Could not get current working directory" );
132+ die_errno ("Invalid path '%s'" ,
133+ resolved -> buf );
80134 else
81135 goto error_out ;
82136 }
137+ } else if (S_ISLNK (st .st_mode )) {
138+ ssize_t len ;
139+ strbuf_reset (& symlink );
83140
84- if (chdir ( sb . buf ) ) {
141+ if (num_symlinks ++ > MAXSYMLINKS ) {
85142 if (die_on_error )
86- die_errno ( "Could not switch to '%s'" ,
87- sb . buf );
143+ die ( "More than %d nested symlinks "
144+ "on path '%s'" , MAXSYMLINKS , path );
88145 else
89146 goto error_out ;
90147 }
91- }
92- if (strbuf_getcwd (& sb )) {
93- if (die_on_error )
94- die_errno ("Could not get current working directory" );
95- else
96- goto error_out ;
97- }
98-
99- if (last_elem ) {
100- if (sb .len && !is_dir_sep (sb .buf [sb .len - 1 ]))
101- strbuf_addch (& sb , '/' );
102- strbuf_addstr (& sb , last_elem );
103- free (last_elem );
104- last_elem = NULL ;
105- }
106148
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 );
149+ len = strbuf_readlink (& symlink , resolved -> buf ,
150+ st .st_size );
110151 if (len < 0 ) {
111152 if (die_on_error )
112153 die_errno ("Invalid symlink '%s'" ,
113- sb . buf );
154+ resolved -> buf );
114155 else
115156 goto error_out ;
116157 }
117- strbuf_swap (& sb , & next_sb );
118- strbuf_release (& next_sb );
119- } else
120- break ;
158+
159+ if (is_absolute_path (symlink .buf )) {
160+ /* absolute symlink; set resolved to root */
161+ get_root_part (resolved , & symlink );
162+ } else {
163+ /*
164+ * relative symlink
165+ * strip off the last component since it will
166+ * be replaced with the contents of the symlink
167+ */
168+ strip_last_component (resolved );
169+ }
170+
171+ /*
172+ * if there are still remaining components to resolve
173+ * then append them to symlink
174+ */
175+ if (remaining .len ) {
176+ strbuf_addch (& symlink , '/' );
177+ strbuf_addbuf (& symlink , & remaining );
178+ }
179+
180+ /*
181+ * use the symlink as the remaining components that
182+ * need to be resloved
183+ */
184+ strbuf_swap (& symlink , & remaining );
185+ }
121186 }
122187
123- retval = sb .buf ;
188+ retval = resolved -> buf ;
189+
124190error_out :
125- free (last_elem );
126- if (cwd .len && chdir (cwd .buf ))
127- die_errno ("Could not change back to '%s'" , cwd .buf );
128- strbuf_release (& cwd );
191+ strbuf_release (& remaining );
192+ strbuf_release (& next );
193+ strbuf_release (& symlink );
194+
195+ if (!retval )
196+ strbuf_reset (resolved );
129197
130198 return retval ;
131199}
132200
133201const char * real_path (const char * path )
134202{
135- return real_path_internal (path , 1 );
203+ static struct strbuf realpath = STRBUF_INIT ;
204+ return strbuf_realpath (& realpath , path , 1 );
136205}
137206
138207const char * real_path_if_valid (const char * path )
139208{
140- return real_path_internal (path , 0 );
209+ static struct strbuf realpath = STRBUF_INIT ;
210+ return strbuf_realpath (& realpath , path , 0 );
211+ }
212+
213+ char * real_pathdup (const char * path )
214+ {
215+ struct strbuf realpath = STRBUF_INIT ;
216+ char * retval = NULL ;
217+
218+ if (strbuf_realpath (& realpath , path , 0 ))
219+ retval = strbuf_detach (& realpath , NULL );
220+
221+ strbuf_release (& realpath );
222+
223+ return retval ;
141224}
142225
143226/*
0 commit comments