428
|
1 /*
|
|
2 * realpath.c -- canonicalize pathname by removing symlinks
|
|
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
|
2526
|
4 * Copyright (C) 2001, 2002, 2004 Ben Wing.
|
428
|
5 *
|
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Synched up with: Not in FSF. */
|
|
25
|
771
|
26 /* This file has been Mule-ized, June 2001 by Ben Wing.
|
|
27
|
|
28 Everything in this file now works in terms of internal, not external,
|
|
29 data. This is the only way to be safe, and it makes the code cleaner. */
|
|
30
|
428
|
31 #include <config.h>
|
446
|
32 #include "lisp.h"
|
442
|
33
|
2526
|
34 #include "profile.h"
|
|
35
|
558
|
36 #include "sysfile.h"
|
1116
|
37 #include "sysdir.h"
|
428
|
38
|
771
|
39 #define MAX_READLINKS 32
|
|
40
|
1116
|
41 #ifdef WIN32_ANY
|
446
|
42 #include "syswindows.h"
|
|
43 #ifndef ELOOP
|
|
44 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
|
|
45 #endif
|
1116
|
46 #endif
|
|
47
|
2526
|
48 Lisp_Object QSin_qxe_realpath;
|
|
49
|
446
|
50 /* Length of start of absolute filename. */
|
|
51 static int
|
1116
|
52 abs_start (const Ibyte *name)
|
446
|
53 {
|
1116
|
54 #ifdef WIN32_ANY
|
446
|
55 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
|
|
56 && IS_DIRECTORY_SEP (name[2]))
|
|
57 return 3;
|
|
58 else if (IS_DIRECTORY_SEP (*name))
|
|
59 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
|
|
60 else
|
|
61 return 0;
|
1116
|
62 #else /* not WIN32_ANY */
|
|
63 return IS_DIRECTORY_SEP (*name) ? 1 : 0;
|
|
64 #endif
|
446
|
65 }
|
1116
|
66
|
2526
|
67 /* Find real name of a file by resolving symbolic links and/or shortcuts
|
|
68 under Windows (.LNK links), if such support is enabled.
|
|
69
|
|
70 If no link found, and LINKS_ONLY is false, look up the correct case in
|
|
71 the file system of the last component.
|
1116
|
72
|
|
73 Under Windows, UNC servers and shares are lower-cased. Directories must
|
|
74 be given without trailing '/'. One day, this could read Win2K's reparse
|
2526
|
75 points.
|
|
76
|
|
77 Returns length of characters copied info BUF.
|
|
78 DOES NOT ZERO TERMINATE!!!!!
|
|
79 */
|
1116
|
80
|
|
81 static int
|
2526
|
82 readlink_or_correct_case (const Ibyte *name, Ibyte *buf, Bytecount size,
|
|
83 Boolint links_only)
|
1116
|
84 {
|
|
85 #ifndef WIN32_ANY
|
2526
|
86 return qxe_readlink (name, buf, (size_t) size);
|
1116
|
87 #else
|
|
88 # ifdef CYGWIN
|
|
89 Ibyte *tmp;
|
2526
|
90 int n = qxe_readlink (name, buf, (size_t) size);
|
1116
|
91 if (n >= 0 || errno != EINVAL)
|
|
92 return n;
|
|
93
|
|
94 /* The file may exist, but isn't a symlink. Try to find the
|
|
95 right name. */
|
2367
|
96 tmp =
|
|
97 alloca_ibytes (cygwin_posix_to_win32_path_list_buf_size ((char *) name));
|
1116
|
98 cygwin_posix_to_win32_path_list ((char *) name, (char *) tmp);
|
|
99 name = tmp;
|
2526
|
100 # else
|
|
101 if (mswindows_shortcuts_are_symlinks)
|
|
102 {
|
|
103 Ibyte *tmp = mswindows_read_link (name);
|
|
104
|
|
105 if (tmp != NULL)
|
|
106 {
|
|
107 /* Fucking fixed buffers. */
|
|
108 Bytecount len = qxestrlen (tmp);
|
|
109 if (len > size)
|
|
110 {
|
|
111 errno = ENAMETOOLONG;
|
|
112 return -1;
|
|
113 }
|
|
114 memcpy (buf, tmp, len);
|
|
115 xfree (tmp, Ibyte *);
|
|
116 return len;
|
|
117 }
|
|
118 }
|
1116
|
119 # endif
|
|
120
|
2526
|
121 if (links_only)
|
|
122 {
|
|
123 errno = EINVAL;
|
|
124 return -1;
|
|
125 }
|
|
126
|
1116
|
127 {
|
|
128 int len = 0;
|
|
129 int err = 0;
|
|
130 const Ibyte *lastname;
|
|
131 int count = 0;
|
1204
|
132 const Ibyte *nn;
|
1116
|
133 DECLARE_EISTRING (result);
|
|
134
|
|
135 assert (*name);
|
|
136
|
|
137 /* Sort of check we have a valid filename. */
|
|
138 if (qxestrpbrk (name, "*?|<>\""))
|
|
139 {
|
|
140 errno = ENOENT;
|
|
141 return -1;
|
|
142 }
|
2421
|
143 else if (qxestrlen (name) >= PATH_MAX_INTERNAL)
|
1116
|
144 {
|
|
145 errno = ENAMETOOLONG;
|
|
146 return -1;
|
|
147 }
|
|
148
|
|
149 /* Find start of filename */
|
|
150 lastname = name + qxestrlen (name);
|
|
151 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
|
|
152 --lastname;
|
|
153
|
|
154 /* Count slashes in unc path */
|
|
155 if (abs_start (name) == 2)
|
1204
|
156 for (nn = name; *nn; nn++)
|
|
157 if (IS_DIRECTORY_SEP (*nn))
|
1116
|
158 count++;
|
|
159
|
|
160 if (count >= 2 && count < 4)
|
|
161 {
|
|
162 eicpy_rawz (result, lastname);
|
|
163 eilwr (result);
|
|
164 }
|
|
165 else
|
|
166 {
|
|
167 WIN32_FIND_DATAW find_data;
|
|
168 Extbyte *nameext;
|
|
169 HANDLE dir_handle;
|
|
170
|
|
171 C_STRING_TO_TSTR (name, nameext);
|
|
172 dir_handle = qxeFindFirstFile (nameext, &find_data);
|
|
173 if (dir_handle == INVALID_HANDLE_VALUE)
|
|
174 {
|
|
175 errno = ENOENT;
|
|
176 return -1;
|
|
177 }
|
|
178 eicpy_ext (result, (Extbyte *) find_data.cFileName, Qmswindows_tstr);
|
|
179 FindClose (dir_handle);
|
|
180 }
|
|
181
|
2526
|
182 if ((len = eilen (result)) <= size)
|
1116
|
183 {
|
|
184 DECLARE_EISTRING (eilastname);
|
|
185
|
|
186 eicpy_rawz (eilastname, lastname);
|
|
187 if (eicmp_ei (eilastname, result) == 0)
|
2526
|
188 /* Signal that the name is already OK. */
|
|
189 err = EINVAL;
|
1116
|
190 else
|
2526
|
191 memcpy (buf, eidata (result), len);
|
1116
|
192 }
|
|
193 else
|
|
194 err = ENAMETOOLONG;
|
|
195
|
|
196 errno = err;
|
|
197 return err ? -1 : len;
|
|
198 }
|
|
199 #endif /* WIN32_ANY */
|
|
200 }
|
446
|
201
|
771
|
202 /* Mule Note: This function works with and returns
|
2526
|
203 internally-formatted strings.
|
|
204
|
|
205 if LINKS_ONLY is true, don't do case canonicalization under
|
|
206 Windows. */
|
440
|
207
|
867
|
208 Ibyte *
|
2526
|
209 qxe_realpath (const Ibyte *path, Ibyte *resolved_path, Boolint links_only)
|
428
|
210 {
|
2421
|
211 Ibyte copy_path[PATH_MAX_INTERNAL];
|
867
|
212 Ibyte *new_path = resolved_path;
|
|
213 Ibyte *max_path;
|
2526
|
214 Ibyte *retval = NULL;
|
1116
|
215 #if defined (HAVE_READLINK) || defined (WIN32_ANY)
|
428
|
216 int readlinks = 0;
|
2421
|
217 Ibyte link_path[PATH_MAX_INTERNAL];
|
428
|
218 int n;
|
1116
|
219 int abslen = abs_start (path);
|
428
|
220 #endif
|
|
221
|
2526
|
222 PROFILE_DECLARE ();
|
|
223
|
|
224 PROFILE_RECORD_ENTERING_SECTION (QSin_qxe_realpath);
|
|
225
|
1760
|
226 restart:
|
|
227
|
428
|
228 /* Make a copy of the source path since we may need to modify it. */
|
771
|
229 qxestrcpy (copy_path, path);
|
428
|
230 path = copy_path;
|
2421
|
231 max_path = copy_path + PATH_MAX_INTERNAL - 2;
|
446
|
232
|
819
|
233 if (0)
|
|
234 ;
|
1116
|
235 #ifdef WIN32_ANY
|
446
|
236 /* Check for c:/... or //server/... */
|
988
|
237 else if (abslen == 3 || abslen == 2)
|
428
|
238 {
|
462
|
239 /* Make sure drive letter is lowercased. */
|
1116
|
240 if (abslen == 3)
|
|
241 {
|
|
242 *new_path = tolower (*path);
|
|
243 new_path++;
|
|
244 path++;
|
|
245 abslen--;
|
|
246 }
|
988
|
247 /* Coerce directory chars. */
|
1116
|
248 while (abslen-- > 0)
|
|
249 {
|
|
250 if (IS_DIRECTORY_SEP (*path))
|
|
251 *new_path++ = DIRECTORY_SEP;
|
|
252 else
|
|
253 *new_path++ = *path;
|
|
254 path++;
|
|
255 }
|
428
|
256 }
|
819
|
257 #endif
|
|
258 #ifdef WIN32_NATIVE
|
446
|
259 /* No drive letter, but a beginning slash? Prepend drive letter. */
|
|
260 else if (abslen == 1)
|
428
|
261 {
|
2421
|
262 get_initial_directory (new_path, PATH_MAX_INTERNAL - 1);
|
428
|
263 new_path += 3;
|
|
264 path++;
|
|
265 }
|
446
|
266 /* Just a path name, prepend the current directory */
|
1116
|
267 else
|
428
|
268 {
|
2421
|
269 get_initial_directory (new_path, PATH_MAX_INTERNAL - 1);
|
771
|
270 new_path += qxestrlen (new_path);
|
446
|
271 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
272 *new_path++ = DIRECTORY_SEP;
|
428
|
273 }
|
|
274 #else
|
771
|
275 /* If it's a relative pathname use get_initial_directory for starters. */
|
819
|
276 else if (abslen == 0)
|
428
|
277 {
|
2421
|
278 get_initial_directory (new_path, PATH_MAX_INTERNAL - 1);
|
771
|
279 new_path += qxestrlen (new_path);
|
446
|
280 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
281 *new_path++ = DIRECTORY_SEP;
|
428
|
282 }
|
|
283 else
|
|
284 {
|
446
|
285 /* Copy first directory sep. May have two on cygwin. */
|
771
|
286 qxestrncpy (new_path, path, abslen);
|
446
|
287 new_path += abslen;
|
|
288 path += abslen;
|
428
|
289 }
|
|
290 #endif
|
|
291 /* Expand each slash-separated pathname component. */
|
|
292 while (*path != '\0')
|
|
293 {
|
|
294 /* Ignore stray "/". */
|
446
|
295 if (IS_DIRECTORY_SEP (*path))
|
428
|
296 {
|
|
297 path++;
|
|
298 continue;
|
|
299 }
|
|
300
|
|
301 if (*path == '.')
|
|
302 {
|
|
303 /* Ignore ".". */
|
446
|
304 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
|
428
|
305 {
|
|
306 path++;
|
|
307 continue;
|
|
308 }
|
|
309
|
442
|
310 /* Handle ".." */
|
|
311 if (path[1] == '.' &&
|
446
|
312 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
|
428
|
313 {
|
442
|
314 path += 2;
|
428
|
315
|
442
|
316 /* Ignore ".." at root. */
|
1116
|
317 if (new_path == resolved_path + abs_start (resolved_path))
|
442
|
318 continue;
|
428
|
319
|
442
|
320 /* Handle ".." by backing up. */
|
446
|
321 --new_path;
|
|
322 while (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
323 --new_path;
|
442
|
324 continue;
|
428
|
325 }
|
|
326 }
|
|
327
|
|
328 /* Safely copy the next pathname component. */
|
446
|
329 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
|
428
|
330 {
|
|
331 if (path > max_path)
|
|
332 {
|
|
333 errno = ENAMETOOLONG;
|
2526
|
334 goto done;
|
428
|
335 }
|
|
336 *new_path++ = *path++;
|
|
337 }
|
|
338
|
1116
|
339 #if defined (HAVE_READLINK) || defined (WIN32_ANY)
|
771
|
340 /* See if latest pathname component is a symlink or needs case
|
|
341 correction. */
|
428
|
342 *new_path = '\0';
|
2526
|
343 n = readlink_or_correct_case (resolved_path, link_path,
|
|
344 PATH_MAX_INTERNAL - 1, links_only);
|
428
|
345
|
|
346 if (n < 0)
|
|
347 {
|
771
|
348 /* EINVAL means the file exists but isn't a symlink or doesn't
|
|
349 need case correction. */
|
1116
|
350 #ifdef WIN32_ANY
|
462
|
351 if (errno != EINVAL && errno != ENOENT)
|
|
352 #else
|
|
353 if (errno != EINVAL)
|
|
354 #endif
|
2526
|
355 goto done;
|
428
|
356 }
|
|
357 else
|
|
358 {
|
|
359 /* Protect against infinite loops. */
|
|
360 if (readlinks++ > MAX_READLINKS)
|
|
361 {
|
|
362 errno = ELOOP;
|
2526
|
363 goto done;
|
428
|
364 }
|
|
365
|
|
366 /* Note: readlink doesn't add the null byte. */
|
|
367 link_path[n] = '\0';
|
446
|
368
|
1760
|
369 abslen = abs_start (link_path);
|
|
370 if (abslen > 0)
|
|
371 {
|
|
372 /* Start over for an absolute symlink. */
|
|
373 new_path = resolved_path;
|
|
374 qxestrcat (link_path, path);
|
|
375 path = link_path;
|
|
376 goto restart;
|
|
377 }
|
|
378
|
|
379 /* Otherwise back up over this component. */
|
|
380 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
|
|
381 assert (new_path > resolved_path);
|
428
|
382
|
|
383 /* Safe sex check. */
|
2421
|
384 if (qxestrlen (path) + n >= PATH_MAX_INTERNAL)
|
428
|
385 {
|
|
386 errno = ENAMETOOLONG;
|
2526
|
387 goto done;
|
428
|
388 }
|
|
389
|
|
390 /* Insert symlink contents into path. */
|
771
|
391 qxestrcat (link_path, path);
|
|
392 qxestrcpy (copy_path, link_path);
|
428
|
393 path = copy_path;
|
|
394 }
|
1116
|
395 #endif /* HAVE_READLINK || WIN32_ANY */
|
446
|
396 *new_path++ = DIRECTORY_SEP;
|
428
|
397 }
|
|
398
|
|
399 /* Delete trailing slash but don't whomp a lone slash. */
|
1116
|
400 if (new_path != resolved_path + abs_start (resolved_path) &&
|
771
|
401 IS_DIRECTORY_SEP (new_path[-1]))
|
428
|
402 new_path--;
|
|
403
|
|
404 /* Make sure it's null terminated. */
|
|
405 *new_path = '\0';
|
446
|
406
|
2526
|
407 retval = resolved_path;
|
|
408 done:
|
|
409 PROFILE_RECORD_EXITING_SECTION (QSin_qxe_realpath);
|
|
410 return retval;
|
428
|
411 }
|
2526
|
412
|
|
413 void
|
|
414 vars_of_realpath (void)
|
|
415 {
|
|
416 QSin_qxe_realpath =
|
|
417 build_msg_string ("(in qxe_realpath)");
|
|
418 staticpro (&QSin_qxe_realpath);
|
|
419 }
|